def test_webhook_mutate_pod_merge_fail(caplog): conf_file = os.path.join(util.cmk_root(), "tests", "data", "webhook", "mutations.yaml") ar = get_admission_review() with patch('intel.webhook.merge', MagicMock(side_effect=YamlReaderError)): with pytest.raises(webhook.MutationError): webhook.mutate(ar, conf_file)
def test_webhook_mutate_not_a_pod_fail(caplog): ar_mock = MagicMock() ar_mock['request']['kind']['kind'] = "NotAPod" with pytest.raises(webhook.MutationError): webhook.mutate(ar_mock, None) log = "Resource is not a pod" caplog_tuple = caplog.record_tuples assert caplog_tuple[-1][2] == log
def test_webhook_mutate_container_merge_fail(caplog): conf_file = os.path.join(util.cmk_root(), "tests", "data", "webhook", MUTATIONS_YAML) ar = get_admission_review() with patch('intel.webhook.merge', MagicMock(side_effect=[None, YamlReaderError])): with pytest.raises(webhook.MutationError): webhook.mutate(ar, conf_file)
def test_webhook_mutate_success(): conf_file = os.path.join(util.cmk_root(), "tests", "data", "webhook", "mutations.yaml") ar = get_admission_review() request_uid = ar["request"]["uid"] webhook.mutate(ar, conf_file) assert "response" in ar assert "request" not in ar assert ar["response"]["uid"] == request_uid assert ar["response"]["allowed"] assert type(ar["response"]["patch"]) is str
def test_webhook_mutate_not_required(caplog): conf_file = os.path.join(util.cmk_root(), "tests", "data", "webhook", "mutations.yaml") ar = get_admission_review() request_uid = ar["request"]["uid"] with patch('intel.webhook.is_mutation_required', MagicMock(return_value=False)): webhook.mutate(ar, conf_file) assert "response" in ar assert "request" not in ar assert ar["response"]["uid"] == request_uid assert ar["response"]["allowed"] assert "patch" not in ar["response"] log = "Mutation is not required. Skipping..." caplog_tuple = caplog.record_tuples assert caplog_tuple[-1][2] == log
def test_webhook_mutate_success_core_number_mismatch(): conf_file = os.path.join(util.cmk_root(), "tests", "data", "webhook", MUTATIONS_YAML) ar = get_admission_review() ar['request']['object']['spec'] = { "containers": [get_cmk_container3(), get_cmk_container()] } request_uid = ar['request']['uid'] webhook.mutate(ar, conf_file) assert "response" in ar assert "request" not in ar assert ar['response']['uid'] == request_uid assert ar['response']['allowed'] assert "patch" in ar['response'] patch = json.loads(base64.b64decode(ar['response']['patch'])) containers = patch[1]['value']['containers'] assert len(containers) == 2 assert containers[0]['env'][1]['value'] == "1" assert containers[1]['env'][1]['value'] == "2"