def test_list_container_instances(self): ecs_client = boto3.client('ecs', region_name='eu-west-1') ec2 = boto3.resource('ec2', region_name='eu-west-1') test_cluster_name = 'test_ecs_cluster' _ = ecs_client.create_cluster( clusterName=test_cluster_name ) instance_to_create = 3 test_instance_arns = [] amiobj, srcimgid = self.create_ami() for i in range(0, instance_to_create): test_instance = ec2.create_instances( ImageId=srcimgid['ImageId'], MinCount=1, MaxCount=1, )[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance) ) response = ecs_client.register_container_instance( cluster=test_cluster_name, instanceIdentityDocument=instance_id_document) test_instance_arns.append(response['containerInstance'][ 'containerInstanceArn']) response = ecs_client.list_container_instances(cluster=test_cluster_name) assert len(response['containerInstanceArns']) == instance_to_create for arn in test_instance_arns: assert arn in response['containerInstanceArns']
def test_get_all_tasks(self): ecs_client = boto3.client('ecs', region_name='eu-west-1') ec2 = boto3.resource('ec2', region_name='eu-west-1') test_cluster_name = 'test_ecs_cluster' _ = ecs_client.create_cluster( clusterName=test_cluster_name ) instance_to_create = 3 test_instance_arns = [] amiobj, srcimgid = self.create_ami() for i in range(0, instance_to_create): test_instance = ec2.create_instances( ImageId=srcimgid['ImageId'], MinCount=1, MaxCount=1, )[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance) ) response = ecs_client.register_container_instance( cluster=test_cluster_name, instanceIdentityDocument=instance_id_document) test_instance_arns.append(response['containerInstance'][ 'containerInstanceArn']) _ = ecs_client.register_task_definition( family='test_ecs_task', containerDefinitions=[ { 'name': 'hello_world', 'image': 'docker/hello-world:latest', 'cpu': 1024, 'memory': 400, 'essential': True, 'environment': [{ 'name': 'AWS_ACCESS_KEY_ID', 'value': 'SOME_ACCESS_KEY' }], 'logConfiguration': {'logDriver': 'json-file'} } ] ) tasks_arns = [ task['taskArn'] for task in ecs_client.run_task( cluster='test_ecs_cluster', overrides={}, taskDefinition='test_ecs_task', count=2, startedBy='moto' )['tasks'] ] response = ecs_client.describe_tasks( cluster='test_ecs_cluster', tasks=tasks_arns ) assert len(response['tasks']) == 2
def test_list_container_instances(): ecs_client = boto3.client('ecs', region_name='us-east-1') ec2 = boto3.resource('ec2', region_name='us-east-1') test_cluster_name = 'test_ecs_cluster' _ = ecs_client.create_cluster(clusterName=test_cluster_name) instance_to_create = 3 test_instance_arns = [] for i in range(0, instance_to_create): test_instance = ec2.create_instances( ImageId="ami-1234abcd", MinCount=1, MaxCount=1, )[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance)) response = ecs_client.register_container_instance( cluster=test_cluster_name, instanceIdentityDocument=instance_id_document) test_instance_arns.append( response['containerInstance']['containerInstanceArn']) response = ecs_client.list_container_instances(cluster=test_cluster_name) len(response['containerInstanceArns']).should.equal(instance_to_create) for arn in test_instance_arns: response['containerInstanceArns'].should.contain(arn)
def test_get_container_instances(fetcher, ecs, ec2): cluster_name = "test_ecs_cluster" response = ecs.client.create_cluster(clusterName=cluster_name) cluster_arn = response["cluster"]["clusterArn"] test_instances = ec2.resource.create_instances(ImageId="ami-bb9a6bc2", MinCount=3, MaxCount=3) instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instances[0])) response = ecs.client.register_container_instance( cluster=cluster_name, instanceIdentityDocument=instance_id_document) toolbox.pstruct(response, "register_container_instance") container_instances = fetcher.get_container_instances(cluster_arn) assert len(container_instances) == 1 assert (response["containerInstance"]["containerInstanceArn"] in container_instances.keys()) container_instances = fetcher.get_container_instances( cluster_arn, arns=fetcher.get_container_instance_arns(cluster_arn)) assert len(container_instances) == 1 assert (response["containerInstance"]["containerInstanceArn"] in container_instances.keys())
def test_get_task_arns_with_run_task(fetcher, ecs, ec2): cluster_name = "test_ecs_cluster" response = ecs.client.create_cluster(clusterName=cluster_name) cluster_arn = response["cluster"]["clusterArn"] test_instances = ec2.resource.create_instances( ImageId="ami-bb9a6bc2", MinCount=3, MaxCount=3 ) for test_instance in test_instances: instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance) ) _ = ecs.client.register_container_instance( cluster=cluster_name, instanceIdentityDocument=instance_id_document ) _ = ecs.client.register_task_definition( family="test_ecs_task", containerDefinitions=[ { "name": "hello_world", "image": "docker/hello-world:latest", "cpu": 1024, "memory": 400, "essential": True, "logConfiguration": {"logDriver": "json-file"}, } ], ) response_ec2 = ecs.client.run_task( cluster=cluster_name, overrides={}, taskDefinition="test_ecs_task", count=2, launchType="EC2", startedBy="moto", ) toolbox.pstruct(response, "run_task ec2") _ = ecs.client.run_task( cluster=cluster_name, overrides={}, taskDefinition="test_ecs_task", count=1, launchType="EC2", startedBy="moto", ) toolbox.pstruct(response, "run_task fargate") assert len(response_ec2["tasks"]) == 2 # Currently moto does not support launch type. assert "EC2" != response_ec2["tasks"][0].get("launchType", None) assert "EC2" != response_ec2["tasks"][1].get("launchType", None) task_arns = fetcher.get_task_arns(cluster_arn=cluster_arn) assert len(task_arns) == 3
def cluster(): boto3.setup_default_session(region_name='eu-west-1') moto.mock_ecs().start() moto.mock_ec2().start() ec2 = boto3.resource('ec2', region_name='eu-west-1') ecs = boto3.client('ecs', region_name='eu-west-1') test_instance = ec2.create_instances( ImageId="ami-1234abcd", MinCount=1, MaxCount=1, )[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance)) cluster = ecs.create_cluster(clusterName='default') ecs.register_container_instance( cluster='default', instanceIdentityDocument=instance_id_document) yield cluster moto.mock_ecs().stop() moto.mock_ec2().stop()
def test_put_account_setting_changes_containerinstance_arn(): ecs_client = boto3.client("ecs", region_name="us-east-1") ec2 = boto3.resource("ec2", region_name="us-east-1") test_cluster_name = "test_ecs_cluster" ecs_client.create_cluster(clusterName=test_cluster_name) test_instance = ec2.create_instances(ImageId=EXAMPLE_AMI_ID, MinCount=1, MaxCount=1)[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance)) # Initial ARN should be long response = ecs_client.register_container_instance( cluster=test_cluster_name, instanceIdentityDocument=instance_id_document) full_arn = response["containerInstance"]["containerInstanceArn"] full_arn.should.match( f"arn:aws:ecs:us-east-1:{ACCOUNT_ID}:container-instance/{test_cluster_name}/[a-z0-9-]+$" ) # Now disable long-format ecs_client.put_account_setting(name="containerInstanceLongArnFormat", value="disabled") response = ecs_client.register_container_instance( cluster=test_cluster_name, instanceIdentityDocument=instance_id_document) full_arn = response["containerInstance"]["containerInstanceArn"] full_arn.should.match( f"arn:aws:ecs:us-east-1:{ACCOUNT_ID}:container-instance/[a-z0-9-]+$")
def test_list_container_instances(): ecs_client = boto3.client('ecs', region_name='us-east-1') ec2 = boto3.resource('ec2', region_name='us-east-1') test_cluster_name = 'test_ecs_cluster' _ = ecs_client.create_cluster( clusterName=test_cluster_name ) instance_to_create = 3 test_instance_arns = [] for i in range(0, instance_to_create): test_instance = ec2.create_instances( ImageId="ami-1234abcd", MinCount=1, MaxCount=1, )[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance) ) response = ecs_client.register_container_instance( cluster=test_cluster_name, instanceIdentityDocument=instance_id_document) test_instance_arns.append(response['containerInstance']['containerInstanceArn']) response = ecs_client.list_container_instances(cluster=test_cluster_name) len(response['containerInstanceArns']).should.equal(instance_to_create) for arn in test_instance_arns: response['containerInstanceArns'].should.contain(arn)
def test_describe_container_instances(): ecs_client = boto3.client('ecs', region_name='us-east-1') ec2 = boto3.resource('ec2', region_name='us-east-1') test_cluster_name = 'test_ecs_cluster' _ = ecs_client.create_cluster( clusterName=test_cluster_name ) instance_to_create = 3 test_instance_arns = [] for i in range(0, instance_to_create): test_instance = ec2.create_instances( ImageId="ami-1234abcd", MinCount=1, MaxCount=1, )[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance) ) response = ecs_client.register_container_instance( cluster=test_cluster_name, instanceIdentityDocument=instance_id_document) test_instance_arns.append(response['containerInstance']['containerInstanceArn']) test_instance_ids = list(map((lambda x: x.split('/')[1]), test_instance_arns)) response = ecs_client.describe_container_instances(cluster=test_cluster_name, containerInstances=test_instance_ids) len(response['failures']).should.equal(0) len(response['containerInstances']).should.equal(instance_to_create) response_arns = [ci['containerInstanceArn'] for ci in response['containerInstances']] for arn in test_instance_arns: response_arns.should.contain(arn)
def ecs_container_instance_arn(ec2_instance_id, ecs_cluster_name, ecs_cluster_arn): ecs_client = boto3.client('ecs') ec2_client = boto3.client('ec2') ec2_resource = boto3.resource('ec2') instance = ec2_resource.Instance(ec2_instance_id) instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(instance)) resp = ecs_client.register_container_instance( cluster=ecs_cluster_name, instanceIdentityDocument=instance_id_document) instance_arn = resp['containerInstance']['containerInstanceArn'] ec2_client.create_tags(Resources=[ ec2_instance_id, ], Tags=[{ 'Key': 'clusterArn', 'Value': ecs_cluster_arn }, { 'Key': 'containerInstanceArn', 'Value': instance_arn }]) yield instance_arn
def test_get_container_instance_arns(fetcher, ecs, ec2): cluster_name = "test_ecs_cluster" response = ecs.client.create_cluster(clusterName=cluster_name) cluster_arn = response["cluster"]["clusterArn"] test_instance = ec2.resource.create_instances(ImageId="ami-bb9a6bc2", MinCount=1, MaxCount=1)[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance)) response = ecs.client.register_container_instance( cluster=cluster_name, instanceIdentityDocument=instance_id_document) expected_container_instance_arn = response["containerInstance"][ "containerInstanceArn"] assert response["containerInstance"]["ec2InstanceId"] == test_instance.id container_instance_arns = fetcher.get_container_instance_arns( cluster_arn=cluster_arn) assert len(container_instance_arns) == 1 assert container_instance_arns[0] == expected_container_instance_arn
def test_stop_task(): client = boto3.client('ecs', region_name='us-east-1') ec2 = boto3.resource('ec2', region_name='us-east-1') test_cluster_name = 'test_ecs_cluster' _ = client.create_cluster( clusterName=test_cluster_name ) test_instance = ec2.create_instances( ImageId="ami-1234abcd", MinCount=1, MaxCount=1, )[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance) ) _ = client.register_container_instance( cluster=test_cluster_name, instanceIdentityDocument=instance_id_document ) _ = client.register_task_definition( family='test_ecs_task', containerDefinitions=[ { 'name': 'hello_world', 'image': 'docker/hello-world:latest', 'cpu': 1024, 'memory': 400, 'essential': True, 'environment': [{ 'name': 'AWS_ACCESS_KEY_ID', 'value': 'SOME_ACCESS_KEY' }], 'logConfiguration': {'logDriver': 'json-file'} } ] ) run_response = client.run_task( cluster='test_ecs_cluster', overrides={}, taskDefinition='test_ecs_task', count=1, startedBy='moto' ) stop_response = client.stop_task( cluster='test_ecs_cluster', task=run_response['tasks'][0].get('taskArn'), reason='moto testing' ) stop_response['task']['taskArn'].should.equal( run_response['tasks'][0].get('taskArn')) stop_response['task']['lastStatus'].should.equal('STOPPED') stop_response['task']['desiredStatus'].should.equal('STOPPED') stop_response['task']['stoppedReason'].should.equal('moto testing')
def test_describe_tasks(): client = boto3.client('ecs', region_name='us-east-1') ec2 = boto3.resource('ec2', region_name='us-east-1') test_cluster_name = 'test_ecs_cluster' _ = client.create_cluster( clusterName=test_cluster_name ) test_instance = ec2.create_instances( ImageId="ami-1234abcd", MinCount=1, MaxCount=1, )[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance) ) response = client.register_container_instance( cluster=test_cluster_name, instanceIdentityDocument=instance_id_document ) _ = client.register_task_definition( family='test_ecs_task', containerDefinitions=[ { 'name': 'hello_world', 'image': 'docker/hello-world:latest', 'cpu': 1024, 'memory': 400, 'essential': True, 'environment': [{ 'name': 'AWS_ACCESS_KEY_ID', 'value': 'SOME_ACCESS_KEY' }], 'logConfiguration': {'logDriver': 'json-file'} } ] ) tasks_arns = [ task['taskArn'] for task in client.run_task( cluster='test_ecs_cluster', overrides={}, taskDefinition='test_ecs_task', count=2, startedBy='moto' )['tasks'] ] response = client.describe_tasks( cluster='test_ecs_cluster', tasks=tasks_arns ) len(response['tasks']).should.equal(2) set([response['tasks'][0]['taskArn'], response['tasks'] [1]['taskArn']]).should.equal(set(tasks_arns))
def test_run_task(): client = boto3.client('ecs', region_name='us-east-1') ec2 = boto3.resource('ec2', region_name='us-east-1') test_cluster_name = 'test_ecs_cluster' _ = client.create_cluster( clusterName=test_cluster_name ) test_instance = ec2.create_instances( ImageId="ami-1234abcd", MinCount=1, MaxCount=1, )[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance) ) response = client.register_container_instance( cluster=test_cluster_name, instanceIdentityDocument=instance_id_document ) _ = client.register_task_definition( family='test_ecs_task', containerDefinitions=[ { 'name': 'hello_world', 'image': 'docker/hello-world:latest', 'cpu': 1024, 'memory': 400, 'essential': True, 'environment': [{ 'name': 'AWS_ACCESS_KEY_ID', 'value': 'SOME_ACCESS_KEY' }], 'logConfiguration': {'logDriver': 'json-file'} } ] ) response = client.run_task( cluster='test_ecs_cluster', overrides={}, taskDefinition='test_ecs_task', count=2, startedBy='moto' ) len(response['tasks']).should.equal(2) response['tasks'][0]['taskArn'].should.contain('arn:aws:ecs:us-east-1:012345678910:task/') response['tasks'][0]['clusterArn'].should.equal('arn:aws:ecs:us-east-1:012345678910:cluster/test_ecs_cluster') response['tasks'][0]['taskDefinitionArn'].should.equal('arn:aws:ecs:us-east-1:012345678910:task-definition/test_ecs_task:1') response['tasks'][0]['containerInstanceArn'].should.contain('arn:aws:ecs:us-east-1:012345678910:container-instance/') response['tasks'][0]['overrides'].should.equal({}) response['tasks'][0]['lastStatus'].should.equal("RUNNING") response['tasks'][0]['desiredStatus'].should.equal("RUNNING") response['tasks'][0]['startedBy'].should.equal("moto") response['tasks'][0]['stoppedReason'].should.equal("")
def test_stop_task(): client = boto3.client('ecs', region_name='us-east-1') ec2 = boto3.resource('ec2', region_name='us-east-1') test_cluster_name = 'test_ecs_cluster' _ = client.create_cluster(clusterName=test_cluster_name) test_instance = ec2.create_instances( ImageId="ami-1234abcd", MinCount=1, MaxCount=1, )[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance)) _ = client.register_container_instance( cluster=test_cluster_name, instanceIdentityDocument=instance_id_document) _ = client.register_task_definition(family='test_ecs_task', containerDefinitions=[{ 'name': 'hello_world', 'image': 'docker/hello-world:latest', 'cpu': 1024, 'memory': 400, 'essential': True, 'environment': [{ 'name': 'AWS_ACCESS_KEY_ID', 'value': 'SOME_ACCESS_KEY' }], 'logConfiguration': { 'logDriver': 'json-file' } }]) run_response = client.run_task(cluster='test_ecs_cluster', overrides={}, taskDefinition='test_ecs_task', count=1, startedBy='moto') stop_response = client.stop_task( cluster='test_ecs_cluster', task=run_response['tasks'][0].get('taskArn'), reason='moto testing') stop_response['task']['taskArn'].should.equal( run_response['tasks'][0].get('taskArn')) stop_response['task']['lastStatus'].should.equal('STOPPED') stop_response['task']['desiredStatus'].should.equal('STOPPED') stop_response['task']['stoppedReason'].should.equal('moto testing')
def test_describe_tasks(): client = boto3.client('ecs', region_name='us-east-1') ec2 = boto3.resource('ec2', region_name='us-east-1') test_cluster_name = 'test_ecs_cluster' _ = client.create_cluster(clusterName=test_cluster_name) test_instance = ec2.create_instances( ImageId="ami-1234abcd", MinCount=1, MaxCount=1, )[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance)) response = client.register_container_instance( cluster=test_cluster_name, instanceIdentityDocument=instance_id_document) _ = client.register_task_definition(family='test_ecs_task', containerDefinitions=[{ 'name': 'hello_world', 'image': 'docker/hello-world:latest', 'cpu': 1024, 'memory': 400, 'essential': True, 'environment': [{ 'name': 'AWS_ACCESS_KEY_ID', 'value': 'SOME_ACCESS_KEY' }], 'logConfiguration': { 'logDriver': 'json-file' } }]) tasks_arns = [ task['taskArn'] for task in client.run_task(cluster='test_ecs_cluster', overrides={}, taskDefinition='test_ecs_task', count=2, startedBy='moto')['tasks'] ] response = client.describe_tasks(cluster='test_ecs_cluster', tasks=tasks_arns) len(response['tasks']).should.equal(2) set([response['tasks'][0]['taskArn'], response['tasks'][1]['taskArn']]).should.equal(set(tasks_arns))
def test_get_tasks(fetcher, ecs, ec2): cluster_name = "test_ecs_cluster" response = ecs.client.create_cluster(clusterName=cluster_name) cluster_arn = response["cluster"]["clusterArn"] test_instances = ec2.resource.create_instances( ImageId="ami-bb9a6bc2", MinCount=3, MaxCount=3 ) for test_instance in test_instances: instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance) ) _ = ecs.client.register_container_instance( cluster=cluster_name, instanceIdentityDocument=instance_id_document ) _ = ecs.client.register_task_definition( family="test_ecs_task", containerDefinitions=[ { "name": "hello_world", "image": "docker/hello-world:latest", "cpu": 1024, "memory": 400, "essential": True, "logConfiguration": {"logDriver": "json-file"}, } ], ) response = ecs.client.run_task( cluster=cluster_name, overrides={}, taskDefinition="test_ecs_task", count=2, launchType="EC2", startedBy="moto", ) tasks = fetcher.get_tasks(cluster_arn=cluster_arn) assert len(tasks) == 2 assert response["tasks"][0]["taskArn"] in tasks.keys() assert response["tasks"][1]["taskArn"] in tasks.keys() tasks = fetcher.get_tasks( cluster_arn=cluster_arn, task_arns=fetcher.get_task_arns(cluster_arn=cluster_arn) ) assert len(tasks) == 2 assert response["tasks"][0]["taskArn"] in tasks.keys() assert response["tasks"][1]["taskArn"] in tasks.keys()
def test_run_task_default_cluster_new_arn_format(): client = boto3.client("ecs", region_name="us-east-1") ec2 = boto3.resource("ec2", region_name="us-east-1") test_cluster_name = "default" client.create_cluster(clusterName=test_cluster_name) test_instance = ec2.create_instances(ImageId=EXAMPLE_AMI_ID, MinCount=1, MaxCount=1)[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance)) client.register_container_instance( cluster=test_cluster_name, instanceIdentityDocument=instance_id_document) client.register_task_definition( family="test_ecs_task", containerDefinitions=[{ "name": "hello_world", "image": "docker/hello-world:latest", "cpu": 1024, "memory": 400, }], ) # Initial ARN is long-format client.put_account_setting(name="taskLongArnFormat", value="enabled") response = client.run_task( launchType="FARGATE", overrides={}, taskDefinition="test_ecs_task", count=1, startedBy="moto", ) response["tasks"][0]["taskArn"].should.match( f"arn:aws:ecs:us-east-1:{ACCOUNT_ID}:task/{test_cluster_name}/[a-z0-9-]+$" ) # Enable short-format for the next task client.put_account_setting(name="taskLongArnFormat", value="disabled") response = client.run_task( launchType="FARGATE", overrides={}, taskDefinition="test_ecs_task", count=1, startedBy="moto", ) response["tasks"][0]["taskArn"].should.match( f"arn:aws:ecs:us-east-1:{ACCOUNT_ID}:task/[a-z0-9-]+$")
def test_stop_task(self): from lambda_handler import fargate client = boto3.client("ecs", region_name="us-east-1") response = client.create_cluster(clusterName="test_ecs_cluster") client = boto3.client("ecs", region_name="us-east-1") ec2 = boto3.resource("ec2", region_name="us-east-1") test_cluster_name = "test_ecs_cluster" test_instance = ec2.create_instances( ImageId="ami-1234abcd", MinCount=1, MaxCount=1 )[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance) ) response = client.register_container_instance( cluster=test_cluster_name, instanceIdentityDocument=instance_id_document ) _ = client.register_task_definition( family="test_ecs_task", containerDefinitions=[ { "name": "hello_world", "image": "docker/hello-world:latest", "cpu": 1024, "memory": 400, "essential": True, "environment": [ {"name": "AWS_ACCESS_KEY_ID", "value": "SOME_ACCESS_KEY"} ], "logConfiguration": {"logDriver": "json-file"}, } ], ) response = client.run_task( cluster="test_ecs_cluster", overrides={}, taskDefinition="test_ecs_task", count=2, startedBy="moto", ) task_arn = response["tasks"][0]["taskArn"] boto_session = boto3.session.Session(region_name="us-east-1") task_dict = dict(taskArn=task_arn, clusterArn=test_cluster_name) result = fargate.stop_task(boto_session=boto_session, task_dict=task_dict) assert result is not None
def ecs_init(): client = boto3.client("ecs") ec2 = boto3.resource("ec2") client.create_cluster(clusterName="abc") test_instance = ec2.create_instances(MinCount=1, MaxCount=1)[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance)) client.register_container_instance( cluster="abc", instanceIdentityDocument=instance_id_document) client.register_task_definition( family="xyz", containerDefinitions=[], )
def test_update_container_instances_state(): ecs_client = boto3.client('ecs', region_name='us-east-1') ec2 = boto3.resource('ec2', region_name='us-east-1') test_cluster_name = 'test_ecs_cluster' _ = ecs_client.create_cluster( clusterName=test_cluster_name ) instance_to_create = 3 test_instance_arns = [] for i in range(0, instance_to_create): test_instance = ec2.create_instances( ImageId="ami-1234abcd", MinCount=1, MaxCount=1, )[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance) ) response = ecs_client.register_container_instance( cluster=test_cluster_name, instanceIdentityDocument=instance_id_document) test_instance_arns.append(response['containerInstance']['containerInstanceArn']) test_instance_ids = list(map((lambda x: x.split('/')[1]), test_instance_arns)) response = ecs_client.update_container_instances_state(cluster=test_cluster_name, containerInstances=test_instance_ids, status='DRAINING') len(response['failures']).should.equal(0) len(response['containerInstances']).should.equal(instance_to_create) response_statuses = [ci['status'] for ci in response['containerInstances']] for status in response_statuses: status.should.equal('DRAINING') response = ecs_client.update_container_instances_state(cluster=test_cluster_name, containerInstances=test_instance_ids, status='DRAINING') len(response['failures']).should.equal(0) len(response['containerInstances']).should.equal(instance_to_create) response_statuses = [ci['status'] for ci in response['containerInstances']] for status in response_statuses: status.should.equal('DRAINING') response = ecs_client.update_container_instances_state(cluster=test_cluster_name, containerInstances=test_instance_ids, status='ACTIVE') len(response['failures']).should.equal(0) len(response['containerInstances']).should.equal(instance_to_create) response_statuses = [ci['status'] for ci in response['containerInstances']] for status in response_statuses: status.should.equal('ACTIVE') ecs_client.update_container_instances_state.when.called_with(cluster=test_cluster_name, containerInstances=test_instance_ids, status='test_status').should.throw(Exception)
def ecs_cluster(autoscaling_group): fake_ecs_client = boto3.client('ecs') fake_ec2_client = boto3.client('ec2') cluster_name = 'test_ecs_cluster' print(f"Creating ecs cluster {cluster_name}") _, instance_id = autoscaling_group cluster_response = fake_ecs_client.create_cluster( clusterName=cluster_name ) ec2 = boto3.resource('ec2') instance = ec2.Instance(instance_id) instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(instance) ) container_instance_resp = fake_ecs_client.register_container_instance( cluster=cluster_name, instanceIdentityDocument=instance_id_document ) container_instance_arn = \ container_instance_resp['containerInstance']['containerInstanceArn'] cluster_arn = cluster_response['cluster']['clusterArn'] fake_ec2_client.create_tags( Resources=[ instance_id, ], Tags=[ { 'Key': 'clusterArn', 'Value': cluster_arn }, { 'Key': 'containerInstanceArn', 'Value': container_instance_arn } ] ) yield cluster_arn, cluster_arn, container_instance_arn
def test_register_container_instance(): ecs_client = boto3.client('ecs', region_name='us-east-1') ec2 = boto3.resource('ec2', region_name='us-east-1') test_cluster_name = 'test_ecs_cluster' _ = ecs_client.create_cluster( clusterName=test_cluster_name ) test_instance = ec2.create_instances( ImageId="ami-1234abcd", MinCount=1, MaxCount=1, )[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance) ) response = ecs_client.register_container_instance( cluster=test_cluster_name, instanceIdentityDocument=instance_id_document ) response['containerInstance'][ 'ec2InstanceId'].should.equal(test_instance.id) full_arn = response['containerInstance']['containerInstanceArn'] arn_part = full_arn.split('/') arn_part[0].should.equal( 'arn:aws:ecs:us-east-1:012345678910:container-instance') arn_part[1].should.equal(str(UUID(arn_part[1]))) response['containerInstance']['status'].should.equal('ACTIVE') len(response['containerInstance']['registeredResources']).should.equal(0) len(response['containerInstance']['remainingResources']).should.equal(0) response['containerInstance']['agentConnected'].should.equal(True) response['containerInstance']['versionInfo'][ 'agentVersion'].should.equal('1.0.0') response['containerInstance']['versionInfo'][ 'agentHash'].should.equal('4023248') response['containerInstance']['versionInfo'][ 'dockerVersion'].should.equal('DockerVersion: 1.5.0')
def test_check_ecs_service_deployment_daemon_service_check_verifies_task_on_all_instances( self): # add another instance to the ecs cluster test_cluster_name = self.cluster['cluster']['clusterName'] test_instance = self.ec2_client.create_instances( ImageId="ami-1234abcd", MinCount=1, MaxCount=1, )[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance)) response = self.ecs_client.register_container_instance( cluster=test_cluster_name, instanceIdentityDocument=instance_id_document) new_container_instance_id = response['containerInstance'][ 'containerInstanceArn'].split('/')[-1] ecs_deploy_checker = checker.ECSDeployDaemonServiceChecker( 'us-east-1', self.cluster['cluster']['clusterArn'], self.service['service']['serviceArn'], self.task_definition['taskDefinition']['taskDefinitionArn'], 10, 1, ) # Verify daemon check fails because task is not running on one of the instances self.assertFalse( ecs_deploy_checker.check_daemon_service_is_fully_deployed()) # Start task on old instance and verify check still fails self.start_task(self.container_instance_id) self.assertFalse( ecs_deploy_checker.check_daemon_service_is_fully_deployed()) # Now start task on the new instance and verify check passes self.start_task(new_container_instance_id) self.assertTrue( ecs_deploy_checker.check_daemon_service_is_fully_deployed())
def cluster(): with moto.mock_ecs(), moto.mock_ec2(): boto3.setup_default_session(region_name="eu-west-1") ec2 = boto3.resource("ec2", region_name="eu-west-1") ecs = boto3.client("ecs", region_name="eu-west-1") known_amis = list(ec2_backend.describe_images()) test_instance = ec2.create_instances(ImageId=known_amis[0].id, MinCount=1, MaxCount=1)[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance)) cluster = ecs.create_cluster(clusterName="default") ecs.register_container_instance( cluster="default", instanceIdentityDocument=instance_id_document) yield cluster
def setUp(self): self.ecs_client = boto3.client('ecs', region_name='ca-central-1') self.ec2_client = boto3.client('ec2', region_name='ca-central-1') self.ec2 = boto3.resource('ec2', region_name='ca-central-1') # create a VPC self.vpc = self.ec2_client.create_vpc(CidrBlock='172.12.0.0/16') # create a route table self.route_table = self.ec2_client.create_route_table( VpcId=self.vpc['Vpc']['VpcId']) # create the OpenVPN ECS cluster self.test_cluster_name = 'test_ecs_cluster' self.ecs_client.create_cluster(clusterName=self.test_cluster_name) # create two instances and register them to the OpenVPN ECS cluster instance_to_create = 2 self.test_instance_ids = [] for i in range(0, instance_to_create): test_instance = self.ec2.create_instances( ImageId='ami-1234abcd', MinCount=1, MaxCount=1, )[0] self.instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance)) response = self.ecs_client.register_container_instance( cluster=self.test_cluster_name, instanceIdentityDocument=self.instance_id_document) self.test_instance_ids.append( response['containerInstance']['ec2InstanceId']) # create the internal cidr route entries self.internal_ip_cidrs = ['1.0.0.0/8', '2.0.0.0/8', '3.0.0.0/8'] for cidr in self.internal_ip_cidrs: self.ec2_client.create_route( RouteTableId=self.route_table['RouteTable']['RouteTableId'], DestinationCidrBlock=cidr, InstanceId=self.test_instance_ids[0])
def test_register_container_instance(): ecs_client = boto3.client('ecs', region_name='us-east-1') ec2 = boto3.resource('ec2', region_name='us-east-1') test_cluster_name = 'test_ecs_cluster' _ = ecs_client.create_cluster(clusterName=test_cluster_name) test_instance = ec2.create_instances( ImageId="ami-1234abcd", MinCount=1, MaxCount=1, )[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance)) response = ecs_client.register_container_instance( cluster=test_cluster_name, instanceIdentityDocument=instance_id_document) response['containerInstance']['ec2InstanceId'].should.equal( test_instance.id) full_arn = response['containerInstance']['containerInstanceArn'] arn_part = full_arn.split('/') arn_part[0].should.equal( 'arn:aws:ecs:us-east-1:012345678910:container-instance') arn_part[1].should.equal(str(UUID(arn_part[1]))) response['containerInstance']['status'].should.equal('ACTIVE') len(response['containerInstance']['registeredResources']).should.equal(0) len(response['containerInstance']['remainingResources']).should.equal(0) response['containerInstance']['agentConnected'].should.equal(True) response['containerInstance']['versionInfo']['agentVersion'].should.equal( '1.0.0') response['containerInstance']['versionInfo']['agentHash'].should.equal( '4023248') response['containerInstance']['versionInfo']['dockerVersion'].should.equal( 'DockerVersion: 1.5.0')
def cluster(): """Create the mock Airflow cluster. moto doesn't support the Fargate launch type, so we have to pretend like we're going to launch our containers in EC2. There's a little hand waving to make this work. moto comes with some predefined images that seem to work fine. Also see the ``patch_cluster_config`` fixture below. """ C = namedtuple('C', ['name', 'scheduler', 'worker', 'web']) cluster = C('airflow-test', 'airflow-test-scheduler', 'airflow-test-worker', 'airflow-test-web') with mock_ecs(), mock_ec2(): ec2_client = boto3.client('ec2') ec2 = boto3.resource('ec2') ecs = boto3.client('ecs') image = ec2_client.describe_images()['Images'][0] instance = ec2.create_instances(ImageId=image['ImageId'], MinCount=1, MaxCount=1)[0] doc = json.dumps(generate_instance_identity_document(instance)) ecs.create_cluster(clusterName=cluster.name) ecs.register_container_instance(cluster=cluster.name, instanceIdentityDocument=doc) for service in cluster[1:]: ecs.register_task_definition(family=service, containerDefinitions=[]) ecs.create_service(cluster=cluster.name, serviceName=service, desiredCount=1, taskDefinition=f'{service}:1') ecs.update_service(cluster=cluster.name, service=cluster.worker, desiredCount=3) yield cluster
def test_describe_container_instances(): ecs_client = boto3.client('ecs', region_name='us-east-1') ec2 = boto3.resource('ec2', region_name='us-east-1') test_cluster_name = 'test_ecs_cluster' _ = ecs_client.create_cluster(clusterName=test_cluster_name) instance_to_create = 3 test_instance_arns = [] for i in range(0, instance_to_create): test_instance = ec2.create_instances( ImageId="ami-1234abcd", MinCount=1, MaxCount=1, )[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance)) response = ecs_client.register_container_instance( cluster=test_cluster_name, instanceIdentityDocument=instance_id_document) test_instance_arns.append( response['containerInstance']['containerInstanceArn']) test_instance_ids = list( map((lambda x: x.split('/')[1]), test_instance_arns)) response = ecs_client.describe_container_instances( cluster=test_cluster_name, containerInstances=test_instance_ids) len(response['failures']).should.equal(0) len(response['containerInstances']).should.equal(instance_to_create) response_arns = [ ci['containerInstanceArn'] for ci in response['containerInstances'] ] for arn in test_instance_arns: response_arns.should.contain(arn)
def test_drain_ecs_container_instances(self): ecs_client = boto3.client('ecs', region_name='eu-west-1') ec2 = boto3.resource('ec2', region_name='eu-west-1') test_cluster_name = 'test_ecs_cluster' _ = ecs_client.create_cluster( clusterName=test_cluster_name ) instance_to_create = 3 test_instance_arns = [] amiobj, srcimgid = self.create_ami() for i in range(0, instance_to_create): test_instance = ec2.create_instances( ImageId=srcimgid['ImageId'], MinCount=1, MaxCount=1, )[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance) ) response = ecs_client.register_container_instance( cluster=test_cluster_name, instanceIdentityDocument=instance_id_document) test_instance_arns.append(response['containerInstance'][ 'containerInstanceArn']) test_instance_ids = list(map((lambda x: x.split('/')[1]), test_instance_arns)) response = ecs_client.update_container_instances_state(cluster=test_cluster_name, containerInstances=test_instance_ids, status='DRAINING') response_statuses = [ci['status'] for ci in response['containerInstances']] for status in response_statuses: assert status == 'DRAINING'
def test_get_task_definition_arns(fetcher, ecs, ec2): cluster_name = "test_ecs_cluster" _ = ecs.client.create_cluster(clusterName=cluster_name) test_instances = ec2.resource.create_instances(ImageId="ami-bb9a6bc2", MinCount=3, MaxCount=3) for test_instance in test_instances: instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance)) _ = ecs.client.register_container_instance( cluster=cluster_name, instanceIdentityDocument=instance_id_document) response = ecs.client.register_task_definition( family="test_ecs_task", containerDefinitions=[{ "name": "hello_world", "image": "docker/hello-world:latest", "cpu": 1024, "memory": 400, "essential": True, "logConfiguration": { "logDriver": "json-file" }, }], ) toolbox.pstruct(response["taskDefinition"], "register_task_definition") task_definition_arns = fetcher.get_task_definition_arns() assert len(task_definition_arns) == 1 assert task_definition_arns[0] == response["taskDefinition"][ "taskDefinitionArn"]
def setup_ecs_resources(self): # Setup a test ECS cluster with a task definition already registered test_cluster_name = 'test_ecs_cluster' cluster = self.ecs_client.create_cluster(clusterName=test_cluster_name) task_definition = self.ecs_client.register_task_definition( family='test_ecs_task', containerDefinitions=[{ 'name': 'hello_world', 'image': 'docker/hello-world:latest', 'cpu': 1024, 'memory': 400, 'essential': True, 'environment': [{ 'name': 'AWS_ACCESS_KEY_ID', 'value': 'SOME_ACCESS_KEY' }], 'logConfiguration': { 'logDriver': 'json-file' } }]) # ... and then register a service for that task with a load balancer targeting it load_balancer, target_group = self.setup_elb_resources() service = self.ecs_client.create_service( cluster=test_cluster_name, serviceName='test_ecs_service', taskDefinition='test_ecs_task', desiredCount=2, loadBalancers=[{ 'targetGroupArn': target_group['TargetGroupArn'], 'loadBalancerName': load_balancer['LoadBalancerName'], 'containerName': 'test_container_name', 'containerPort': 123 }]) # The mock ecs service will not "start" the task for us, nor does it # create the instances, so we must simulate it ourselves test_instance = self.ec2_client.create_instances( ImageId="ami-1234abcd", MinCount=1, MaxCount=1, )[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance)) self.ecs_client.register_container_instance( cluster=test_cluster_name, instanceIdentityDocument=instance_id_document) container_instances = self.ecs_client.list_container_instances( cluster=test_cluster_name) container_instance_id = container_instances['containerInstanceArns'][ 0].split('/')[-1] return cluster, service, task_definition, container_instance_id
def run_task(): client = boto3.client('ecs', region_name='us-east-1') ec2 = boto3.resource('ec2', region_name='us-east-1') conn = boto3.resource('s3', region_name='us-east-1') s3 = boto3.client('s3', region_name='us-east-1') conn.create_bucket(Bucket='mybucket') test_cluster_name = 'test_ecs_cluster' _ = client.create_cluster(clusterName=test_cluster_name) test_instance = ec2.create_instances( ImageId="ami-1234abcd", MinCount=1, MaxCount=1, )[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance)) response = client.register_container_instance( cluster=test_cluster_name, instanceIdentityDocument=instance_id_document) _ = client.register_task_definition(family='test_ecs_task', containerDefinitions=[{ 'name': 'hello_world', 'image': 'docker/hello-world:latest', 'cpu': 1024, 'memory': 400, 'essential': True, 'environment': [{ 'name': 'AWS_ACCESS_KEY_ID', 'value': 'SOME_ACCESS_KEY' }], 'logConfiguration': { 'logDriver': 'json-file' } }]) response = client.run_task(cluster='test_ecs_cluster', overrides={}, taskDefinition='test_ecs_task', count=1, startedBy='moto') # Put Task Arn in S3 Bucket s3.put_object(Bucket='mybucket', Key='taskArn', Body=response['tasks'][0]['taskArn']) taskArn = conn.Object('mybucket', 'taskArn').get()['Body'].read().decode() print("The S3 taskArn was: " + taskArn) # Print out response attributes of ECS Task print("Number of tasks: " + str(len(response['tasks']))) print("Task ARN: " + response['tasks'][0]['taskArn']) print("Cluster ARN: " + response['tasks'][0]['clusterArn']) print("TaskDefinition ARN: " + response['tasks'][0]['taskDefinitionArn']) print("Container Instance ARN: " + response['tasks'][0]['containerInstanceArn']) print("Last Status: " + response['tasks'][0]['lastStatus']) print("Desired Status: " + response['tasks'][0]['desiredStatus']) print("Started By: " + response['tasks'][0]['startedBy'])
def test_list_tasks(): client = boto3.client('ecs', region_name='us-east-1') ec2 = boto3.resource('ec2', region_name='us-east-1') test_cluster_name = 'test_ecs_cluster' _ = client.create_cluster(clusterName=test_cluster_name) test_instance = ec2.create_instances( ImageId="ami-1234abcd", MinCount=1, MaxCount=1, )[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance)) response = client.register_container_instance( cluster=test_cluster_name, instanceIdentityDocument=instance_id_document) container_instances = client.list_container_instances( cluster=test_cluster_name) container_instance_id = container_instances['containerInstanceArns'][ 0].split('/')[-1] _ = client.register_task_definition(family='test_ecs_task', containerDefinitions=[{ 'name': 'hello_world', 'image': 'docker/hello-world:latest', 'cpu': 1024, 'memory': 400, 'essential': True, 'environment': [{ 'name': 'AWS_ACCESS_KEY_ID', 'value': 'SOME_ACCESS_KEY' }], 'logConfiguration': { 'logDriver': 'json-file' } }]) _ = client.start_task(cluster='test_ecs_cluster', taskDefinition='test_ecs_task', overrides={}, containerInstances=[container_instance_id], startedBy='foo') _ = client.start_task(cluster='test_ecs_cluster', taskDefinition='test_ecs_task', overrides={}, containerInstances=[container_instance_id], startedBy='bar') assert len(client.list_tasks()['taskArns']).should.equal(2) assert len(client.list_tasks( cluster='test_ecs_cluster')['taskArns']).should.equal(2) assert len(client.list_tasks(startedBy='foo')['taskArns']).should.equal(1)
def setup(): mock_ec2 = moto.mock_ec2() mock_ecs = moto.mock_ecs() mock_ec2.start() mock_ecs.start() ecs = client('ecs', region_name="us-east-1") ecs.create_cluster(clusterName='UploadEventCluster', tags=[ { 'key': 'Cost', 'value': 'AI' }, ], capacityProviders=[ 'FARGATE', ]) ec2_resource = resource("ec2", "us-east-1") response = ec2_resource.create_instances( ImageId='ami_id', MinCount=1, MaxCount=1, BlockDeviceMappings=[{ "DeviceName": "/dev/sda1", "Ebs": { "VolumeSize": 50, "DeleteOnTermination": False }, }], ) instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(response[0])) ecs.register_container_instance( cluster='UploadEventCluster', instanceIdentityDocument=instance_id_document) ecs.register_task_definition( family='kairon-task', taskRoleArn='arn:aws:iam::014936247795:role/ecsTaskExecutionRole', networkMode='awsvpc', containerDefinitions=[ { 'name': 'kairon-task', 'image': 'digite/kairon-data-importer:latest', 'cpu': 4096, 'memory': 8192, 'essential': True, 'environment': [{ 'name': 'BOT', 'value': 'demo' }, { 'name': 'USER', 'value': 'demo' }, { 'name': 'IMPORT_DATA', 'value': 'demo' }, { 'name': 'OVERWRITE', 'value': 'demo' }] }, ], requiresCompatibilities=[ 'FARGATE', ], tags=[ { 'key': 'Cost', 'value': 'AI' }, ])
def test_ecs_run_task(): """ Save and return an object using the Helper function :return: """ # client and setup mock bucket s3 = boto3.resource('s3', region_name='us-west-2') s3_client = boto3.client('s3') s3_bucket = "s3bucket" s3_key = "arnkey" s3.create_bucket( Bucket=s3_bucket, CreateBucketConfiguration={'LocationConstraint': 'us-west-2'}) # s3 needs a file-like object # moto is oddly specific in how it names this arn_io = io.BytesIO( b'arn:aws:ecs:us-east-1:012345678910:task-definition/test_ecs_task:1') # Upload the file to "S3" s3_client.upload_fileobj(arn_io, s3_bucket, s3_key) # This is an s3 PUT trigger event event = { "Records": [{ "eventVersion": "2.0", "eventSource": "aws:s3", "awsRegion": "us-east-1", "eventTime": "1970-01-01T00:00:00.000Z", "eventName": "ObjectCreated:Put", "userIdentity": { "principalId": "AIDAJDPLRKLG7UEXAMPLE" }, "requestParameters": { "sourceIPAddress": "127.0.0.1" }, "responseElements": { "x-amz-request-id": "C3D13FE58DE4C810", "x-amz-id-2": "FMyUVURIY8/IgAtTv8xRjskZQpcIZ9KG4V5Wp6S7S/JRWeUWerMUE5JgHvANOjpD" }, "s3": { "s3SchemaVersion": "1.0", "configurationId": "testConfigRule", "bucket": { "name": "s3bucket", "ownerIdentity": { "principalId": "A3NL1KOZZKExample" }, "arn": "arn:aws:s3:::mybucket" }, "object": { "key": "arnkey", "size": 1024, "eTag": "d41d8cd98f00b204e9800998ecf8427e", "versionId": "096fKKXTRTtl3on89fVO.nfljtsv6qko", "sequencer": "0055AED6DCD90281E5" } } }] } # setup ecs cluster # This setup lifted from https://github.com/spulec/moto/blob/master/tests/test_ecs/test_ecs_boto3.py ecs_client = boto3.client('ecs', region_name='us-east-1') ec2_client = boto3.resource('ec2', region_name='us-east-1') test_cluster_name = 'test_ecs_cluster' _ = ecs_client.create_cluster(clusterName=test_cluster_name) test_instance = ec2_client.create_instances( ImageId="ami-1234abcd", MinCount=1, MaxCount=1, )[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance)) response = ecs_client.register_container_instance( cluster=test_cluster_name, instanceIdentityDocument=instance_id_document) _ = ecs_client.register_task_definition(family='test_ecs_task', containerDefinitions=[{ 'name': 'hello_world', 'image': 'docker/hello-world:latest', 'cpu': 1024, 'memory': 400, 'essential': True, 'environment': [{ 'name': 'AWS_ACCESS_KEY_ID', 'value': 'SOME_ACCESS_KEY' }], 'logConfiguration': { 'logDriver': 'json-file' } }]) # response = ecs_client.run_task( # cluster='test_ecs_cluster', # overrides={}, # taskDefinition='test_ecs_task', # count=2, # startedBy='moto' # ) response = execute_ecs_event(event, None) print(response)
def test_start_task(): client = boto3.client('ecs', region_name='us-east-1') ec2 = boto3.resource('ec2', region_name='us-east-1') test_cluster_name = 'test_ecs_cluster' _ = client.create_cluster(clusterName=test_cluster_name) test_instance = ec2.create_instances( ImageId="ami-1234abcd", MinCount=1, MaxCount=1, )[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance)) response = client.register_container_instance( cluster=test_cluster_name, instanceIdentityDocument=instance_id_document) container_instances = client.list_container_instances( cluster=test_cluster_name) container_instance_id = container_instances['containerInstanceArns'][ 0].split('/')[-1] _ = client.register_task_definition(family='test_ecs_task', containerDefinitions=[{ 'name': 'hello_world', 'image': 'docker/hello-world:latest', 'cpu': 1024, 'memory': 400, 'essential': True, 'environment': [{ 'name': 'AWS_ACCESS_KEY_ID', 'value': 'SOME_ACCESS_KEY' }], 'logConfiguration': { 'logDriver': 'json-file' } }]) response = client.start_task(cluster='test_ecs_cluster', taskDefinition='test_ecs_task', overrides={}, containerInstances=[container_instance_id], startedBy='moto') len(response['tasks']).should.equal(1) response['tasks'][0]['taskArn'].should.contain( 'arn:aws:ecs:us-east-1:012345678910:task/') response['tasks'][0]['clusterArn'].should.equal( 'arn:aws:ecs:us-east-1:012345678910:cluster/test_ecs_cluster') response['tasks'][0]['taskDefinitionArn'].should.equal( 'arn:aws:ecs:us-east-1:012345678910:task-definition/test_ecs_task:1') response['tasks'][0]['containerInstanceArn'].should.equal( 'arn:aws:ecs:us-east-1:012345678910:container-instance/{0}'.format( container_instance_id)) response['tasks'][0]['overrides'].should.equal({}) response['tasks'][0]['lastStatus'].should.equal("RUNNING") response['tasks'][0]['desiredStatus'].should.equal("RUNNING") response['tasks'][0]['startedBy'].should.equal("moto") response['tasks'][0]['stoppedReason'].should.equal("")
def test_list_tasks(): client = boto3.client('ecs', region_name='us-east-1') ec2 = boto3.resource('ec2', region_name='us-east-1') test_cluster_name = 'test_ecs_cluster' _ = client.create_cluster( clusterName=test_cluster_name ) test_instance = ec2.create_instances( ImageId="ami-1234abcd", MinCount=1, MaxCount=1, )[0] instance_id_document = json.dumps( ec2_utils.generate_instance_identity_document(test_instance) ) response = client.register_container_instance( cluster=test_cluster_name, instanceIdentityDocument=instance_id_document ) container_instances = client.list_container_instances( cluster=test_cluster_name) container_instance_id = container_instances[ 'containerInstanceArns'][0].split('/')[-1] _ = client.register_task_definition( family='test_ecs_task', containerDefinitions=[ { 'name': 'hello_world', 'image': 'docker/hello-world:latest', 'cpu': 1024, 'memory': 400, 'essential': True, 'environment': [{ 'name': 'AWS_ACCESS_KEY_ID', 'value': 'SOME_ACCESS_KEY' }], 'logConfiguration': {'logDriver': 'json-file'} } ] ) _ = client.start_task( cluster='test_ecs_cluster', taskDefinition='test_ecs_task', overrides={}, containerInstances=[container_instance_id], startedBy='foo' ) _ = client.start_task( cluster='test_ecs_cluster', taskDefinition='test_ecs_task', overrides={}, containerInstances=[container_instance_id], startedBy='bar' ) assert len(client.list_tasks()['taskArns']).should.equal(2) assert len(client.list_tasks(cluster='test_ecs_cluster') ['taskArns']).should.equal(2) assert len(client.list_tasks(startedBy='foo')['taskArns']).should.equal(1)