def test_store(self):
     # instantiate storage backend
     tpb = TangoProjectFilesystemBackend(self.default_args)
     self.assertIsNotNone(tpb)
     # unpack package and keep active working dir.
     napdr = self.p._do_unpackage()
     wd = napdr.metadata.get("_napd_path").replace(
         "/TOSCA-Metadata/NAPD.yaml", "")
     # call store using active working dir
     new_napdr = tpb.store(napdr, wd, self.default_args.unpackage)
     # check result
     self.assertIsNotNone(new_napdr.metadata.get("_storage_location"))
     sl = new_napdr.metadata.get("_storage_location")
     # check created project
     pd = self.default_args.output
     self.assertTrue(os.path.exists(pd))
     self.assertTrue(
         os.path.exists(
             os.path.join(pd, "5gtango-ns-package-example/project.yml")))
     self.assertTrue(os.path.exists(os.path.join(sl, "project.yml")))
     self.assertTrue(os.path.exists(os.path.join(sl, "sources/")))
     self.assertTrue(
         os.path.exists(os.path.join(sl, "sources/nsd/nsd-sample.yml")))
     self.assertTrue(
         os.path.exists(os.path.join(sl, "sources/vnfd/vnfd-sample.yml")))
     shutil.rmtree(pd)
    def test_store_idempotent(self):
        self.default_args = parse_args(["-o", tempfile.mkdtemp()])
        self.default_args.unpackage = misc_file(
            "eu.5gtango.idempotency_test.0.1.tgo")
        self.p = PM.new_packager(self.default_args,
                                 pkg_format="eu.5gtango",
                                 storage_backend=None)
        tpb = TangoProjectFilesystemBackend(self.default_args)

        napdr = self.p._do_unpackage()
        wd = napdr.metadata.get("_napd_path").replace(
            "/TOSCA-Metadata/NAPD.yaml", "")
        # call store using active working dir
        new_napdr = tpb.store(napdr, wd, self.default_args.unpackage)
        # check result
        self.assertIsNotNone(new_napdr.metadata.get("_storage_location"))
        sl = new_napdr.metadata.get("_storage_location")
        # check created project
        pd = self.default_args.output
        self.assertTrue(os.path.exists(pd))
        self.assertTrue(
            os.path.exists(
                os.path.join(pd,
                             "eu.5gtango.idempotency_test.0.1/project.yml")))
        self.assertTrue(os.path.exists(os.path.join(sl, "project.yml")))
        # files in root dir
        self.assertTrue(os.path.exists(os.path.join(sl, "vnfd-a10-3.yml")))
        self.assertTrue(os.path.exists(os.path.join(sl, "vnfd-nginx-3.yml")))
        # files in sources/
        self.assertTrue(os.path.exists(os.path.join(sl, "sources/")))
        self.assertTrue(
            os.path.exists(os.path.join(sl, "sources/vnfd-a10-4.yml")))
        self.assertTrue(
            os.path.exists(os.path.join(sl, "sources/vnfd-nginx-4.yml")))
        # files in sources/[...]/
        self.assertTrue(os.path.exists(os.path.join(sl,
                                                    "sources/nsd/nsd.yml")))
        self.assertTrue(
            os.path.exists(os.path.join(sl, "sources/vnfd/vnfd-a10.yml")))
        self.assertTrue(
            os.path.exists(os.path.join(sl, "sources/vnfd/vnfd-nginx.yml")))
        # files in other folders of root dir
        self.assertTrue(os.path.exists(os.path.join(sl, "Definitions/")))
        self.assertTrue(
            os.path.exists(os.path.join(sl, "Definitions/vnfd-a10-2.yml")))
        self.assertTrue(
            os.path.exists(os.path.join(sl, "Definitions/vnfd-nginx-2.yml")))
        shutil.rmtree(pd)
예제 #3
0
    def _do_unpackage(self, wd=None):
        """
        Unpack a 5GTANGO package.
        """
        # TODO re-factor: single try block with multiple excepts.
        # extract package contents
        if wd is None:
            wd = extract_zip_file_to_temp(self.args.unpackage)
        # fuzzy find right wd path
        wd = fuzzy_find_wd(wd)
        # collect metadata
        napdr = None
        try:
            napdr = self.collect_metadata(wd)
        except BaseException as e:
            LOG.error(str(e))
            self.error_msg = str(e)
            return NapdRecord(error=str(e))
        # LOG.debug("Collected metadata: {}".format(napdr))
        # validate metadata
        try:
            self._assert_usable_tango_package(napdr)
        except MetadataValidationException as e:
            LOG.error(str(e))
            self.error_msg = str(e)
            napdr.error = str(e)
            return napdr
        # validate checksums
        try:
            self._validate_package_content_checksums(wd, napdr)
        except ChecksumException as e:
            LOG.error(str(e))
            self.error_msg = str(e)
            napdr.error = str(e)
            return napdr
        except MissingFileException as e:
            LOG.error(str(e))
            self.error_msg = str(e)
            napdr.error = str(e)
            return napdr
        # validate network service using tng-validate
        try:
            # we do a trick here, since tng-validate needs a
            # 5GTANGO project strcuture to work on, and we not
            # always use the 5GTANGO project storage backend:
            # Solution: we store it to a temporary 5GTANGO project
            # only used for the validation step.
            if self.args.skip_validation:
                LOG.warning("Skipping validation (--skip-validation).")
            else:  # ok, do the validation
                tmp_project_path = tempfile.mkdtemp()
                tmp_tpfbe = TangoProjectFilesystemBackend(self.args)
                tmp_napdr = tmp_tpfbe.store(napdr,
                                            wd,
                                            self.args.unpackage,
                                            output=tmp_project_path)
                tmp_project_path = tmp_napdr.metadata["_storage_location"]
                validate_project_with_external_validator(
                    self.args, tmp_project_path)
                shutil.rmtree(tmp_project_path)
        except BaseException as e:
            LOG.exception(str(e))
            self.error_msg = str(e)
            napdr.error = str(e)
            return napdr
        # call storage backend
        if self.storage_backend is not None:
            try:
                # store/upload contents of package and get updated napdr
                napdr = self.storage_backend.store(napdr, wd,
                                                   self.args.unpackage)
            except BaseException as e:
                LOG.error(str(e))
                LOG.debug("Args: {}".format(self.args))
                self.error_msg = str(e)
                napdr.error = str(e)
                return napdr

        # TODO clean up temporary files and folders
        return napdr