Update from master
[osm/NBI.git] / osm_nbi / tests / test_admin_topics.py
index 6c8083f..938ab93 100755 (executable)
@@ -18,12 +18,12 @@ __author__ = "Pedro de la Cruz Ramos, pedro.delacruzramos@altran.com"
 __date__ = "$2019-10-019"
 
 import unittest
 __date__ = "$2019-10-019"
 
 import unittest
+import random
 from unittest import TestCase
 from unittest.mock import Mock, patch, call, ANY
 from uuid import uuid4
 from http import HTTPStatus
 from time import time
 from unittest import TestCase
 from unittest.mock import Mock, patch, call, ANY
 from uuid import uuid4
 from http import HTTPStatus
 from time import time
-from random import randint
 from osm_common import dbbase, fsbase, msgbase
 from osm_nbi import authconn, validation
 from osm_nbi.admin_topics import (
 from osm_common import dbbase, fsbase, msgbase
 from osm_nbi import authconn, validation
 from osm_nbi.admin_topics import (
@@ -241,7 +241,10 @@ class Test_ProjectTopicAuth(TestCase):
         with self.subTest(i=1):
             self.auth.get_project_list.side_effect = [[proj], []]
             new_name = "new-project-name"
         with self.subTest(i=1):
             self.auth.get_project_list.side_effect = [[proj], []]
             new_name = "new-project-name"
-            quotas = {"vnfds": randint(0, 100), "nsds": randint(0, 100)}
+            quotas = {
+                "vnfds": random.SystemRandom().randint(0, 100),
+                "nsds": random.SystemRandom().randint(0, 100),
+            }
             self.topic.edit(
                 self.fake_session, pid, {"name": new_name, "quotas": quotas}
             )
             self.topic.edit(
                 self.fake_session, pid, {"name": new_name, "quotas": quotas}
             )
@@ -256,7 +259,7 @@ class Test_ProjectTopicAuth(TestCase):
             self.assertEqual(content["quotas"], quotas, "Wrong quotas")
         with self.subTest(i=2):
             new_name = "other-project-name"
             self.assertEqual(content["quotas"], quotas, "Wrong quotas")
         with self.subTest(i=2):
             new_name = "other-project-name"
-            quotas = {"baditems": randint(0, 100)}
+            quotas = {"baditems": random.SystemRandom().randint(0, 100)}
             self.auth.get_project_list.side_effect = [[proj], []]
             with self.assertRaises(EngineException, msg="Accepted wrong quotas") as e:
                 self.topic.edit(
             self.auth.get_project_list.side_effect = [[proj], []]
             with self.assertRaises(EngineException, msg="Accepted wrong quotas") as e:
                 self.topic.edit(
@@ -1620,11 +1623,18 @@ class TestVimAccountTopic(TestCase):
     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)
     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"])
+        mock_common_vim_wim_sdn.assert_called_with(self.fake_session, indata)
+        error_msg = "Invalid config 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):
         self.assertEqual(str(error.exception), error_msg)
 
     def test_check_conflict_on_new_vim_type_paas(self, mock_common_vim_wim_sdn):
+        valid_juju_paas_config = {
+            "paas_provider": "juju",
+            "ca_cert_content": "file_content",
+            "cloud": "microk8s",
+            "cloud_credentials": "cloud_credential_name",
+            "authorized_keys": "keys",
+        }
         indata = {
             "name": "juju_paas",
             "vim_type": "paas",
         indata = {
             "name": "juju_paas",
             "vim_type": "paas",
@@ -1633,26 +1643,11 @@ class TestVimAccountTopic(TestCase):
             "vim_user": "some_user",
             "vim_password": "some_password",
             "vim_tenant_name": "null",
             "vim_user": "some_user",
             "vim_password": "some_password",
             "vim_tenant_name": "null",
-            "config": {"paas_provider": "juju"},
+            "config": valid_juju_paas_config,
         }
         self.topic.check_conflict_on_new(self.fake_session, indata)
         mock_common_vim_wim_sdn.assert_called_once_with(self.fake_session, indata)
 
         }
         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
     ):
     def test_check_conflict_on_new_vim_type_paas_config_missing(
         self, mock_common_vim_wim_sdn
     ):
@@ -1667,22 +1662,65 @@ class TestVimAccountTopic(TestCase):
         }
         self.check_invalid_indata_raises_exception(indata, mock_common_vim_wim_sdn)
 
         }
         self.check_invalid_indata_raises_exception(indata, mock_common_vim_wim_sdn)
 
-    def test_check_conflict_on_new_vim_type_paas_provider_missing(
+    def test_check_conflict_on_new_vim_type_paas_invalid_config(
         self, mock_common_vim_wim_sdn
     ):
         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)
+        invalid_configs = [
+            {
+                "paas_provider": "some_paas_provider",
+                "ca_cert_content": "file_content",
+                "cloud": "microk8s",
+                "cloud_credentials": "cloud_credential_name",
+            },
+            {
+                "ca_cert_content": "file_content",
+                "cloud": "microk8s",
+                "cloud_credentials": "cloud_credential_name",
+            },
+            {
+                "paas_provider": "juju",
+                "cloud": "microk8s",
+                "cloud_credentials": "cloud_credential_name",
+            },
+            {
+                "paas_provider": "juju",
+                "ca_cert_content": "file_content",
+                "cloud_credentials": "cloud_credential_name",
+            },
+            {
+                "paas_provider": "juju",
+                "ca_cert_content": "file_content",
+                "cloud": "microk8s",
+            },
+            {
+                "some_param": None,
+            },
+            {},
+        ]
+        for config in invalid_configs:
+            with self.subTest():
+                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": config,
+                }
+                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):
 
     def test_kafka_message_is_not_sent_if_paas_vim(self, mock_common_vim_wim_sdn):
+        valid_juju_paas_config = {
+            "paas_provider": "juju",
+            "ca_cert_content": "file_content",
+            "cloud": "microk8s",
+            "cloud_credentials": "cloud_credential_name",
+            "authorized_keys": "keys",
+        }
         indata = {
             "name": "juju_paas",
             "vim_type": "paas",
         indata = {
             "name": "juju_paas",
             "vim_type": "paas",
@@ -1691,7 +1729,7 @@ class TestVimAccountTopic(TestCase):
             "vim_user": "some_user",
             "vim_password": "some_password",
             "vim_tenant_name": "null",
             "vim_user": "some_user",
             "vim_password": "some_password",
             "vim_tenant_name": "null",
-            "config": {"paas_provider": "juju"},
+            "config": valid_juju_paas_config,
         }
         rollback = []
         self.topic.temporal = Mock()
         }
         rollback = []
         self.topic.temporal = Mock()