Feature 10908: NBI Validation of packages upload
[osm/NBI.git] / osm_nbi / tests / test_descriptor_topics.py
index 280c93f..6b9b2c3 100755 (executable)
@@ -17,6 +17,7 @@
 __author__ = "Pedro de la Cruz Ramos, pedro.delacruzramos@altran.com"
 __date__ = "2019-11-20"
 
+from contextlib import contextmanager
 import unittest
 from unittest import TestCase
 from unittest.mock import Mock, patch
@@ -92,6 +93,18 @@ class Test_VnfdTopic(TestCase):
         self.topic = VnfdTopic(self.db, self.fs, self.msg, self.auth)
         self.topic.check_quota = Mock(return_value=None)  # skip quota
 
+    @contextmanager
+    def assertNotRaises(self, exception_type):
+        try:
+            yield None
+        except exception_type:
+            raise self.failureException("{} raised".format(exception_type.__name__))
+
+    def create_desc_temp(self, template):
+        old_desc = deepcopy(template)
+        new_desc = deepcopy(template)
+        return old_desc, new_desc
+
     @patch("osm_nbi.descriptor_topics.shutil")
     @patch("osm_nbi.descriptor_topics.os.rename")
     def test_new_vnfd(self, mock_rename, mock_shutil):
@@ -294,7 +307,6 @@ class Test_VnfdTopic(TestCase):
                 self.topic.upload_content(
                     fake_session, did, test_vnfd, {}, {"Content-Type": []}
                 )
-            print(str(e.exception))
             self.assertEqual(
                 e.exception.http_code, HTTPStatus.BAD_REQUEST, "Wrong HTTP status code"
             )
@@ -802,6 +814,52 @@ class Test_VnfdTopic(TestCase):
             self.fs.file_delete.assert_not_called()
         return
 
+    @patch("osm_nbi.descriptor_topics.yaml")
+    def test_validate_descriptor_changes(self, mock_yaml):
+        descriptor_name = "test_descriptor"
+        self.fs.file_open.side_effect = lambda path, mode: open(
+            "/tmp/" + str(uuid4()), "a+b"
+        )
+        with self.subTest(i=1, t="VNFD has changes in day1-2 config primitive"):
+            old_vnfd, new_vnfd = self.create_desc_temp(db_vnfd_content)
+            new_vnfd["df"][0]["lcm-operations-configuration"]["operate-vnf-op-config"][
+                "day1-2"
+            ][0]["config-primitive"][0]["name"] = "new_action"
+            mock_yaml.load.side_effect = [old_vnfd, new_vnfd]
+            with self.assertNotRaises(EngineException):
+                self.topic._validate_descriptor_changes(
+                    descriptor_name, "/tmp/", "/tmp:1/"
+                )
+        with self.subTest(i=2, t="VNFD sw version changed"):
+            # old vnfd uses the default software version: 1.0
+            new_vnfd["software-version"] = "1.3"
+            new_vnfd["sw-image-desc"][0]["name"] = "new-image"
+            mock_yaml.load.side_effect = [old_vnfd, new_vnfd]
+            with self.assertNotRaises(EngineException):
+                self.topic._validate_descriptor_changes(
+                    descriptor_name, "/tmp/", "/tmp:1/"
+                )
+        with self.subTest(
+            i=3, t="VNFD sw version is not changed and mgmt-cp has changed"
+        ):
+            old_vnfd, new_vnfd = self.create_desc_temp(db_vnfd_content)
+            new_vnfd["mgmt-cp"] = "new-mgmt-cp"
+            mock_yaml.load.side_effect = [old_vnfd, new_vnfd]
+            with self.assertRaises(EngineException) as e:
+                self.topic._validate_descriptor_changes(
+                    descriptor_name, "/tmp/", "/tmp:1/"
+                )
+            self.assertEqual(
+                e.exception.http_code,
+                HTTPStatus.UNPROCESSABLE_ENTITY,
+                "Wrong HTTP status code",
+            )
+            self.assertIn(
+                norm("there are disallowed changes in the vnf descriptor"),
+                norm(str(e.exception)),
+                "Wrong exception text",
+            )
+
     def test_validate_mgmt_interface_connection_point_on_valid_descriptor(self):
         indata = deepcopy(db_vnfd_content)
         self.topic.validate_mgmt_interface_connection_point(indata)
@@ -1247,6 +1305,18 @@ class Test_NsdTopic(TestCase):
         self.topic = NsdTopic(self.db, self.fs, self.msg, self.auth)
         self.topic.check_quota = Mock(return_value=None)  # skip quota
 
+    @contextmanager
+    def assertNotRaises(self, exception_type):
+        try:
+            yield None
+        except exception_type:
+            raise self.failureException("{} raised".format(exception_type.__name__))
+
+    def create_desc_temp(self, template):
+        old_desc = deepcopy(template)
+        new_desc = deepcopy(template)
+        return old_desc, new_desc
+
     @patch("osm_nbi.descriptor_topics.shutil")
     @patch("osm_nbi.descriptor_topics.os.rename")
     def test_new_nsd(self, mock_rename, mock_shutil):
@@ -1732,6 +1802,46 @@ class Test_NsdTopic(TestCase):
             self.fs.file_delete.assert_not_called()
         return
 
+    @patch("osm_nbi.descriptor_topics.yaml")
+    def test_validate_descriptor_changes(self, mock_yaml):
+        descriptor_name = "test_ns_descriptor"
+        self.fs.file_open.side_effect = lambda path, mode: open(
+            "/tmp/" + str(uuid4()), "a+b"
+        )
+        with self.subTest(
+            i=1, t="NSD has changes in ns-configuration:config-primitive"
+        ):
+            old_nsd, new_nsd = self.create_desc_temp(db_nsd_content)
+            old_nsd.update(
+                {"ns-configuration": {"config-primitive": [{"name": "add-user"}]}}
+            )
+            new_nsd.update(
+                {"ns-configuration": {"config-primitive": [{"name": "del-user"}]}}
+            )
+            mock_yaml.load.side_effect = [old_nsd, new_nsd]
+            with self.assertNotRaises(EngineException):
+                self.topic._validate_descriptor_changes(
+                    descriptor_name, "/tmp", "/tmp:1"
+                )
+        with self.subTest(i=2, t="NSD name has changed"):
+            old_nsd, new_nsd = self.create_desc_temp(db_nsd_content)
+            new_nsd["name"] = "nscharm-ns2"
+            mock_yaml.load.side_effect = [old_nsd, new_nsd]
+            with self.assertRaises(EngineException) as e:
+                self.topic._validate_descriptor_changes(
+                    descriptor_name, "/tmp", "/tmp:1"
+                )
+            self.assertEqual(
+                e.exception.http_code,
+                HTTPStatus.UNPROCESSABLE_ENTITY,
+                "Wrong HTTP status code",
+            )
+            self.assertIn(
+                norm("there are disallowed changes in the ns descriptor"),
+                norm(str(e.exception)),
+                "Wrong exception text",
+            )
+
     def test_validate_vld_mgmt_network_with_virtual_link_protocol_data_on_valid_descriptor(
         self,
     ):