diff --git a/.changes/next-release/bugfix-ecs-72910.json b/.changes/next-release/bugfix-ecs-72910.json new file mode 100644 index 000000000000..e30e03f87828 --- /dev/null +++ b/.changes/next-release/bugfix-ecs-72910.json @@ -0,0 +1,5 @@ +{ + "type": "bugfix", + "category": "ecs", + "description": "Fix `aws ecs deploy` falling back to the correct behavior for `--cluster \"\"` (an empty string, e.g. from an unset shell variable) the same way it already does when `--cluster` is omitted, instead of passing the empty string through to the ECS API" +} diff --git a/awscli/customizations/ecs/deploy.py b/awscli/customizations/ecs/deploy.py index 8743ccb12ed4..1053d4e8c490 100644 --- a/awscli/customizations/ecs/deploy.py +++ b/awscli/customizations/ecs/deploy.py @@ -449,7 +449,7 @@ def __init__(self, session, parsed_args, parsed_globals, user_agent_extra): def get_service_details(self): cluster = self._args.cluster - if cluster is None or '': + if not cluster: cluster = 'default' try: diff --git a/tests/unit/customizations/ecs/test_ecsclient.py b/tests/unit/customizations/ecs/test_ecsclient.py index 4601d581ce0e..b4c33b7fc1ef 100644 --- a/tests/unit/customizations/ecs/test_ecsclient.py +++ b/tests/unit/customizations/ecs/test_ecsclient.py @@ -47,3 +47,40 @@ def test_client_config(self): create_args[1]['config'].user_agent_extra, expected_user_agent_extra, ) + + def _get_service_details_with_cluster(self, cluster): + args = Namespace(cluster=cluster, service='my-service') + test_client = ECSClient( + self.session, args, self.global_args, ECSDeploy.USER_AGENT_EXTRA + ) + test_client._client = mock.Mock() + test_client._client.describe_services.return_value = { + 'services': [ + { + 'serviceArn': ( + 'arn:aws:ecs:us-east-1:123456789012:service/my-service' + ), + 'serviceName': 'my-service', + 'clusterArn': ( + 'arn:aws:ecs:us-east-1:123456789012:cluster/default' + ), + } + ] + } + test_client.get_service_details() + return test_client._client.describe_services.call_args[1]['cluster'] + + def test_get_service_details_defaults_cluster_when_not_specified(self): + used_cluster = self._get_service_details_with_cluster(None) + self.assertEqual(used_cluster, 'default') + + def test_get_service_details_defaults_cluster_when_empty_string(self): + # A caller may pass an empty string for --cluster (e.g. by + # interpolating an unset shell variable), which should be treated + # the same as not specifying a cluster at all. + used_cluster = self._get_service_details_with_cluster('') + self.assertEqual(used_cluster, 'default') + + def test_get_service_details_uses_specified_cluster(self): + used_cluster = self._get_service_details_with_cluster('my-cluster') + self.assertEqual(used_cluster, 'my-cluster')