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,
):
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")
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()
)
+@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