def test__handle_api_error(error: ApiException, raises_exception: bool) -> None: if raises_exception: with pytest.raises(ApiException): Kubernetes._handle_api_error(error) else: Kubernetes._handle_api_error(error)
def test_create_secret_qa(kubernetes: Kubernetes, test_namespace: str) -> None: track = "qa" project = Project(track=track) kubernetes.create_secret( data={"test_secret": "1234"}, namespace=K8S_NAMESPACE, track=track, project=project, secret_name=project.secret_name, ) kubernetes.get(resource="secret", namespace=K8S_NAMESPACE, name=project.secret_name)
def test_create_mysql_database(kubernetes: Kubernetes, test_namespace: str, helm: Helm) -> None: helm_test_repo_url = os.environ.get("TEST_HELM_REGISTRY", "http://localhost:8080") helm.add_repo("testing", helm_test_repo_url) track = DEFAULT_TRACK mysql_service = MysqlService(track=track, chart="testing/mysql") kubernetes.deploy_service(service=mysql_service, namespace=test_namespace, track=track)
def test_create_secret_stable(kubernetes: Kubernetes, test_namespace: str) -> None: track = DEFAULT_TRACK data = {"test_secret": "1234"} project = Project(track=track) kubernetes.create_secret( data=data, namespace=K8S_NAMESPACE, track=track, project=project, secret_name=project.secret_name, ) kubernetes.get(resource="secret", namespace=K8S_NAMESPACE, name=project.secret_name)
def test__b64_encode_file() -> None: content = "test:$apr1$35522gYe$r3E.NGo0m0bbOXppHr3g0." expected = "dGVzdDokYXByMSQzNTUyMmdZZSRyM0UuTkdvMG0wYmJPWHBwSHIzZzAu" with tempfile.NamedTemporaryFile() as f: encoded_string = str.encode(content, encoding="UTF-8") f.write(encoded_string) f.seek(0) path = Path(f.name) assert Kubernetes._b64_encode_file(path=path) == expected
def test__create_basic_auth_data(kubernetes: Kubernetes) -> None: basic_auth_users = [ BasicAuthUser(username="******", password="******"), BasicAuthUser(username="******", password="******"), ] data = kubernetes._create_basic_auth_data( basic_auth_users=basic_auth_users) auth_data = data["auth"] decoded_data = base64.b64decode(auth_data).decode("UTF-8") user_split = decoded_data.split("\n")[:-1] for i, user in enumerate(user_split): username, password = user.split(":") assert password assert username == basic_auth_users[i].username
def test_delete_all(kubernetes: Kubernetes, test_namespace: str) -> None: track = DEFAULT_TRACK deploy_name = get_deploy_name(track=track) project = Project(track=track) kubernetes.create_secret( data={"test": "test"}, namespace=test_namespace, track=track, project=project, secret_name=project.secret_name, ) kubernetes.delete_all(namespace=test_namespace, labels={"release": deploy_name}) with pytest.raises(Exception): kubernetes.get(resource="secret", name=test_namespace)
def test_lifecycle_hooks_deployment(_: mock.MagicMock) -> None: ns = track = "testing" project = Project(track=track, url="example.com") service = mock.MagicMock() k = Kubernetes(track=track) k.helm = mock.MagicMock(**{"upgrade_chart.return_value.return_code": 0}) mock.patch("kolga.libs.kubernetes.KubeLoggerThread") Plugin, hook_calls = call_tracking_plugin_factory() with load_plugin(Plugin): k.deploy_service(namespace=ns, service=service, track=track) k.create_application_deployment(namespace=ns, project=project, track=track) assert [*hook_calls.keys()] == [ "service_deployment_begin", "service_deployment_complete", "project_deployment_begin", "project_deployment_complete", ]
def test_namespace(kubernetes: Kubernetes) -> Generator[str, None, None]: namespace = kubernetes.create_namespace() yield namespace kubernetes.delete_namespace()
def kubernetes() -> Kubernetes: return Kubernetes()
def test__encode_secret() -> None: test_data = {"password": "******", "username": "******"} expected_data = {"password": "******", "username": "******"} assert Kubernetes._encode_secret(test_data) == expected_data
def test_labels_to_string() -> None: test_labels = {"app": "testapp", "release": "testrelease", "lizard": "-1"} expected_string = "app=testapp,release=testrelease,lizard=-1" assert Kubernetes.labels_to_string(labels=test_labels) == expected_string
def test__is_client_error(value: str, expected: bool) -> None: assert Kubernetes._is_client_error(value) == expected
def test_delete_namespace(kubernetes: Kubernetes, test_namespace: str) -> None: kubernetes.delete(resource="namespace", name=test_namespace, namespace=K8S_NAMESPACE) with pytest.raises(Exception): kubernetes.get(resource="namespace", name=test_namespace)
def test_create_namespace_named(kubernetes: Kubernetes) -> None: namespace = "testing-2" assert kubernetes.create_namespace(namespace) == namespace kubernetes.delete_namespace(namespace)