From: David Garcia Date: Wed, 10 Mar 2021 16:09:44 +0000 (+0100) Subject: Minor improvements to libjuju.py X-Git-Tag: branch-sol006v331-start~10 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=refs%2Fchanges%2F73%2F10473%2F5;p=osm%2FN2VC.git Minor improvements to libjuju.py - Increase default timeout to connect to the controller - Minor fix in destroy_model function - Fix unit tests. Now they run faster :-) Change-Id: Ia2cbcb3e052fe91f4e712ea572ecf444b1d5894f Signed-off-by: David Garcia --- diff --git a/n2vc/libjuju.py b/n2vc/libjuju.py index a6c1c42..191821a 100644 --- a/n2vc/libjuju.py +++ b/n2vc/libjuju.py @@ -117,7 +117,7 @@ class Libjuju: def _create_health_check_task(self): return self.loop.create_task(self.health_check()) - async def get_controller(self, timeout: float = 5.0) -> Controller: + async def get_controller(self, timeout: float = 15.0) -> Controller: """ Get controller @@ -854,8 +854,9 @@ class Libjuju: """ controller = await self.get_controller() - model = await self.get_model(controller, model_name) + model = None try: + model = await self.get_model(controller, model_name) self.log.debug("Destroying model {}".format(model_name)) uuid = model.info.uuid @@ -889,6 +890,10 @@ class Libjuju: raise Exception( "Timeout waiting for model {} to be destroyed".format(model_name) ) + except Exception as e: + if model: + await self.disconnect_model(model) + raise e finally: await self.disconnect_controller(controller) diff --git a/n2vc/tests/unit/test_juju_watcher.py b/n2vc/tests/unit/test_juju_watcher.py index 41065bf..d333b33 100644 --- a/n2vc/tests/unit/test_juju_watcher.py +++ b/n2vc/tests/unit/test_juju_watcher.py @@ -142,6 +142,7 @@ class StatusTest(TestCase): self.assertTrue(isinstance(value, str)) +@asynctest.mock.patch("asyncio.sleep") class WaitForModelTest(asynctest.TestCase): @asynctest.mock.patch("juju.client.connector.Connector.connect") def setUp(self, mock_connect=None): @@ -149,7 +150,7 @@ class WaitForModelTest(asynctest.TestCase): self.model = Model() @asynctest.mock.patch("juju.model.Model.block_until") - def test_wait_for_model(self, mock_block_until): + def test_wait_for_model(self, mock_block_until, mock_sleep): self.loop.run_until_complete( JujuModelWatcher.wait_for_model(self.model, timeout=None) ) @@ -157,7 +158,7 @@ class WaitForModelTest(asynctest.TestCase): @asynctest.mock.patch("asyncio.ensure_future") @asynctest.mock.patch("asyncio.wait") - def test_wait_for_model_exception(self, mock_wait, mock_ensure_future): + def test_wait_for_model_exception(self, mock_wait, mock_ensure_future, mock_sleep): task = Mock() mock_ensure_future.return_value = task mock_wait.side_effect = Exception diff --git a/n2vc/tests/unit/test_k8s_juju_conn.py b/n2vc/tests/unit/test_k8s_juju_conn.py index f486649..ae262b2 100644 --- a/n2vc/tests/unit/test_k8s_juju_conn.py +++ b/n2vc/tests/unit/test_k8s_juju_conn.py @@ -200,6 +200,9 @@ class InitEnvTest(K8sJujuConnTestCase): self.k8s_juju_conn._create_cluster_role = Mock() self.k8s_juju_conn._create_service_account = Mock() self.k8s_juju_conn._create_cluster_role_binding = Mock() + self.k8s_juju_conn._delete_cluster_role = Mock() + self.k8s_juju_conn._delete_service_account = Mock() + self.k8s_juju_conn._delete_cluster_role_binding = Mock() self.k8s_juju_conn._get_secret_data = AsyncMock() self.k8s_juju_conn._get_secret_data.return_value = ("token", "cacert")