import requests_mock import unittest from unittest.mock import patch from fargate.deploy import create_new_task_definition, image_uri class TestCreateNewTaskDefinition(unittest.TestCase): """Test ECS task definition creation""" @patch('fargate.deploy.image_name', 'repository') @patch('fargate.deploy.container_name', 'test_container_name') @patch('fargate.deploy.ecr_account_id', '123456789012') @patch('boto3.client') def test_create_new_task_definition(self, mock_boto_client): # Mock the ECR client response mock_boto_client.return_value.get_authorization_token.return_value = { 'authorizationData': [{ 'authorizationToken': 'dGVzdHVzZXI6dGVzdHBhc3N3b3Jk', # base64 for 'testuser:testpassword' 'proxyEndpoint': 'https://123456789012.dkr.ecr.region.amazonaws.com' }] } # Mock the ECS client mock_ecs_client = mock_boto_client.return_value # Set up mock responses mock_current_task_def = { 'taskDefinition': { 'containerDefinitions': [ {'name': 'test_container_name', 'image': image_uri}, {'name': 'TwistlockDefender', 'image': 'defender:latest'}, {'name': 'datadog_agent', 'image': 'datadog:latest'}, {'name': 'firelens_agent', 'image': 'firelens:latest'}, ] } } mock_ecs_client.describe_task_definition.return_value = mock_current_task_def # Mock the Docker Registry API responses with requests_mock.Mocker() as m: m.get('https://123456789012.dkr.ecr.region.amazonaws.com/v2/repository/manifests/latest', json={'config': {'digest': 'config-digest'}}) m.get('https://123456789012.dkr.ecr.region.amazonaws.com/v2/repository/blobs/config-digest', json={'config': {'Entrypoint': ['/bin/sh'], 'Cmd': ['echo', 'Hello World']}}) old_task_def, new_task_def = create_new_task_definition(mock_ecs_client, 'test_task_family') mock_ecs_client.get_authorization_token.assert_called_once_with(registryIds=['123456789012']) for container in new_task_def['containerDefinitions']: if container['name'] == 'test_container_name': self.assertEqual(container['entryPoint'], ['/var/lib/twistlock/fargate/defender', 'fargate', 'entrypoint', '/bin/sh', 'echo', 'Hello World'])