github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/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  
     8  from docker import errors
     9  
    10  # pylint: disable=no-name-in-module,import-error,wrong-import-order
    11  from test.python.docker.compat import common, constant
    12  
    13  
    14  class TestImages(common.DockerTestCase):
    15      """TestCase for exercising images."""
    16  
    17      def test_tag_valid_image(self):
    18          """Validates if the image is tagged successfully"""
    19          alpine = self.docker.images.get(constant.ALPINE)
    20          self.assertTrue(alpine.tag("demo", constant.ALPINE_SHORTNAME))
    21  
    22          alpine = self.docker.images.get(constant.ALPINE)
    23          for tag in alpine.tags:
    24              self.assertIn("alpine", tag)
    25  
    26      def test_retag_valid_image(self):
    27          """Validates if name updates when the image is re-tagged."""
    28          alpine = self.docker.images.get(constant.ALPINE)
    29          self.assertTrue(alpine.tag("demo", "rename"))
    30  
    31          alpine = self.docker.images.get(constant.ALPINE)
    32          self.assertNotIn("demo:test", alpine.tags)
    33  
    34      def test_list_images(self):
    35          """List images"""
    36          self.assertEqual(len(self.docker.images.list()), 1)
    37  
    38          # Add more images
    39          self.docker.images.pull(constant.BB)
    40          self.assertEqual(len(self.docker.images.list()), 2)
    41          self.assertEqual(len(self.docker.images.list(all=True)), 2)
    42  
    43          # List images with filter
    44          self.assertEqual(len(self.docker.images.list(filters={"reference": "alpine"})), 1)
    45  
    46      def test_search_image(self):
    47          """Search for image"""
    48          for registry in self.docker.images.search("alpine"):
    49              # registry matches if string is in either one
    50              self.assertIn("alpine", registry["Name"] + " " + registry["Description"].lower())
    51  
    52      def test_search_bogus_image(self):
    53          """Search for bogus image should throw exception"""
    54          with self.assertRaises(errors.APIError):
    55              self.docker.images.search("bogus/bogus")
    56  
    57      def test_remove_image(self):
    58          """Remove image"""
    59          # Check for error with wrong image name
    60          with self.assertRaises(errors.NotFound):
    61              self.docker.images.remove("dummy")
    62  
    63          common.remove_all_containers(self.docker)
    64          self.assertEqual(len(self.docker.images.list()), 1)
    65          self.docker.images.remove(constant.ALPINE)
    66          self.assertEqual(len(self.docker.images.list()), 0)
    67  
    68      def test_image_history(self):
    69          """Image history"""
    70          img = self.docker.images.get(constant.ALPINE)
    71          history = img.history()
    72          image_id = img.id[7:] if img.id.startswith("sha256:") else img.id
    73  
    74          found = False
    75          for change in history:
    76              found |= image_id in change.values()
    77          self.assertTrue(found, f"image id {image_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  
   129  if __name__ == "__main__":
   130      # Setup temporary space
   131      unittest.main()