def test_clusterinit_wait_for_pod_phase_error2(caplog): fake_http_resp = FakeHTTPResponse(500, "fake reason", "fake body") fake_api_exception = K8sApiException(http_resp=fake_http_resp) with pytest.raises(SystemExit): with patch('intel.k8s.get_pod_list', MagicMock(side_effect=fake_api_exception)): clusterinit.wait_for_pod_phase("fakepod1", "Running")
def test_remove_node_cmk_er_failure(caplog): fake_http_resp = FakeHTTPResponse(500, "{\"reason\":\"fake reason\"}", NON_EXISTANT_MGS) fake_api_exception = K8sApiException(http_resp=fake_http_resp) with patch(PATCH_NODE_STATUS, MagicMock(side_effect=fake_api_exception)): uninstall.remove_node_cmk_er() caplog_tuple = caplog.record_tuples assert caplog_tuple[-2][2] == "CMK ER does not exist." assert caplog_tuple[-1][2] == "Removed node ERs"
def test_custom_resource_type_exists_failure(): fake_http_resp = FakeHTTPResponse(500, FAKE_REASON, FAKE_BODY) fake_api_exception = K8sApiException(http_resp=fake_http_resp) mock = MagicMock() with patch(CLIENT_CONFIG, MagicMock(return_value=mock)): fake_type = FakeCRD.generate_crd_type() mock.api_client.call_api = MagicMock(side_effect=fake_api_exception) with pytest.raises(K8sApiException): fake_type.exists()
def test_third_party_resource_type_exists_failure(): fake_http_resp = FakeHTTPResponse(500, FAKE_REASON, FAKE_BODY) fake_api_exception = K8sApiException(http_resp=fake_http_resp) mock = MagicMock() with patch(K8S_EXTENSIONS_CLIENT, MagicMock(return_value=mock)): fake_type = FakeTPR.generate_tpr_type() mock.api_client.call_api = MagicMock(side_effect=fake_api_exception) with pytest.raises(K8sApiException): fake_type.exists()
def test_custom_resource_type_exists_failure(): fake_http_resp = FakeHTTPResponse(500, "fake reason", "fake body") fake_api_exception = K8sApiException(http_resp=fake_http_resp) mock = MagicMock() with patch('intel.k8s.extensions_client_from_config', MagicMock(return_value=mock)): fake_type = FakeCRD.generate_crd_type() mock.api_client.call_api = MagicMock(side_effect=fake_api_exception) with pytest.raises(K8sApiException): fake_type.exists()
def test_third_party_resource_type_exists_success2(): fake_http_resp = FakeHTTPResponse(client.NOT_FOUND, FAKE_REASON, FAKE_BODY) fake_api_exception = K8sApiException(http_resp=fake_http_resp) assert fake_api_exception.status == client.NOT_FOUND mock = MagicMock() with patch(K8S_EXTENSIONS_CLIENT, MagicMock(return_value=mock)): fake_type = FakeTPR.generate_tpr_type() mock.api_client.call_api = MagicMock(side_effect=fake_api_exception) exists = fake_type.exists() assert not exists
def test_remove_node_cmk_er_failure(caplog): fake_http_resp = FakeHTTPResponse(500, "{\"reason\":\"fake reason\"}", "{\"message\":\"nonexistant\"}") fake_api_exception = K8sApiException(http_resp=fake_http_resp) with patch('intel.discover.patch_k8s_node_status', MagicMock(side_effect=fake_api_exception)): uninstall.remove_node_cmk_er() caplog_tuple = caplog.record_tuples assert caplog_tuple[-2][2] == "CMK ER does not exist." assert caplog_tuple[-1][2] == "Removed node ERs"
def test_third_party_resource_type_save_failure(): fake_http_resp = FakeHTTPResponse(500, "fake reason", "fake body") fake_api_exception = K8sApiException(http_resp=fake_http_resp) mock = MagicMock() with patch('intel.k8s.extensions_client_from_config', MagicMock(return_value=mock)): mock.create_third_party_resource = \ MagicMock(side_effect=fake_api_exception) fake_type = FakeTPR.generate_tpr_type() with pytest.raises(K8sApiException): fake_type.save()
def test_remove_node_cmk_er_failure2(caplog): fake_http_resp = FakeHTTPResponse(500, "{\"reason\":\"fake reason\"}", FAKE_MESSAGE) fake_api_exception = K8sApiException(http_resp=fake_http_resp) with patch(PATCH_NODE_STATUS, MagicMock(side_effect=fake_api_exception)): with pytest.raises(SystemExit): uninstall.remove_node_cmk_er() caplog_tuple = caplog.record_tuples exp_err = "Aborting uninstall: Exception when removing ER: " \ "{}".format(fake_api_exception) assert caplog_tuple[-1][2] == exp_err
def test_third_party_resource_save_failure(caplog): fake_http_resp = FakeHTTPResponse(500, FAKE_REASON, FAKE_BODY) fake_api_exception = K8sApiException(http_resp=fake_http_resp) mock = MagicMock() with patch(K8S_EXTENSIONS_CLIENT, MagicMock(return_value=mock)): fake_tpr = FakeTPR.generate_tpr() mock_create = MagicMock(side_effect=fake_api_exception) with patch(THIRD_PARTY_RESOURCE_CREATE, mock_create): with pytest.raises(K8sApiException): fake_tpr.save() assert mock_create.called
def test_third_party_resource_save_failure(caplog): fake_http_resp = FakeHTTPResponse(500, FAKE_REASON, FAKE_BODY) fake_api_exception = K8sApiException(http_resp=fake_http_resp) mock = MagicMock() with patch(CLIENT_CONFIG, MagicMock(return_value=mock)): fake_crd = FakeCRD.generate_crd() mock_create = MagicMock(side_effect=fake_api_exception) with patch(CUSTOM_RESOURSE, mock_create): with pytest.raises(K8sApiException): fake_crd.save() assert mock_create.called
def test_custom_resource_definition_type_save_failure(): fake_http_resp = FakeHTTPResponse(500, FAKE_REASON, FAKE_BODY) fake_api_exception = K8sApiException(http_resp=fake_http_resp) mock = MagicMock() with patch(CLIENT_CONFIG, MagicMock(return_value=mock)): fake_type = FakeCRD.generate_crd_type() mock.api_client.call_api = MagicMock(side_effect=fake_api_exception) with pytest.raises(K8sApiException): fake_type.save() assert mock.method_calls[0][0] == CLIENT_API_CALL assert 'POST' in mock.method_calls[0][1]
def test_custom_resource_type_exists_success2(): fake_http_resp = FakeHTTPResponse(client.NOT_FOUND, FAKE_REASON, FAKE_BODY) fake_api_exception = K8sApiException(http_resp=fake_http_resp) assert fake_api_exception.status == client.NOT_FOUND mock = MagicMock() with patch(CLIENT_CONFIG, MagicMock(return_value=mock)): fake_type = FakeCRD.generate_crd_type() mock.api_client.call_api = MagicMock(side_effect=fake_api_exception) exists = fake_type.exists() assert not exists
def test_discover_add_taint_failure1(caplog): fake_http_resp = FakeHTTPResponse(500, "fake reason", "fake body") fake_api_exception = K8sApiException(http_resp=fake_http_resp) with patch('intel.discover.get_k8s_node', MagicMock(side_effect=fake_api_exception)): with pytest.raises(SystemExit): discover.add_node_taint() exp_err = "Exception when getting the node obj" exp_log_err = get_expected_log_error(exp_err) caplog_tuple = caplog.record_tuples assert caplog_tuple[0][2] == exp_log_err
def test_custom_resource_definition_type_save_failure(): fake_http_resp = FakeHTTPResponse(500, "fake reason", "fake body") fake_api_exception = K8sApiException(http_resp=fake_http_resp) mock = MagicMock() with patch('intel.k8s.extensions_client_from_config', MagicMock(return_value=mock)): fake_type = FakeCRD.generate_crd_type() mock.api_client.call_api = MagicMock(side_effect=fake_api_exception) with pytest.raises(K8sApiException): fake_type.save() assert mock.method_calls[0][0] == 'api_client.call_api' assert 'POST' in mock.method_calls[0][1]
def test_third_party_resource_type_exists_success2(): fake_http_resp = FakeHTTPResponse(client.NOT_FOUND, "fake reason", "fake body") fake_api_exception = K8sApiException(http_resp=fake_http_resp) assert fake_api_exception.status == client.NOT_FOUND mock = MagicMock() with patch('intel.k8s.extensions_client_from_config', MagicMock(return_value=mock)): fake_type = FakeTPR.generate_tpr_type() mock.api_client.call_api = MagicMock(side_effect=fake_api_exception) exists = fake_type.exists() assert not exists
def test_remove_node_cmk_er_failure2(caplog): fake_http_resp = FakeHTTPResponse(500, "{\"reason\":\"fake reason\"}", "{\"message\":\"fake message\"}") fake_api_exception = K8sApiException(http_resp=fake_http_resp) with patch('intel.discover.patch_k8s_node_status', MagicMock(side_effect=fake_api_exception)): with pytest.raises(SystemExit): uninstall.remove_node_cmk_er() caplog_tuple = caplog.record_tuples exp_err = "Aborting uninstall: Exception when removing ER: " \ "{}".format(fake_api_exception) assert caplog_tuple[-1][2] == exp_err
def test_third_party_resource_save_failure(caplog): fake_http_resp = FakeHTTPResponse(500, "fake reason", "fake body") fake_api_exception = K8sApiException(http_resp=fake_http_resp) mock = MagicMock() with patch('intel.k8s.extensions_client_from_config', MagicMock(return_value=mock)): fake_tpr = FakeTPR.generate_tpr() mock_create = MagicMock(side_effect=fake_api_exception) with patch('intel.third_party.ThirdPartyResource.create', mock_create): with pytest.raises(K8sApiException): fake_tpr.save() assert mock_create.called
def test_add_node_er_failure(caplog): conf_dir = helpers.conf_dir("ok") fake_http_resp = FakeHTTPResponse(500, "fake reason", "fake body") fake_api_exception = K8sApiException(http_resp=fake_http_resp) with patch('intel.discover.patch_k8s_node_status', MagicMock(side_effect=fake_api_exception)): with pytest.raises(SystemExit): discover.add_node_er(conf_dir) exp_err = "Exception when patching node with OIR" exp_log_err = get_expected_log_error(exp_err) caplog_tuple = caplog.record_tuples assert caplog_tuple[-2][2] == exp_log_err assert caplog_tuple[-1][2] == "Aborting discover ..."
def test_third_party_resource_save_recreate_failure(caplog): fake_http_resp = FakeHTTPResponse(500, FAKE_REASON, FAKE_BODY) fake_api_exception = K8sApiException(http_resp=fake_http_resp) fake_http_409_resp = FakeHTTPResponse(client.CONFLICT, FAKE_REASON, FAKE_BODY) fake_api_409_exception = K8sApiException(http_resp=fake_http_409_resp) mock = MagicMock() with patch(CLIENT_CONFIG, MagicMock(return_value=mock)): fake_crd = FakeCRD.generate_crd() mock_create = MagicMock(side_effect=fake_api_409_exception) mock_remove = MagicMock(side_effect=fake_api_exception) with patch(CUSTOM_RESOURSE, mock_create), \ patch('intel.custom_resource.CustomResourceDefinition.remove', mock_remove): with pytest.raises(K8sApiException): fake_crd.save() exp_log_err = "Previous definition has been detected. Recreating..." caplog_tuple = caplog.record_tuples assert caplog_tuple[-1][2] == exp_log_err assert mock_create.called assert mock_remove.called
def test_clusterinit_run_cmd_pods_nodereport_failure(caplog): fake_http_resp = FakeHTTPResponse(500, "fake reason", "fake body") fake_api_exception = K8sApiException(http_resp=fake_http_resp) with patch('intel.k8s.create_ds', MagicMock(side_effect=fake_api_exception)): with pytest.raises(SystemExit): clusterinit.run_pods(["nodereport"], None, "fake_img", "Never", "fake-conf-dir", "fake-install-dir", "2", "2", ["fakenode"], "", "") exp_err = "Exception when creating pod for ['nodereport'] command(s)" exp_log_err = get_expected_log_error(exp_err) caplog_tuple = caplog.record_tuples assert caplog_tuple[1][2] == exp_log_err
def test_uninstall_remove_node_label_success(caplog): fake_http_resp = FakeHTTPResponse(500, FAKE_REASON, NON_EXISTANT_MGS) fake_api_exception = K8sApiException(http_resp=fake_http_resp) with patch(DISCOVER_PATCH_K8S_NODE, MagicMock(side_effect=fake_api_exception)): uninstall.remove_node_label() caplog_tuple = caplog.record_tuples patch_path = MATADATA_LABELS_CMK_NODE exp_str = "Removed node label \"{}\".".format(patch_path) exp_str2 = "Label \"{}\" does not exist.".format(patch_path) assert caplog_tuple[-2][2] == exp_str2 assert caplog_tuple[-1][2] == exp_str
def test_clusterinit_run_cmd_pods_install_failure(caplog): fake_http_resp = FakeHTTPResponse(500, FAKE_REASON, FAKE_BODY) fake_api_exception = K8sApiException(http_resp=fake_http_resp) with patch(CREATE_POD, MagicMock(side_effect=fake_api_exception)): with pytest.raises(SystemExit): clusterinit.run_pods(None, ["install"], "fake_img", "Never", "fake-install-dir", "2", "2", ["fakenode"], "", "", "vertical", "vertical", "default", "-1", False) exp_err = "Exception when creating pod for ['install'] command(s)" exp_log_err = get_expected_log_error(exp_err) caplog_tuple = caplog.record_tuples assert caplog_tuple[1][2] == exp_log_err
def test_clusterinit_get_cmk_node_list_all_hosts_error(caplog): fake_http_resp = FakeHTTPResponse(500, "fake reason", "fake body") fake_api_exception = K8sApiException(http_resp=fake_http_resp) with pytest.raises(SystemExit): with patch('intel.k8s.get_compute_nodes', MagicMock(side_effect=fake_api_exception)): clusterinit.get_cmk_node_list(None, True) exp_err = "Exception when getting the node list: {}" exp_log_err = get_expected_log_error(exp_err) caplog_tuple = caplog.record_tuples assert caplog_tuple[-2][2] == exp_log_err exp_err = "Aborting cluster-init ..." assert caplog_tuple[-1][2] == exp_err
def test_discover_oir_update_failure(caplog): c = MockConfig(get_fake_config()) with patch('intel.config.Config', MagicMock(return_value=c)): fake_http_resp = FakeHTTPResponse(500, "fake reason", "fake body") fake_api_exception = K8sApiException(http_resp=fake_http_resp) with patch('intel.discover.patch_k8s_node_status', MagicMock(side_effect=fake_api_exception)): with pytest.raises(SystemExit): discover.add_node_oir() exp_err = "Exception when patching node with OIR" exp_log_err = get_expected_log_error(exp_err) caplog_tuple = caplog.record_tuples assert caplog_tuple[0][2] == exp_log_err
def test_uninstall_remove_node_label_success(caplog): fake_http_resp = FakeHTTPResponse(500, "fake reason", "{\"message\":\"nonexistant\"}") fake_api_exception = K8sApiException(http_resp=fake_http_resp) with patch('intel.discover.patch_k8s_node', MagicMock(side_effect=fake_api_exception)): uninstall.remove_node_label() caplog_tuple = caplog.record_tuples patch_path = '/metadata/labels/cmk.intel.com~1cmk-node' exp_str = "Removed node label \"{}\".".format(patch_path) exp_str2 = "Label \"{}\" does not exist.".format(patch_path) assert caplog_tuple[-2][2] == exp_str2 assert caplog_tuple[-1][2] == exp_str
def test_third_party_resource_save_recreate_failure(caplog): fake_http_resp = FakeHTTPResponse(500, "fake reason", "fake body") fake_api_exception = K8sApiException(http_resp=fake_http_resp) fake_http_409_resp = FakeHTTPResponse(client.CONFLICT, "fake reason", "fake body") fake_api_409_exception = K8sApiException(http_resp=fake_http_409_resp) mock = MagicMock() with patch('intel.k8s.extensions_client_from_config', MagicMock(return_value=mock)): fake_crd = FakeCRD.generate_crd() mock_create = MagicMock(side_effect=fake_api_409_exception) mock_remove = MagicMock(side_effect=fake_api_exception) with patch('intel.custom_resource.CustomResourceDefinition.create', mock_create), \ patch('intel.custom_resource.CustomResourceDefinition.remove', mock_remove): with pytest.raises(K8sApiException): fake_crd.save() exp_log_err = "Previous definition has been detected. Recreating..." caplog_tuple = caplog.record_tuples assert caplog_tuple[-1][2] == exp_log_err assert mock_create.called assert mock_remove.called
def test_third_party_resource_save_api_blocked_failure(caplog): fake_http_resp = FakeHTTPResponse(client.METHOD_NOT_ALLOWED, FAKE_REASON, FAKE_BODY) fake_api_exception = K8sApiException(http_resp=fake_http_resp) mock = MagicMock() with patch(CLIENT_CONFIG, MagicMock(return_value=mock)): fake_crd = FakeCRD.generate_crd() mock_create = MagicMock(side_effect=fake_api_exception) with patch(CUSTOM_RESOURSE, mock_create): fake_crd.save() assert mock_create.called exp_log_err = "API is blocked. Report will be skipped" caplog_tuple = caplog.record_tuples assert caplog_tuple[-1][2] == exp_log_err
def test_custom_resource_save_crd_not_ready_failure(caplog): fake_http_resp = FakeHTTPResponse(client.NOT_FOUND, FAKE_REASON, FAKE_BODY) fake_api_exception = K8sApiException(http_resp=fake_http_resp) mock = MagicMock() with patch(CLIENT_CONFIG, MagicMock(return_value=mock)): fake_crd = FakeCRD.generate_crd() mock_create = MagicMock(side_effect=fake_api_exception) with patch(CUSTOM_RESOURSE, mock_create): fake_crd.save() assert mock_create.called exp_log_err = ("Custom Resource Definition is not ready yet. " "Report will be skipped") caplog_tuple = caplog.record_tuples assert caplog_tuple[-1][2] == exp_log_err
def test_discover_add_taint_failure2(caplog): fake_node_resp = {"metadata": {"annotations": {}}} fake_http_resp = FakeHTTPResponse(500, "fake reason", "fake body") fake_api_exception = K8sApiException(http_resp=fake_http_resp) with patch('intel.discover.get_k8s_node', MagicMock(return_value=fake_node_resp)), \ patch('intel.discover.patch_k8s_node', MagicMock(side_effect=fake_api_exception)): with pytest.raises(SystemExit): discover.add_node_taint() exp_err = "Exception when tainting the node" exp_log_err = get_expected_log_error(exp_err) caplog_tuple = caplog.record_tuples assert caplog_tuple[0][2] == exp_log_err