Merge pull request #1697 from spulec/ecs-tasks
Improve ECS update_service and describing tasks.
This commit is contained in:
commit
bb6da93891
11
moto/ecs/exceptions.py
Normal file
11
moto/ecs/exceptions.py
Normal 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))
|
@ -10,6 +10,8 @@ from moto.core import BaseBackend, BaseModel
|
|||||||
from moto.ec2 import ec2_backends
|
from moto.ec2 import ec2_backends
|
||||||
from copy import copy
|
from copy import copy
|
||||||
|
|
||||||
|
from .exceptions import ServiceNotFoundException
|
||||||
|
|
||||||
|
|
||||||
class BaseObject(BaseModel):
|
class BaseObject(BaseModel):
|
||||||
|
|
||||||
@ -601,8 +603,9 @@ class EC2ContainerServiceBackend(BaseBackend):
|
|||||||
raise Exception("tasks cannot be empty")
|
raise Exception("tasks cannot be empty")
|
||||||
response = []
|
response = []
|
||||||
for cluster, cluster_tasks in self.tasks.items():
|
for cluster, cluster_tasks in self.tasks.items():
|
||||||
for task_id, task in cluster_tasks.items():
|
for task_arn, task in cluster_tasks.items():
|
||||||
if task_id in tasks or task.task_arn in tasks:
|
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)
|
response.append(task)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@ -700,8 +703,7 @@ class EC2ContainerServiceBackend(BaseBackend):
|
|||||||
cluster_service_pair].desired_count = desired_count
|
cluster_service_pair].desired_count = desired_count
|
||||||
return self.services[cluster_service_pair]
|
return self.services[cluster_service_pair]
|
||||||
else:
|
else:
|
||||||
raise Exception("cluster {0} or service {1} does not exist".format(
|
raise ServiceNotFoundException(service_name)
|
||||||
cluster_name, service_name))
|
|
||||||
|
|
||||||
def delete_service(self, cluster_name, service_name):
|
def delete_service(self, cluster_name, service_name):
|
||||||
cluster_service_pair = '{0}:{1}'.format(cluster_name, service_name)
|
cluster_service_pair = '{0}:{1}'.format(cluster_name, service_name)
|
||||||
|
@ -2,6 +2,7 @@ from __future__ import unicode_literals
|
|||||||
|
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
|
|
||||||
|
from botocore.exceptions import ClientError
|
||||||
import boto3
|
import boto3
|
||||||
import sure # noqa
|
import sure # noqa
|
||||||
import json
|
import json
|
||||||
@ -450,6 +451,21 @@ def test_update_service():
|
|||||||
response['service']['desiredCount'].should.equal(0)
|
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
|
@mock_ecs
|
||||||
def test_delete_service():
|
def test_delete_service():
|
||||||
client = boto3.client('ecs', region_name='us-east-1')
|
client = boto3.client('ecs', region_name='us-east-1')
|
||||||
@ -1054,6 +1070,13 @@ def test_describe_tasks():
|
|||||||
set([response['tasks'][0]['taskArn'], response['tasks']
|
set([response['tasks'][0]['taskArn'], response['tasks']
|
||||||
[1]['taskArn']]).should.equal(set(tasks_arns))
|
[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
|
@mock_ecs
|
||||||
def describe_task_definition():
|
def describe_task_definition():
|
||||||
|
Loading…
Reference in New Issue
Block a user