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 _span_container(self): try: self._container = self._instance._get_gcloud_container() # pylint: disable=protected-access except Exception as exc: try: ContainerManager.destroy_container(self._instance, self._name) except Exception: # pylint: disable=broad-except pass raise exc from None
def _destroy_container(self): self._span_counter -= 1 if self._span_counter != 0: return try: ContainerManager.destroy_container(self._instance, self._name) except Exception: # pylint: disable=broad-except pass self._container = None
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 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_destroy(self): with self.subTest("Try to destroy non-existent container"): self.assertRaises(NotFound, ContainerManager.destroy_container, self.node, "c2") with self.subTest("without *_container_logfile hook"): self.assertTrue(ContainerManager.destroy_container(self.node, "c1")) self.assertRaises(NotFound, ContainerManager.get_container, self.node, "c1") self.assertRaises(NotFound, ContainerManager.destroy_container, self.node, "c1")
def test_destroy_logfile(self): self.node.c1_container_logfile = "container.log" with patch("builtins.open", mock_open()) as mock_file: self.assertTrue(ContainerManager.destroy_container(self.node, "c1")) mock_file.assert_called_once_with("container.log", "ab") mock_file().write.assert_called_once_with("container logs") self.assertRaises(NotFound, ContainerManager.get_container, self.node, "c1") self.assertRaises(NotFound, ContainerManager.destroy_container, self.node, "c1")
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 _destroy_container(self): try: ContainerManager.destroy_container(self._instance, self._name) except Exception: # pylint: disable=broad-except pass self._container = None