Add get_metrics command to n2vc_juju_conn and libjuju
[osm/N2VC.git] / n2vc / tests / unit / test_libjuju.py
index ae39078..57a6ccc 100644 (file)
@@ -656,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):
@@ -1088,9 +1152,7 @@ class AddK8sTest(LibjujuTestCase):
 
     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", {}, "")
-            )
+            self.loop.run_until_complete(self.libjuju.add_k8s("cloud", {}, ""))
         mock_add_cloud.assert_not_called()