Skip to content

Commit

Permalink
fixup! feat(action): migrate steps from yaml to python
Browse files Browse the repository at this point in the history
  • Loading branch information
marns93 committed Nov 20, 2024
1 parent df72b52 commit 5ad7869
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 62 deletions.
13 changes: 7 additions & 6 deletions actions_helper/commands/get_image_uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand Down
14 changes: 11 additions & 3 deletions actions_helper/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from enum import StrEnum, auto

import boto3
import click

Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -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")
Expand Down
58 changes: 19 additions & 39 deletions tests/test_create_task_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,55 +15,35 @@ 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,
task_definition_arn=Mock(),
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",
Expand Down
6 changes: 2 additions & 4 deletions tests/test_deregister_task_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"),
Expand Down
1 change: 0 additions & 1 deletion tests/test_get_active_task_definition_by_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 1 addition & 5 deletions tests/test_get_image_uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
5 changes: 1 addition & 4 deletions tests/test_run_preflight.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 5ad7869

Please sign in to comment.