from unittest.mock import MagicMock import pytest from datadog_tools.connectors.aws import AWS from datadog_tools.models.aws import AWSResource @pytest.fixture def mock_search_response(): return { "Resources": [ { "Arn": "arn:aws:ecs:us-east-1:123456789012:service/prod-test-service/prod-test-service", "OwningAccountId": "123456789012", "Properties": [ { "Name": "tags", "Data": [ {"Key": "service_name", "Value": "test-service"}, {"Key": "application_family", "Value": "test-app-family"}, {"Key": "environment", "Value": "prod"}, {"Key": "terraform_github_repository", "Value": "terraform-infra"}, {"Key": "terraform_github_path", "Value": "prod/test-service"}, {"Key": "terraformed", "Value": "true"}, ], } ], "Region": "us-east-1", "ResourceType": "ecs:service", }, { "Arn": "arn:aws:ecs:us-east-1:234567890123:service/qa-test-service/qa-test-service", "OwningAccountId": "234567890123", "Properties": [ { "Name": "tags", "Data": [], } ], "Region": "us-east-1", "ResourceType": "ecs:service", }, ] } @pytest.fixture def mock_resource_explorer_client(mock_search_response): mock_client = MagicMock() mock_client.search.return_value = mock_search_response return mock_client @pytest.fixture def mock_resource_explorer_view_arn(): return "arn:aws:resource-explorer-2:us-east-1:123456789012:view/my-view" def test_resource_search(mock_search_response, mock_resource_explorer_client, mock_resource_explorer_view_arn): aws = AWS( resource_explorer_view_arn=mock_resource_explorer_view_arn, resource_explorer_client=mock_resource_explorer_client, ) query_string = "tag:service_name=test-service resourcetype:ecs:service" resources = aws.resource_search(query_string) mock_resource_explorer_client.search.assert_called_with( ViewArn=mock_resource_explorer_view_arn, QueryString=query_string ) assert resources == [ AWSResource( arn="arn:aws:ecs:us-east-1:123456789012:service/prod-test-service/prod-test-service", account_id="123456789012", application_family="test-app-family", environment="prod", resource_type="ecs:service", region="us-east-1", terraform_github_repository="terraform-infra", terraform_github_path="prod/test-service", terraformed=True, ), AWSResource( arn="arn:aws:ecs:us-east-1:234567890123:service/qa-test-service/qa-test-service", account_id="234567890123", application_family=None, environment=None, resource_type="ecs:service", region="us-east-1", terraform_github_repository=None, terraform_github_path=None, terraformed=False, ), ] def test_find_resources_by_type_and_service_name(mock_resource_explorer_client, mock_resource_explorer_view_arn): aws = AWS( resource_explorer_view_arn=mock_resource_explorer_view_arn, resource_explorer_client=mock_resource_explorer_client, ) resources = aws.find_resources_by_type_and_service_name("ecs:service", "test-service") mock_resource_explorer_client.search.assert_called_with( ViewArn=mock_resource_explorer_view_arn, QueryString="tag:service_name=test-service resourcetype:ecs:service" ) assert resources == [ AWSResource( arn="arn:aws:ecs:us-east-1:123456789012:service/prod-test-service/prod-test-service", account_id="123456789012", application_family="test-app-family", environment="prod", resource_type="ecs:service", region="us-east-1", terraform_github_repository="terraform-infra", terraform_github_path="prod/test-service", terraformed=True, ), AWSResource( arn="arn:aws:ecs:us-east-1:234567890123:service/qa-test-service/qa-test-service", account_id="234567890123", application_family=None, environment=None, resource_type="ecs:service", region="us-east-1", terraform_github_repository=None, terraform_github_path=None, terraformed=False, ), ]