Add get_metrics command to n2vc_juju_conn and libjuju
[osm/N2VC.git] / n2vc / tests / unit / test_libjuju.py
index 41b32d1..57a6ccc 100644 (file)
@@ -96,10 +96,11 @@ class GetControllerTest(LibjujuTestCase):
     ):
         self.libjuju.endpoints = []
         mock__update_api_endpoints_db.side_effect = Exception()
+        controller = None
         with self.assertRaises(JujuControllerFailedConnecting):
             controller = self.loop.run_until_complete(self.libjuju.get_controller())
-            self.assertIsNone(controller)
-            mock_disconnect_controller.assert_called_once()
+        self.assertIsNone(controller)
+        mock_disconnect_controller.assert_called_once()
 
     def test_same_endpoint_get_controller(
         self, mock__update_api_endpoints_db, mock_api_endpoints, mock_connect
@@ -151,7 +152,7 @@ class AddModelTest(LibjujuTestCase):
                 self.libjuju.add_model("existing_model", "cloud")
             )
 
-            mock_disconnect_controller.assert_called()
+        mock_disconnect_controller.assert_called()
 
     # TODO Check two job executing at the same time and one returning without doing anything.
 
@@ -254,7 +255,7 @@ class GetModelStatusTest(LibjujuTestCase):
 
         self.assertEqual(status, {"status"})
 
-    def test_excpetion(
+    def test_exception(
         self,
         mock_get_status,
         mock_disconnect_controller,
@@ -264,16 +265,16 @@ class GetModelStatusTest(LibjujuTestCase):
     ):
         mock_get_model.return_value = juju.model.Model()
         mock_get_status.side_effect = Exception()
-
+        status = None
         with self.assertRaises(Exception):
             status = self.loop.run_until_complete(
                 self.libjuju.get_model_status("model")
             )
 
-            mock_disconnect_controller.assert_called_once()
-            mock_disconnect_model.assert_called_once()
+        mock_disconnect_controller.assert_called_once()
+        mock_disconnect_model.assert_called_once()
 
-            self.assertIsNone(status)
+        self.assertIsNone(status)
 
 
 @asynctest.mock.patch("n2vc.libjuju.Libjuju.get_controller")
@@ -319,16 +320,18 @@ class CreateMachineTest(LibjujuTestCase):
         mock_get_model,
         mock_get_controller,
     ):
+        machine = None
+        bool_res = None
         mock_get_model.return_value = juju.model.Model()
         with self.assertRaises(JujuMachineNotFound):
             machine, bool_res = self.loop.run_until_complete(
                 self.libjuju.create_machine("model", "non_existing_machine")
             )
-            self.assertIsNone(machine)
-            self.assertIsNone(bool_res)
+        self.assertIsNone(machine)
+        self.assertIsNone(bool_res)
 
-            mock_disconnect_controller.assert_called()
-            mock_disconnect_model.assert_called()
+        mock_disconnect_controller.assert_called()
+        mock_disconnect_model.assert_called()
 
     def test_no_machine(
         self,
@@ -390,14 +393,15 @@ class DeployCharmTest(LibjujuTestCase):
         mock_get_model.return_value = juju.model.Model()
         mock_applications.return_value = {"existing_app"}
 
+        application = None
         with self.assertRaises(JujuApplicationExists):
             application = self.loop.run_until_complete(
                 self.libjuju.deploy_charm("existing_app", "path", "model", "machine",)
             )
-            self.assertIsNone(application)
+        self.assertIsNone(application)
 
-            mock_disconnect_controller.assert_called()
-            mock_disconnect_model.assert_called()
+        mock_disconnect_controller.assert_called()
+        mock_disconnect_model.assert_called()
 
     def test_non_existing_machine(
         self,
@@ -413,15 +417,16 @@ class DeployCharmTest(LibjujuTestCase):
     ):
         mock_get_model.return_value = juju.model.Model()
         mock_machines.return_value = {"existing_machine": FakeMachine()}
+        application = None
         with self.assertRaises(JujuMachineNotFound):
             application = self.loop.run_until_complete(
                 self.libjuju.deploy_charm("app", "path", "model", "machine",)
             )
 
-            self.assertIsNone(application)
+        self.assertIsNone(application)
 
-            mock_disconnect_controller.assert_called()
-            mock_disconnect_model.assert_called()
+        mock_disconnect_controller.assert_called()
+        mock_disconnect_model.assert_called()
 
     def test_2_units(
         self,
@@ -532,16 +537,17 @@ class ExecuteActionTest(LibjujuTestCase):
     ):
         mock__get_application.return_value = None
         mock_get_model.return_value = juju.model.Model()
-
+        output = None
+        status = None
         with self.assertRaises(JujuApplicationNotFound):
             output, status = self.loop.run_until_complete(
                 self.libjuju.execute_action("app", "model", "action",)
             )
-            self.assertIsNone(output)
-            self.assertIsNone(status)
+        self.assertIsNone(output)
+        self.assertIsNone(status)
 
-            mock_disconnect_controller.assert_called()
-            mock_disconnect_model.assert_called()
+        mock_disconnect_controller.assert_called()
+        mock_disconnect_model.assert_called()
 
     def test_no_action(
         self,
@@ -557,15 +563,17 @@ class ExecuteActionTest(LibjujuTestCase):
 
         mock_get_model.return_value = juju.model.Model()
         mock__get_application.return_value = FakeApplication()
+        output = None
+        status = None
         with self.assertRaises(JujuActionNotFound):
             output, status = self.loop.run_until_complete(
                 self.libjuju.execute_action("app", "model", "action",)
             )
-            self.assertIsNone(output)
-            self.assertIsNone(status)
+        self.assertIsNone(output)
+        self.assertIsNone(status)
 
-            mock_disconnect_controller.assert_called()
-            mock_disconnect_model.assert_called()
+        mock_disconnect_controller.assert_called()
+        mock_disconnect_model.assert_called()
 
     # TODO no leader unit found exception
 
@@ -614,15 +622,15 @@ class GetActionTest(LibjujuTestCase):
         mock_get_controller,
     ):
         mock_get_application.side_effect = Exception()
-
+        actions = None
         with self.assertRaises(Exception):
             actions = self.loop.run_until_complete(
                 self.libjuju.get_actions("app", "model")
             )
 
-            self.assertIsNone(actions)
-            mock_disconnect_controller.assert_called_once()
-            mock_disconnect_model.assert_called_once()
+        self.assertIsNone(actions)
+        mock_disconnect_controller.assert_called_once()
+        mock_disconnect_model.assert_called_once()
 
     def test_success(
         self,
@@ -648,6 +656,70 @@ class GetActionTest(LibjujuTestCase):
 @asynctest.mock.patch("n2vc.libjuju.Libjuju.get_model")
 @asynctest.mock.patch("n2vc.libjuju.Libjuju.disconnect_model")
 @asynctest.mock.patch("n2vc.libjuju.Libjuju.disconnect_controller")
+@asynctest.mock.patch("juju.application.Application.get_metrics")
+@asynctest.mock.patch("n2vc.libjuju.Libjuju._get_application")
+class GetMetricsTest(LibjujuTestCase):
+    def setUp(self):
+        super(GetMetricsTest, self).setUp()
+
+    def test_get_metrics_success(
+        self,
+        mock_get_application,
+        mock_get_metrics,
+        mock_disconnect_controller,
+        mock_disconnect_model,
+        mock_get_model,
+        mock_get_controller,
+    ):
+        mock_get_application.return_value = FakeApplication()
+        mock_get_model.return_value = juju.model.Model()
+
+        self.loop.run_until_complete(self.libjuju.get_metrics("model", "app1"))
+
+        mock_disconnect_controller.assert_called_once()
+        mock_disconnect_model.assert_called_once()
+
+    def test_get_metrics_exception(
+        self,
+        mock_get_application,
+        mock_get_metrics,
+        mock_disconnect_controller,
+        mock_disconnect_model,
+        mock_get_model,
+        mock_get_controller,
+    ):
+        mock_get_model.return_value = juju.model.Model()
+        mock_get_metrics.side_effect = Exception()
+        with self.assertRaises(Exception):
+            self.loop.run_until_complete(self.libjuju.get_metrics("model", "app1"))
+
+        mock_disconnect_controller.assert_called_once()
+        mock_disconnect_model.assert_called_once()
+
+    def test_missing_args_exception(
+        self,
+        mock_get_application,
+        mock_get_metrics,
+        mock_disconnect_controller,
+        mock_disconnect_model,
+        mock_get_model,
+        mock_get_controller,
+    ):
+        mock_get_model.return_value = juju.model.Model()
+
+        with self.assertRaises(Exception):
+            self.loop.run_until_complete(self.libjuju.get_metrics("", ""))
+
+        mock_get_controller.assert_not_called()
+        mock_get_model.assert_not_called()
+        mock_disconnect_controller.assert_not_called()
+        mock_disconnect_model.assert_not_called()
+
+
+@asynctest.mock.patch("n2vc.libjuju.Libjuju.get_controller")
+@asynctest.mock.patch("n2vc.libjuju.Libjuju.get_model")
+@asynctest.mock.patch("n2vc.libjuju.Libjuju.disconnect_model")
+@asynctest.mock.patch("n2vc.libjuju.Libjuju.disconnect_controller")
 @asynctest.mock.patch("juju.model.Model.add_relation")
 class AddRelationTest(LibjujuTestCase):
     def setUp(self):
@@ -718,8 +790,8 @@ class AddRelationTest(LibjujuTestCase):
                 self.libjuju.add_relation("model", "app1:relation1", "app2:relation2",)
             )
 
-            mock_disconnect_controller.assert_called_once()
-            mock_disconnect_model.assert_called_once()
+        mock_disconnect_controller.assert_called_once()
+        mock_disconnect_model.assert_called_once()
 
     def test_success(
         self,
@@ -735,9 +807,7 @@ class AddRelationTest(LibjujuTestCase):
             self.libjuju.add_relation("model", "app1:relation1", "app2:relation2",)
         )
 
-        mock_add_relation.assert_called_with(
-            "app1:relation1", "app2:relation2"
-        )
+        mock_add_relation.assert_called_with("app1:relation1", "app2:relation2")
         mock_disconnect_controller.assert_called_once()
         mock_disconnect_model.assert_called_once()
 
@@ -755,9 +825,7 @@ class AddRelationTest(LibjujuTestCase):
             self.libjuju.add_relation("model", "app1:relation1", "saas_name",)
         )
 
-        mock_add_relation.assert_called_with(
-            "app1:relation1", "saas_name"
-        )
+        mock_add_relation.assert_called_with("app1:relation1", "saas_name")
         mock_disconnect_controller.assert_called_once()
         mock_disconnect_model.assert_called_once()
 
@@ -840,8 +908,8 @@ class ConfigureApplicationTest(LibjujuTestCase):
             self.loop.run_until_complete(
                 self.libjuju.configure_application("model", "app", {"config"},)
             )
-            mock_disconnect_controller.assert_called_once()
-            mock_disconnect_model.assert_called_once()
+        mock_disconnect_controller.assert_called_once()
+        mock_disconnect_model.assert_called_once()
 
 
 # TODO _get_api_endpoints_db test case
@@ -976,9 +1044,7 @@ class ConsumeTest(LibjujuTestCase):
         mock_get_controller.return_value = juju.controller.Controller()
         mock_get_model.return_value = juju.model.Model()
 
-        self.loop.run_until_complete(
-            self.libjuju.consume("offer_url", "model_name")
-        )
+        self.loop.run_until_complete(self.libjuju.consume("offer_url", "model_name"))
         mock_consume.assert_called_once()
         mock_disconnect_model.assert_called_once()
         mock_disconnect_controller.assert_called_once()
@@ -1033,12 +1099,9 @@ class ConsumeTest(LibjujuTestCase):
     ):
         mock_get_controller.return_value = juju.controller.Controller()
         mock_get_model.return_value = juju.model.Model()
-        mock_consume.side_effect = juju.errors.JujuAPIError({
-            "error": "",
-            "response": "",
-            "request-id": "",
-
-        })
+        mock_consume.side_effect = juju.errors.JujuAPIError(
+            {"error": "", "response": "", "request-id": ""}
+        )
 
         with self.assertRaises(juju.errors.JujuAPIError):
             self.loop.run_until_complete(
@@ -1047,3 +1110,160 @@ class ConsumeTest(LibjujuTestCase):
         mock_consume.assert_called_once()
         mock_disconnect_model.assert_called_once()
         mock_disconnect_controller.assert_called_once()
+
+
+@asynctest.mock.patch("n2vc.libjuju.Libjuju.add_cloud")
+class AddK8sTest(LibjujuTestCase):
+    def setUp(self):
+        super(AddK8sTest, self).setUp()
+        self.auth_data = {
+            "server": "https://192.168.0.21:16443",
+            "token": "1234",
+            "cacert": "cacert",
+        }
+
+    def test_add_k8s(self, mock_add_cloud):
+        self.loop.run_until_complete(
+            self.libjuju.add_k8s("cloud", self.auth_data, "storage_class")
+        )
+        mock_add_cloud.assert_called_once()
+
+    def test_add_k8s_exception(self, mock_add_cloud):
+        mock_add_cloud.side_effect = Exception()
+        with self.assertRaises(Exception):
+            self.loop.run_until_complete(
+                self.libjuju.add_k8s("cloud", self.auth_data, "storage_class")
+            )
+        mock_add_cloud.assert_called_once()
+
+    def test_add_k8s_missing_name(self, mock_add_cloud):
+        with self.assertRaises(Exception):
+            self.loop.run_until_complete(
+                self.libjuju.add_k8s("", self.auth_data, "storage_class")
+            )
+        mock_add_cloud.assert_not_called()
+
+    def test_add_k8s_missing_storage_name(self, mock_add_cloud):
+        with self.assertRaises(Exception):
+            self.loop.run_until_complete(
+                self.libjuju.add_k8s("cloud", self.auth_data, "")
+            )
+        mock_add_cloud.assert_not_called()
+
+    def test_add_k8s_missing_auth_data_keys(self, mock_add_cloud):
+        with self.assertRaises(Exception):
+            self.loop.run_until_complete(self.libjuju.add_k8s("cloud", {}, ""))
+        mock_add_cloud.assert_not_called()
+
+
+@asynctest.mock.patch("n2vc.libjuju.Libjuju.get_controller")
+@asynctest.mock.patch("n2vc.libjuju.Libjuju.disconnect_controller")
+@asynctest.mock.patch("juju.controller.Controller.add_cloud")
+@asynctest.mock.patch("juju.controller.Controller.add_credential")
+class AddCloudTest(LibjujuTestCase):
+    def setUp(self):
+        super(AddCloudTest, self).setUp()
+        self.cloud = juju.client.client.Cloud()
+        self.credential = juju.client.client.CloudCredential()
+
+    def test_add_cloud_with_credential(
+        self,
+        mock_add_credential,
+        mock_add_cloud,
+        mock_disconnect_controller,
+        mock_get_controller,
+    ):
+        mock_get_controller.return_value = juju.controller.Controller()
+
+        cloud = self.loop.run_until_complete(
+            self.libjuju.add_cloud("cloud", self.cloud, credential=self.credential)
+        )
+        self.assertEqual(cloud, self.cloud)
+        mock_add_cloud.assert_called_once_with("cloud", self.cloud)
+        mock_add_credential.assert_called_once_with(
+            "cloud", credential=self.credential, cloud="cloud"
+        )
+        mock_disconnect_controller.assert_called_once()
+
+    def test_add_cloud_no_credential(
+        self,
+        mock_add_credential,
+        mock_add_cloud,
+        mock_disconnect_controller,
+        mock_get_controller,
+    ):
+        mock_get_controller.return_value = juju.controller.Controller()
+
+        cloud = self.loop.run_until_complete(
+            self.libjuju.add_cloud("cloud", self.cloud)
+        )
+        self.assertEqual(cloud, self.cloud)
+        mock_add_cloud.assert_called_once_with("cloud", self.cloud)
+        mock_add_credential.assert_not_called()
+        mock_disconnect_controller.assert_called_once()
+
+    def test_add_cloud_exception(
+        self,
+        mock_add_credential,
+        mock_add_cloud,
+        mock_disconnect_controller,
+        mock_get_controller,
+    ):
+        mock_get_controller.return_value = juju.controller.Controller()
+        mock_add_cloud.side_effect = Exception()
+        with self.assertRaises(Exception):
+            self.loop.run_until_complete(
+                self.libjuju.add_cloud("cloud", self.cloud, credential=self.credential)
+            )
+
+        mock_add_cloud.assert_called_once_with("cloud", self.cloud)
+        mock_add_credential.assert_not_called()
+        mock_disconnect_controller.assert_called_once()
+
+    def test_add_credential_exception(
+        self,
+        mock_add_credential,
+        mock_add_cloud,
+        mock_disconnect_controller,
+        mock_get_controller,
+    ):
+        mock_get_controller.return_value = juju.controller.Controller()
+        mock_add_credential.side_effect = Exception()
+        with self.assertRaises(Exception):
+            self.loop.run_until_complete(
+                self.libjuju.add_cloud("cloud", self.cloud, credential=self.credential)
+            )
+
+        mock_add_cloud.assert_called_once_with("cloud", self.cloud)
+        mock_add_credential.assert_called_once_with(
+            "cloud", credential=self.credential, cloud="cloud"
+        )
+        mock_disconnect_controller.assert_called_once()
+
+
+@asynctest.mock.patch("n2vc.libjuju.Libjuju.get_controller")
+@asynctest.mock.patch("n2vc.libjuju.Libjuju.disconnect_controller")
+@asynctest.mock.patch("juju.controller.Controller.remove_cloud")
+class RemoveCloudTest(LibjujuTestCase):
+    def setUp(self):
+        super(RemoveCloudTest, self).setUp()
+
+    def test_remove_cloud(
+        self, mock_remove_cloud, mock_disconnect_controller, mock_get_controller,
+    ):
+        mock_get_controller.return_value = juju.controller.Controller()
+
+        self.loop.run_until_complete(self.libjuju.remove_cloud("cloud"))
+        mock_remove_cloud.assert_called_once_with("cloud")
+        mock_disconnect_controller.assert_called_once()
+
+    def test_remove_cloud_exception(
+        self, mock_remove_cloud, mock_disconnect_controller, mock_get_controller,
+    ):
+        mock_get_controller.return_value = juju.controller.Controller()
+        mock_remove_cloud.side_effect = Exception()
+
+        with self.assertRaises(Exception):
+            self.loop.run_until_complete(self.libjuju.remove_cloud("cloud"))
+        mock_remove_cloud.assert_called_once_with("cloud")
+        mock_disconnect_controller.assert_called_once()