Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(RELEASE-999): check for existing Pyxis image #319

Closed
wants to merge 1 commit into from
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: 5 additions & 6 deletions pyxis/create_container_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,18 @@ def proxymap(repository: str) -> str:
return repository.split("/")[-1].replace("----", "/")


def image_already_exists(args, digest: str, repository: str) -> Any:
def image_already_exists(args, repository: str) -> Any:
"""Function to check if a containerImage with the given digest and repository
already exists in the pyxis instance

If `repository` is None, then the return True if the image exists at all.

:return: the image id, if one exists, else None if not found
"""

# quote is needed to urlparse the quotation marks
raw_filter = f'repositories.manifest_schema2_digest=="{digest}";not(deleted==true)'
raw_filter = f'image_id=="{args.architecture_digest}";not(deleted==true)'
if repository:
raw_filter += f';repositories.repository=="{proxymap(repository)}"'
# quote is needed to urlparse the quotation marks
filter_str = quote(raw_filter)

check_url = urljoin(args.pyxis_url, f"v1/images?page_size=1&filter={filter_str}")
Expand Down Expand Up @@ -378,12 +377,12 @@ def main(): # pragma: no cover

# First check if it exists at all
LOGGER.info(f"Checking to see if digest {args.architecture_digest} exists in pyxis")
image = image_already_exists(args, args.architecture_digest, repository=None)
image = image_already_exists(args, repository=None)
if image:
# Then, check if it exists in association with the given repository
identifier = image["_id"]
LOGGER.info(f"It does! Checking to see if it's associated with {args.name}")
if image_already_exists(args, args.architecture_digest, repository=args.name):
if image_already_exists(args, repository=args.name):
LOGGER.info(
f"Image with given docker_image_digest already exists as {identifier} "
f"and is associated with repository {args.name}. "
Expand Down
11 changes: 5 additions & 6 deletions pyxis/test_create_container_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ def test_image_already_exists__image_does_exist(mock_get):
mock_rsp.json.return_value = {"data": [{"_id": 0}]}

# Act
exists = image_already_exists(args, args.architecture_digest, args.name)
exists = image_already_exists(args, args.name)

# Assert
assert exists
mock_get.assert_called_once_with(
mock_pyxis_url
+ "v1/images?page_size=1&filter="
+ "repositories.manifest_schema2_digest%3D%3D%22some_digest%22"
+ "image_id%3D%3D%22some_digest%22"
+ "%3Bnot%28deleted%3D%3Dtrue%29"
+ "%3Brepositories.repository%3D%3D%22some_name%22"
)
Expand All @@ -48,14 +48,13 @@ def test_image_already_exists__image_does_not_exist(mock_get):
mock_get.return_value = mock_rsp
args = MagicMock()
args.pyxis_url = mock_pyxis_url
digest = "some_digest"
name = "server/org/some----name"

# Image doesn't exist
mock_rsp.json.return_value = {"data": []}

# Act
exists = image_already_exists(args, digest, name)
exists = image_already_exists(args, name)

# Assert
assert not exists
Expand All @@ -75,14 +74,14 @@ def test_image_already_exists__image_does_exist_but_no_repo(mock_get):
mock_rsp.json.return_value = {"data": [{"_id": 0}]}

# Act
exists = image_already_exists(args, args.architecture_digest, None)
exists = image_already_exists(args, None)

# Assert
assert exists
mock_get.assert_called_once_with(
mock_pyxis_url
+ "v1/images?page_size=1&filter="
+ "repositories.manifest_schema2_digest%3D%3D%22some_digest%22"
+ "image_id%3D%3D%22some_digest%22"
+ "%3Bnot%28deleted%3D%3Dtrue%29"
)

Expand Down