From 79e47fd98ff005848f1592a62b18a485e199ebba Mon Sep 17 00:00:00 2001 From: Kyle Decot Date: Wed, 27 Feb 2019 15:05:58 -0500 Subject: [PATCH 1/2] Returns an empty list when the cluster does not exist --- moto/ecs/models.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/moto/ecs/models.py b/moto/ecs/models.py index 4a6737ceb..13de67268 100644 --- a/moto/ecs/models.py +++ b/moto/ecs/models.py @@ -428,9 +428,6 @@ class EC2ContainerServiceBackend(BaseBackend): if cluster_name in self.clusters: list_clusters.append( self.clusters[cluster_name].response_object) - else: - raise Exception( - "{0} is not a cluster".format(cluster_name)) return list_clusters def delete_cluster(self, cluster_str): From d181897ec91f3776a44b21d8307b72d407c044c0 Mon Sep 17 00:00:00 2001 From: Nick Venenga Date: Fri, 22 Mar 2019 12:08:02 -0400 Subject: [PATCH 2/2] Add proper failure response to describe_clusters --- moto/ecs/models.py | 19 ++++++++++++++++++- moto/ecs/responses.py | 4 ++-- tests/test_ecs/test_ecs_boto3.py | 9 +++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/moto/ecs/models.py b/moto/ecs/models.py index 13de67268..c9fdb9808 100644 --- a/moto/ecs/models.py +++ b/moto/ecs/models.py @@ -358,6 +358,20 @@ class ContainerInstance(BaseObject): return formatted_attr +class ClusterFailure(BaseObject): + def __init__(self, reason, cluster_name): + self.reason = reason + self.arn = "arn:aws:ecs:us-east-1:012345678910:cluster/{0}".format( + cluster_name) + + @property + def response_object(self): + response_object = self.gen_response_object() + response_object['reason'] = self.reason + response_object['arn'] = self.arn + return response_object + + class ContainerInstanceFailure(BaseObject): def __init__(self, reason, container_instance_id): @@ -419,6 +433,7 @@ class EC2ContainerServiceBackend(BaseBackend): def describe_clusters(self, list_clusters_name=None): list_clusters = [] + failures = [] if list_clusters_name is None: if 'default' in self.clusters: list_clusters.append(self.clusters['default'].response_object) @@ -428,7 +443,9 @@ class EC2ContainerServiceBackend(BaseBackend): if cluster_name in self.clusters: list_clusters.append( self.clusters[cluster_name].response_object) - return list_clusters + else: + failures.append(ClusterFailure('MISSING', cluster_name)) + return list_clusters, failures def delete_cluster(self, cluster_str): cluster_name = cluster_str.split('/')[-1] diff --git a/moto/ecs/responses.py b/moto/ecs/responses.py index e0bfefc02..964ef59d2 100644 --- a/moto/ecs/responses.py +++ b/moto/ecs/responses.py @@ -45,10 +45,10 @@ class EC2ContainerServiceResponse(BaseResponse): def describe_clusters(self): list_clusters_name = self._get_param('clusters') - clusters = self.ecs_backend.describe_clusters(list_clusters_name) + clusters, failures = self.ecs_backend.describe_clusters(list_clusters_name) return json.dumps({ 'clusters': clusters, - 'failures': [] + 'failures': [cluster.response_object for cluster in failures] }) def delete_cluster(self): diff --git a/tests/test_ecs/test_ecs_boto3.py b/tests/test_ecs/test_ecs_boto3.py index a0d470935..3bf25b8fc 100644 --- a/tests/test_ecs/test_ecs_boto3.py +++ b/tests/test_ecs/test_ecs_boto3.py @@ -47,6 +47,15 @@ def test_list_clusters(): 'arn:aws:ecs:us-east-1:012345678910:cluster/test_cluster1') +@mock_ecs +def test_describe_clusters(): + client = boto3.client('ecs', region_name='us-east-1') + response = client.describe_clusters(clusters=["some-cluster"]) + response['failures'].should.contain({ + 'arn': 'arn:aws:ecs:us-east-1:012345678910:cluster/some-cluster', + 'reason': 'MISSING' + }) + @mock_ecs def test_delete_cluster(): client = boto3.client('ecs', region_name='us-east-1')