__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
+from unittest.mock import Mock, patch
from uuid import uuid4
from http import HTTPStatus
from copy import deepcopy
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):
+ @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):
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(
self.assertEqual(
storage["descriptor"], "package", "Wrong storage descriptor"
)
+ self.assertEqual(
+ admin["revision"], 1, "Wrong revision number"
+ )
+
compare_desc(self, test_vnfd, db_args[2], "VNFD")
finally:
test_vnfd["vdu"][0]["cloud-init-file"] = tmp1
norm(str(e.exception)),
"Wrong exception text",
)
+ db_args = self.db.replace.call_args[0]
+ admin = db_args[2]["_admin"]
+ self.assertEqual(
+ admin["revision"], 1, "Wrong revision number"
+ )
+
finally:
del test_vnfd["extra-property"]
with self.subTest(i=3, t="Check Pyangbind Validation: property types"):
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"
)
"Wrong DB NSD vnfd-id",
)
+ db_del_args = self.db.del_list.call_args[0]
+ self.assertEqual(
+ self.db.del_list.call_args[0][0],
+ self.topic.topic+"_revisions",
+ "Wrong DB topic",
+ )
+
+ self.assertEqual(
+ self.db.del_list.call_args[0][1]['_id']['$regex'],
+ did,
+ "Wrong ID for rexep delete",
+ )
+
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.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)
"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,
+ 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
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):
+ @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):
did = db_nsd_content["_id"]
self.fs.get_params.return_value = {}
self.fs.file_exists.return_value = False
"Wrong read-write project list",
)
try:
+ self.fs.path = ""
self.db.get_one.side_effect = [
{"_id": did, "_admin": db_nsd_content["_admin"]},
None,
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"
+ )
+
finally:
pass
self.db.get_one.side_effect = (
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,
):