# contact: alfonso.tiernosepulveda@telefonica.com
##
import logging
-import os
from unittest.mock import Mock, patch, MagicMock
from unittest import TestCase
class TestLcmBase(TestCase):
- test_nsr_id = "b63aa1ba-996e-43a7-921a-1aca5ccbc63f"
- nsd_package_path = "/" + test_nsr_id
+ test_nsr_id = "f48163a6-c807-47bc-9682-f72caef5af85"
+ test_nsd_id = "8c2f8b95-bb1b-47ee-8001-36dc090678da"
+ nsd_package_path = "/" + test_nsd_id
nsd_package_name = "test_nsd"
charm_metadata_file = "/path/charm/metadata.yaml"
Database.instance = None
self.db = Database({"database": {"driver": "memory"}}).instance.db
self.db.create_list("nsds", yaml.safe_load(descriptors.db_nsds_text))
+ self.db.create_list("nsds_revisions", yaml.safe_load(descriptors.db_nsds_text))
self.db.create_list("nsrs", yaml.safe_load(descriptors.db_nsrs_text))
# Filesystem
self.fs = Filesystem({"storage": {"driver": "local", "path": "/"}}).instance.fs
fs.path = "/app/storage"
instance.fs = fs
charm_folder_name = "simple_charm"
- expected_result = "/app/storage/b63aa1ba-996e-43a7-921a-1aca5ccbc63f/test_nsd/charms/simple_charm"
+ expected_result = (
+ "/app/storage/" + TestLcmBase.test_nsd_id + "/test_nsd/charms/simple_charm"
+ )
result = instance._get_charm_path(
- TestLcmBase.nsd_package_path, TestLcmBase.nsd_package_name, charm_folder_name
+ TestLcmBase.nsd_package_path,
+ TestLcmBase.nsd_package_name,
+ charm_folder_name,
)
self.assertEqual(result, expected_result, "wrong_charm_path")
instance.fs = fs
mock_zipfile = MagicMock(create_autospec=True)
charm_folder_name = "simple_charm"
- charm_path = "/app/storage/b63aa1ba-996e-43a7-921a-1aca5ccbc63f/test_nsd/charms/simple_charm"
- expected_result = "/app/storage/b63aa1ba-996e-43a7-921a-1aca5ccbc63f/test_nsd/charms/simple_charm/metadata.yaml"
+ charm_path = (
+ "/app/storage/" + TestLcmBase.test_nsd_id + "/test_nsd/charms/simple_charm"
+ )
+ expected_result = (
+ "/app/storage/"
+ + TestLcmBase.test_nsd_id
+ + "/test_nsd/charms/simple_charm/metadata.yaml"
+ )
with patch("osm_lcm.lcm_utils.ZipFile", mock_zipfile):
result = instance._get_charm_metadata_file(
instance.fs = fs
mock_zipfile = MagicMock(create_autospec=True)
mock_zipfile.side_effect = None
- charm_folder_name = "simple_charm2.charm"
- charm_path = "/app/storage/b63aa1ba-996e-43a7-921a-1aca5ccbc63f/test_nsd/charms/simple_charm"
- expected_result = "/app/storage/b63aa1ba-996e-43a7-921a-1aca5ccbc63f/test_nsd/charms/simple_charm2/metadata.yaml"
+ charm_folder_name = "ubuntu_18.04_simple_charm2.charm"
+ charm_path = (
+ "/app/storage/" + TestLcmBase.test_nsd_id + "/test_nsd/charms/simple_charm"
+ )
+ expected_result = (
+ "/app/storage/"
+ + TestLcmBase.test_nsd_id
+ + "/test_nsd/charms/ubuntu_18.04_simple_charm2/metadata.yaml"
+ )
with patch("osm_lcm.lcm_utils.ZipFile", mock_zipfile):
result = instance._get_charm_metadata_file(
def test_find_charm_name_successfully(self):
db_nsr = self.db.get_one("nsrs", {"_id": TestLcmBase.test_nsr_id})
instance = self.my_ns
- mock_listdir = MagicMock(os.listdir())
- mock_listdir.side_effect = None
mock_charm_path = MagicMock()
mock_metadata_file = MagicMock()
mock_metadata_file.return_value = (
- "/b63aa1ba-996e-43a7-921a-1aca5ccbc63f/test_nsd/charms/simple/metadata.yaml"
+ "/" + TestLcmBase.test_nsd_id + "/new_test_nsd/charms/simple/metadata.yaml"
)
mock_charm_name = MagicMock()
mock_charm_name.return_value = "test_charm"
expected_result = "test_charm"
with patch("osm_lcm.lcm_utils.LcmBase._get_charm_path", mock_charm_path), patch(
- "osm_lcm.lcm_utils.LcmBase._get_charm_metadata_file", mock_metadata_file
- ), patch("osm_lcm.lcm_utils.LcmBase.get_charm_name", mock_charm_name), patch(
- "osm_lcm.lcm_utils.os.listdir", mock_listdir
- ):
+ "osm_lcm.lcm_utils.LcmBase._get_charm_metadata_file", mock_metadata_file
+ ), patch("osm_lcm.lcm_utils.LcmBase.get_charm_name", mock_charm_name):
result = instance.find_charm_name(db_nsr, "simple")
self.assertEqual(result, expected_result, "Wrong charm name")
mock_charm_path.assert_called_once()
mock_metadata_file.assert_called_once()
mock_charm_name.assert_called_once_with(
- "/b63aa1ba-996e-43a7-921a-1aca5ccbc63f/test_nsd/charms/simple/metadata.yaml"
+ "/"
+ + TestLcmBase.test_nsd_id
+ + "/new_test_nsd/charms/simple/metadata.yaml"
)
- self.assertEqual(mock_listdir.call_count, 1)
def test_find_charm_name_charm_bad_zipfile(self):
db_nsr = self.db.get_one("nsrs", {"_id": TestLcmBase.test_nsr_id})
instance = self.my_ns
- mock_listdir = MagicMock(os.listdir())
- mock_listdir.side_effect = None
mock_charm_path = MagicMock()
mock_metadata_file = MagicMock()
mock_metadata_file.side_effect = BadZipfile
mock_charm_name = MagicMock()
with patch("osm_lcm.lcm_utils.LcmBase._get_charm_path", mock_charm_path), patch(
- "osm_lcm.lcm_utils.LcmBase._get_charm_metadata_file", mock_metadata_file
- ), patch("osm_lcm.lcm_utils.LcmBase.get_charm_name", mock_charm_name), patch(
- "osm_lcm.lcm_utils.os.listdir", mock_listdir
- ):
+ "osm_lcm.lcm_utils.LcmBase._get_charm_metadata_file", mock_metadata_file
+ ), patch("osm_lcm.lcm_utils.LcmBase.get_charm_name", mock_charm_name):
with self.assertRaises(LcmException):
instance.find_charm_name(db_nsr, "simple")
- self.assertEqual(mock_listdir.call_count, 1)
self.assertEqual(mock_charm_path.call_count, 1)
self.assertEqual(mock_metadata_file.call_count, 1)
mock_charm_name.assert_not_called()
- def test_find_charm_name_charm_nsd_package_does_not_exist(self):
- db_nsr = self.db.get_one("nsrs", {"_id": TestLcmBase.test_nsr_id})
- instance = self.my_ns
- mock_listdir = MagicMock(os.listdir())
- mock_listdir.side_effect = FileNotFoundError
- mock_charm_path = MagicMock()
- mock_metadata_file = MagicMock()
- mock_charm_name = MagicMock()
-
- with patch("osm_lcm.lcm_utils.LcmBase._get_charm_path", mock_charm_path), patch(
- "osm_lcm.lcm_utils.LcmBase._get_charm_metadata_file", mock_metadata_file
- ), patch("osm_lcm.lcm_utils.LcmBase.get_charm_name", mock_charm_name), patch(
- "osm_lcm.lcm_utils.os.listdir", mock_listdir
- ):
-
- with self.assertRaises(LcmException):
- instance.find_charm_name(db_nsr, "simple")
- self.assertEqual(mock_listdir.call_count, 1)
- mock_charm_path.assert_not_called()
- mock_metadata_file.assert_not_called()
- mock_charm_name.assert_not_called()
-
def test_find_charm_name_missing_input_charm_folder_name(self):
db_nsr = self.db.get_one("nsrs", {"_id": TestLcmBase.test_nsr_id})
instance = self.my_ns
- mock_listdir = MagicMock(os.listdir())
- mock_charm_path = MagicMock()
mock_metadata_file = MagicMock()
mock_charm_name = MagicMock()
+ mock_charm_path = MagicMock()
with patch("osm_lcm.lcm_utils.LcmBase._get_charm_path", mock_charm_path), patch(
- "osm_lcm.lcm_utils.LcmBase._get_charm_metadata_file", mock_metadata_file
- ), patch("osm_lcm.lcm_utils.LcmBase.get_charm_name", mock_charm_name), patch(
- "osm_lcm.lcm_utils.os.listdir", mock_listdir
- ):
+ "osm_lcm.lcm_utils.LcmBase._get_charm_metadata_file", mock_metadata_file
+ ), patch("osm_lcm.lcm_utils.LcmBase.get_charm_name", mock_charm_name):
with self.assertRaises(LcmException):
instance.find_charm_name(db_nsr, "")
mock_charm_path.assert_not_called()
mock_metadata_file.assert_not_called()
mock_charm_name.assert_not_called()
- mock_listdir.assert_not_called()
def test_find_charm_name_can_not_open_metadata_file(self):
- db_nsr = self.db.get_one("nsrs", {"_id": TestLcmBase.test_nsr_id})
+ db_nsr = self.db.get_one("nsrs", {"_id": TestLcmBase.test_nsr_id})
instance = self.my_ns
- mock_listdir = MagicMock(os.listdir())
+
mock_charm_path = MagicMock()
mock_metadata_file = MagicMock()
mock_charm_name = MagicMock()
mock_charm_name.side_effect = yaml.YAMLError
with patch("osm_lcm.lcm_utils.LcmBase._get_charm_path", mock_charm_path), patch(
- "osm_lcm.lcm_utils.LcmBase._get_charm_metadata_file", mock_metadata_file
- ), patch("osm_lcm.lcm_utils.LcmBase.get_charm_name", mock_charm_name), patch(
- "osm_lcm.lcm_utils.os.listdir", mock_listdir
- ):
+ "osm_lcm.lcm_utils.LcmBase._get_charm_metadata_file", mock_metadata_file
+ ), patch("osm_lcm.lcm_utils.LcmBase.get_charm_name", mock_charm_name):
with self.assertRaises(LcmException):
instance.find_charm_name(db_nsr, "simple")
self.assertEqual(mock_charm_path.call_count, 1)
self.assertEqual(mock_metadata_file.call_count, 1)
self.assertEqual(mock_charm_name.call_count, 1)
- self.assertEqual(mock_listdir.call_count, 1)