Feature-9904: Enhancing NG-UI to enable Juju operational view dashboard 94/10494/4
authorksaikiranr <saikiran.k@tataelxsi.co.in>
Wed, 17 Mar 2021 07:23:20 +0000 (12:53 +0530)
committerksaikiranr <saikiran.k@tataelxsi.co.in>
Wed, 14 Apr 2021 04:37:47 +0000 (10:07 +0530)
Unit testing added for 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 ad0933c..36110f6 100644 (file)
@@ -30,6 +30,7 @@ from n2vc.exceptions import (
     JujuApplicationExists,
     JujuInvalidK8sConfiguration,
     JujuLeaderUnitNotFound,
+    JujuError,
 )
 from n2vc.k8s_juju_conn import generate_rbac_id
 
@@ -258,6 +259,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 64b93cc..1f723fe 100644 (file)
@@ -81,6 +81,50 @@ class GetMetricssTest(N2VCJujuConnTestCase):
         mock_get_metrics.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.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("n2vc.libjuju.Libjuju.model_exists")
 @asynctest.mock.patch("osm_common.fslocal.FsLocal.file_exists")
 @asynctest.mock.patch(
index ac86cdd..a727072 100644 (file)
@@ -162,6 +162,9 @@ class FakeApplication(AsyncMock):
     async def get_actions(self):
         return ["existing_action"]
 
+    async def get_config(self):
+        return ["app_config"]
+
     units = [FakeUnit(), FakeUnit()]