getmoto / moto

A library that allows you to easily mock out tests based on AWS infrastructure.
http://docs.getmoto.org/en/latest/
Apache License 2.0
7.66k stars 2.05k forks source link

ecs create_task_set does not create tasks (and obviously not propagate tags) #8267

Closed suonto closed 1 week ago

suonto commented 2 weeks ago

Moto ecs create_task_set does not result in running tasks. Additionally, it should propagate tags to the tasks, depending on service PropagateTags setting.

Pseudo context

Given service propagateTags is configured to "TASK_DEFINITION"
and task definition has TASK_DEF_TAGS
and os.environ["MOTO_ECS_SERVICE_RUNNING"] == "1"

ecs.create_task_set(...)

tasks = ecs.list_tasks(...), followed by ecs.describe_tasks(...)

assert one task is running
assert the task has TASK_DEF_TAGS

how to reproduce the issue

Mock aws, and then follow this:

TASK_DEF_TAGS: list[TagTypeDef] = [
    {
        "key": "test-task-def-tag-1",
        "value": "test-task-def-tag-1-value",
    },
    {
        "key": "test-task-def-tag-2",
        "value": "test-task-def-tag-2-value",
    },
]

ecs = boto3.client("ecs")

response = ecs.create_cluster(
    clusterName="test-cluster-a",
)

cluster_a = response["cluster"]

response = ecs.register_task_definition(
    family="test-family",
    containerDefinitions=[
        {
            "name": "test-container-def",
            "image": "foo",
            "memory": 256,
        }
    ],
    tags=TASK_DEF_TAGS,
)

task_def = response["taskDefinition"]

os.environ["MOTO_ECS_SERVICE_RUNNING"] = "1"

service_response = ecs.create_service(
    cluster=cluster_a["clusterArn"],
    serviceName="test-service",
    taskDefinition=task_def["taskDefinitionArn"],
    desiredCount=1,
    deploymentController={"type": "EXTERNAL"},
    propagateTags="TASK_DEFINITION",
)
service = service_response["service"]

ecs.create_task_set(
    service=service["serviceName"],
    cluster=cluster_a["clusterName"],
    externalId="test-ext-id",
    taskDefinition=task_def["taskDefinitionArn"],
    scale={"unit": "PERCENT", "value": 100},
    tags=task_def["tags"],
)

what you expected to happen

These assertions should be true:

list_tasks_response = ecs.list_tasks(
    cluster=cluster_a["clusterName"],
    serviceName=service["serviceName"],
)

task_arns = list_tasks_response["taskArns"]
assert len(task_arns) == 1

describe_tasks_response = ecs.describe_tasks(
    cluster=cluster_a["clusterName"],
    tasks=task_arns,
    include=["TAGS"],
)

tasks = describe_tasks_response["tasks"]

assert len(tasks) == 1
assert tasks[0]["tags"] == TASK_DEF_TAGS

what actually happens

Tasks are not created. That's easily explained by looking at the code that does not create any.

what version of Moto you're using

5.0.18, via pip

bblommers commented 1 week ago

Thanks for raising this @suonto - tasks are now created in moto==5.0.19.

I'm not too familiar with ECS, so please let us know if there are still any issues with this implementation.