0

I have a python method that lists certain ECS Fargate services based on some tags and some names using boto3 get_paginator.

def list_service_name(environment,
                      resource_owner_name,
                      ecs_client):
    list_of_service = list()
    cluster_name = "my cluster name " + environment
    target_string = "-somedummy"
    resource_owner_tag = resource_owner_name
    service_paginator = ecs_client.get_paginator('list_services')
    for page in service_paginator.paginate(cluster=cluster_name,
                                           launchType='FARGATE'):
       
        for service in page['serviceArns']:

           
            response = ecs_client.list_tags_for_resource(resourceArn=service)
           
            for tags in response['tags']:
                if tags['key'] == 'ResourceOwner' and \
                        tags['value'] == resource_owner_tag and \
                        service.endswith(target_string):
                    list_of_service.append(service)

    return list_of_service

Now I would like to test this using moto. Hence I have created conftest.py where I have defined all moto mock connections to the services like, ecs. Also, I have created test_main.py file like below where I have created dummy services connected to ECS Fargate. But for some reason, if I try to assert the outcome of the main method in the test file the service lists return empty. Whereas I would like to see test-service-for-successful as the outcome. Is there something I'm missing or pagination is still not available in moto?

from my_code.main import *


@pytest.fixture
def env_name():
    return "local"


@pytest.fixture
def cluster_name(env_name):
    return "my dummy" + env_name + "cluster_name"


@pytest.fixture
def successful_service_name():
    return "test-service-for-successful"


@pytest.fixture
def un_successful_service_name():
    return "test-service-for-un-successful"


@pytest.fixture
def resource_owner():
    return "dummy_tag"


@pytest.fixture
def test_create_service(ecs_client,
                        cluster_name,
                        successful_service_name,
                        un_successful_service_name,
                        resource_owner):
    _ = ecs_client.create_cluster(clusterName=cluster_name)
    _ = 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": "environment", "value": "local"}
                ],
                "logConfiguration": {"logDriver": "json-file"},
            }
        ],
    )
    ecs_client.create_service(
        cluster=cluster_name,
        serviceName=successful_service_name,
        taskDefinition="test_ecs_task",
        desiredCount=0,
        launchType="FARGATE",
        tags=[{"key": "resource_owner", "value": resource_owner}]
    )

    ecs_client.create_service(
        cluster=cluster_name,
        serviceName=un_successful_service_name,
        taskDefinition="test_ecs_task",
        desiredCount=0,
        launchType="FARGATE",
        tags=[{"key": "resource_owner", "value": resource_owner}]
    )
    yield


def test_list_service_name(env_name,
                           resource_owner,
                           ecs_client):

    objects = list_service_name(env_name,
                                resource_owner,
                                ecs_client)
    # here object is []
    # Where as I should see successful_service_name
change198
  • 1,647
  • 3
  • 21
  • 60
  • The clustername used in 'list_service_name' is wrong (my_cluster_name_local vs my_dummy_local_cluster_name). The tag-key is wrong as well (resource_owner vs ResourceOwner). Lastly, 'target_string' should have value '_successful' (if you don't want the _unsuccessful service) – Bert Blommers Apr 12 '21 at 09:19
  • @BertBlommers I think you are right, pointed me to the mistake, Let me fix it and test again. get back to this soon. – change198 Apr 12 '21 at 11:42

0 Answers0