Skip to content

Commit 463163d

Browse files
authored
fix: use tag prefix matching to clean up samcli/lambda-* images (#8647)
Docker's images.list(name='samcli/lambda') does exact repository matching and won't match repositories like 'samcli/lambda-python'. This caused stale images to persist across parameterized test classes, leading to flaky test_download_two_layers failures where Layer2 should overwrite Layer1 but the image was never rebuilt. Fix by: 1. Adding _cleanup_samcli_images() that lists all images and filters by 'samcli/lambda-' tag prefix 2. Using this method in both tearDown and tearDownClass 3. Fixing the same pattern in TestLayerVersionThatDoNotCreateCache
1 parent 4c7e101 commit 463163d

1 file changed

Lines changed: 32 additions & 15 deletions

File tree

tests/integration/local/invoke/test_integrations_cli.py

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -803,13 +803,27 @@ class TestLayerVersionBase(InvokeIntegBase):
803803
def setUp(self):
804804
self.layer_cache = Path().home().joinpath("integ_layer_cache")
805805

806+
@staticmethod
807+
def _cleanup_samcli_images(docker_client):
808+
"""Remove all samcli/lambda-* images.
809+
810+
Docker's images.list(name="samcli/lambda") does exact repository matching
811+
and won't match repositories like "samcli/lambda-python". We list all images
812+
and filter by tag prefix instead.
813+
"""
814+
try:
815+
all_images = docker_client.images.list()
816+
for image in all_images:
817+
for tag in image.tags:
818+
if tag.startswith("samcli/lambda-"):
819+
docker_client.remove_image_safely(image.id, force=True)
820+
break
821+
except Exception:
822+
pass
823+
806824
def tearDown(self):
807825
docker_client = get_validated_container_client()
808-
samcli_images = docker_client.images.list(name="samcli/lambda")
809-
for image in samcli_images:
810-
# Use strategy pattern method for runtime-aware image cleanup
811-
docker_client.remove_image_safely(image.id, force=True)
812-
826+
self._cleanup_samcli_images(docker_client)
813827
shutil.rmtree(str(self.layer_cache), ignore_errors=True)
814828

815829
@classmethod
@@ -823,10 +837,7 @@ def tearDownClass(cls):
823837
cls.layer_utils.delete_layers()
824838
# Added to handle the case where ^C failed the test due to invalid cleanup of layers
825839
docker_client = get_validated_container_client()
826-
samcli_images = docker_client.images.list(name="samcli/lambda")
827-
for image in samcli_images:
828-
# Use strategy pattern method for runtime-aware image cleanup
829-
docker_client.remove_image_safely(image.id, force=True)
840+
cls._cleanup_samcli_images(docker_client)
830841
integ_layer_cache_dir = Path().home().joinpath("integ_layer_cache")
831842
if integ_layer_cache_dir.exists():
832843
shutil.rmtree(str(integ_layer_cache_dir))
@@ -1069,12 +1080,18 @@ def setUp(self):
10691080
def tearDown(self):
10701081
docker_client = get_validated_container_client()
10711082

1072-
# Use strategy pattern method for runtime-aware image cleanup
1073-
# This handles both Docker and Finch cleanup strategies automatically
1074-
samcli_images = docker_client.images.list(name="samcli/lambda")
1075-
for image in samcli_images:
1076-
# Use strategy pattern method that handles runtime-specific cleanup logic
1077-
docker_client.remove_image_safely(image.id, force=True)
1083+
# Use the same prefix-based cleanup as TestLayerVersionBase.
1084+
# Docker's images.list(name="samcli/lambda") does exact repository matching
1085+
# and won't match "samcli/lambda-python" etc.
1086+
try:
1087+
all_images = docker_client.images.list()
1088+
for image in all_images:
1089+
for tag in image.tags:
1090+
if tag.startswith("samcli/lambda-"):
1091+
docker_client.remove_image_safely(image.id, force=True)
1092+
break
1093+
except Exception:
1094+
pass
10781095

10791096
def test_layer_does_not_exist(self):
10801097
self.layer_utils.upsert_layer(LayerUtils.generate_layer_name(), "LayerOneArn", "layer1.zip")

0 commit comments

Comments
 (0)