Feature-9904: Enhancing NG-UI to enable Juju operational view dashboard 30/10330/7
authorgspri <priyadharshini.g@tataelxsi.co.in>
Wed, 24 Feb 2021 05:03:17 +0000 (05:03 +0000)
committerrbara <barath.r@tataelxsi.co.in>
Wed, 24 Feb 2021 05:37:51 +0000 (06:37 +0100)
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 <priyadharshini.g@tataelxsi.co.in>
Signed-off-by: ksaikiranr <saikiran.k@tataelxsi.co.in>
n2vc/tests/unit/test_libjuju.py
n2vc/tests/unit/test_n2vc_juju_conn.py
n2vc/tests/unit/utils.py

index b9cc529..2f9ea1e 100644 (file)
@@ -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):
index 80c4c87..7efd34c 100644 (file)
@@ -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
index d960c70..92ab7a8 100644 (file)
@@ -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()]