From 4843eac27c4661396630362d09313808c78fdc9d Mon Sep 17 00:00:00 2001 From: Jian Date: Fri, 18 Aug 2023 11:30:59 -0400 Subject: [PATCH 1/4] feat: label review app namespaces --- hokusai/commands/namespace.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/hokusai/commands/namespace.py b/hokusai/commands/namespace.py index 08765495..0859128e 100644 --- a/hokusai/commands/namespace.py +++ b/hokusai/commands/namespace.py @@ -25,7 +25,10 @@ def create_new_app_yaml(source_file, app_name): ('apiVersion', 'v1'), ('kind', 'Namespace'), ('metadata', { - 'name': clean_string(app_name) + 'name': clean_string(app_name), + 'labels': { + 'app-phase': 'review' + } }) ]) yaml_content = [new_namespace] + yaml_content From fc67e356e8ec5b5bad285b449f2aa3d960579e6d Mon Sep 17 00:00:00 2001 From: Jian Date: Fri, 18 Aug 2023 12:53:23 -0400 Subject: [PATCH 2/4] feat: add test for creating review app namespace --- test/integration/test_review_app.py | 46 +++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/test/integration/test_review_app.py b/test/integration/test_review_app.py index 5d49fa1f..a982af8c 100644 --- a/test/integration/test_review_app.py +++ b/test/integration/test_review_app.py @@ -3,14 +3,32 @@ import sys from unittest.mock import patch -from test import HokusaiIntegrationTestCase +from test import HokusaiIntegrationTestCase, TEST_KUBE_CONTEXT +from test.utils import captured_output +from test.mocks.mock_ecr import MockECR from hokusai import CWD +from hokusai.lib.common import shout +from hokusai.commands import kubernetes from hokusai.commands.namespace import create_new_app_yaml +from hokusai.lib.common import shout +from hokusai.services.kubectl import Kubectl class TestReviewApp(HokusaiIntegrationTestCase): - @patch('hokusai.lib.command.sys.exit') + @classmethod + def setUpClass(cls): + kubernetes.ECR = MockECR + kubernetes.HOKUSAI_CONFIG_DIR = os.path.join(CWD, 'test/fixtures/project/hokusai') + cls.kubernetes_yml = os.path.abspath(os.path.join(kubernetes.HOKUSAI_CONFIG_DIR, 'a-review-app.yml')) + cls.kctl = Kubectl(TEST_KUBE_CONTEXT) + + @classmethod + def tearDownClass(cls): + with captured_output() as (out, err): + shout(cls.kctl.command("delete ns a-review-app"), print_output=True) + @httpretty.activate + @patch('hokusai.lib.command.sys.exit') def test_create_review_app_yaml_file(self, mocked_sys_exit): httpretty.register_uri(httpretty.POST, "https://sts.amazonaws.com/", body=self.fixture('sts-get-caller-identity-response.xml'), @@ -25,3 +43,27 @@ def test_create_review_app_yaml_file(self, mocked_sys_exit): self.assertTrue(os.path.isfile(review_app_yaml_file)) finally: os.remove(review_app_yaml_file) + + @httpretty.activate + @patch('hokusai.lib.command.sys.exit') + def test_01_k8s_create(self, mocked_sys_exit): + httpretty.register_uri(httpretty.POST, "https://sts.amazonaws.com/", + body=self.fixture('sts-get-caller-identity-response.xml'), + content_type="application/xml") + httpretty.register_uri(httpretty.POST, "https://api.ecr.us-east-1.amazonaws.com/", + body=self.fixture('ecr-repositories-response.json'), + content_type="application/x-amz-json-1.1") + + review_app_yaml_file = os.path.join(CWD, 'hokusai', 'a-review-app.yml') + with captured_output() as (out, err): + try: + create_new_app_yaml(os.path.abspath(os.path.join(CWD, 'test/fixtures/project/hokusai/minikube.yml')), 'a-review-app') + mocked_sys_exit.assert_called_once_with(0) + mocked_sys_exit.reset_mock() + kubernetes.k8s_create(TEST_KUBE_CONTEXT, filename=review_app_yaml_file) + mocked_sys_exit.assert_called_once_with(0) + namespaces = shout(self.kctl.command("get ns -l app-phase=review")) + self.assertIn("a-review-app", namespaces) + self.assertNotIn("default", namespaces) + finally: + os.remove(review_app_yaml_file) From 9b4407347319af312c9c35950cd810e8411d7241 Mon Sep 17 00:00:00 2001 From: Jian Date: Fri, 18 Aug 2023 16:39:03 -0400 Subject: [PATCH 3/4] fix: review app yml created in source code dir causing coverage to error --- test/integration/test_review_app.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/test/integration/test_review_app.py b/test/integration/test_review_app.py index a982af8c..85903b2c 100644 --- a/test/integration/test_review_app.py +++ b/test/integration/test_review_app.py @@ -1,14 +1,15 @@ -import os import httpretty +import os import sys + from unittest.mock import patch from test import HokusaiIntegrationTestCase, TEST_KUBE_CONTEXT from test.utils import captured_output -from test.mocks.mock_ecr import MockECR + +import hokusai.commands.namespace from hokusai import CWD -from hokusai.lib.common import shout from hokusai.commands import kubernetes from hokusai.commands.namespace import create_new_app_yaml from hokusai.lib.common import shout @@ -17,10 +18,10 @@ class TestReviewApp(HokusaiIntegrationTestCase): @classmethod def setUpClass(cls): - kubernetes.ECR = MockECR - kubernetes.HOKUSAI_CONFIG_DIR = os.path.join(CWD, 'test/fixtures/project/hokusai') - cls.kubernetes_yml = os.path.abspath(os.path.join(kubernetes.HOKUSAI_CONFIG_DIR, 'a-review-app.yml')) cls.kctl = Kubectl(TEST_KUBE_CONTEXT) + config_dir = 'test/fixtures/project/hokusai' + cls.HOKUSAI_CONFIG_DIR = config_dir + hokusai.commands.namespace.HOKUSAI_CONFIG_DIR = config_dir @classmethod def tearDownClass(cls): @@ -37,9 +38,10 @@ def test_create_review_app_yaml_file(self, mocked_sys_exit): body=self.fixture('ecr-repositories-response.json'), content_type="application/x-amz-json-1.1") - review_app_yaml_file = os.path.join(CWD, 'hokusai', 'a-review-app.yml') + review_app_yaml_file = os.path.join(CWD, self.__class__.HOKUSAI_CONFIG_DIR, 'a-review-app.yml') try: - create_new_app_yaml(os.path.abspath(os.path.join(CWD, 'test/fixtures/project/hokusai/minikube.yml')), 'a-review-app') + create_new_app_yaml(os.path.join(CWD, self.__class__.HOKUSAI_CONFIG_DIR, 'minikube.yml'), 'a-review-app') + mocked_sys_exit.assert_called_once_with(0) self.assertTrue(os.path.isfile(review_app_yaml_file)) finally: os.remove(review_app_yaml_file) @@ -54,10 +56,10 @@ def test_01_k8s_create(self, mocked_sys_exit): body=self.fixture('ecr-repositories-response.json'), content_type="application/x-amz-json-1.1") - review_app_yaml_file = os.path.join(CWD, 'hokusai', 'a-review-app.yml') + review_app_yaml_file = os.path.join(CWD, self.__class__.HOKUSAI_CONFIG_DIR, 'a-review-app.yml') with captured_output() as (out, err): try: - create_new_app_yaml(os.path.abspath(os.path.join(CWD, 'test/fixtures/project/hokusai/minikube.yml')), 'a-review-app') + create_new_app_yaml(os.path.join(CWD, self.__class__.HOKUSAI_CONFIG_DIR, 'minikube.yml'), 'a-review-app') mocked_sys_exit.assert_called_once_with(0) mocked_sys_exit.reset_mock() kubernetes.k8s_create(TEST_KUBE_CONTEXT, filename=review_app_yaml_file) From 89f2229ba5db59716cd58816576b4ac085fa1d77 Mon Sep 17 00:00:00 2001 From: Jian Date: Fri, 18 Aug 2023 16:54:19 -0400 Subject: [PATCH 4/4] fix: local dev hokusai reports version 0.0.0 which some artsy projects do not allow --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index c6f31090..88b68217 100644 --- a/README.md +++ b/README.md @@ -195,6 +195,14 @@ To update dependencies: poetry lock ``` +### Local Dev Hokusai Version + +The version reported by `hokusai version` is taken from `hokusai/_version.py` file. The file is dynamically generated by [setuptools_scm](https://github.com/pypa/setuptools_scm) during CI and it is not version controlled. To have the file in local dev, run: + +``` +python -m setuptools_scm +``` + ## Testing ### Install Minikube