Merge pull request #1697 from spulec/ecs-tasks

Improve ECS update_service and describing tasks.
This commit is contained in:
Steve Pulec 2018-06-24 20:39:16 -04:00 committed by GitHub
commit bb6da93891
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 4 deletions

11
moto/ecs/exceptions.py Normal file
View File

@ -0,0 +1,11 @@
from __future__ import unicode_literals
from moto.core.exceptions import RESTError
class ServiceNotFoundException(RESTError):
code = 400
def __init__(self, service_name):
super(ServiceNotFoundException, self).__init__(
error_type="ServiceNotFoundException",
message="The service {0} does not exist".format(service_name))

View File

@ -10,6 +10,8 @@ from moto.core import BaseBackend, BaseModel
from moto.ec2 import ec2_backends
from copy import copy
from .exceptions import ServiceNotFoundException
class BaseObject(BaseModel):
@ -601,8 +603,9 @@ class EC2ContainerServiceBackend(BaseBackend):
raise Exception("tasks cannot be empty")
response = []
for cluster, cluster_tasks in self.tasks.items():
for task_id, task in cluster_tasks.items():
if task_id in tasks or task.task_arn in tasks:
for task_arn, task in cluster_tasks.items():
task_id = task_arn.split("/")[-1]
if task_arn in tasks or task.task_arn in tasks or any(task_id in task for task in tasks):
response.append(task)
return response
@ -700,8 +703,7 @@ class EC2ContainerServiceBackend(BaseBackend):
cluster_service_pair].desired_count = desired_count
return self.services[cluster_service_pair]
else:
raise Exception("cluster {0} or service {1} does not exist".format(
cluster_name, service_name))
raise ServiceNotFoundException(service_name)
def delete_service(self, cluster_name, service_name):
cluster_service_pair = '{0}:{1}'.format(cluster_name, service_name)

View File

@ -2,6 +2,7 @@ from __future__ import unicode_literals
from copy import deepcopy
from botocore.exceptions import ClientError
import boto3
import sure # noqa
import json
@ -450,6 +451,21 @@ def test_update_service():
response['service']['desiredCount'].should.equal(0)
@mock_ecs
def test_update_missing_service():
client = boto3.client('ecs', region_name='us-east-1')
_ = client.create_cluster(
clusterName='test_ecs_cluster'
)
client.update_service.when.called_with(
cluster='test_ecs_cluster',
service='test_ecs_service',
taskDefinition='test_ecs_task',
desiredCount=0
).should.throw(ClientError)
@mock_ecs
def test_delete_service():
client = boto3.client('ecs', region_name='us-east-1')
@ -1054,6 +1070,13 @@ def test_describe_tasks():
set([response['tasks'][0]['taskArn'], response['tasks']
[1]['taskArn']]).should.equal(set(tasks_arns))
# Test we can pass task ids instead of ARNs
response = client.describe_tasks(
cluster='test_ecs_cluster',
tasks=[tasks_arns[0].split("/")[-1]]
)
len(response['tasks']).should.equal(1)
@mock_ecs
def describe_task_definition():