Skip to content

Commit

Permalink
Sqash the commits
Browse files Browse the repository at this point in the history
Signed-off-by: Milan Balazs <milanbalazs01@gmail.com>
  • Loading branch information
milanbalazs committed Jul 8, 2024
1 parent de94d9e commit 2aee85e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
27 changes: 22 additions & 5 deletions podman/domain/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import logging
from typing import Any, Dict, Iterator, List, Optional, Union

import urllib.parse

from podman import api
from podman.domain.manager import PodmanResource
from podman.errors import ImageNotFound
from podman.errors import ImageNotFound, InvalidArgument

logger = logging.getLogger("podman.images")

Expand Down Expand Up @@ -68,7 +70,7 @@ def remove(
def save(
self,
chunk_size: Optional[int] = api.DEFAULT_CHUNK_SIZE,
named: Union[str, bool] = False, # pylint: disable=unused-argument
named: Union[str, bool] = False,
) -> Iterator[bytes]:
"""Returns Image as tarball.
Expand All @@ -77,13 +79,28 @@ def save(
Args:
chunk_size: If None, data will be streamed in received buffer size.
If not None, data will be returned in sized buffers. Default: 2MB
named: Ignored.
named (str or bool): If ``False`` (default), the tarball will not
retain repository and tag information for this image. If set
to ``True``, the first tag in the :py:attr:`~tags` list will
be used to identify the image. Alternatively, any element of
the :py:attr:`~tags` list can be used as an argument to use
that specific tag as the saved identifier.
Raises:
APIError: when service returns an error
APIError: When service returns an error
InvalidArgument: When the provided Tag name is not valid for the image.
"""

img = self.id
if named:
img = urllib.parse.quote(self.tags[0] if self.tags else img)
if isinstance(named, str):
if named not in self.tags:
raise InvalidArgument(f"{named} is not a valid tag for this image")
img = urllib.parse.quote(named)

response = self.client.get(
f"/images/{self.id}/get", params={"format": ["docker-archive"]}, stream=True
f"/images/{img}/get", params={"format": ["docker-archive"]}, stream=True
)
response.raise_for_status(not_found=ImageNotFound)
return response.iter_content(chunk_size=chunk_size)
Expand Down
13 changes: 10 additions & 3 deletions podman/tests/integration/test_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,21 @@ def test_image_crud(self):

with self.subTest("Export Image to tarball (in memory)"):
buffer = io.BytesIO()
for chunk in image.save():
for chunk in image.save(named=True):
buffer.write(chunk)
buffer.seek(0, 0)

with tarfile.open(fileobj=buffer, mode="r") as tar:
items = tar.getnames()
self.assertIn("manifest.json", items)
self.assertIn("repositories", items)
# Check if required files are available in the tarball
self.assertIn("manifest.json", items)
self.assertIn("repositories", items)
# Extract the 'repositories' file
repositories_file = tar.extractfile("repositories")
if repositories_file is not None:
# Check the content of the "repositories" file.
repositories_content = repositories_file.read().decode("utf-8")
self.assertTrue("quay.io/libpod/alpine" in str(repositories_content))

with self.subTest("List images"):
image_list = self.client.images.list()
Expand Down

0 comments on commit 2aee85e

Please sign in to comment.