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)
def post(self, **kwargs): t_start = time.time() args = packages_parser.parse_args() LOG.info("POST to /packages w. args: {}".format(args), extra={"start_stop": "START"}) if args.package.filename is None: LOG.warning("Posted package filename was None.") args.package.filename = "temp_pkg.tgo" temppkg_path = _write_to_temp_file(args.package) args.package = None args.unpackage = temppkg_path # pass CLI args to REST args args.offline = False args.no_checksums = False args.no_autoversion = False args.store_skip = False if app.cliargs is not None: args.output = None args.workspace = None args.offline = app.cliargs.offline args.no_checksums = app.cliargs.no_checksums args.no_autoversion = app.cliargs.skip_autoversion args.store_skip = app.cliargs.store_skip args.skip_validation = app.cliargs.skip_validation # select and instantiate storage backend sb = None if (not args.store_skip # from CLI and not args.skip_store # from request and not os.environ.get("STORE_SKIP", "False") == "True"): sb_env = os.environ.get("STORE_BACKEND", "TangoCatalogBackend") if sb_env == "TangoCatalogBackend": sb = TangoCatalogBackend(args) elif sb_env == "TangoProjectFilesystemBackend": sb = TangoProjectFilesystemBackend(args) elif sb_env == "OsmNbiBackend": sb = OsmNbiBackend(args) else: LOG.warning("Unknown storage backend: {}.".format(sb_env)) # instantiate packager p = PM.new_packager(args, storage_backend=sb) try: p.unpackage(callback_func=on_unpackaging_done) except BaseException as e: LOG.exception("Unpackaging error: {}".format(e)) LOG.info("POST to /packages done", extra={ "start_stop": "STOP", "status": p.status, "time_elapsed": str(time.time() - t_start) }) return { "package_process_uuid": str(p.uuid), "status": p.status, "error_msg": p.error_msg }
def test_package_unpackage_with_subfolder_compression(self): self.default_args = parse_args([]) self.default_args.package = misc_file( "mixed-ns-project-subfolder-test") self.default_args.output = os.path.join(tempfile.mkdtemp(), "test.tgo") pkg_path = self.default_args.output p = PM.new_packager(self.default_args, pkg_format="eu.5gtango") r = p._do_package() self.assertIsNone(r.error) # check *.tgo file self.assertTrue(os.path.exists(self.default_args.output)) pp = misc_file("mixed-ns-project-subfolder-test") subfolder_files = os.path.join(pp, "subfolder") subfolder_files = get_files(subfolder_files) tmp = tempfile.mkdtemp() with zipfile.ZipFile(pkg_path) as zf: zf.extract("subfolder.zip", path=tmp) with zipfile.ZipFile(os.path.join(tmp, "subfolder.zip")) as zf: names = zf.namelist() names = [os.path.basename(file) for file in names] for file in subfolder_files: self.assertIn(file, names) self.default_args = parse_args([]) self.default_args.unpackage = pkg_path self.default_args.output = tempfile.mkdtemp() self.p = PM.new_packager(self.default_args, pkg_format="eu.5gtango", storage_backend=TangoProjectFilesystemBackend( self.default_args)) r = self.p._do_unpackage() storage_location = r.metadata.get("_storage_location") self.assertIsNone(r.error) self.assertTrue(os.path.exists(storage_location), msg=storage_location) self.assertTrue(os.path.exists( os.path.join(storage_location, "subfolder")), msg=os.listdir(storage_location)) self.assertTrue( os.path.isdir(os.path.join(storage_location, "subfolder"))) names = get_files(os.path.join(storage_location, "subfolder")) for file in subfolder_files: self.assertIn(file, names)
def dispatch(args): # trigger pack/unpack if args.package: # instantiate packager p = PM.new_packager(args, pkg_format=args.pkg_format) p.package() LOG.debug("Packager result: {}".format(p.result)) display_result_package(args, p.result) elif args.unpackage: # select and instantiate storage backend # default in CLI mode: TangoProjectFilesystemBackend sb = None if (not args.store_skip and not os.environ.get("STORE_SKIP", "False") == "True"): sb_env = args.store_backend if sb_env is None: sb_env = os.environ.get("STORE_BACKEND", "TangoProjectFilesystemBackend") if sb_env == "TangoCatalogBackend": sb = TangoCatalogBackend(args) elif sb_env == "TangoProjectFilesystemBackend": sb = TangoProjectFilesystemBackend(args) elif sb_env == "OsmNbiBackend": sb = OsmNbiBackend(args) else: LOG.warning( "Unknown storage backend: {}. Stop.".format(sb_env)) exit(1) # instantiate packager p = PM.new_packager(args, storage_backend=sb) p.unpackage() LOG.debug("Packager result: {}".format(p.result)) display_result_unpackage(args, p.result) else: print("Missing arguments. Type tng-package -h.") exit(1) return p.result
def test_init(self): tpb = TangoProjectFilesystemBackend(self.default_args) self.assertIsNotNone(tpb)
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