Minor improvements to libjuju.py 73/10473/5
authorDavid Garcia <david.garcia@canonical.com>
Wed, 10 Mar 2021 16:09:44 +0000 (17:09 +0100)
committerDavid Garcia <david.garcia@canonical.com>
Wed, 10 Mar 2021 16:53:52 +0000 (17:53 +0100)
- 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 <david.garcia@canonical.com>
n2vc/libjuju.py
n2vc/tests/unit/test_juju_watcher.py
n2vc/tests/unit/test_k8s_juju_conn.py

index a6c1c42..191821a 100644 (file)
@@ -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)
 
index 41065bf..d333b33 100644 (file)
@@ -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
index f486649..ae262b2 100644 (file)
@@ -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")