X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FNBI.git;a=blobdiff_plain;f=osm_nbi%2Ftests%2Ftest_descriptor_topics.py;h=5fbb33793e80e429cc70f3110577f7f9467b063e;hp=6e39030ef97b5eaeafdd47c5a240b3efe70f2aee;hb=960531ab7417bf6705399dae790899463a659da9;hpb=ad682a52ef94fa2662e2a0f6e3f81fb7c8f5e0fe diff --git a/osm_nbi/tests/test_descriptor_topics.py b/osm_nbi/tests/test_descriptor_topics.py index 6e39030..5fbb337 100755 --- a/osm_nbi/tests/test_descriptor_topics.py +++ b/osm_nbi/tests/test_descriptor_topics.py @@ -32,14 +32,12 @@ from osm_nbi.engine import EngineException from osm_common.dbbase import DbException import yaml - -test_pid = str(uuid4()) test_name = "test-user" -fake_session = {"username": test_name, "project_id": (test_pid,), "method": None, - "admin": True, "force": False, "public": False, "allow_show_user_project_role": True} - db_vnfd_content = yaml.load(db_vnfds_text, Loader=yaml.Loader)[0] db_nsd_content = yaml.load(db_nsds_text, Loader=yaml.Loader)[0] +test_pid = db_vnfd_content["_admin"]["projects_read"][0] +fake_session = {"username": test_name, "project_id": (test_pid,), "method": None, + "admin": True, "force": False, "public": False, "allow_show_user_project_role": True} def norm(str): @@ -62,10 +60,10 @@ def compare_desc(tc, d1, d2, k): if isinstance(d1, dict) and isinstance(d2, dict): for key in d1.keys(): if key in d2: - compare_desc(tc, d1[key], d2[key], k+"[{}]".format(key)) + compare_desc(tc, d1[key], d2[key], k + "[{}]".format(key)) elif isinstance(d1, list) and isinstance(d2, list) and len(d1) == len(d2): for i in range(len(d1)): - compare_desc(tc, d1[i], d2[i], k+"[{}]".format(i)) + compare_desc(tc, d1[i], d2[i], k + "[{}]".format(i)) else: tc.assertEqual(d1, d2, "Wrong descriptor content: {}".format(k)) @@ -84,8 +82,9 @@ class Test_VnfdTopic(TestCase): self.db = Mock(dbbase.DbBase()) self.fs = Mock(fsbase.FsBase()) self.msg = Mock(msgbase.MsgBase()) - self.auth = Mock(authconn.Authconn(None, None)) + self.auth = Mock(authconn.Authconn(None, None, None)) self.topic = VnfdTopic(self.db, self.fs, self.msg, self.auth) + self.topic.check_quota = Mock(return_value=None) # skip quota def test_new_vnfd(self): did = db_vnfd_content["_id"] @@ -147,7 +146,7 @@ class Test_VnfdTopic(TestCase): finally: test_vnfd["vdu"][0]["cloud-init-file"] = tmp1 test_vnfd["vnf-configuration"]["juju"] = tmp2 - self.db.get_one.side_effect = lambda table, filter, fail_on_empty=None, fail_on_more=None:\ + self.db.get_one.side_effect = lambda table, filter, fail_on_empty=None, fail_on_more=None: \ {"_id": did, "_admin": db_vnfd_content["_admin"]} with self.subTest(i=2, t='Check Pyangbind Validation: required properties'): tmp = test_vnfd["id"] @@ -366,7 +365,7 @@ class Test_VnfdTopic(TestCase): sg["vdu"][0]["vdu-id-ref"] = test_vnfd["vdu"][0]["id"] sg["scaling-config-action"] = [{"trigger": "pre-scale-in"}] try: - with self.assertRaises(EngineException, msg="Accepted non-existent Scaling Group VDU ID Reference")\ + with self.assertRaises(EngineException, msg="Accepted non-existent Scaling Group VDU ID Reference") \ as e: self.topic.upload_content(fake_session, did, test_vnfd, {}, {"Content-Type": []}) self.assertEqual(e.exception.http_code, HTTPStatus.UNPROCESSABLE_ENTITY, "Wrong HTTP status code") @@ -443,6 +442,7 @@ class Test_VnfdTopic(TestCase): def test_delete_vnfd(self): did = db_vnfd_content["_id"] self.db.get_one.return_value = db_vnfd_content + p_id = db_vnfd_content["_admin"]["projects_read"][0] with self.subTest(i=1, t='Normal Deletion'): self.db.get_list.return_value = [] self.db.del_one.return_value = {"deleted": 1} @@ -454,7 +454,7 @@ class Test_VnfdTopic(TestCase): self.assertEqual(msg_args[2], {"_id": did}, "Wrong message content") self.assertEqual(db_args[0], self.topic.topic, "Wrong DB topic") self.assertEqual(db_args[1]["_id"], did, "Wrong DB ID") - self.assertEqual(db_args[1]["_admin.projects_read"], [[], ['ANY']], "Wrong DB filter") + self.assertEqual(db_args[1]["_admin.projects_write.cont"], [p_id, 'ANY'], "Wrong DB filter") db_g1_args = self.db.get_one.call_args[0] self.assertEqual(db_g1_args[0], self.topic.topic, "Wrong DB topic") self.assertEqual(db_g1_args[1]["_id"], did, "Wrong DB VNFD ID") @@ -464,16 +464,11 @@ class Test_VnfdTopic(TestCase): self.assertEqual(db_gl_calls[1][0][0], "nsds", "Wrong DB topic") self.assertEqual(db_gl_calls[1][0][1]["constituent-vnfd.ANYINDEX.vnfd-id-ref"], db_vnfd_content["id"], "Wrong DB NSD constituent-vnfd id-ref") - db_s1_args = self.db.set_one.call_args - self.assertEqual(db_s1_args[0][0], self.topic.topic, "Wrong DB topic") - self.assertEqual(db_s1_args[0][1]["_id"], did, "Wrong DB ID") - self.assertIn(test_pid, db_s1_args[0][1]["_admin.projects_write.cont"], "Wrong DB filter") - self.assertIsNone(db_s1_args[1]["update_dict"], "Wrong DB update dictionary") - self.assertEqual(db_s1_args[1]["pull"]["_admin.projects_read"]["$in"], fake_session["project_id"], - "Wrong DB pull dictionary") + + self.db.set_one.assert_not_called() fs_del_calls = self.fs.file_delete.call_args_list self.assertEqual(fs_del_calls[0][0][0], did, "Wrong FS file id") - self.assertEqual(fs_del_calls[1][0][0], did+'_', "Wrong FS folder id") + self.assertEqual(fs_del_calls[1][0][0], did + '_', "Wrong FS folder id") with self.subTest(i=2, t='Conflict on Delete - VNFD in use by VNFR'): self.db.get_list.return_value = [{"_id": str(uuid4()), "name": "fake-vnfr"}] with self.assertRaises(EngineException, msg="Accepted VNFD in use by VNFR") as e: @@ -495,8 +490,238 @@ class Test_VnfdTopic(TestCase): self.topic.delete(fake_session, did) self.assertEqual(e.exception.http_code, HTTPStatus.NOT_FOUND, "Wrong HTTP status code") self.assertIn(norm(excp_msg), norm(str(e.exception)), "Wrong exception text") + with self.subTest(i=5, t='No delete because referenced by other project'): + db_vnfd_content["_admin"]["projects_read"].append("other_project") + self.db.get_one = Mock(return_value=db_vnfd_content) + self.db.get_list = Mock(return_value=[]) + self.msg.write.reset_mock() + self.db.del_one.reset_mock() + self.fs.file_delete.reset_mock() + + self.topic.delete(fake_session, did) + self.db.del_one.assert_not_called() + self.msg.write.assert_not_called() + db_g1_args = self.db.get_one.call_args[0] + self.assertEqual(db_g1_args[0], self.topic.topic, "Wrong DB topic") + self.assertEqual(db_g1_args[1]["_id"], did, "Wrong DB VNFD ID") + db_s1_args = self.db.set_one.call_args + self.assertEqual(db_s1_args[0][0], self.topic.topic, "Wrong DB topic") + self.assertEqual(db_s1_args[0][1]["_id"], did, "Wrong DB ID") + self.assertIn(p_id, db_s1_args[0][1]["_admin.projects_write.cont"], "Wrong DB filter") + self.assertIsNone(db_s1_args[1]["update_dict"], "Wrong DB update dictionary") + self.assertEqual(db_s1_args[1]["pull_list"], + {"_admin.projects_read": (p_id,), "_admin.projects_write": (p_id,)}, + "Wrong DB pull_list dictionary") + self.fs.file_delete.assert_not_called() return + def test_validate_mgmt_interfaces_connection_points_on_valid_descriptor(self): + indata = deepcopy(db_vnfd_content) + self.topic.validate_mgmt_interfaces_connection_points(indata) + + def test_validate_mgmt_interfaces_connection_points_when_missing_connection_point(self): + indata = deepcopy(db_vnfd_content) + indata['connection-point'] = [] + with self.assertRaises(EngineException) as e: + self.topic.validate_mgmt_interfaces_connection_points(indata) + self.assertEqual(e.exception.http_code, HTTPStatus.UNPROCESSABLE_ENTITY, "Wrong HTTP status code") + self.assertIn(norm("mgmt-interface:cp='{}' must match an existing connection-point" + .format(indata["mgmt-interface"]["cp"])), + norm(str(e.exception)), "Wrong exception text") + + def test_validate_mgmt_interfaces_connection_points_when_missing_mgmt_interface(self): + indata = deepcopy(db_vnfd_content) + indata.pop('mgmt-interface') + with self.assertRaises(EngineException) as e: + self.topic.validate_mgmt_interfaces_connection_points(indata) + self.assertEqual(e.exception.http_code, HTTPStatus.UNPROCESSABLE_ENTITY, "Wrong HTTP status code") + self.assertIn(norm("'mgmt-interface' is a mandatory field and it is not defined"), + norm(str(e.exception)), "Wrong exception text") + + def test_validate_vdu_connection_point_refs_on_valid_descriptor(self): + indata = db_vnfd_content + vdu = indata['vdu'][0] + self.topic.validate_vdu_connection_point_refs(vdu, indata) + + def test_validate_vdu_connection_point_refs_when_missing_internal_connection_point(self): + indata = deepcopy(db_vnfd_content) + vdu = indata['vdu'][0] + vdu.pop('internal-connection-point') + with self.assertRaises(EngineException) as e: + self.topic.validate_vdu_connection_point_refs(vdu, indata) + self.assertEqual(e.exception.http_code, HTTPStatus.UNPROCESSABLE_ENTITY, "Wrong HTTP status code") + self.assertIn(norm("vdu[id='{}']:interface[name='{}']:internal-connection-point-ref='{}' " + "must match an existing vdu:internal-connection-point" + .format(vdu["id"], vdu['interface'][1]["name"], + vdu['interface'][1]["internal-connection-point-ref"])), + norm(str(e.exception)), "Wrong exception text") + + def test_validate_vdu_connection_point_refs_when_missing_external_connection_point(self): + indata = deepcopy(db_vnfd_content) + vdu = indata['vdu'][0] + indata.pop('connection-point') + with self.assertRaises(EngineException) as e: + self.topic.validate_vdu_connection_point_refs(vdu, indata) + self.assertEqual(e.exception.http_code, HTTPStatus.UNPROCESSABLE_ENTITY, "Wrong HTTP status code") + self.assertIn(norm("vdu[id='{}']:interface[name='{}']:external-connection-point-ref='{}' " + "must match an existing connection-point" + .format(vdu["id"], vdu['interface'][0]["name"], + vdu['interface'][0]["external-connection-point-ref"])), + norm(str(e.exception)), "Wrong exception text") + + def test_validate_vdu_connection_point_refs_on_duplicated_internal_connection_point_ref(self): + indata = deepcopy(db_vnfd_content) + vdu = indata['vdu'][0] + duplicated_interface = {'name': 'dup-mgmt-eth1', 'position': 3, + 'internal-connection-point-ref': 'mgmtVM-internal'} + vdu['interface'].insert(0, duplicated_interface) + with self.assertRaises(EngineException) as e: + self.topic.validate_vdu_connection_point_refs(vdu, indata) + self.assertEqual(e.exception.http_code, HTTPStatus.UNPROCESSABLE_ENTITY, "Wrong HTTP status code") + self.assertIn(norm("vdu[id='{}']:interface[name='{}']:internal-connection-point-ref='{}' " + "is referenced by other interface" + .format(vdu["id"], vdu['interface'][2]["name"], + vdu['interface'][2]["internal-connection-point-ref"])), + norm(str(e.exception)), "Wrong exception text") + + def test_validate_vdu_connection_point_refs_on_duplicated_external_connection_point_ref(self): + indata = deepcopy(db_vnfd_content) + vdu = indata['vdu'][0] + duplicated_interface = {'name': 'dup-mgmt-eth0', 'position': 3, 'external-connection-point-ref': 'vnf-mgmt'} + vdu['interface'].insert(0, duplicated_interface) + with self.assertRaises(EngineException) as e: + self.topic.validate_vdu_connection_point_refs(vdu, indata) + self.assertEqual(e.exception.http_code, HTTPStatus.UNPROCESSABLE_ENTITY, "Wrong HTTP status code") + self.assertIn(norm("vdu[id='{}']:interface[name='{}']:external-connection-point-ref='{}' " + "is referenced by other interface" + .format(vdu["id"], vdu['interface'][1]["name"], + vdu['interface'][1]["external-connection-point-ref"])), + norm(str(e.exception)), "Wrong exception text") + + def test_validate_internal_vlds_on_valid_descriptor(self): + indata = db_vnfd_content + self.topic.validate_internal_vlds(indata) + + def test_validate_internal_vlds_on_duplicated_vld(self): + indata = deepcopy(db_vnfd_content) + duplicated_vld = {'id': 'internal', 'name': 'internal', + 'internal-connection-point': [{'id-ref': 'mgmtVM-internal'}, {'id-ref': 'dataVM-internal'}]} + indata['internal-vld'].insert(0, duplicated_vld) + with self.assertRaises(EngineException) as e: + self.topic.validate_internal_vlds(indata) + self.assertEqual(e.exception.http_code, HTTPStatus.UNPROCESSABLE_ENTITY, "Wrong HTTP status code") + self.assertIn(norm("Duplicated VLD name '{}' in vnfd[id={}]:internal-vld[id={}]" + .format(indata['internal-vld'][1]["name"], indata["id"], indata['internal-vld'][1]["id"])), + norm(str(e.exception)), "Wrong exception text") + + def test_validate_internal_vlds_when_missing_internal_connection_point(self): + indata = deepcopy(db_vnfd_content) + ivld = indata['internal-vld'][0] + icp = ivld['internal-connection-point'][0] + indata['vdu'][0]['internal-connection-point'].pop() + with self.assertRaises(EngineException) as e: + self.topic.validate_internal_vlds(indata) + self.assertEqual(e.exception.http_code, HTTPStatus.UNPROCESSABLE_ENTITY, "Wrong HTTP status code") + self.assertIn(norm("internal-vld[id='{}']:internal-connection-point='{}' must match an existing " + "vdu:internal-connection-point".format(ivld["id"], icp["id-ref"])), + norm(str(e.exception)), "Wrong exception text") + + def test_validate_internal_vlds_when_missing_ip_profile(self): + indata = deepcopy(db_vnfd_content) + ivld = indata['internal-vld'][0] + ivld['ip-profile-ref'] = 'non-existing-ip-profile' + with self.assertRaises(EngineException) as e: + self.topic.validate_internal_vlds(indata) + self.assertEqual(e.exception.http_code, HTTPStatus.UNPROCESSABLE_ENTITY, "Wrong HTTP status code") + self.assertIn(norm("internal-vld[id='{}']:ip-profile-ref='{}' does not exist".format( + ivld["id"], ivld["ip-profile-ref"])), + norm(str(e.exception)), "Wrong exception text") + + def test_validate_monitoring_params_on_valid_descriptor(self): + indata = db_vnfd_content + self.topic.validate_monitoring_params(indata) + + def test_validate_monitoring_params_when_missing_vdu(self): + indata = deepcopy(db_vnfd_content) + monitoring_param = indata['monitoring-param'][0] + indata['vdu'].pop() + with self.assertRaises(EngineException) as e: + self.topic.validate_monitoring_params(indata) + self.assertEqual(e.exception.http_code, HTTPStatus.UNPROCESSABLE_ENTITY, "Wrong HTTP status code") + self.assertIn(norm("monitoring-param:vdu-monitoring-param:vdu-monitoring-param-ref='{}' not " + "defined at vdu[id='{}'] or vdu does not exist" + .format(monitoring_param["vdu-monitoring-param"]["vdu-monitoring-param-ref"], + monitoring_param["vdu-monitoring-param"]["vdu-ref"])), + norm(str(e.exception)), "Wrong exception text") + + def test_validate_monitoring_params_when_missing_vdu_monitoring_param_ref(self): + indata = deepcopy(db_vnfd_content) + monitoring_param = indata['monitoring-param'][0] + indata['vdu'][1]['monitoring-param'] = [] + with self.assertRaises(EngineException) as e: + self.topic.validate_monitoring_params(indata) + self.assertEqual(e.exception.http_code, HTTPStatus.UNPROCESSABLE_ENTITY, "Wrong HTTP status code") + self.assertIn(norm("monitoring-param:vdu-monitoring-param:vdu-monitoring-param-ref='{}' not " + "defined at vdu[id='{}'] or vdu does not exist" + .format(monitoring_param["vdu-monitoring-param"]["vdu-monitoring-param-ref"], + monitoring_param["vdu-monitoring-param"]["vdu-ref"])), + norm(str(e.exception)), "Wrong exception text") + + def test_validate_scaling_group_descriptor_on_valid_descriptor(self): + indata = db_vnfd_content + self.topic.validate_scaling_group_descriptor(indata) + + def test_validate_scaling_group_descriptor_when_missing_vnf_monitoring_param_ref(self): + indata = deepcopy(db_vnfd_content) + sgd = indata['scaling-group-descriptor'][0] + sc = sgd['scaling-policy'][0]['scaling-criteria'][0] + indata['monitoring-param'] = [] + with self.assertRaises(EngineException) as e: + self.topic.validate_scaling_group_descriptor(indata) + self.assertEqual(e.exception.http_code, HTTPStatus.UNPROCESSABLE_ENTITY, "Wrong HTTP status code") + self.assertIn(norm("scaling-group-descriptor[name='{}']:scaling-criteria[name='{}']:" + "vnf-monitoring-param-ref='{}' not defined in any monitoring-param" + .format(sgd["name"], sc["name"], sc["vnf-monitoring-param-ref"])), + norm(str(e.exception)), "Wrong exception text") + + def test_validate_scaling_group_descriptor_when_missing_vdu(self): + indata = deepcopy(db_vnfd_content) + sgd = indata['scaling-group-descriptor'][0] + sgd_vdu = sgd['vdu'][0] + indata['vdu'].pop() + with self.assertRaises(EngineException) as e: + self.topic.validate_scaling_group_descriptor(indata) + self.assertEqual(e.exception.http_code, HTTPStatus.UNPROCESSABLE_ENTITY, "Wrong HTTP status code") + self.assertIn(norm("scaling-group-descriptor[name='{}']:vdu-id-ref={} does not match any vdu" + .format(sgd["name"], sgd_vdu["vdu-id-ref"])), + norm(str(e.exception)), "Wrong exception text") + + def test_validate_scaling_group_descriptor_when_missing_vnf_configuration(self): + indata = deepcopy(db_vnfd_content) + sgd = indata['scaling-group-descriptor'][0] + indata.pop('vnf-configuration') + with self.assertRaises(EngineException) as e: + self.topic.validate_scaling_group_descriptor(indata) + self.assertEqual(e.exception.http_code, HTTPStatus.UNPROCESSABLE_ENTITY, "Wrong HTTP status code") + self.assertIn(norm("'vnf-configuration' not defined in the descriptor but it is referenced by " + "scaling-group-descriptor[name='{}']:scaling-config-action" + .format(sgd["name"])), + norm(str(e.exception)), "Wrong exception text") + + def test_validate_scaling_group_descriptor_when_missing_scaling_config_action_primitive(self): + indata = deepcopy(db_vnfd_content) + sgd = indata['scaling-group-descriptor'][0] + sca = sgd['scaling-config-action'][0] + indata['vnf-configuration']['config-primitive'] = [] + with self.assertRaises(EngineException) as e: + self.topic.validate_scaling_group_descriptor(indata) + self.assertEqual(e.exception.http_code, HTTPStatus.UNPROCESSABLE_ENTITY, "Wrong HTTP status code") + self.assertIn(norm("scaling-group-descriptor[name='{}']:scaling-config-action:vnf-config-" + "primitive-name-ref='{}' does not match any " + "vnf-configuration:config-primitive:name" + .format(sgd["name"], sca["vnf-config-primitive-name-ref"])), + norm(str(e.exception)), "Wrong exception text") + class Test_NsdTopic(TestCase): @@ -512,8 +737,9 @@ class Test_NsdTopic(TestCase): self.db = Mock(dbbase.DbBase()) self.fs = Mock(fsbase.FsBase()) self.msg = Mock(msgbase.MsgBase()) - self.auth = Mock(authconn.Authconn(None, None)) + self.auth = Mock(authconn.Authconn(None, None, None)) self.topic = NsdTopic(self.db, self.fs, self.msg, self.auth) + self.topic.check_quota = Mock(return_value=None) # skip quota def test_new_nsd(self): did = db_nsd_content["_id"] @@ -570,7 +796,7 @@ class Test_NsdTopic(TestCase): compare_desc(self, test_nsd, db_args[2], "NSD") finally: pass - self.db.get_one.side_effect = lambda table, filter, fail_on_empty=None, fail_on_more=None:\ + self.db.get_one.side_effect = lambda table, filter, fail_on_empty=None, fail_on_more=None: \ {"_id": did, "_admin": db_nsd_content["_admin"]} with self.subTest(i=2, t='Check Pyangbind Validation: required properties'): tmp = test_nsd["id"] @@ -696,19 +922,12 @@ class Test_NsdTopic(TestCase): .format(test_nsd["vld"][0]["id"], test_nsd["vld"][0]["vnfd-connection-point-ref"][0]["member-vnf-index-ref"], test_nsd["vld"][0]["vnfd-connection-point-ref"][0] - ["vnfd-connection-point-ref"], + ["vnfd-connection-point-ref"], db_vnfd_content["id"])), norm(str(e.exception)), "Wrong exception text") finally: test_nsd["vld"][0]["vnfd-connection-point-ref"][0]["vnfd-connection-point-ref"] = tmp return - with self.subTest(i=11, t='Check Input Validation: everything right'): - test_nsd["id"] = "fake-nsd-id" - self.db.get_one.side_effect = [{"_id": did, "_admin": db_nsd_content["_admin"]}, None] - self.db.get_list.return_value = [db_vnfd_content] - rc = self.topic.upload_content(fake_session, did, test_nsd, {}, {"Content-Type": []}) - self.assertTrue(rc, "Input Validation: Unexpected failure") - return def test_edit_nsd(self): did = db_nsd_content["_id"] @@ -756,6 +975,7 @@ class Test_NsdTopic(TestCase): def test_delete_nsd(self): did = db_nsd_content["_id"] self.db.get_one.return_value = db_nsd_content + p_id = db_nsd_content["_admin"]["projects_read"][0] with self.subTest(i=1, t='Normal Deletion'): self.db.get_list.return_value = [] self.db.del_one.return_value = {"deleted": 1} @@ -767,7 +987,7 @@ class Test_NsdTopic(TestCase): self.assertEqual(msg_args[2], {"_id": did}, "Wrong message content") self.assertEqual(db_args[0], self.topic.topic, "Wrong DB topic") self.assertEqual(db_args[1]["_id"], did, "Wrong DB ID") - self.assertEqual(db_args[1]["_admin.projects_read"], [[], ['ANY']], "Wrong DB filter") + self.assertEqual(db_args[1]["_admin.projects_write.cont"], [p_id, 'ANY'], "Wrong DB filter") db_g1_args = self.db.get_one.call_args[0] self.assertEqual(db_g1_args[0], self.topic.topic, "Wrong DB topic") self.assertEqual(db_g1_args[1]["_id"], did, "Wrong DB NSD ID") @@ -777,17 +997,10 @@ class Test_NsdTopic(TestCase): self.assertEqual(db_gl_calls[1][0][0], "nsts", "Wrong DB topic") self.assertEqual(db_gl_calls[1][0][1]["netslice-subnet.ANYINDEX.nsd-ref"], db_nsd_content["id"], "Wrong DB NSD netslice-subnet nsd-ref") - db_s1_args = self.db.set_one.call_args - self.assertEqual(db_s1_args[0][0], self.topic.topic, "Wrong DB topic") - self.assertEqual(db_s1_args[0][1]["_id"], did, "Wrong DB ID") - self.assertIn(test_pid, db_s1_args[0][1]["_admin.projects_write.cont"], "Wrong DB filter") - self.assertIsNone(db_s1_args[1]["update_dict"], "Wrong DB update dictionary") - self.assertEqual(db_s1_args[1]["pull"]["_admin.projects_read"]["$in"], fake_session["project_id"], - "Wrong DB pull dictionary") + self.db.set_one.assert_not_called() fs_del_calls = self.fs.file_delete.call_args_list self.assertEqual(fs_del_calls[0][0][0], did, "Wrong FS file id") - self.assertEqual(fs_del_calls[1][0][0], did+'_', "Wrong FS folder id") - return # TO REMOVE + self.assertEqual(fs_del_calls[1][0][0], did + '_', "Wrong FS folder id") with self.subTest(i=2, t='Conflict on Delete - NSD in use by nsr'): self.db.get_list.return_value = [{"_id": str(uuid4()), "name": "fake-nsr"}] with self.assertRaises(EngineException, msg="Accepted NSD in use by NSR") as e: @@ -809,8 +1022,122 @@ class Test_NsdTopic(TestCase): self.topic.delete(fake_session, did) self.assertEqual(e.exception.http_code, HTTPStatus.NOT_FOUND, "Wrong HTTP status code") self.assertIn(norm(excp_msg), norm(str(e.exception)), "Wrong exception text") + with self.subTest(i=5, t='No delete because referenced by other project'): + db_nsd_content["_admin"]["projects_read"].append("other_project") + self.db.get_one = Mock(return_value=db_nsd_content) + self.db.get_list = Mock(return_value=[]) + self.msg.write.reset_mock() + self.db.del_one.reset_mock() + self.fs.file_delete.reset_mock() + + self.topic.delete(fake_session, did) + self.db.del_one.assert_not_called() + self.msg.write.assert_not_called() + db_g1_args = self.db.get_one.call_args[0] + self.assertEqual(db_g1_args[0], self.topic.topic, "Wrong DB topic") + self.assertEqual(db_g1_args[1]["_id"], did, "Wrong DB VNFD ID") + db_s1_args = self.db.set_one.call_args + self.assertEqual(db_s1_args[0][0], self.topic.topic, "Wrong DB topic") + self.assertEqual(db_s1_args[0][1]["_id"], did, "Wrong DB ID") + self.assertIn(p_id, db_s1_args[0][1]["_admin.projects_write.cont"], "Wrong DB filter") + self.assertIsNone(db_s1_args[1]["update_dict"], "Wrong DB update dictionary") + self.assertEqual(db_s1_args[1]["pull_list"], + {"_admin.projects_read": (p_id,), "_admin.projects_write": (p_id,)}, + "Wrong DB pull_list dictionary") + self.fs.file_delete.assert_not_called() return + def test_validate_vld_mgmt_network_with_ip_profile_ref_on_valid_descriptor(self): + indata = deepcopy(db_nsd_content) + vld = indata['vld'][0] + self.topic.validate_vld_mgmt_network_with_ip_profile_ref(vld) + + def test_validate_vld_mgmt_network_with_ip_profile_ref_when_both_defined(self): + indata = deepcopy(db_nsd_content) + vld = indata['vld'][0] + vld['ip-profile-ref'] = 'a-profile-ref' + with self.assertRaises(EngineException) as e: + self.topic.validate_vld_mgmt_network_with_ip_profile_ref(vld) + self.assertEqual(e.exception.http_code, HTTPStatus.UNPROCESSABLE_ENTITY, "Wrong HTTP status code") + self.assertIn(norm("Error at vld[id='{}']:ip-profile-ref" + " You cannot set an ip-profile when mgmt-network is True" + .format(vld["id"])), + norm(str(e.exception)), "Wrong exception text") + + def test_validate_vld_connection_point_refs_on_valid_descriptor(self): + indata = deepcopy(db_nsd_content) + vld = indata['vld'][0] + self.topic.validate_vld_connection_point_refs(vld, indata) + + def test_validate_vld_connection_point_refs_when_missing_constituent_vnfd(self): + indata = deepcopy(db_nsd_content) + vld = indata['vld'][0] + vnfd_cp = vld['vnfd-connection-point-ref'][0] + indata['constituent-vnfd'] = [] + with self.assertRaises(EngineException) as e: + self.topic.validate_vld_connection_point_refs(vld, indata) + self.assertEqual(e.exception.http_code, HTTPStatus.UNPROCESSABLE_ENTITY, "Wrong HTTP status code") + self.assertIn(norm("Error at vld[id='{}']:vnfd-connection-point-ref[member-vnf-index-ref='{}'] " + "does not match any constituent-vnfd:member-vnf-index" + .format(vld["id"], vnfd_cp["member-vnf-index-ref"])), + norm(str(e.exception)), "Wrong exception text") + + def test_validate_vld_connection_point_refs_on_unmatched_constituent_vnfd(self): + indata = deepcopy(db_nsd_content) + vld = indata['vld'][0] + vnfd_cp = vld['vnfd-connection-point-ref'][0] + constituent_vnfd = indata['constituent-vnfd'][0] + constituent_vnfd['vnfd-id-ref'] = 'unmatched-vnfd-id-ref' + with self.assertRaises(EngineException) as e: + self.topic.validate_vld_connection_point_refs(vld, indata) + self.assertEqual(e.exception.http_code, HTTPStatus.UNPROCESSABLE_ENTITY, "Wrong HTTP status code") + self.assertIn(norm("Error at vld[id='{}']:vnfd-connection-point-ref[vnfd-id-ref='{}'] " + "does not match constituent-vnfd[member-vnf-index='{}']:vnfd-id-ref" + " '{}'".format(vld["id"], vnfd_cp["vnfd-id-ref"], + constituent_vnfd["member-vnf-index"], + constituent_vnfd["vnfd-id-ref"])), + norm(str(e.exception)), "Wrong exception text") + + def test_validate_vld_connection_point_refs_vnfd_connection_points_on_valid_descriptor(self): + nsd_descriptor = deepcopy(db_nsd_content) + vnfd_1_descriptor = deepcopy(db_vnfd_content) + vnfd_2_descriptor = deepcopy(db_vnfd_content) + vld = nsd_descriptor['vld'][0] + member_vnfd_index = {'1': vnfd_1_descriptor, '2': vnfd_2_descriptor} + self.topic.validate_vld_connection_point_refs_vnfd_connection_points(vld, member_vnfd_index) + + def test_validate_vld_connection_point_refs_vnfd_connection_points_when_missing_connection_point(self): + nsd_descriptor = deepcopy(db_nsd_content) + vnfd_1_descriptor = deepcopy(db_vnfd_content) + vnfd_2_descriptor = deepcopy(db_vnfd_content) + vld = nsd_descriptor['vld'][0] + referenced_vnfd_cp = vld['vnfd-connection-point-ref'][0] + member_vnfd_index = {'1': vnfd_1_descriptor, '2': vnfd_2_descriptor} + vnfd_1_descriptor['connection-point'] = [] + with self.assertRaises(EngineException) as e: + self.topic.validate_vld_connection_point_refs_vnfd_connection_points(vld, member_vnfd_index) + self.assertEqual(e.exception.http_code, HTTPStatus.UNPROCESSABLE_ENTITY, "Wrong HTTP status code") + self.assertIn(norm("Error at vld[id='{}']:vnfd-connection-point-ref[member-vnf-index-ref='{}']:vnfd-" + "connection-point-ref='{}' references a non existing conection-point:name inside vnfd '{}'" + .format(vld["id"], referenced_vnfd_cp["member-vnf-index-ref"], + referenced_vnfd_cp["vnfd-connection-point-ref"], vnfd_1_descriptor["id"])), + norm(str(e.exception)), "Wrong exception text") + + def test_check_conflict_on_edit_when_missing_vnfd_index(self): + nsd_descriptor = deepcopy(db_nsd_content) + invalid_vnfd_id_ref = 'invalid-vnfd-id-ref' + nsd_descriptor['id'] = 'invalid-vnfd-id-ns' + nsd_descriptor['constituent-vnfd'][0]['vnfd-id-ref'] = invalid_vnfd_id_ref + nsd_descriptor['vld'][0]['vnfd-connection-point-ref'][0]['vnfd-id-ref'] = invalid_vnfd_id_ref + nsd_descriptor['vld'][1]['vnfd-connection-point-ref'][0]['vnfd-id-ref'] = invalid_vnfd_id_ref + with self.assertRaises(EngineException) as e: + self.db.get_list.return_value = [] + self.topic.check_conflict_on_edit(fake_session, nsd_descriptor, [], 'id') + self.assertEqual(e.exception.http_code, HTTPStatus.CONFLICT, "Wrong HTTP status code") + self.assertIn(norm("Descriptor error at 'constituent-vnfd':'vnfd-id-ref'='{}' references a non " + "existing vnfd".format(invalid_vnfd_id_ref)), + norm(str(e.exception)), "Wrong exception text") + if __name__ == '__main__': unittest.main()