def test_custom_resource_definition_deploy(self, custom_resource_definition, service_type, kind_logger): with kind_logger(): name, fiaas_application, expected = custom_resource_definition # check that k8s objects for name doesn't already exist kinds = self._select_kinds(expected) for kind in kinds: with pytest.raises(NotFound): kind.get(name) # First deploy fiaas_application.save() app_uid = fiaas_application.metadata.uid # Check that deployment status is RUNNING def _assert_status(): status = FiaasApplicationStatus.get(create_name(name, DEPLOYMENT_ID1)) assert status.result == u"RUNNING" assert len(status.logs) > 0 assert any("Saving result RUNNING for default/{}".format(name) in line for line in status.logs) wait_until(_assert_status, patience=PATIENCE) # Check that annotations and labels are applied to status object status_labels = fiaas_application.spec.additional_labels.status if status_labels: status = FiaasApplicationStatus.get(create_name(name, DEPLOYMENT_ID1)) label_difference = status_labels.viewitems() - status.metadata.labels.viewitems() assert label_difference == set() # Check deploy success wait_until(_deploy_success(name, kinds, service_type, IMAGE1, expected, DEPLOYMENT_ID1, app_uid=app_uid), patience=PATIENCE) # Redeploy, new image, possibly new init-container fiaas_application.spec.image = IMAGE2 fiaas_application.metadata.labels["fiaas/deployment_id"] = DEPLOYMENT_ID2 strongbox_groups = [] if "strongbox" in name: strongbox_groups = ["foo", "bar"] fiaas_application.spec.config["extensions"]["strongbox"]["groups"] = strongbox_groups fiaas_application.save() app_uid = fiaas_application.metadata.uid # Check success wait_until(_deploy_success(name, kinds, service_type, IMAGE2, expected, DEPLOYMENT_ID2, strongbox_groups, app_uid=app_uid), patience=PATIENCE) # Cleanup FiaasApplication.delete(name) def cleanup_complete(): for kind in kinds: with pytest.raises(NotFound): kind.get(name) wait_until(cleanup_complete, patience=PATIENCE)
def test_create_new_fiaasapplication(self, post, get): get.side_effect = NotFound() fiaas_app = FiaasApplication( metadata=_create_default_metadata(), spec=_create_default_fiaasapplicationspec()) assert fiaas_app._new expected_call = fiaas_app.as_dict() mock_response = mock.create_autospec(Response) mock_response.json.return_value = expected_call post.return_value = mock_response fiaas_app.save() pytest.helpers.assert_any_call(post, API_BASE_PATH, expected_call)
def test_multiple_ingresses(self, request, kind_logger): with kind_logger(): fiaas_path = "v3/data/examples/multiple_ingress.yml" fiaas_yml = read_yml(request.fspath.dirpath().join("specs").join(fiaas_path).strpath) name = sanitize_resource_name(fiaas_path) expected = { name: read_yml(request.fspath.dirpath().join("e2e_expected/multiple_ingress1.yml").strpath), "{}-1".format(name): read_yml(request.fspath.dirpath().join("e2e_expected/multiple_ingress2.yml").strpath) } metadata = ObjectMeta(name=name, namespace="default", labels={"fiaas/deployment_id": DEPLOYMENT_ID1}) spec = FiaasApplicationSpec(application=name, image=IMAGE1, config=fiaas_yml) fiaas_application = FiaasApplication(metadata=metadata, spec=spec) fiaas_application.save() app_uid = fiaas_application.metadata.uid # Check that deployment status is RUNNING def _assert_status(): status = FiaasApplicationStatus.get(create_name(name, DEPLOYMENT_ID1)) assert status.result == u"RUNNING" assert len(status.logs) > 0 assert any("Saving result RUNNING for default/{}".format(name) in line for line in status.logs) wait_until(_assert_status, patience=PATIENCE) def _check_two_ingresses(): assert Ingress.get(name) assert Ingress.get("{}-1".format(name)) for ingress_name, expected_dict in expected.items(): actual = Ingress.get(ingress_name) assert_k8s_resource_matches(actual, expected_dict, IMAGE1, None, DEPLOYMENT_ID1, None, app_uid) wait_until(_check_two_ingresses, patience=PATIENCE) # Remove 2nd ingress to make sure cleanup works fiaas_application.spec.config["ingress"].pop() fiaas_application.metadata.labels["fiaas/deployment_id"] = DEPLOYMENT_ID2 fiaas_application.save() def _check_one_ingress(): assert Ingress.get(name) with pytest.raises(NotFound): Ingress.get("{}-1".format(name)) wait_until(_check_one_ingress, patience=PATIENCE) # Cleanup FiaasApplication.delete(name) def cleanup_complete(): for name, _ in expected.items(): with pytest.raises(NotFound): Ingress.get(name) wait_until(cleanup_complete, patience=PATIENCE)
def custom_resource_definition(self, request, k8s_version): additional_labels = None if len(request.param) == 2: fiaas_path, expected = request.param elif len(request.param) == 3: fiaas_path, expected, additional_labels = request.param skip_if_crd_not_supported(k8s_version) fiaas_yml = read_yml( request.fspath.dirpath().join("specs").join(fiaas_path).strpath) expected = { kind: read_yml(request.fspath.dirpath().join(path).strpath) for kind, path in expected.items() } name = sanitize_resource_name(fiaas_path) metadata = ObjectMeta(name=name, namespace="default", labels={"fiaas/deployment_id": DEPLOYMENT_ID1}) spec = FiaasApplicationSpec(application=name, image=IMAGE1, config=fiaas_yml, additional_labels=additional_labels) request.addfinalizer(lambda: self._ensure_clean(name, expected)) return name, FiaasApplication(metadata=metadata, spec=spec), expected
def test_update_existing_fiaasapplication(self, put, get): get_response = mock.Mock() get_response.json.return_value = { "kind": "Application", "apiVersion": API_VERSION, "metadata": _create_default_metadata().as_dict(), "spec": _create_default_fiaasapplicationspec().as_dict() } get.return_value = get_response get.side_effect = None spec = _create_default_fiaasapplicationspec(replicas=2, host='example.org', ports=[{ "protocol": 'tcp', "target_port": 1992 }]) fiaas_app = FiaasApplication.get_or_create( metadata=_create_default_metadata(), spec=spec) assert not fiaas_app._new assert fiaas_app.spec.config["namespace"] == NAMESPACE assert fiaas_app.spec.config["replicas"] == 2 assert fiaas_app.spec.config["host"] == 'example.org' assert fiaas_app.spec.config["ports"][0]["protocol"] == 'tcp' assert fiaas_app.spec.config["ports"][0]["target_port"] == 1992 expected_call = fiaas_app.as_dict() mock_response = mock.create_autospec(Response) mock_response.json.return_value = expected_call put.return_value = mock_response fiaas_app.save() pytest.helpers.assert_any_call(put, API_INSTANCE_PATH, expected_call)
def custom_resource_definition_test_case(self, fiaas_path, namespace, labels, expected): fiaas_yml = read_yml(file_relative_path(fiaas_path)) expected = {kind: read_yml_if_exists(path) for kind, path in expected.items()} name = sanitize_resource_name(fiaas_path) metadata = ObjectMeta(name=name, namespace=namespace, labels=merge_dicts(labels, {"fiaas/deployment_id": DEPLOYMENT_ID})) spec = FiaasApplicationSpec(application=name, image=IMAGE, config=fiaas_yml) return name, FiaasApplication(metadata=metadata, spec=spec), expected
def test_delete_fiaasapplication(self, delete): FiaasApplication.delete(NAME, NAMESPACE) pytest.helpers.assert_any_call(delete, API_INSTANCE_PATH)