github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/test/python/docker/test_images.py (about) 1 import collections 2 import os 3 import subprocess 4 import sys 5 import time 6 import unittest 7 8 from docker import DockerClient, errors 9 10 from test.python.docker import Podman, common, constant 11 12 13 class TestImages(unittest.TestCase): 14 podman = None # initialized podman configuration for tests 15 service = None # podman service instance 16 17 def setUp(self): 18 super().setUp() 19 self.client = DockerClient(base_url="tcp://127.0.0.1:8080", timeout=15) 20 21 TestImages.podman.restore_image_from_cache(self.client) 22 23 def tearDown(self): 24 common.remove_all_images(self.client) 25 self.client.close() 26 return super().tearDown() 27 28 @classmethod 29 def setUpClass(cls): 30 super().setUpClass() 31 TestImages.podman = Podman() 32 TestImages.service = TestImages.podman.open( 33 "system", "service", "tcp:127.0.0.1:8080", "--time=0" 34 ) 35 # give the service some time to be ready... 36 time.sleep(2) 37 38 returncode = TestImages.service.poll() 39 if returncode is not None: 40 raise subprocess.CalledProcessError(returncode, "podman system service") 41 42 @classmethod 43 def tearDownClass(cls): 44 TestImages.service.terminate() 45 stdout, stderr = TestImages.service.communicate(timeout=0.5) 46 if stdout: 47 sys.stdout.write("\nImages Service Stdout:\n" + stdout.decode("utf-8")) 48 if stderr: 49 sys.stderr.write("\nImAges Service Stderr:\n" + stderr.decode("utf-8")) 50 51 TestImages.podman.tear_down() 52 return super().tearDownClass() 53 54 def test_tag_valid_image(self): 55 """Validates if the image is tagged successfully""" 56 alpine = self.client.images.get(constant.ALPINE) 57 self.assertTrue(alpine.tag("demo", constant.ALPINE_SHORTNAME)) 58 59 alpine = self.client.images.get(constant.ALPINE) 60 for t in alpine.tags: 61 self.assertIn("alpine", t) 62 63 # @unittest.skip("doesn't work now") 64 def test_retag_valid_image(self): 65 """Validates if name updates when the image is retagged""" 66 alpine = self.client.images.get(constant.ALPINE) 67 self.assertTrue(alpine.tag("demo", "rename")) 68 69 alpine = self.client.images.get(constant.ALPINE) 70 self.assertNotIn("demo:test", alpine.tags) 71 72 def test_list_images(self): 73 """List images""" 74 self.assertEqual(len(self.client.images.list()), 1) 75 76 # Add more images 77 self.client.images.pull(constant.BB) 78 self.assertEqual(len(self.client.images.list()), 2) 79 80 # List images with filter 81 self.assertEqual(len(self.client.images.list(filters={"reference": "alpine"})), 1) 82 83 def test_search_image(self): 84 """Search for image""" 85 for r in self.client.images.search("libpod/alpine"): 86 self.assertIn("quay.io/libpod/alpine", r["Name"]) 87 88 def test_remove_image(self): 89 """Remove image""" 90 # Check for error with wrong image name 91 with self.assertRaises(errors.NotFound): 92 self.client.images.remove("dummy") 93 self.assertEqual(len(self.client.images.list()), 1) 94 95 self.client.images.remove(constant.ALPINE) 96 self.assertEqual(len(self.client.images.list()), 0) 97 98 def test_image_history(self): 99 """Image history""" 100 img = self.client.images.get(constant.ALPINE) 101 history = img.history() 102 image_id = img.id[7:] if img.id.startswith("sha256:") else img.id 103 104 found = False 105 for change in history: 106 found |= image_id in change.values() 107 self.assertTrue(found, f"image id {image_id} not found in history") 108 109 def test_get_image_exists_not(self): 110 """Negative test for get image""" 111 with self.assertRaises(errors.NotFound): 112 response = self.client.images.get("image_does_not_exists") 113 collections.deque(response) 114 115 def test_save_image(self): 116 """Export Image""" 117 image = self.client.images.pull(constant.BB) 118 119 file = os.path.join(TestImages.podman.image_cache, "busybox.tar") 120 with open(file, mode="wb") as tarball: 121 for frame in image.save(named=True): 122 tarball.write(frame) 123 sz = os.path.getsize(file) 124 self.assertGreater(sz, 0) 125 126 def test_load_image(self): 127 """Import|Load Image""" 128 self.assertEqual(len(self.client.images.list()), 1) 129 130 image = self.client.images.pull(constant.BB) 131 file = os.path.join(TestImages.podman.image_cache, "busybox.tar") 132 with open(file, mode="wb") as tarball: 133 for frame in image.save(): 134 tarball.write(frame) 135 136 with open(file, mode="rb") as saved: 137 _ = self.client.images.load(saved) 138 139 self.assertEqual(len(self.client.images.list()), 2) 140 141 142 if __name__ == "__main__": 143 # Setup temporary space 144 unittest.main()