Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: label review app namespaces #370

Merged
merged 4 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this command is not run, hokusai version reports v0.0.0 (the dummy string in pyproject.toml). that causes conflict with projects that require certain versions of Hokusai (e.g. hokusai-sandbox).

## Testing

### Install Minikube
Expand Down
5 changes: 4 additions & 1 deletion hokusai/commands/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

potentially another use case for a ~/.hokusai.conf config file. i would have a var there like org=acme, then this label can become acme/app-phase: review which should never conflict with any label k8s might automatically add to this resource in the future.

}
})
])
yaml_content = [new_namespace] + yaml_content
Expand Down
54 changes: 49 additions & 5 deletions test/integration/test_review_app.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
import os
import httpretty
import os
import sys

from unittest.mock import patch

from test import HokusaiIntegrationTestCase
from test import HokusaiIntegrationTestCase, TEST_KUBE_CONTEXT
from test.utils import captured_output

import hokusai.commands.namespace

from hokusai import CWD
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):
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):
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'),
Expand All @@ -19,9 +38,34 @@ 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)

@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, self.__class__.HOKUSAI_CONFIG_DIR, 'a-review-app.yml')
with captured_output() as (out, err):
try:
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)
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)