github.com/containers/podman/v5@v5.1.0-rc1/test/python/docker/compat/test_images.py (about)

     1  """
     2  Integration tests for exercising docker-py against Podman Service.
     3  """
     4  import io
     5  import os
     6  import unittest
     7  import json
     8  
     9  from docker import errors
    10  
    11  # pylint: disable=no-name-in-module,import-error,wrong-import-order
    12  from test.python.docker.compat import common, constant
    13  
    14  
    15  class TestImages(common.DockerTestCase):
    16      """TestCase for exercising images."""
    17  
    18      def test_tag_valid_image(self):
    19          """Validates if the image is tagged successfully"""
    20          alpine = self.docker.images.get(constant.ALPINE)
    21          self.assertTrue(alpine.tag("demo", constant.ALPINE_SHORTNAME))
    22  
    23          alpine = self.docker.images.get(constant.ALPINE)
    24          for tag in alpine.tags:
    25              self.assertIn("alpine", tag)
    26  
    27      def test_retag_valid_image(self):
    28          """Validates if name updates when the image is re-tagged."""
    29          alpine = self.docker.images.get(constant.ALPINE)
    30          self.assertTrue(alpine.tag("demo", "rename"))
    31  
    32          alpine = self.docker.images.get(constant.ALPINE)
    33          self.assertNotIn("demo:test", alpine.tags)
    34  
    35      def test_list_images(self):
    36          """List images"""
    37          self.assertEqual(len(self.docker.images.list()), 1)
    38  
    39          # Add more images
    40          self.docker.images.pull(constant.BB)
    41          self.assertEqual(len(self.docker.images.list()), 2)
    42          self.assertEqual(len(self.docker.images.list(all=True)), 2)
    43  
    44          # List images with filter
    45          self.assertEqual(len(self.docker.images.list(filters={"reference": "alpine"})), 1)
    46  
    47      def test_search_image(self):
    48          """Search for image"""
    49          for registry in self.docker.images.search("alpine"):
    50              # registry matches if string is in either one
    51              self.assertIn("alpine", registry["Name"] + " " + registry["Description"].lower())
    52  
    53      def test_search_bogus_image(self):
    54          """Search for bogus image should throw exception"""
    55          with self.assertRaises(errors.APIError):
    56              self.docker.images.search("bogus/bogus")
    57  
    58      def test_remove_image(self):
    59          """Remove image"""
    60          # Check for error with wrong image name
    61          with self.assertRaises(errors.NotFound):
    62              self.docker.images.remove("dummy")
    63  
    64          common.remove_all_containers(self.docker)
    65          self.assertEqual(len(self.docker.images.list()), 1)
    66          self.docker.images.remove(constant.ALPINE)
    67          self.assertEqual(len(self.docker.images.list()), 0)
    68  
    69      def test_image_history(self):
    70          """Image history"""
    71          img = self.docker.images.get(constant.ALPINE)
    72          history = img.history()
    73  
    74          found = False
    75          for change in history:
    76              found |= img.id in change.values()
    77          self.assertTrue(found, f"image id {img.id} not found in history")
    78  
    79      def test_get_image_exists_not(self):
    80          """Negative test for get image"""
    81          with self.assertRaises(errors.NotFound):
    82              self.docker.images.get("image_does_not_exists")
    83  
    84      def test_save_image(self):
    85          """Export Image"""
    86          image = self.docker.images.pull(constant.BB)
    87  
    88          file = os.path.join(TestImages.podman.image_cache, "busybox.tar")
    89          with open(file, mode="wb") as tarball:
    90              for frame in image.save(named=True):
    91                  tarball.write(frame)
    92          self.assertGreater(os.path.getsize(file), 0)
    93  
    94      def test_load_image(self):
    95          """Import|Load Image"""
    96          self.assertEqual(len(self.docker.images.list()), 1)
    97  
    98          image = self.docker.images.pull(constant.BB)
    99          file = os.path.join(TestImages.podman.image_cache, "busybox.tar")
   100          with open(file, mode="wb") as tarball:
   101              for frame in image.save():
   102                  tarball.write(frame)
   103  
   104          with open(file, mode="rb") as saved:
   105              self.docker.images.load(saved)
   106  
   107          self.assertEqual(len(self.docker.images.list()), 2)
   108  
   109      def test_load_corrupt_image(self):
   110          """Import|Load Image failure"""
   111          tarball = io.BytesIO("This is a corrupt tarball".encode("utf-8"))
   112          with self.assertRaises(errors.APIError):
   113              self.docker.images.load(tarball)
   114  
   115      def test_build_image(self):
   116          """Build Image with custom labels."""
   117          labels = {"apple": "red", "grape": "green"}
   118          self.docker.images.build(
   119              path="test/python/docker/build_labels",
   120              labels=labels,
   121              tag="labels",
   122              isolation="default",
   123          )
   124          image = self.docker.images.get("labels")
   125          self.assertEqual(image.labels["apple"], labels["apple"])
   126          self.assertEqual(image.labels["grape"], labels["grape"])
   127  
   128      def test_build_image_via_api_client(self):
   129          api_client = self.docker.api
   130          for line in api_client.build(path="test/python/docker/build_labels"):
   131              try:
   132                  parsed = json.loads(line.decode("utf-8"))
   133              except json.JSONDecodeError as e:
   134                  raise IOError(f"Line '{line}' was not JSON parsable")
   135              assert "errorDetail" not in parsed
   136  
   137  if __name__ == "__main__":
   138      # Setup temporary space
   139      unittest.main()