+ def test_validate_scaling_group_descriptor_when_missing_vnf_configuration(self):
+ indata = deepcopy(db_vnfd_content)
+ df = indata["df"][0]
+ affected_sa = df["scaling-aspect"][0]
+ indata["df"][0]["lcm-operations-configuration"]["operate-vnf-op-config"][
+ "day1-2"
+ ].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(
+ "'day1-2 configuration' not defined in the descriptor but it is referenced "
+ "by df[id='{}']:scaling-aspect[id='{}']:scaling-config-action".format(
+ df["id"], affected_sa["id"]
+ )
+ ),
+ 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)
+ df = indata["df"][0]
+ affected_sa = df["scaling-aspect"][0]
+ affected_sca_primitive = affected_sa["scaling-config-action"][0][
+ "vnf-config-primitive-name-ref"
+ ]
+ df["lcm-operations-configuration"]["operate-vnf-op-config"]["day1-2"][0][
+ "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(
+ "df[id='{}']:scaling-aspect[id='{}']:scaling-config-action:vnf-"
+ "config-primitive-name-ref='{}' does not match any "
+ "day1-2 configuration:config-primitive:name".format(
+ df["id"], affected_sa["id"], affected_sca_primitive
+ )
+ ),
+ norm(str(e.exception)),
+ "Wrong exception text",
+ )
+
+ def test_new_vnfd_revision(self):
+ did = db_vnfd_content["_id"]
+ self.fs.get_params.return_value = {}
+ self.fs.file_exists.return_value = False
+ self.fs.file_open.side_effect = lambda path, mode: open(
+ "/tmp/" + str(uuid4()), "a+b"
+ )
+ test_vnfd = deepcopy(db_vnfd_content)
+ del test_vnfd["_id"]
+ del test_vnfd["_admin"]
+ self.db.create.return_value = did
+ rollback = []
+ did2, oid = self.topic.new(rollback, fake_session, {})
+ db_args = self.db.create.call_args[0]
+ self.assertEqual(
+ db_args[1]["_admin"]["revision"], 0, "New package should be at revision 0"
+ )
+
+ @patch("osm_nbi.descriptor_topics.shutil")
+ @patch("osm_nbi.descriptor_topics.os.rename")
+ def test_update_vnfd(self, mock_rename, mock_shutil):
+ old_revision = 5
+ did = db_vnfd_content["_id"]
+ self.fs.path = ""
+ self.fs.get_params.return_value = {}
+ self.fs.file_exists.return_value = False
+ self.fs.file_open.side_effect = lambda path, mode: open(
+ "/tmp/" + str(uuid4()), "a+b"
+ )
+ new_vnfd = deepcopy(db_vnfd_content)
+ del new_vnfd["_id"]
+ self.db.create.return_value = did
+ rollback = []
+ did2, oid = self.topic.new(rollback, fake_session, {})
+ del new_vnfd["vdu"][0]["cloud-init-file"]
+ del new_vnfd["df"][0]["lcm-operations-configuration"]["operate-vnf-op-config"][
+ "day1-2"
+ ][0]["execution-environment-list"][0]["juju"]
+
+ old_vnfd = {"_id": did, "_admin": deepcopy(db_vnfd_content["_admin"])}
+ old_vnfd["_admin"]["revision"] = old_revision
+
+ 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]
+ self.assertEqual(
+ db_args[2]["_admin"]["revision"],
+ old_revision + 1,
+ "Revision should increment",
+ )
+
+
+class Test_NsdTopic(TestCase):
+ @classmethod
+ def setUpClass(cls):
+ cls.test_name = "test-nsd-topic"
+
+ @classmethod
+ def tearDownClass(cls):
+ pass
+
+ 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 = 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
+
+ def prepare_nsd_creation(self):
+ self.fs.path = ""
+ did = db_nsd_content["_id"]
+ self.fs.get_params.return_value = {}
+ self.fs.file_exists.return_value = False
+ self.fs.file_open.side_effect = lambda path, mode: tempfile.TemporaryFile(
+ mode="a+b"
+ )
+ self.db.get_one.side_effect = [
+ {"_id": did, "_admin": deepcopy(db_nsd_content["_admin"])},
+ None,
+ ]
+ test_nsd = deepcopy(db_nsd_content)
+ del test_nsd["_id"]
+ del test_nsd["_admin"]
+ return did, test_nsd
+
+ @patch("osm_nbi.descriptor_topics.shutil")
+ @patch("osm_nbi.descriptor_topics.os.rename")
+ def test_new_nsd_normal_creation(self, mock_rename, mock_shutil):
+ did, test_nsd = self.prepare_nsd_creation()
+ self.db.create.return_value = did
+ rollback = []
+
+ did2, oid = self.topic.new(rollback, fake_session, {})
+ db_args = self.db.create.call_args[0]
+ msg_args = self.msg.write.call_args[0]
+ self.assertEqual(len(rollback), 1, "Wrong rollback length")
+ self.assertEqual(msg_args[0], self.topic.topic_msg, "Wrong message topic")
+ self.assertEqual(msg_args[1], "created", "Wrong message action")
+ self.assertEqual(msg_args[2], {"_id": did}, "Wrong message content")
+ self.assertEqual(db_args[0], self.topic.topic, "Wrong DB topic")
+ self.assertEqual(did2, did, "Wrong DB NSD id")
+ self.assertIsNotNone(db_args[1]["_admin"]["created"], "Wrong creation time")
+ self.assertEqual(
+ db_args[1]["_admin"]["modified"],
+ db_args[1]["_admin"]["created"],
+ "Wrong modification time",
+ )
+ self.assertEqual(
+ db_args[1]["_admin"]["projects_read"],
+ [test_pid],
+ "Wrong read-only project list",
+ )
+ self.assertEqual(
+ db_args[1]["_admin"]["projects_write"],
+ [test_pid],
+ "Wrong read-write project list",
+ )
+
+ self.db.get_list.return_value = [db_vnfd_content]
+
+ self.topic.upload_content(fake_session, did, test_nsd, {}, {"Content-Type": []})
+ msg_args = self.msg.write.call_args[0]
+ test_nsd["_id"] = did
+ self.assertEqual(msg_args[0], self.topic.topic_msg, "Wrong message topic")
+ self.assertEqual(msg_args[1], "edited", "Wrong message action")
+ self.assertEqual(msg_args[2], test_nsd, "Wrong message content")
+
+ db_args = self.db.get_one.mock_calls[0][1]
+ self.assertEqual(db_args[0], self.topic.topic, "Wrong DB topic")
+ self.assertEqual(db_args[1]["_id"], did, "Wrong DB NSD id")
+
+ db_args = self.db.replace.call_args[0]
+ self.assertEqual(db_args[0], self.topic.topic, "Wrong DB topic")
+ self.assertEqual(db_args[1], did, "Wrong DB NSD id")
+
+ admin = db_args[2]["_admin"]
+ db_admin = db_nsd_content["_admin"]
+ self.assertEqual(admin["created"], db_admin["created"], "Wrong creation time")
+ self.assertGreater(
+ admin["modified"], db_admin["created"], "Wrong modification time"
+ )
+ self.assertEqual(
+ admin["projects_read"],
+ db_admin["projects_read"],
+ "Wrong read-only project list",
+ )
+ self.assertEqual(
+ admin["projects_write"],
+ db_admin["projects_write"],
+ "Wrong read-write project list",
+ )
+ self.assertEqual(
+ admin["onboardingState"], "ONBOARDED", "Wrong onboarding state"
+ )
+ self.assertEqual(
+ admin["operationalState"], "ENABLED", "Wrong operational state"
+ )
+ self.assertEqual(admin["usageState"], "NOT_IN_USE", "Wrong usage state")
+
+ storage = admin["storage"]
+ self.assertEqual(storage["folder"], did + ":1", "Wrong storage folder")
+ self.assertEqual(storage["descriptor"], "package", "Wrong storage descriptor")
+
+ compare_desc(self, test_nsd, db_args[2], "NSD")
+ revision_args = self.db.create.call_args[0]
+ self.assertEqual(
+ revision_args[0], self.topic.topic + "_revisions", "Wrong topic"
+ )
+ self.assertEqual(revision_args[1]["id"], db_args[2]["id"], "Wrong revision id")
+ self.assertEqual(
+ revision_args[1]["_id"], db_args[2]["_id"] + ":1", "Wrong revision _id"
+ )
+
+ @patch("osm_nbi.descriptor_topics.shutil")
+ @patch("osm_nbi.descriptor_topics.os.rename")
+ def test_new_nsd_check_pyangbind_validation_required_properties(
+ self, mock_rename, mock_shutil
+ ):
+ did, test_nsd = self.prepare_nsd_creation()
+ del test_nsd["id"]
+
+ with self.assertRaises(
+ EngineException, msg="Accepted NSD with a missing required property"
+ ) as e:
+ self.topic.upload_content(
+ fake_session, did, test_nsd, {}, {"Content-Type": []}
+ )
+ self.assertEqual(
+ e.exception.http_code,
+ HTTPStatus.UNPROCESSABLE_ENTITY,
+ "Wrong HTTP status code",
+ )
+ self.assertIn(
+ norm("Error in pyangbind validation: '{}'".format("id")),
+ norm(str(e.exception)),
+ "Wrong exception text",
+ )
+
+ @patch("osm_nbi.descriptor_topics.shutil")
+ @patch("osm_nbi.descriptor_topics.os.rename")
+ def test_new_nsd_check_pyangbind_validation_additional_properties(
+ self, mock_rename, mock_shutil
+ ):
+ did, test_nsd = self.prepare_nsd_creation()
+ test_nsd["extra-property"] = 0
+
+ with self.assertRaises(
+ EngineException, msg="Accepted NSD with an additional property"
+ ) as e:
+ self.topic.upload_content(
+ fake_session, did, test_nsd, {}, {"Content-Type": []}
+ )
+ self.assertEqual(
+ e.exception.http_code,
+ HTTPStatus.UNPROCESSABLE_ENTITY,
+ "Wrong HTTP status code",
+ )
+ self.assertIn(
+ norm(
+ "Error in pyangbind validation: {} ({})".format(
+ "json object contained a key that did not exist", "extra-property"
+ )
+ ),
+ norm(str(e.exception)),
+ "Wrong exception text",
+ )
+
+ @patch("osm_nbi.descriptor_topics.shutil")
+ @patch("osm_nbi.descriptor_topics.os.rename")
+ def test_new_nsd_check_pyangbind_validation_property_types(
+ self, mock_rename, mock_shutil
+ ):
+ did, test_nsd = self.prepare_nsd_creation()
+ test_nsd["designer"] = {"key": 0}
+
+ with self.assertRaises(
+ EngineException, msg="Accepted NSD with a wrongly typed property"
+ ) as e:
+ self.topic.upload_content(
+ fake_session, did, test_nsd, {}, {"Content-Type": []}
+ )