Skip to content

Commit

Permalink
ECR: list_images() should list images with multiple tags separately (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bblommers authored Nov 7, 2024
1 parent 092bd10 commit 60828f4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
9 changes: 0 additions & 9 deletions moto/ecr/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,15 +337,6 @@ def response_object(self) -> Dict[str, Any]: # type: ignore[misc]
k: v for k, v in response_object.items() if v is not None and v != [None]
}

@property
def response_list_object(self) -> Dict[str, Any]: # type: ignore[misc]
response_object = self.gen_response_object()
response_object["imageTag"] = self.image_tag
response_object["imageDigest"] = self.get_image_digest()
return {
k: v for k, v in response_object.items() if v is not None and v != [None]
}

@property
def response_describe_object(self) -> Dict[str, Any]: # type: ignore[misc]
response_object = self.gen_response_object()
Expand Down
8 changes: 5 additions & 3 deletions moto/ecr/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ def list_images(self) -> str:
repository_str = self._get_param("repositoryName")
registry_id = self._get_param("registryId")
images = self.ecr_backend.list_images(repository_str, registry_id)
return json.dumps(
{"imageIds": [image.response_list_object for image in images]}
)
resp = []
for image in images:
for tag in image.image_tags or [None]: # type: ignore
resp.append({"imageDigest": image.image_digest, "imageTag": tag})
return json.dumps({"imageIds": resp})

def describe_images(self) -> str:
repository_str = self._get_param("repositoryName")
Expand Down
29 changes: 29 additions & 0 deletions tests/test_ecr/test_ecr_boto3.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,35 @@ def test_list_images():
assert "sha" in response["imageIds"][0]["imageDigest"]


@mock_aws
def test_list_same_image_with_multiple_tags():
ecr_client = boto3.client("ecr", "us-east-1")

ecr_client.create_repository(repositoryName="reponame")

manifest = json.dumps(_create_image_manifest())

ecr_client.put_image(
repositoryName="reponame", imageTag="tag1", imageManifest=manifest
)
ecr_client.put_image(
repositoryName="reponame", imageTag="tag2", imageManifest=manifest
)

images = ecr_client.list_images(repositoryName="reponame")["imageIds"]
assert len(images) == 2
assert images[0]["imageDigest"] == images[1]["imageDigest"]
assert images[0]["imageTag"] == "tag1"
assert images[1]["imageTag"] == "tag2"

# Ensure tag is not required
manifest2 = _create_image_manifest()
ecr_client.put_image(repositoryName="reponame", imageManifest=json.dumps(manifest2))

images = ecr_client.list_images(repositoryName="reponame")["imageIds"]
assert {"imageDigest": manifest2["config"]["digest"]} in images


@mock_aws
def test_list_images_from_repository_that_doesnt_exist():
client = boto3.client("ecr", region_name=ECR_REGION)
Expand Down

0 comments on commit 60828f4

Please sign in to comment.