Add Juju PaaS as a VIM
[osm/NBI.git] / osm_nbi / tests / test_admin_topics.py
index 8124ce4..ce6a21b 100755 (executable)
@@ -19,7 +19,7 @@ __date__ = "$2019-10-019"
 
 import unittest
 from unittest import TestCase
-from unittest.mock import Mock, patch, call
+from unittest.mock import Mock, patch, call, ANY
 from uuid import uuid4
 from http import HTTPStatus
 from time import time
@@ -32,6 +32,7 @@ from osm_nbi.admin_topics import (
     UserTopicAuth,
     CommonVimWimSdn,
     VcaTopic,
+    VimAccountTopic,
 )
 from osm_nbi.engine import EngineException
 from osm_nbi.authconn import AuthconnNotFoundException
@@ -1013,7 +1014,9 @@ class Test_UserTopicAuth(TestCase):
                 },
             )
             content = self.auth.update_user.call_args[0][0]
-            self.assertEqual(content["old_password"], old_password, "Wrong old password")
+            self.assertEqual(
+                content["old_password"], old_password, "Wrong old password"
+            )
             self.assertEqual(content["password"], new_pasw, "Wrong user password")
 
     def test_delete_user(self):
@@ -1594,5 +1597,125 @@ class Test_CommonVimWimSdn(TestCase):
             )
 
 
+@patch("osm_nbi.admin_topics.CommonVimWimSdn.check_conflict_on_new")
+class TestVimAccountTopic(TestCase):
+    def setUp(self):
+        self.db = Mock(dbbase.DbBase())
+        self.fs = Mock(fsbase.FsBase())
+        self.msg = Mock(msgbase.MsgBase())
+        self.auth = Mock(authconn.Authconn(None, None, None))
+        self.topic = VimAccountTopic(self.db, self.fs, self.msg, self.auth)
+        self.topic.check_quota = Mock(return_value=None)  # skip quota
+
+        self.fake_session = {
+            "username": test_name,
+            "project_id": (test_pid,),
+            "method": None,
+            "admin": True,
+            "force": False,
+            "public": False,
+            "allow_show_user_project_role": True,
+        }
+
+    def check_invalid_indata_raises_exception(self, indata, mock_common_vim_wim_sdn):
+        with self.assertRaises(EngineException) as error:
+            self.topic.check_conflict_on_new(self.fake_session, indata)
+        mock_common_vim_wim_sdn.assert_called_once_with(self.fake_session, indata)
+        error_msg = "Invalid paas_provider for VIM account '{}'.".format(indata["name"])
+        self.assertEqual(str(error.exception), error_msg)
+
+    def test_check_conflict_on_new_vim_type_paas(self, mock_common_vim_wim_sdn):
+        indata = {
+            "name": "juju_paas",
+            "vim_type": "paas",
+            "description": None,
+            "vim_url": "http://0.0.0.0:0",
+            "vim_user": "some_user",
+            "vim_password": "some_password",
+            "vim_tenant_name": "null",
+            "config": {"paas_provider": "juju"},
+        }
+        self.topic.check_conflict_on_new(self.fake_session, indata)
+        mock_common_vim_wim_sdn.assert_called_once_with(self.fake_session, indata)
+
+    def test_check_conflict_on_new_vim_type_paas_incorrect_provider(
+        self, mock_common_vim_wim_sdn
+    ):
+        indata = {
+            "name": "juju_paas",
+            "vim_type": "paas",
+            "description": None,
+            "vim_url": "http://0.0.0.0:0",
+            "vim_user": "some_user",
+            "vim_password": "some_password",
+            "vim_tenant_name": "null",
+            "config": {"paas_provider": "some_provider"},
+        }
+        self.check_invalid_indata_raises_exception(indata, mock_common_vim_wim_sdn)
+
+    def test_check_conflict_on_new_vim_type_paas_config_missing(
+        self, mock_common_vim_wim_sdn
+    ):
+        indata = {
+            "name": "juju_paas",
+            "vim_type": "paas",
+            "description": None,
+            "vim_url": "http://0.0.0.0:0",
+            "vim_user": "some_user",
+            "vim_password": "some_password",
+            "vim_tenant_name": "null",
+        }
+        self.check_invalid_indata_raises_exception(indata, mock_common_vim_wim_sdn)
+
+    def test_check_conflict_on_new_vim_type_paas_provider_missing(
+        self, mock_common_vim_wim_sdn
+    ):
+        indata = {
+            "name": "juju_paas",
+            "vim_type": "paas",
+            "description": None,
+            "vim_url": "http://0.0.0.0:0",
+            "vim_user": "some_user",
+            "vim_password": "some_password",
+            "vim_tenant_name": "null",
+            "config": {"some_param": None},
+        }
+        self.check_invalid_indata_raises_exception(indata, mock_common_vim_wim_sdn)
+
+    def test_kafka_message_is_not_sent_if_paas_vim(self, mock_common_vim_wim_sdn):
+        indata = {
+            "name": "juju_paas",
+            "vim_type": "paas",
+            "description": None,
+            "vim_url": "http://0.0.0.0:0",
+            "vim_user": "some_user",
+            "vim_password": "some_password",
+            "vim_tenant_name": "null",
+            "config": {"paas_provider": "juju"},
+        }
+        rollback = []
+
+        self.topic.new(rollback, self.fake_session, indata)
+        self.assertEqual(len(rollback), 1, "Wrong rollback length")
+        self.msg.write.assert_not_called()
+
+    def test_kafka_message_is_sent_if_not_paas_vim(self, mock_common_vim_wim_sdn):
+        indata = {
+            "name": "juju_paas",
+            "vim_type": "openstack",
+            "description": None,
+            "vim_url": "http://0.0.0.0:0",
+            "vim_user": "some_user",
+            "vim_password": "some_password",
+            "vim_tenant_name": "null",
+        }
+        rollback = []
+
+        self.topic.new(rollback, self.fake_session, indata)
+        self.assertEqual(len(rollback), 1, "Wrong rollback length")
+        mock_common_vim_wim_sdn.assert_called_once_with(self.fake_session, indata)
+        self.msg.write.assert_called_once_with("vim_account", "created", ANY)
+
+
 if __name__ == "__main__":
     unittest.main()