Fixing Basic 12 Ns Primitives 75/12475/2
authoraticig <gulsum.atici@canonical.com>
Sat, 20 Aug 2022 17:56:19 +0000 (20:56 +0300)
committercubag <gcuba@whitestack.com>
Mon, 22 Aug 2022 23:07:24 +0000 (01:07 +0200)
NSD folder name can be different than the NSD package name.
Getting the exact NSD folder name while finding charm name.
Removing redefined the method async def upgrade_charm causing flake8 error.

Change-Id: I772c6a47104271de0a18c748d380ed71849194f0
Signed-off-by: aticig <gulsum.atici@canonical.com>
osm_lcm/lcm_helm_conn.py
osm_lcm/lcm_utils.py
osm_lcm/tests/test_lcm_utils.py

index c8ba44d..6fd3374 100644 (file)
@@ -301,10 +301,6 @@ class LCMHelmConn(N2VCConnector, LcmBase):
         # not used for this connector
         pass
 
-    async def upgrade_charm(self, *args, **kwargs):
-        # not used for this connector
-        pass
-
     async def get_ee_ssh_public__key(
         self,
         ee_id: str,
index 6d20318..7af63a8 100644 (file)
@@ -290,7 +290,7 @@ class LcmBase:
                 + "/"
                 + nsd_package_name
                 + "/charms/"
-                + charm_folder_name.split(".")[0]
+                + charm_folder_name.replace(".charm", "")
             )
             # Extract .charm to extract path
             with ZipFile(charm_path, "r") as zipfile:
@@ -315,10 +315,22 @@ class LcmBase:
 
             # Find nsd_package details: path, name
             revision = db_nsr.get("revision", "")
-            nsd_package_path = (
-                db_nsr["nsd-id"] + ":" + str(revision) if revision else db_nsr["nsd-id"]
-            )
-            nsd_package_name = os.listdir(self.fs.path + nsd_package_path)[0]
+
+            # Get the NSD package path
+            if revision:
+
+                nsd_package_path = (
+                    db_nsr["nsd-id"] + ":" + str(revision)
+                )
+                db_nsd = self.db.get_one("nsds_revisions", {"_id": nsd_package_path})
+
+            else:
+                nsd_package_path = db_nsr["nsd-id"]
+
+                db_nsd = self.db.get_one("nsds", {"_id": nsd_package_path})
+
+            # Get the NSD package name
+            nsd_package_name = db_nsd["_admin"]["storage"]["pkg-dir"]
 
             # Remove the existing nsd package and sync from FsMongo
             shutil.rmtree(self.fs.path + nsd_package_path, ignore_errors=True)
index 13788e2..6067b2d 100644 (file)
@@ -16,7 +16,6 @@
 # contact: alfonso.tiernosepulveda@telefonica.com
 ##
 import logging
-import os
 from unittest.mock import Mock, patch, MagicMock
 from unittest import TestCase
 
@@ -32,8 +31,9 @@ from zipfile import BadZipfile
 
 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"
 
@@ -42,6 +42,7 @@ class TestLcmBase(TestCase):
         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
@@ -101,9 +102,13 @@ class TestLcmBase(TestCase):
         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")
 
@@ -114,8 +119,14 @@ class TestLcmBase(TestCase):
         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(
@@ -134,9 +145,15 @@ class TestLcmBase(TestCase):
         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(
@@ -151,117 +168,80 @@ class TestLcmBase(TestCase):
     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)