update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[osm/SO.git] / rwlaunchpad / plugins / rwpkgmgr / test / utest_filesystem_proxy_dts.py
index 6bc5bd4..f26cf8d 100755 (executable)
@@ -27,6 +27,10 @@ import unittest
 import uuid
 import xmlrunner
 
+# Setting RIFT_VAR_ROOT if not already set for unit test execution
+if "RIFT_VAR_ROOT" not in os.environ:
+    os.environ['RIFT_VAR_ROOT'] = os.path.join(os.environ['RIFT_INSTALL'], 'var/rift/unittest')
+
 import gi
 gi.require_version('RwDts', '1.0')
 gi.require_version('RwPkgMgmtYang', '1.0')
@@ -39,9 +43,36 @@ from rift.tasklets.rwpkgmgr.proxy import filesystem
 import rift.tasklets.rwpkgmgr.publisher as pkg_publisher
 import rift.tasklets.rwpkgmgr.rpc as rpc
 import rift.test.dts
+from rift.mano.utils.project import ManoProject, DEFAULT_PROJECT
 
 TEST_STRING = "foobar"
 
+
+class MockPublisher(object):
+    def __init__(self, uid):
+        self.assert_uid = uid
+
+    @asyncio.coroutine
+    def register_downloader(self, *args):
+        return self.assert_uid
+
+
+class MockProject(ManoProject):
+    def __init__(self, log, uid=None):
+        super().__init__(log, name=DEFAULT_PROJECT)
+        self.job_handler = MockPublisher(uid)
+
+
+class MockTasklet:
+    def __init__(self, log, uid=None):
+        self.log = log
+        self.projects = {}
+        project = MockProject(self.log,
+                              uid=uid)
+        project.publisher = None
+        self.projects[project.name] = project
+
+
 class TestCase(rift.test.dts.AbstractDTSTest):
     @classmethod
     def configure_schema(cls):
@@ -59,11 +90,12 @@ class TestCase(rift.test.dts.AbstractDTSTest):
     def tearDown(self):
         super().tearDown()
 
-    def create_mock_package(self):
+    def create_mock_package(self, project):
         uid = str(uuid.uuid4())
         path = os.path.join(
-                os.getenv('RIFT_ARTIFACTS'),
+                os.getenv('RIFT_VAR_ROOT'),
                 "launchpad/packages/vnfd",
+                project,
                 uid)
 
         asset_path = os.path.join(path, "icons")
@@ -80,13 +112,14 @@ class TestCase(rift.test.dts.AbstractDTSTest):
         Verifies the following:
             The endpoint RPC returns a URL
         """
-        proxy = filesystem.FileSystemProxy(self.loop, self.log)
+        proxy = filesystem.FileSystemProxy(self.loop, self.log, self.dts)
         endpoint = rpc.EndpointDiscoveryRpcHandler(self.log, self.dts, self.loop, proxy)
         yield from endpoint.register()
 
         ip = RwPkgMgmtYang.YangInput_RwPkgMgmt_GetPackageEndpoint.from_dict({
                 "package_type": "VNFD",
-                "package_id": "BLAHID"})
+                "package_id": "BLAHID",
+                "project_name": DEFAULT_PROJECT})
 
         rpc_out = yield from self.dts.query_rpc(
                     "I,/get-package-endpoint",
@@ -95,7 +128,7 @@ class TestCase(rift.test.dts.AbstractDTSTest):
 
         for itr in rpc_out:
             result = yield from itr
-            assert result.result.endpoint == 'https://127.0.0.1:4567/api/package/vnfd/BLAHID'
+            assert result.result.endpoint == 'https://127.0.0.1:8008/mano/api/package/vnfd/{}/BLAHID'.format(DEFAULT_PROJECT)
 
     @rift.test.dts.async_test
     def test_schema_rpc(self):
@@ -103,12 +136,13 @@ class TestCase(rift.test.dts.AbstractDTSTest):
         Verifies the following:
             The schema RPC return the schema structure
         """
-        proxy = filesystem.FileSystemProxy(self.loop, self.log)
+        proxy = filesystem.FileSystemProxy(self.loop, self.log, self.dts)
         endpoint = rpc.SchemaRpcHandler(self.log, self.dts, self.loop, proxy)
         yield from endpoint.register()
 
         ip = RwPkgMgmtYang.YangInput_RwPkgMgmt_GetPackageSchema.from_dict({
-                "package_type": "VNFD"})
+                "package_type": "VNFD",
+                "project_name": DEFAULT_PROJECT})
 
         rpc_out = yield from self.dts.query_rpc(
                     "I,/get-package-schema",
@@ -125,27 +159,24 @@ class TestCase(rift.test.dts.AbstractDTSTest):
             1. The file RPC returns a valid UUID thro' DTS
         """
         assert_uid = str(uuid.uuid4())
-        class MockPublisher:
-            @asyncio.coroutine
-            def register_downloader(self, *args):
-                return assert_uid
 
-        uid, path = self.create_mock_package()
+        uid, path = self.create_mock_package(DEFAULT_PROJECT)
 
-        proxy = filesystem.FileSystemProxy(self.loop, self.log)
+        proxy = filesystem.FileSystemProxy(self.loop, self.log, self.dts)
         endpoint = rpc.PackageOperationsRpcHandler(
             self.log,
             self.dts,
             self.loop,
             proxy,
-            MockPublisher())
+            MockTasklet(self.log, uid=assert_uid))
         yield from endpoint.register()
 
         ip = RwPkgMgmtYang.YangInput_RwPkgMgmt_PackageFileAdd.from_dict({
                 "package_type": "VNFD",
                 "package_id": uid,
                 "external_url": "https://raw.githubusercontent.com/RIFTIO/RIFT.ware/master/rift-shell",
-                "package_path": "script/rift-shell"})
+                "package_path": "script/rift-shell",
+                "project_name": DEFAULT_PROJECT})
 
         rpc_out = yield from self.dts.query_rpc(
                     "I,/rw-pkg-mgmt:package-file-add",
@@ -164,16 +195,19 @@ class TestCase(rift.test.dts.AbstractDTSTest):
             Integration test:
                 1. Verify the end to end flow of package ADD (NO MOCKS)
         """
-        uid, path = self.create_mock_package()
+        uid, path = self.create_mock_package(DEFAULT_PROJECT)
 
-        proxy = filesystem.FileSystemProxy(self.loop, self.log)
-        publisher = pkg_publisher.DownloadStatusPublisher(self.log, self.dts, self.loop)
+        proxy = filesystem.FileSystemProxy(self.loop, self.log, self.dts)
+        tasklet = MockTasklet(self.log, uid=uid)
+        project = tasklet.projects[DEFAULT_PROJECT]
+        publisher = pkg_publisher.DownloadStatusPublisher(self.log, self.dts, self.loop, project)
+        project.job_handler = publisher
         endpoint = rpc.PackageOperationsRpcHandler(
             self.log,
             self.dts,
             self.loop,
             proxy,
-            publisher)
+            tasklet)
 
         yield from publisher.register()
         yield from endpoint.register()
@@ -182,6 +216,7 @@ class TestCase(rift.test.dts.AbstractDTSTest):
                 "package_type": "VNFD",
                 "package_id": uid,
                 "external_url": "https://raw.githubusercontent.com/RIFTIO/RIFT.ware/master/rift-shell",
+                "project_name": DEFAULT_PROJECT,
                 "vnfd_file_type": "ICONS",
                 "package_path": "rift-shell"})
 
@@ -192,6 +227,7 @@ class TestCase(rift.test.dts.AbstractDTSTest):
 
         yield from asyncio.sleep(5, loop=self.loop)
         filepath = os.path.join(path, ip.vnfd_file_type.lower(), ip.package_path)
+        self.log.debug("Filepath: {}".format(filepath))
         assert os.path.isfile(filepath)
         mode = oct(os.stat(filepath)[stat.ST_MODE])
         assert str(mode) == "0o100664"
@@ -205,9 +241,9 @@ class TestCase(rift.test.dts.AbstractDTSTest):
             Integration test:
                 1. Verify the end to end flow of package ADD (NO MOCKS)
         """
-        uid, path = self.create_mock_package()
+        uid, path = self.create_mock_package(DEFAULT_PROJECT)
 
-        proxy = filesystem.FileSystemProxy(self.loop, self.log)
+        proxy = filesystem.FileSystemProxy(self.loop, self.log, self.dts)
         endpoint = rpc.PackageDeleteOperationsRpcHandler(
             self.log,
             self.dts,
@@ -219,8 +255,9 @@ class TestCase(rift.test.dts.AbstractDTSTest):
         ip = RwPkgMgmtYang.YangInput_RwPkgMgmt_PackageFileDelete.from_dict({
                 "package_type": "VNFD",
                 "package_id": uid,
+                "package_path": "logo.png",
                 "vnfd_file_type": "ICONS",
-                "package_path": "logo.png"})
+                "project_name": DEFAULT_PROJECT})
 
         assert os.path.isfile(os.path.join(path, ip.vnfd_file_type.lower(), ip.package_path))