From: gspri Date: Wed, 24 Feb 2021 05:03:17 +0000 (+0000) Subject: Feature-9904: Enhancing NG-UI to enable Juju operational view dashboard X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=refs%2Fchanges%2F30%2F10330%2F7;p=osm%2FN2VC.git Feature-9904: Enhancing NG-UI to enable Juju operational view dashboard New functions implemented in N2VC (Specific for VNF) - update_vca_status() in n2vc.juju.conn.py - get_executed_actions() in libjuju.py - get_application_configs() in libjuju.py Unit testing added for the above implemented new functions - UpdateVcaStatus in test_n2vc_juju_conn.py - GetExecutedActionsTest in test_libjuju.py - GetApplicationConfigsTest in test_libjuju.py Change-Id: I7d750d8c39bc7bc061568c28d29f53d1b87c4c23 Signed-off-by: gspri Signed-off-by: ksaikiranr --- diff --git a/n2vc/tests/unit/test_libjuju.py b/n2vc/tests/unit/test_libjuju.py index b9cc529..2f9ea1e 100644 --- a/n2vc/tests/unit/test_libjuju.py +++ b/n2vc/tests/unit/test_libjuju.py @@ -26,6 +26,7 @@ from n2vc.exceptions import ( JujuApplicationNotFound, JujuActionNotFound, JujuApplicationExists, + JujuError, ) @@ -175,6 +176,118 @@ class AddModelTest(LibjujuTestCase): mock_disconnect_model.assert_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.applications", new_callable=asynctest.PropertyMock +) +@asynctest.mock.patch("juju.model.Model.get_action_status") +@asynctest.mock.patch("juju.model.Model.get_action_output") +@asynctest.mock.patch("n2vc.libjuju.Libjuju.get_actions") +class GetExecutedActionsTest(LibjujuTestCase): + def setUp(self): + super(GetExecutedActionsTest, self).setUp() + + def test_exception( + self, + mock_get_actions, + mock_get_action_output, + mock_get_action_status, + mock_applications, + mock_disconnect_controller, + mock_disconnect_model, + mock_get_model, + mock_get_controller, + ): + mock_get_model.return_value = None + with self.assertRaises(JujuError): + self.loop.run_until_complete(self.libjuju.get_executed_actions("model")) + + mock_get_controller.assert_called_once() + mock_disconnect_controller.assert_called_once() + mock_get_model.assert_called_once() + mock_disconnect_model.assert_not_called() + + def test_success( + self, + mock_get_actions, + mock_get_action_output, + mock_get_action_status, + mock_applications, + mock_disconnect_controller, + mock_disconnect_model, + mock_get_model, + mock_get_controller, + ): + mock_get_model.return_value = juju.model.Model() + mock_applications.return_value = {"existing_app"} + mock_get_actions.return_value = {"action_name": "description"} + mock_get_action_status.return_value = {"id": "status"} + mock_get_action_output.return_value = {"output": "completed"} + + executed_actions = self.loop.run_until_complete( + self.libjuju.get_executed_actions("model") + ) + expected_result = [{'id': 'id', 'action': 'action_name', + 'status': 'status', 'output': 'completed'}] + self.assertListEqual(expected_result, executed_actions) + self.assertIsInstance(executed_actions, list) + + mock_get_controller.assert_called_once() + mock_get_model.assert_called_once() + mock_disconnect_controller.assert_called_once() + 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("n2vc.libjuju.Libjuju._get_application") +class GetApplicationConfigsTest(LibjujuTestCase): + def setUp(self): + super(GetApplicationConfigsTest, self).setUp() + + def test_exception( + self, + mock_get_application, + mock_disconnect_controller, + mock_disconnect_model, + mock_get_model, + mock_get_controller, + ): + mock_get_model.return_value = None + with self.assertRaises(JujuError): + self.loop.run_until_complete( + self.libjuju.get_application_configs("model", "app")) + + mock_get_controller.assert_called_once() + mock_disconnect_controller.assert_called_once() + mock_get_model.assert_called_once() + mock_disconnect_model.assert_not_called() + + def test_success( + self, + mock_get_application, + mock_disconnect_controller, + mock_disconnect_model, + mock_get_model, + mock_get_controller, + ): + mock_get_application.return_value = FakeApplication() + application_configs = self.loop.run_until_complete(self.libjuju + .get_application_configs("model", "app")) + + self.assertEqual(application_configs, ["app_config"]) + + mock_get_controller.assert_called_once() + mock_get_model.assert_called_once() + mock_disconnect_controller.assert_called_once() + mock_disconnect_model.assert_called_once() + + @asynctest.mock.patch("juju.controller.Controller.get_model") class GetModelTest(LibjujuTestCase): def setUp(self): diff --git a/n2vc/tests/unit/test_n2vc_juju_conn.py b/n2vc/tests/unit/test_n2vc_juju_conn.py index 80c4c87..7efd34c 100644 --- a/n2vc/tests/unit/test_n2vc_juju_conn.py +++ b/n2vc/tests/unit/test_n2vc_juju_conn.py @@ -60,6 +60,50 @@ class N2VCJujuConnTestCase(asynctest.TestCase): ) +@asynctest.mock.patch("n2vc.libjuju.Libjuju.get_controller") +@asynctest.mock.patch("n2vc.libjuju.Libjuju.get_model") +@asynctest.mock.patch("n2vc.libjuju.Libjuju.get_executed_actions") +@asynctest.mock.patch("n2vc.libjuju.Libjuju.get_actions") +@asynctest.mock.patch("n2vc.libjuju.Libjuju.get_application_configs") +@asynctest.mock.patch("n2vc.libjuju.Libjuju._get_application") +class UpdateVcaStatusTest(N2VCJujuConnTestCase): + def setUp(self): + super(UpdateVcaStatusTest, self).setUp() + + def test_success( + self, + mock_get_application, + mock_get_application_configs, + mock_get_actions, + mock_get_executed_actions, + mock_get_model, + mock_get_controller, + ): + self.loop.run_until_complete(self.n2vc.update_vca_status( + {"model": {"applications": {"app": {"actions": {}}}}})) + mock_get_executed_actions.assert_called_once() + mock_get_actions.assert_called_once() + mock_get_application_configs.assert_called_once() + + def test_exception( + self, + mock_get_application, + mock_get_application_configs, + mock_get_actions, + mock_get_executed_actions, + mock_get_model, + mock_get_controller, + ): + mock_get_model.return_value = None + mock_get_executed_actions.side_effect = Exception() + with self.assertRaises(Exception): + self.loop.run_until_complete(self.n2vc.update_vca_status( + {"model": {"applications": {"app": {"actions": {}}}}})) + mock_get_executed_actions.assert_not_called() + mock_get_actions.assert_not_called_once() + mock_get_application_configs.assert_not_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 diff --git a/n2vc/tests/unit/utils.py b/n2vc/tests/unit/utils.py index d960c70..92ab7a8 100644 --- a/n2vc/tests/unit/utils.py +++ b/n2vc/tests/unit/utils.py @@ -110,6 +110,9 @@ class FakeApplication(AsyncMock): async def get_actions(self): return ["existing_action"] + async def get_config(self): + return ["app_config"] + units = [FakeUnit(), FakeUnit()]