Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ECS: tasks include containers objects #8588

Merged
merged 8 commits into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion moto/ecs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ def __init__(
self.desired_status = "RUNNING"
self.task_definition_arn = task_definition.arn
self.overrides = overrides or {}
self.containers: List[Dict[str, Any]] = []
self.containers = [Container(task_definition)]
self.started_by = started_by
self.tags = tags or []
self.launch_type = launch_type
Expand Down Expand Up @@ -451,6 +451,7 @@ def response_object(self, include_tags: bool = True) -> Dict[str, Any]: # type:
response_object.pop("tags", None)
response_object["taskArn"] = self.task_arn
response_object["lastStatus"] = self.last_status
response_object["containers"] = [self.containers[0].response_object]
return response_object


Expand Down Expand Up @@ -757,6 +758,29 @@ def get_cfn_attribute(self, attribute_name: str) -> str:
raise UnformattedGetAttTemplateException()


class Container(BaseObject, CloudFormationModel):
def __init__(
self,
task_def: TaskDefinition,
):
self.container_arn = f"{task_def.arn}/{str(mock_random.uuid4())}"
self.task_arn = task_def.arn

container_def = task_def.container_definitions[0]
self.image = container_def.get("image")
self.last_status = "PENDING"
self.exitCode = 0

self.network_interfaces: List[Dict[str, Any]] = []
self.health_status = "HEALTHY"

self.cpu = container_def.get("cpu")
self.memory = container_def.get("memory")
self.environment = container_def.get("environment")
self.name = container_def.get("name")
self.command = container_def.get("command")


class ContainerInstance(BaseObject):
def __init__(
self,
Expand Down
27 changes: 17 additions & 10 deletions tests/test_ecs/test_ecs_boto3.py
Original file line number Diff line number Diff line change
Expand Up @@ -1854,31 +1854,27 @@ def test_update_container_instances_state_by_arn():

@mock_aws
def test_run_task():
# Setup
test_cluster_name = "test_ecs_cluster"
image = "docker/hello-world:latest"
client = boto3.client("ecs", region_name=ECS_REGION)
ec2 = boto3.resource("ec2", region_name=ECS_REGION)

test_cluster_name = "test_ecs_cluster"

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)
)

response = client.register_container_instance(
client.register_container_instance(
cluster=test_cluster_name, instanceIdentityDocument=instance_id_document
)

client.register_task_definition(
td = client.register_task_definition(
family="test_ecs_task",
containerDefinitions=[
{
"name": "hello_world",
"image": "docker/hello-world:latest",
"image": image,
"cpu": 1024,
"memory": 400,
"essential": True,
Expand All @@ -1889,12 +1885,16 @@ def test_run_task():
}
],
)

# Execute
response = client.run_task(
cluster="test_ecs_cluster",
overrides={},
taskDefinition="test_ecs_task",
startedBy="moto",
)

# Verify
assert len(response["tasks"]) == 1
response = client.run_task(
cluster="test_ecs_cluster",
Expand Down Expand Up @@ -1928,6 +1928,13 @@ def test_run_task():
assert task["startedBy"] == "moto"
assert task["stoppedReason"] == ""
assert task["tags"][0].get("value") == "tagValue0"
assert len(task["containers"]) == 1

con = task["containers"][0]
assert td["taskDefinition"]["taskDefinitionArn"] in con["containerArn"]
assert con["taskArn"] == td["taskDefinition"]["taskDefinitionArn"]
assert con["lastStatus"] == "PENDING"
assert con["image"] == image


@mock_aws
Expand Down
Loading