X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FNBI.git;a=blobdiff_plain;f=osm_nbi%2Ftests%2Ftest_descriptor_topics.py;fp=osm_nbi%2Ftests%2Ftest_descriptor_topics.py;h=a56916e1fb14c5989132129f469cba6a7038eb11;hp=27c0bd3d9a1ffb58deb824d404d6a4a1b282c8bb;hb=2b5e123a2174e8c14a3e3aab67ac7d38b33571f0;hpb=6f18ee4526bd33b54ed8da49dcb62abcf935c905 diff --git a/osm_nbi/tests/test_descriptor_topics.py b/osm_nbi/tests/test_descriptor_topics.py index 27c0bd3..a56916e 100755 --- a/osm_nbi/tests/test_descriptor_topics.py +++ b/osm_nbi/tests/test_descriptor_topics.py @@ -34,8 +34,8 @@ from osm_common.dbbase import DbException import yaml test_name = "test-user" -db_vnfd_content = yaml.load(db_vnfds_text, Loader=yaml.Loader)[0] -db_nsd_content = yaml.load(db_nsds_text, Loader=yaml.Loader)[0] +db_vnfd_content = yaml.safe_load(db_vnfds_text)[0] +db_nsd_content = yaml.safe_load(db_nsds_text)[0] test_pid = db_vnfd_content["_admin"]["projects_read"][0] fake_session = { "username": test_name, @@ -401,6 +401,8 @@ class Test_VnfdTopic(TestCase): def test_new_vnfd_check_input_validation_vdu_int_cpd( self, mock_rename, mock_shutil ): + """Testing input validation during new vnfd creation + for vdu internal connection point""" did, test_vnfd = self.prepare_vnfd_creation() test_vnfd = self.prepare_test_vnfd(test_vnfd) ext_cpd = test_vnfd["ext-cpd"][1] @@ -432,6 +434,8 @@ class Test_VnfdTopic(TestCase): def test_new_vnfd_check_input_validation_duplicated_vld( self, mock_rename, mock_shutil ): + """Testing input validation during new vnfd creation + for dublicated virtual link description""" did, test_vnfd = self.prepare_vnfd_creation() test_vnfd = self.prepare_test_vnfd(test_vnfd) test_vnfd["int-virtual-link-desc"].insert(0, {"id": "internal"}) @@ -462,6 +466,8 @@ class Test_VnfdTopic(TestCase): def test_new_vnfd_check_input_validation_vdu_int_virtual_link_desc( self, mock_rename, mock_shutil ): + """Testing input validation during new vnfd creation + for vdu internal virtual link description""" did, test_vnfd = self.prepare_vnfd_creation() test_vnfd = self.prepare_test_vnfd(test_vnfd) vdu = test_vnfd["vdu"][0] @@ -495,6 +501,8 @@ class Test_VnfdTopic(TestCase): def test_new_vnfd_check_input_validation_virtual_link_profile( self, mock_rename, mock_shutil ): + """Testing input validation during new vnfd creation + for virtual link profile""" did, test_vnfd = self.prepare_vnfd_creation() test_vnfd = self.prepare_test_vnfd(test_vnfd) fake_ivld_profile = {"id": "fake-profile-ref", "flavour": "fake-flavour"} @@ -526,6 +534,8 @@ class Test_VnfdTopic(TestCase): def test_new_vnfd_check_input_validation_scaling_criteria_monitoring_param_ref( self, mock_rename, mock_shutil ): + """Testing input validation during new vnfd creation + for scaling criteria without monitoring parameter""" did, test_vnfd = self.prepare_vnfd_creation() test_vnfd = self.prepare_test_vnfd(test_vnfd) vdu = test_vnfd["vdu"][1] @@ -567,6 +577,8 @@ class Test_VnfdTopic(TestCase): def test_new_vnfd_check_input_validation_scaling_aspect_vnf_configuration( self, mock_rename, mock_shutil ): + """Testing input validation during new vnfd creation + for scaling criteria without day12 configuration""" did, test_vnfd = self.prepare_vnfd_creation() test_vnfd = self.prepare_test_vnfd(test_vnfd) test_vnfd["df"][0]["lcm-operations-configuration"]["operate-vnf-op-config"][ @@ -601,6 +613,8 @@ class Test_VnfdTopic(TestCase): def test_new_vnfd_check_input_validation_scaling_config_action( self, mock_rename, mock_shutil ): + """Testing input validation during new vnfd creation + for scaling criteria wrong config primitive""" did, test_vnfd = self.prepare_vnfd_creation() test_vnfd = self.prepare_test_vnfd(test_vnfd) df = test_vnfd["df"][0] @@ -640,6 +654,8 @@ class Test_VnfdTopic(TestCase): def test_new_vnfd_check_input_validation_everything_right( self, mock_rename, mock_shutil ): + """Testing input validation during new vnfd creation + everything correct""" did, test_vnfd = self.prepare_vnfd_creation() test_vnfd = self.prepare_test_vnfd(test_vnfd) test_vnfd["id"] = "fake-vnfd-id" @@ -763,7 +779,6 @@ class Test_VnfdTopic(TestCase): "Wrong DB NSD vnfd-id", ) - self.db.del_list.call_args[0] self.assertEqual( self.db.del_list.call_args[0][0], self.topic.topic + "_revisions", @@ -863,35 +878,73 @@ class Test_VnfdTopic(TestCase): old_vnfd, new_vnfd = self.create_desc_temp(db_vnfd_content) return descriptor_name, old_vnfd, new_vnfd - @patch("osm_nbi.descriptor_topics.yaml") - def test_validate_vnfd_changes_day12_config_primitive_changed(self, mock_yaml): + @patch("osm_nbi.descriptor_topics.detect_descriptor_usage") + @patch("osm_nbi.descriptor_topics.yaml.safe_load") + def test_validate_vnfd_changes_day12_config_primitive_changed( + self, mock_safe_load, mock_detect_usage + ): + """Validating VNFD for VNFD updates, day1-2 config primitive has changed""" descriptor_name, old_vnfd, new_vnfd = self.prepare_vnfd_validation() + did = old_vnfd["_id"] 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/") + mock_safe_load.side_effect = [old_vnfd, new_vnfd] + mock_detect_usage.return_value = True + self.db.get_one.return_value = old_vnfd - @patch("osm_nbi.descriptor_topics.yaml") - def test_validate_vnfd_changes_sw_version_changed(self, mock_yaml): + with self.assertNotRaises(EngineException): + self.topic._validate_descriptor_changes( + did, descriptor_name, "/tmp/", "/tmp:1/" + ) + self.db.get_one.assert_called_once() + mock_detect_usage.assert_called_once() + self.assertEqual(mock_safe_load.call_count, 2) + + @patch("osm_nbi.descriptor_topics.detect_descriptor_usage") + @patch("osm_nbi.descriptor_topics.yaml.safe_load") + def test_validate_vnfd_changes_sw_version_changed( + self, mock_safe_load, mock_detect_usage + ): + """Validating VNFD for updates, software version has changed""" # old vnfd uses the default software version: 1.0 descriptor_name, old_vnfd, new_vnfd = self.prepare_vnfd_validation() + did = old_vnfd["_id"] new_vnfd["software-version"] = "1.3" new_vnfd["sw-image-desc"][0]["name"] = "new-image" - mock_yaml.load.side_effect = [old_vnfd, new_vnfd] + mock_safe_load.side_effect = [old_vnfd, new_vnfd] + mock_detect_usage.return_value = True + self.db.get_one.return_value = old_vnfd + with self.assertNotRaises(EngineException): - self.topic._validate_descriptor_changes(descriptor_name, "/tmp/", "/tmp:1/") + self.topic._validate_descriptor_changes( + did, descriptor_name, "/tmp/", "/tmp:1/" + ) + self.db.get_one.assert_called_once() + mock_detect_usage.assert_called_once() + self.assertEqual(mock_safe_load.call_count, 2) - @patch("osm_nbi.descriptor_topics.yaml") + @patch("osm_nbi.descriptor_topics.detect_descriptor_usage") + @patch("osm_nbi.descriptor_topics.yaml.safe_load") def test_validate_vnfd_changes_sw_version_not_changed_mgm_cp_changed( - self, mock_yaml + self, mock_safe_load, mock_detect_usage ): + """Validating VNFD for updates, software version has not + changed, mgmt-cp has changed.""" descriptor_name, old_vnfd, new_vnfd = self.prepare_vnfd_validation() 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/") + mock_safe_load.side_effect = [old_vnfd, new_vnfd] + did = old_vnfd["_id"] + mock_detect_usage.return_value = True + self.db.get_one.return_value = old_vnfd + + with self.assertRaises( + EngineException, msg="there are disallowed changes in the vnf descriptor" + ) as e: + self.topic._validate_descriptor_changes( + did, descriptor_name, "/tmp/", "/tmp:1/" + ) + self.assertEqual( e.exception.http_code, HTTPStatus.UNPROCESSABLE_ENTITY, @@ -902,6 +955,32 @@ class Test_VnfdTopic(TestCase): norm(str(e.exception)), "Wrong exception text", ) + self.db.get_one.assert_called_once() + mock_detect_usage.assert_called_once() + self.assertEqual(mock_safe_load.call_count, 2) + + @patch("osm_nbi.descriptor_topics.detect_descriptor_usage") + @patch("osm_nbi.descriptor_topics.yaml.safe_load") + def test_validate_vnfd_changes_sw_version_not_changed_mgm_cp_changed_vnfd_not_in_use( + self, mock_safe_load, mock_detect_usage + ): + """Validating VNFD for updates, software version has not + changed, mgmt-cp has changed, vnfd is not in use.""" + descriptor_name, old_vnfd, new_vnfd = self.prepare_vnfd_validation() + new_vnfd["mgmt-cp"] = "new-mgmt-cp" + mock_safe_load.side_effect = [old_vnfd, new_vnfd] + did = old_vnfd["_id"] + mock_detect_usage.return_value = None + self.db.get_one.return_value = old_vnfd + + with self.assertNotRaises(EngineException): + self.topic._validate_descriptor_changes( + did, descriptor_name, "/tmp/", "/tmp:1/" + ) + + self.db.get_one.assert_called_once() + mock_detect_usage.assert_called_once() + mock_safe_load.assert_not_called() def test_validate_mgmt_interface_connection_point_on_valid_descriptor(self): indata = deepcopy(db_vnfd_content) @@ -1316,7 +1395,8 @@ class Test_VnfdTopic(TestCase): old_vnfd = {"_id": did, "_admin": deepcopy(db_vnfd_content["_admin"])} old_vnfd["_admin"]["revision"] = old_revision - self.db.get_one.side_effect = [old_vnfd, None] + + self.db.get_one.side_effect = [old_vnfd, old_vnfd, None] self.topic.upload_content(fake_session, did, new_vnfd, {}, {"Content-Type": []}) db_args = self.db.replace.call_args[0] @@ -1847,30 +1927,51 @@ class Test_NsdTopic(TestCase): old_nsd, new_nsd = self.create_desc_temp(db_nsd_content) return descriptor_name, old_nsd, new_nsd - @patch("osm_nbi.descriptor_topics.yaml") - def test_validate_descriptor_ns_configuration_changed(self, mock_yaml): - # NSD has changes in ns-configuration:config-primitive + @patch("osm_nbi.descriptor_topics.detect_descriptor_usage") + @patch("osm_nbi.descriptor_topics.yaml.safe_load") + def test_validate_descriptor_ns_configuration_changed( + self, mock_safe_load, mock_detect_usage + ): + """Validating NSD and NSD has changes in ns-configuration:config-primitive""" descriptor_name, old_nsd, new_nsd = self.prepare_nsd_validation() + mock_safe_load.side_effect = [old_nsd, new_nsd] + mock_detect_usage.return_value = True + self.db.get_one.return_value = old_nsd 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") - @patch("osm_nbi.descriptor_topics.yaml") - def test_validate_descriptor_nsd_name_changed(self, mock_yaml): + with self.assertNotRaises(EngineException): + self.topic._validate_descriptor_changes( + old_nsd["_id"], descriptor_name, "/tmp", "/tmp:1" + ) + self.db.get_one.assert_called_once() + mock_detect_usage.assert_called_once() + self.assertEqual(mock_safe_load.call_count, 2) + + @patch("osm_nbi.descriptor_topics.detect_descriptor_usage") + @patch("osm_nbi.descriptor_topics.yaml.safe_load") + def test_validate_descriptor_nsd_name_changed( + self, mock_safe_load, mock_detect_usage + ): + """Validating NSD, NSD name has changed.""" descriptor_name, old_nsd, new_nsd = self.prepare_nsd_validation() + did = old_nsd["_id"] 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") + mock_safe_load.side_effect = [old_nsd, new_nsd] + mock_detect_usage.return_value = True + self.db.get_one.return_value = old_nsd + with self.assertRaises( + EngineException, msg="there are disallowed changes in the ns descriptor" + ) as e: + self.topic._validate_descriptor_changes( + did, descriptor_name, "/tmp", "/tmp:1" + ) self.assertEqual( e.exception.http_code, HTTPStatus.UNPROCESSABLE_ENTITY, @@ -1882,6 +1983,32 @@ class Test_NsdTopic(TestCase): "Wrong exception text", ) + self.db.get_one.assert_called_once() + mock_detect_usage.assert_called_once() + self.assertEqual(mock_safe_load.call_count, 2) + + @patch("osm_nbi.descriptor_topics.detect_descriptor_usage") + @patch("osm_nbi.descriptor_topics.yaml.safe_load") + def test_validate_descriptor_nsd_name_changed_nsd_not_in_use( + self, mock_safe_load, mock_detect_usage + ): + """Validating NSD, NSD name has changed, NSD is not in use.""" + descriptor_name, old_nsd, new_nsd = self.prepare_nsd_validation() + did = old_nsd["_id"] + new_nsd["name"] = "nscharm-ns2" + mock_safe_load.side_effect = [old_nsd, new_nsd] + mock_detect_usage.return_value = None + self.db.get_one.return_value = old_nsd + + with self.assertNotRaises(Exception): + self.topic._validate_descriptor_changes( + did, descriptor_name, "/tmp", "/tmp:1" + ) + + self.db.get_one.assert_called_once() + mock_detect_usage.assert_called_once() + mock_safe_load.assert_not_called() + def test_validate_vld_mgmt_network_with_virtual_link_protocol_data_on_valid_descriptor( self, ):