Add get_metrics command to n2vc_juju_conn and libjuju 26/9726/1
authorDavid Garcia <david.garcia@canonical.com>
Mon, 21 Sep 2020 17:51:23 +0000 (19:51 +0200)
committerDavid Garcia <david.garcia@canonical.com>
Mon, 21 Sep 2020 17:51:23 +0000 (19:51 +0200)
Change-Id: I6cde1528579c8ca7aa72d935c5e570004468bb1a
Signed-off-by: David Garcia <david.garcia@canonical.com>
n2vc/libjuju.py
n2vc/n2vc_juju_conn.py
n2vc/tests/unit/test_libjuju.py
n2vc/tests/unit/test_n2vc_juju_conn.py

index 12730fd..d761adc 100644 (file)
@@ -711,6 +711,26 @@ class Libjuju:
             await self.disconnect_model(model)
             await self.disconnect_controller(controller)
 
+    async def get_metrics(self, model_name: str, application_name: str) -> dict:
+        """Get the metrics collected by the VCA.
+
+        :param model_name The name or unique id of the network service
+        :param application_name The name of the application
+        """
+        if not model_name or not application_name:
+            raise Exception("model_name and application_name must be non-empty strings")
+        metrics = {}
+        controller = await self.get_controller()
+        model = await self.get_model(controller, model_name)
+        try:
+            application = self._get_application(model, application_name)
+            if application is not None:
+                metrics = await application.get_metrics()
+        finally:
+            self.disconnect_model(model)
+            self.disconnect_controller(controller)
+        return metrics
+
     async def add_relation(
         self, model_name: str, endpoint_1: str, endpoint_2: str,
     ):
index fff78c9..0522028 100644 (file)
@@ -655,6 +655,9 @@ class N2VCJujuConnector(N2VCConnector):
         # return public key if exists
         return output["pubkey"] if "pubkey" in output else output
 
+    async def get_metrics(self, model_name: str, application_name: str) -> dict:
+        return await self.libjuju.get_metrics(model_name, application_name)
+
     async def add_relation(
         self, ee_id_1: str, ee_id_2: str, endpoint_1: str, endpoint_2: str
     ):
index ae39078..57a6ccc 100644 (file)
@@ -652,6 +652,70 @@ class GetActionTest(LibjujuTestCase):
         mock_disconnect_model.assert_called_once()
 
 
+@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.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")
@@ -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()
 
 
index 80c4c87..4044637 100644 (file)
@@ -60,6 +60,24 @@ class N2VCJujuConnTestCase(asynctest.TestCase):
         )
 
 
+@asynctest.mock.patch("n2vc.libjuju.Libjuju.get_metrics")
+class GetMetricssTest(N2VCJujuConnTestCase):
+    def setUp(self):
+        super(GetMetricssTest, self).setUp()
+
+    def test_success(self, mock_get_metrics):
+        _ = self.loop.run_until_complete(self.n2vc.get_metrics("model", "application"))
+        mock_get_metrics.assert_called_once()
+
+    def test_except(self, mock_get_metrics):
+        mock_get_metrics.side_effect = Exception()
+        with self.assertRaises(Exception):
+            _ = self.loop.run_until_complete(
+                self.n2vc.get_metrics("model", "application")
+            )
+        mock_get_metrics.assert_called_once()
+
+
 @asynctest.mock.patch("osm_common.fslocal.FsLocal.file_exists")
 @asynctest.mock.patch(
     "osm_common.fslocal.FsLocal.path", new_callable=asynctest.PropertyMock, create=True