def test_register_container(self): c2_container = DummyContainer() with self.subTest("Try to register registered container"): self.assertRaisesRegex(ContainerAlreadyRegistered, "container .* registered already", ContainerManager.register_container, self.node, "c2", container=self.container) with self.subTest("Try to register registered container with replace=True"): self.assertRaisesRegex(ContainerAlreadyRegistered, "container .* registered already", ContainerManager.register_container, self.node, "c2", container=self.container, replace=True) with self.subTest("Try to replace container"): self.assertRaisesRegex(ContainerAlreadyRegistered, "another container registered", ContainerManager.register_container, self.node, "c1", container=c2_container) with self.subTest("Force container replacement"): ContainerManager.register_container(self.node, "c1", container=c2_container, replace=True) self.assertEqual(ContainerManager.get_container(self.node, "c1"), c2_container) with self.subTest("Register container"): ContainerManager.register_container(self.node, "c2", container=self.container) self.assertEqual(ContainerManager.get_container(self.node, "c2"), self.container) with self.subTest("Try to force container replacement with container registered with another name"): self.assertRaisesRegex(ContainerAlreadyRegistered, "container .* registered already", ContainerManager.register_container, self.node, "c1", container=self.container, replace=True)
def test_get_container(self): with self.subTest("Get existent container"): self.assertIs(ContainerManager.get_container(self.node, "c1"), self.container) with self.subTest("Try to get non-existent container without exception"): self.assertIs(ContainerManager.get_container(self.node, "c2", raise_not_found_exc=False), None) with self.subTest("Check NotFound exception"): self.assertRaises(NotFound, ContainerManager.get_container, self.node, "c2")
def destroy_container(self, name): try: if ContainerManager.get_container(self.node, name, raise_not_found_exc=False) is not None: LOGGER.debug("Destroy %s (%s) container", name, self) ContainerManager.destroy_container(self.node, name, ignore_keepalive=True) LOGGER.info("%s (%s) destroyed", name, self) except Exception as exc: # pylint: disable=broad-except LOGGER.error("%s: some exception raised during container '%s' destroying", self, name, exc_info=exc)
def configure_ldap(cls, node, use_ssl=False): ContainerManager.run_container(node, "ldap") if use_ssl: port = node.ldap_ports['ldap_ssl_port'] else: port = node.ldap_ports['ldap_port'] address = get_my_ip() cls.LDAP_ADDRESS = (address, port) if ContainerManager.get_container(node, 'ldap').exec_run("timeout 30s container/tool/wait-process")[0] != 0: raise LdapServerNotReady("LDAP server didn't finish its startup yet...")
def test_destroy_keep_alive(self): ContainerManager.set_container_keep_alive(self.node, "c1") with self.subTest("Try to destroy container with keep-alive tag"): self.assertFalse(ContainerManager.destroy_container(self.node, "c1")) self.assertEqual(ContainerManager.get_container(self.node, "c1"), self.container) with self.subTest("Ignore keep-alive tag"): self.assertTrue(ContainerManager.destroy_container(self.node, "c1", ignore_keepalive=True)) self.assertRaises(NotFound, ContainerManager.get_container, self.node, "c1")
def test_destroy_all(self): ContainerManager.run_container(self.node, "c2") ContainerManager.set_container_keep_alive(self.node, "c1") with self.subTest("Destroy all containers without keep-alive tag"): ContainerManager.destroy_all_containers(self.node) self.assertRaises(NotFound, ContainerManager.get_container, self.node, "c2") self.assertEqual(ContainerManager.get_container(self.node, "c1"), self.container) with self.subTest("Ignore keep-alive tag"): ContainerManager.destroy_all_containers(self.node, ignore_keepalive=True) self.assertRaises(NotFound, ContainerManager.get_container, self.node, "c1")
def destroy_container(self, name): try: if ContainerManager.get_container( self.node, name, raise_not_found_exc=False) is not None: LOGGER.debug(f"Destroy {name} ({self}) container") ContainerManager.destroy_container(self.node, name, ignore_keepalive=True) LOGGER.info(f"{name} ({self}) destroyed") except Exception as exc: # pylint: disable=broad-except LOGGER.error( f"{self}: some exception raised during container `{name}' destroying", exc_info=exc)
def test_run_container(self): with self.subTest("Try to run existent container"): self.assertEqual(ContainerManager.run_container(self.node, "c1"), self.container) with self.subTest("Test no *_container_run_args hook available"): container2 = ContainerManager.run_container(self.node, "c2", arg1="value1", arg2="value2") self.assertEqual(container2.run_args, ((), {"detach": True, "labels": self.node.tags, "arg1": "value1", "arg2": "value2", }, )) self.assertEqual(ContainerManager.get_container(self.node, "c2"), container2) with self.subTest("Test no *_container_run_args hook available and member name"): c2another = ContainerManager.run_container(self.node, "c2:another", arg1="value1", arg2="value2") self.assertNotEqual(container2, c2another) self.assertEqual(c2another.run_args, ((), {"detach": True, "labels": self.node.tags, "arg1": "value1", "arg2": "value2", }, )) self.assertEqual(ContainerManager.get_container(self.node, "c2"), container2) self.assertEqual(ContainerManager.get_container(self.node, "c2:another"), c2another) with self.subTest("Test with *_container_run_args hook"): container3 = ContainerManager.run_container(self.node, "c3", blah="name") self.assertEqual(container3.run_args, ((), {"detach": True, "labels": self.node.tags, "name": "blah", }, )) self.assertEqual(ContainerManager.get_container(self.node, "c3"), container3) with self.subTest("Test with *_container_run_args hook and member name"): c3another = ContainerManager.run_container(self.node, "c3:another", blah="name") self.assertEqual(c3another.run_args, ((), {"detach": True, "labels": self.node.tags, "name": "blah-another", }, )) self.assertEqual(ContainerManager.get_container(self.node, "c3"), container3) self.assertEqual(ContainerManager.get_container(self.node, "c3:another"), c3another) with self.subTest("Verify that all containers are there"): set1 = {self.container, container2, c2another, container3, c3another} set2 = {ContainerManager.get_container(self.node, name) for name in ("c1", "c2", "c2:another", "c3", "c3:another")} self.assertSetEqual(set1, set2) self.assertEqual(len(set1), 5) with self.subTest("Re-run container"): self.container.start = Mock() ContainerManager.run_container(self.node, "c1") self.container.start.assert_called_once_with()
def test_destroy_logfile_callable(self): members = [] def logfile(member=None): members.append(member) return member self.node.c1_container_logfile = logfile c1another = ContainerManager.run_container(self.node, "c1:another") ContainerManager.set_container_keep_alive(self.node, "c1:another") with patch("builtins.open", mock_open()) as mock_file: self.assertTrue(ContainerManager.destroy_container(self.node, "c1")) self.assertFalse(ContainerManager.destroy_container(self.node, "c1:another")) mock_file.assert_called_once_with("another", "ab") mock_file().write.assert_called_once_with("container logs") self.assertEqual(members, [None, "another", ]) self.assertEqual(ContainerManager.get_container(self.node, "c1:another"), c1another) self.assertRaises(NotFound, ContainerManager.get_container, self.node, "c1") self.assertRaises(NotFound, ContainerManager.destroy_container, self.node, "c1")
def restart(self): ContainerManager.get_container(self, "node").restart()