From: David Garcia Date: Mon, 21 Sep 2020 17:51:23 +0000 (+0200) Subject: Add get_metrics command to n2vc_juju_conn and libjuju X-Git-Tag: release-v9.0-start~17 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=refs%2Fchanges%2F26%2F9726%2F1;p=osm%2FN2VC.git Add get_metrics command to n2vc_juju_conn and libjuju Change-Id: I6cde1528579c8ca7aa72d935c5e570004468bb1a Signed-off-by: David Garcia --- diff --git a/n2vc/libjuju.py b/n2vc/libjuju.py index 12730fd..d761adc 100644 --- a/n2vc/libjuju.py +++ b/n2vc/libjuju.py @@ -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, ): diff --git a/n2vc/n2vc_juju_conn.py b/n2vc/n2vc_juju_conn.py index fff78c9..0522028 100644 --- a/n2vc/n2vc_juju_conn.py +++ b/n2vc/n2vc_juju_conn.py @@ -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 ): diff --git a/n2vc/tests/unit/test_libjuju.py b/n2vc/tests/unit/test_libjuju.py index ae39078..57a6ccc 100644 --- a/n2vc/tests/unit/test_libjuju.py +++ b/n2vc/tests/unit/test_libjuju.py @@ -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() diff --git a/n2vc/tests/unit/test_n2vc_juju_conn.py b/n2vc/tests/unit/test_n2vc_juju_conn.py index 80c4c87..4044637 100644 --- a/n2vc/tests/unit/test_n2vc_juju_conn.py +++ b/n2vc/tests/unit/test_n2vc_juju_conn.py @@ -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