Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion samcli/local/docker/lambda_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,16 @@ def get_local_image_digest(self, image_name: str) -> Optional[str]:
str
Image digest, including `sha256:` prefix
"""
image_info = self.docker_client.images.get(image_name)
try:
image_info = self.docker_client.images.get(image_name)
except docker.errors.ImageNotFound:
# Base image not found locally, pull it for digest comparison
try:
self.docker_client.images.pull(image_name)
image_info = self.docker_client.images.get(image_name)
except docker.errors.APIError:
return None

try:
full_digest: str = image_info.attrs.get("RepoDigests", [None])[0]
return full_digest.split("@")[1]
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/local/docker/test_lambda_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,42 @@ def test_get_local_image_digest(self):
lambda_image = LambdaImage("layer_downloader", False, False, docker_client=docker_client_mock)
self.assertEqual("sha256:local-digest", lambda_image.get_local_image_digest("image_name"))

def test_get_local_image_digest_pulls_missing_image(self):
"""Test that get_local_image_digest pulls the image if it's not found locally"""
docker_client_mock = Mock()

# First call to images.get raises ImageNotFound
# Second call (after pull) returns the image data
local_image_data = Mock(
attrs={
"RepoDigests": ["image_name@sha256:pulled-digest"],
},
)
docker_client_mock.images.get.side_effect = [ImageNotFound("Image not found"), local_image_data]
docker_client_mock.images.pull.return_value = None

lambda_image = LambdaImage("layer_downloader", False, False, docker_client=docker_client_mock)
result = lambda_image.get_local_image_digest("image_name")

# Should pull the image and return its digest
docker_client_mock.images.pull.assert_called_once_with("image_name")
self.assertEqual("sha256:pulled-digest", result)

def test_get_local_image_digest_handles_pull_failure(self):
"""Test that get_local_image_digest returns None if pull fails"""
docker_client_mock = Mock()

# First call raises ImageNotFound, pull also fails
docker_client_mock.images.get.side_effect = ImageNotFound("Image not found")
docker_client_mock.images.pull.side_effect = APIError("Pull failed")

lambda_image = LambdaImage("layer_downloader", False, False, docker_client=docker_client_mock)
result = lambda_image.get_local_image_digest("image_name")

# Should attempt to pull and return None on failure
docker_client_mock.images.pull.assert_called_once_with("image_name")
self.assertIsNone(result)

@parameterized.expand(
[
("same-digest", "same-digest", True),
Expand Down
Loading