|
1 | 1 | """Tests for ECS/AgentCore container build integration across modules""" |
2 | 2 |
|
3 | 3 | from unittest import TestCase |
4 | | -from unittest.mock import MagicMock, patch, Mock |
5 | | -from copy import deepcopy |
| 4 | +from unittest.mock import MagicMock, patch |
6 | 5 |
|
7 | 6 | from samcli.lib.build.app_builder import ApplicationBuilder |
8 | 7 | from samcli.lib.build.build_graph import ContainerBuildDefinition |
@@ -184,7 +183,7 @@ def test_includes_container_services( |
184 | 183 | mock_manager.get_repository_mapping.return_value = {"MyFunction": "uri1", "MyAgent": "uri2"} |
185 | 184 | mock_manager_cls.return_value = mock_manager |
186 | 185 |
|
187 | | - result = sync_ecr_stack("template.yaml", "stack", "us-east-1", "bucket", "prefix", {}) |
| 186 | + sync_ecr_stack("template.yaml", "stack", "us-east-1", "bucket", "prefix", {}) |
188 | 187 |
|
189 | 188 | # Verify both function and container service were passed |
190 | 189 | call_args = mock_manager.set_functions.call_args[0] |
@@ -257,3 +256,41 @@ def test_sync_skips_when_no_image(self): |
257 | 256 | flow._physical_id_mapping = {} |
258 | 257 | # Should not raise |
259 | 258 | flow.sync() |
| 259 | + |
| 260 | + |
| 261 | +class TestContainerNameErrorHandling(TestCase): |
| 262 | + def test_update_built_resource_raises_on_container_name_mismatch(self): |
| 263 | + from samcli.lib.build.exceptions import DockerBuildFailed |
| 264 | + |
| 265 | + properties = { |
| 266 | + "ContainerDefinitions": [ |
| 267 | + {"Name": "sidecar", "Image": "sidecar:latest"}, |
| 268 | + {"Name": "web", "Image": "placeholder"}, |
| 269 | + ] |
| 270 | + } |
| 271 | + metadata = {"ContainerName": "typo"} |
| 272 | + with self.assertRaises(DockerBuildFailed): |
| 273 | + ApplicationBuilder._update_built_resource( |
| 274 | + "myimage:latest", properties, AWS_ECS_TASK_DEFINITION, "/path", metadata |
| 275 | + ) |
| 276 | + |
| 277 | + def test_get_target_index_raises_on_container_name_mismatch(self): |
| 278 | + from samcli.commands.package import exceptions |
| 279 | + |
| 280 | + exporter = ECSTaskDefinitionImageResource.__new__(ECSTaskDefinitionImageResource) |
| 281 | + exporter.resource_metadata = {"ContainerName": "typo"} |
| 282 | + container_defs = [{"Name": "web"}, {"Name": "sidecar"}] |
| 283 | + with self.assertRaises(exceptions.ExportFailedError): |
| 284 | + exporter._get_target_index(container_defs) |
| 285 | + |
| 286 | + def test_get_target_index_returns_match(self): |
| 287 | + exporter = ECSTaskDefinitionImageResource.__new__(ECSTaskDefinitionImageResource) |
| 288 | + exporter.resource_metadata = {"ContainerName": "web"} |
| 289 | + container_defs = [{"Name": "sidecar"}, {"Name": "web"}] |
| 290 | + self.assertEqual(exporter._get_target_index(container_defs), 1) |
| 291 | + |
| 292 | + def test_get_target_index_defaults_to_zero_without_name(self): |
| 293 | + exporter = ECSTaskDefinitionImageResource.__new__(ECSTaskDefinitionImageResource) |
| 294 | + exporter.resource_metadata = {} |
| 295 | + container_defs = [{"Name": "web"}] |
| 296 | + self.assertEqual(exporter._get_target_index(container_defs), 0) |
0 commit comments