From 5ad78696a6a97bace1cd2b4eb5ccc00c2ae35673 Mon Sep 17 00:00:00 2001 From: Marcel Arns Date: Wed, 20 Nov 2024 11:15:59 +0100 Subject: [PATCH] fixup! feat(action): migrate steps from yaml to python --- actions_helper/commands/get_image_uri.py | 13 +++-- actions_helper/main.py | 14 ++++- tests/test_create_task_definition.py | 58 ++++++------------- tests/test_deregister_task_definition.py | 6 +- .../test_get_active_task_definition_by_tag.py | 1 - tests/test_get_image_uri.py | 6 +- tests/test_run_preflight.py | 5 +- 7 files changed, 41 insertions(+), 62 deletions(-) diff --git a/actions_helper/commands/get_image_uri.py b/actions_helper/commands/get_image_uri.py index d13f22d..2235898 100644 --- a/actions_helper/commands/get_image_uri.py +++ b/actions_helper/commands/get_image_uri.py @@ -3,13 +3,14 @@ def get_image_uri(ecr_client: BaseClient, ecr_repository: str, tag: str) -> str: - (image_tag,) = ecr_client.describe_images(repositoryName=ecr_repository, imageIds=[{"imageTag": tag}])[ - "imageDetails" - ][0]["imageTags"] + (image_tag,) = ecr_client.describe_images( + repositoryName=ecr_repository, + imageIds=[{"imageTag": tag}], + )["imageDetails"][0]["imageTags"] - repository_uri = ecr_client.describe_repositories(repositoryNames=[ecr_repository])["repositories"][0][ - "repositoryUri" - ] + repository_uri = ecr_client.describe_repositories( + repositoryNames=[ecr_repository], + )["repositories"][0]["repositoryUri"] image_uri = f"{repository_uri}:{image_tag}" diff --git a/actions_helper/main.py b/actions_helper/main.py index eb79ccd..f0ae839 100644 --- a/actions_helper/main.py +++ b/actions_helper/main.py @@ -1,3 +1,5 @@ +from enum import StrEnum, auto + import boto3 import click @@ -7,6 +9,12 @@ from actions_helper.commands.run_preflight import run_preflight_container +class Environment(StrEnum): + DEV = auto() + TEST = auto() + LIVE = auto() + + @click.group() def cli(): pass # pragma: no cover @@ -16,7 +24,7 @@ def cli(): name="ecs-deploy", short_help="Deploy production image to AWS ECS", ) -@click.option("--environment", type=click.Choice(["dev", "test", "live"])) +@click.option("--environment", type=click.Choice(Environment)) @click.option("--allow-feature-branch-deployment", type=bool, default=False) @click.option("--ecr-repository", envvar="ECR_REPOSITORY", type=str) @click.option("--deployment-tag", envvar="DEPLOYMENT_TAG", type=str) @@ -25,7 +33,7 @@ def cli(): @click.option("--desired-count", default=2, type=int) @click.option("--aws-region", envvar="AWS_DEFAULT_REGION", type=str) def cmd_ecs_deploy( - environment: str, + environment: Environment, allow_feature_branch_deployment: bool, ecr_repository: str, deployment_tag: str, @@ -34,7 +42,7 @@ def cmd_ecs_deploy( desired_count: str, aws_region: str, ): - if allow_feature_branch_deployment and environment != "dev": + if allow_feature_branch_deployment and environment != Environment.DEV: raise RuntimeError("Deployments from feature branch only allowed for dev environment") ecs_client = boto3.Session(region_name=aws_region).client("ecs") diff --git a/tests/test_create_task_definition.py b/tests/test_create_task_definition.py index 5b06fea..e91fde4 100644 --- a/tests/test_create_task_definition.py +++ b/tests/test_create_task_definition.py @@ -15,15 +15,11 @@ def setUp(self, boto3_client): self.ecs_client = boto3_client self.image_uri = "test/dummy:master-e0428b7" - def test_rendered_task_definition_invalid(self): + def _test(self, msg, side_effect): with ( - self.subTest("Expects exactly one task definition, got empty"), + self.subTest(msg), self.assertRaises(SystemExit), - patch.object( - self.ecs_client, - "describe_task_definition", - side_effect=Mock(return_value={"taskDefinition": {"containerDefinitions": []}}), - ), + patch.object(self.ecs_client, "describe_task_definition", side_effect=side_effect), ): get_rendered_task_definition( ecs_client=self.ecs_client, @@ -31,39 +27,23 @@ def test_rendered_task_definition_invalid(self): image_uri=self.image_uri, ) - with ( - self.subTest("Expects exactly one task definition, got multiple"), - self.assertRaises(SystemExit), - patch.object( - self.ecs_client, - "describe_task_definition", - side_effect=Mock( - return_value={ - "taskDefinition": {"containerDefinitions": [{"image": "dummy1"}, {"image": "dummy2"}]}, - }, - ), - ), - ): - get_rendered_task_definition( - ecs_client=self.ecs_client, - task_definition_arn=Mock(), - image_uri=self.image_uri, - ) - - with ( - self.subTest(f"containerDefinitions 'image' not equals to placeholder text - {PLACEHOLDER_TEXT}"), - patch.object( - self.ecs_client, - "describe_task_definition", - side_effect=Mock(return_value={"taskDefinition": {"containerDefinitions": [{"image": "dummy"}]}}), + def test_rendered_task_definition_invalid(self): + self._test( + msg="Expects exactly one task definition, got empty", + side_effect=Mock(return_value={"taskDefinition": {"containerDefinitions": []}}), + ) + self._test( + msg="Expects exactly one task definition, got multiple", + side_effect=Mock( + return_value={ + "taskDefinition": {"containerDefinitions": [{"image": "dummy1"}, {"image": "dummy2"}]}, + }, ), - self.assertRaises(SystemExit), - ): - get_rendered_task_definition( - ecs_client=self.ecs_client, - task_definition_arn=Mock(), - image_uri=self.image_uri, - ) + ) + self._test( + msg=f"containerDefinitions 'image' not equals to placeholder text - {PLACEHOLDER_TEXT}", + side_effect=Mock(return_value={"taskDefinition": {"containerDefinitions": [{"image": "dummy"}]}}), + ) @patch( "actions_helper.commands.create_task_definition.KEYS_TO_DELETE_FROM_TASK_DEFINITION", diff --git a/tests/test_deregister_task_definition.py b/tests/test_deregister_task_definition.py index 1ee7ba6..4149bea 100644 --- a/tests/test_deregister_task_definition.py +++ b/tests/test_deregister_task_definition.py @@ -10,7 +10,7 @@ class DeregisterTaskDefinitionTestCase(unittest.TestCase): @patch.object(boto3, "client") - def setUp(self, boto3_client) -> None: + def setUp(self, boto3_client): self.ecs_client = boto3_client @staticmethod @@ -62,9 +62,7 @@ def test_deregister_task_definition_failed_pipeline(self): preflight_task_definition_output=None, ) ecs_describe_services_patch.assert_called() - ecs_deregister_patch.assert_called_once_with( - local_exec_task_definition_1, - ) + ecs_deregister_patch.assert_called_once_with(local_exec_task_definition_1) with ( self.subTest("Failed deployment"), diff --git a/tests/test_get_active_task_definition_by_tag.py b/tests/test_get_active_task_definition_by_tag.py index 1a3966a..d8ed767 100644 --- a/tests/test_get_active_task_definition_by_tag.py +++ b/tests/test_get_active_task_definition_by_tag.py @@ -25,7 +25,6 @@ def test_format_tags(self): with self.subTest("valid tag format"): tags = format_tags("key:value, key2:value2") - self.assertEqual(len(tags), 2) self.assertTupleEqual(tags, (Tag("key", "value"), Tag("key2", "value2"))) def test_get_active_definition_by_tag(self): diff --git a/tests/test_get_image_uri.py b/tests/test_get_image_uri.py index a5d946c..5a09daf 100644 --- a/tests/test_get_image_uri.py +++ b/tests/test_get_image_uri.py @@ -25,9 +25,5 @@ def test_get_image_uri(self): side_effect=Mock(return_value={"repositories": [{"repositoryUri": "dummy"}]}), ), ): - image_uri = get_image_uri( - ecr_client=self.ecr_client, - ecr_repository=Mock(), - tag=Mock(), - ) + image_uri = get_image_uri(ecr_client=self.ecr_client, ecr_repository=Mock(), tag=Mock()) self.assertEqual(image_uri, f"dummy:{self.image_tag}") diff --git a/tests/test_run_preflight.py b/tests/test_run_preflight.py index 2438413..90f215a 100644 --- a/tests/test_run_preflight.py +++ b/tests/test_run_preflight.py @@ -4,10 +4,7 @@ import boto3 from actions_helper.commands.run_preflight import run_preflight_container -from tests.utils import ( - TEST_CLUSTER, - TEST_SERVICE, -) +from tests.utils import TEST_CLUSTER, TEST_SERVICE def patch_services(*arg, **kwarg):