github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/test/python/docker/test_containers.py (about) 1 import subprocess 2 import sys 3 import time 4 import unittest 5 6 from docker import DockerClient, errors 7 8 from test.python.docker import Podman, common, constant 9 10 11 class TestContainers(unittest.TestCase): 12 podman = None # initialized podman configuration for tests 13 service = None # podman service instance 14 topContainerId = "" 15 16 def setUp(self): 17 super().setUp() 18 self.client = DockerClient(base_url="tcp://127.0.0.1:8080", timeout=15) 19 TestContainers.podman.restore_image_from_cache(self.client) 20 TestContainers.topContainerId = common.run_top_container(self.client) 21 self.assertIsNotNone(TestContainers.topContainerId) 22 23 def tearDown(self): 24 common.remove_all_containers(self.client) 25 common.remove_all_images(self.client) 26 self.client.close() 27 return super().tearDown() 28 29 @classmethod 30 def setUpClass(cls): 31 super().setUpClass() 32 TestContainers.podman = Podman() 33 TestContainers.service = TestContainers.podman.open( 34 "system", "service", "tcp:127.0.0.1:8080", "--time=0" 35 ) 36 # give the service some time to be ready... 37 time.sleep(2) 38 39 rc = TestContainers.service.poll() 40 if rc is not None: 41 raise subprocess.CalledProcessError(rc, "podman system service") 42 43 @classmethod 44 def tearDownClass(cls): 45 TestContainers.service.terminate() 46 stdout, stderr = TestContainers.service.communicate(timeout=0.5) 47 if stdout: 48 sys.stdout.write("\nContainers Service Stdout:\n" + stdout.decode("utf-8")) 49 if stderr: 50 sys.stderr.write("\nContainers Service Stderr:\n" + stderr.decode("utf-8")) 51 52 TestContainers.podman.tear_down() 53 return super().tearDownClass() 54 55 def test_create_container(self): 56 # Run a container with detach mode 57 self.client.containers.create(image="alpine", detach=True) 58 self.assertEqual(len(self.client.containers.list(all=True)), 2) 59 60 def test_create_network(self): 61 net = self.client.networks.create("testNetwork", driver="bridge") 62 ctnr = self.client.containers.create(image="alpine", detach=True) 63 64 # TODO fix when ready 65 # This test will not work until all connect|disconnect 66 # code is fixed. 67 # net.connect(ctnr) 68 69 # nets = self.client.networks.list(greedy=True) 70 # self.assertGreaterEqual(len(nets), 1) 71 72 # TODO fix endpoint to include containers 73 # for n in nets: 74 # if n.id == "testNetwork": 75 # self.assertEqual(ctnr.id, n.containers) 76 # self.assertTrue(False, "testNetwork not found") 77 78 def test_start_container(self): 79 # Podman docs says it should give a 304 but returns with no response 80 # # Start a already started container should return 304 81 # response = self.client.api.start(container=TestContainers.topContainerId) 82 # self.assertEqual(error.exception.response.status_code, 304) 83 84 # Create a new container and validate the count 85 self.client.containers.create(image=constant.ALPINE, name="container2") 86 containers = self.client.containers.list(all=True) 87 self.assertEqual(len(containers), 2) 88 89 def test_stop_container(self): 90 top = self.client.containers.get(TestContainers.topContainerId) 91 self.assertEqual(top.status, "running") 92 93 # Stop a running container and validate the state 94 top.stop() 95 top.reload() 96 self.assertIn(top.status, ("stopped", "exited")) 97 98 def test_restart_container(self): 99 # Validate the container state 100 top = self.client.containers.get(TestContainers.topContainerId) 101 top.stop() 102 top.reload() 103 self.assertIn(top.status, ("stopped", "exited")) 104 105 # restart a running container and validate the state 106 top.restart() 107 top.reload() 108 self.assertEqual(top.status, "running") 109 110 def test_remove_container(self): 111 # Remove container by ID with force 112 top = self.client.containers.get(TestContainers.topContainerId) 113 top.remove(force=True) 114 self.assertEqual(len(self.client.containers.list()), 0) 115 116 def test_remove_container_without_force(self): 117 # Validate current container count 118 self.assertTrue(len(self.client.containers.list()), 1) 119 120 # Remove running container should throw error 121 top = self.client.containers.get(TestContainers.topContainerId) 122 with self.assertRaises(errors.APIError) as error: 123 top.remove() 124 self.assertEqual(error.exception.response.status_code, 500) 125 126 # Remove container by ID without force 127 top.stop() 128 top.remove() 129 self.assertEqual(len(self.client.containers.list()), 0) 130 131 def test_pause_container(self): 132 # Validate the container state 133 top = self.client.containers.get(TestContainers.topContainerId) 134 self.assertEqual(top.status, "running") 135 136 # Pause a running container and validate the state 137 top.pause() 138 top.reload() 139 self.assertEqual(top.status, "paused") 140 141 def test_pause_stopped_container(self): 142 # Stop the container 143 top = self.client.containers.get(TestContainers.topContainerId) 144 top.stop() 145 146 # Pause exited container should trow error 147 with self.assertRaises(errors.APIError) as error: 148 top.pause() 149 self.assertEqual(error.exception.response.status_code, 500) 150 151 def test_unpause_container(self): 152 top = self.client.containers.get(TestContainers.topContainerId) 153 154 # Validate the container state 155 top.pause() 156 top.reload() 157 self.assertEqual(top.status, "paused") 158 159 # Pause a running container and validate the state 160 top.unpause() 161 top.reload() 162 self.assertEqual(top.status, "running") 163 164 def test_list_container(self): 165 # Add container and validate the count 166 self.client.containers.create(image="alpine", detach=True) 167 containers = self.client.containers.list(all=True) 168 self.assertEqual(len(containers), 2) 169 170 def test_filters(self): 171 self.skipTest("TODO Endpoint does not yet support filters") 172 173 # List container with filter by id 174 filters = {"id": TestContainers.topContainerId} 175 ctnrs = self.client.containers.list(all=True, filters=filters) 176 self.assertEqual(len(ctnrs), 1) 177 178 # List container with filter by name 179 filters = {"name": "top"} 180 ctnrs = self.client.containers.list(all=True, filters=filters) 181 self.assertEqual(len(ctnrs), 1) 182 183 def test_rename_container(self): 184 top = self.client.containers.get(TestContainers.topContainerId) 185 186 # rename bogus container 187 with self.assertRaises(errors.APIError) as error: 188 top.rename(name="newname") 189 self.assertEqual(error.exception.response.status_code, 404)