def test_round_trip(self): # Setup units = [] manifest_path = os.path.join(self.tmp_dir, MANIFEST_FILE_NAME) for i in range(0, self.NUM_UNITS): unit = dict(unit_id=i, type_id='T', unit_key={}) units.append(unit) # Test units_path = os.path.join(self.tmp_dir, UNITS_FILE_NAME) writer = UnitWriter(units_path) for u in units: writer.add(u) writer.close() manifest = Manifest(self.MANIFEST_ID) manifest.set_units(writer) manifest.write(manifest_path) # Test cfg = DownloaderConfig() downloader = HTTPSCurlDownloader(cfg) working_dir = os.path.join(self.tmp_dir, 'working_dir') os.makedirs(working_dir) path = os.path.join(self.tmp_dir, MANIFEST_FILE_NAME) url = 'file://%s' % path manifest = Manifest() manifest.fetch(url, working_dir, downloader) manifest.fetch_units(url, downloader) # Verify units_in = [] for unit, ref in manifest.get_units(): units_in.append(unit) _unit = ref.fetch() self.assertEqual(unit, _unit) self.verify(units, units_in)
def test_publisher(self): # setup units = [] for n in range(0, 3): fn = 'test_%d' % n relative_path = os.path.join(self.RELATIVE_PATH, fn) path = os.path.join(self.unit_dir, relative_path) fp = open(path, 'w') fp.write(fn) fp.close() unit = { 'type_id': 'unit', 'unit_key': { 'n': n }, 'storage_path': path, 'relative_path': relative_path } units.append(unit) # test # publish repo_id = 'test_repo' base_url = 'file://' publish_dir = os.path.join(self.tmpdir, 'nodes/repos') virtual_host = (publish_dir, publish_dir) p = HttpPublisher(base_url, virtual_host, repo_id) p.publish(units) # verify conf = DownloaderConfig() downloader = HTTPSCurlDownloader(conf) manifest_path = p.manifest_path() working_dir = os.path.join(self.tmpdir, 'working_dir') os.makedirs(working_dir) manifest = Manifest() url = 'file://' + manifest_path manifest.fetch(url, working_dir, downloader) manifest.fetch_units(url, downloader) units = manifest.get_units() n = 0 for unit, ref in units: file_content = 'test_%d' % n _download = unit['_download'] url = _download['url'] self.assertEqual( url, '/'.join((base_url, publish_dir[1:], repo_id, unit['relative_path']))) path = url.split('//', 1)[1] self.assertTrue(os.path.islink(path)) f = open(path) s = f.read() f.close() self.assertEqual(s, file_content) self.assertEqual(unit['unit_key']['n'], n) n += 1
def test_publisher(self): # setup units = self.populate() # test # publish repo_id = 'test_repo' base_url = 'file://' publish_dir = os.path.join(self.tmpdir, 'nodes/repos') virtual_host = (publish_dir, publish_dir) with HttpPublisher(base_url, virtual_host, repo_id) as p: p.publish(units) p.commit() # verify conf = DownloaderConfig() downloader = HTTPSCurlDownloader(conf) manifest_path = p.manifest_path() working_dir = os.path.join(self.tmpdir, 'working_dir') os.makedirs(working_dir) manifest = Manifest() url = pathlib.url_join(base_url, manifest_path) manifest.fetch(url, working_dir, downloader) manifest.fetch_units(url, downloader) units = manifest.get_units() n = 0 for unit, ref in units: if n == 0: self.assertTrue(unit[constants.PUBLISHED_AS_TARBALL]) else: self.assertFalse( unit.get(constants.PUBLISHED_AS_TARBALL, False)) path = pathlib.join(publish_dir, repo_id, unit[constants.RELATIVE_PATH]) self.assertEqual(manifest.publishing_details[constants.BASE_URL], pathlib.url_join(base_url, publish_dir, repo_id)) if n == 0: self.assertTrue(os.path.isfile(path)) else: self.assertTrue(os.path.islink(path)) if n == 0: tb = tarfile.open(path) try: files = sorted(tb.getnames()) finally: tb.close() self.assertEqual(len(files), self.NUM_TARED_FILES + 1) else: with open(path, 'rb') as fp: unit_content = fp.read() self.assertEqual(unit_content, unit_content) self.assertEqual(unit['unit_key']['n'], n) n += 1
def _downloader(self, config): """ Get a configured downloader. The integration between the importer configuration and the download package happens here. The https downloader may be used for both http and https so always chosen for simplicity. :param config: The importer configuration. :param config: pulp.plugins.config.PluginCallConfiguration :return: A configured downloader :rtype: nectar.downloaders.base.Downloader """ ssl = config.get(constants.SSL_KEYWORD, {}) conf = DownloaderConfig(max_concurrent=MAX_CONCURRENCY, ssl_ca_cert_path=self._safe_str( ssl.get(constants.CA_CERT_KEYWORD)), ssl_client_cert_path=self._safe_str( ssl.get(constants.CLIENT_CERT_KEYWORD)), ssl_validation=False) downloader = HTTPSCurlDownloader(conf) return downloader