def test_simple_no_webservice_file(self): # Given fake_index = { "zope.testing-3.8.3-1.egg": { "available": True, "build": 1, "md5": "6041fd75b7fe9187ccef0d40332c6c16", "mtime": 1262725254.0, "name": "zope.testing", "product": "commercial", "python": "2.6", "size": 514439, "type": "egg", "version": "3.8.3", } } index_path = os.path.join(self.tempdir, "index.json") with open(index_path, "w") as fp: fp.write(json.dumps(fake_index)) # When session = mocked_session_factory(self.tempdir) resp = session.fetch(path_to_uri(index_path)) packages = list(parse_index(resp.json(), "", python_version="2.6")) # Then self.assertEqual(len(packages), 1) self.assertEqual(packages[0].key, "zope.testing-3.8.3-1.egg")
def test_simple_no_webservice_file(self): # Given fake_index = { "zope.testing-3.8.3-1.egg": { "available": True, "build": 1, "md5": "6041fd75b7fe9187ccef0d40332c6c16", "mtime": 1262725254.0, "name": "zope.testing", "product": "commercial", "python": "2.6", "size": 514439, "type": "egg", "version": "3.8.3", } } index_path = os.path.join(self.tempdir, "index.json") with open(index_path, "w") as fp: fp.write(json.dumps(fake_index)) # When session = mocked_session_factory(self.tempdir) resp = session.fetch(path_to_uri(index_path)) packages = list(parse_index(resp.json(), "", python_version="2.6")) # Then self.assertEqual(len(packages), 1) self.assertEqual(packages[0].key, "zope.testing-3.8.3-1.egg")
def test_repository_factory(self): self.maxDiff = None # Given store_url = "https://acme.com" config = Configuration(store_url=store_url, use_webservice=False) config.set_repositories_from_names(["enthought/foo"]) responses.add(responses.GET, config.indices[0][0], status=404) session = mocked_session_factory(self.tempdir) r_warning = textwrap.dedent("""\ Warning: Could not fetch the following indices: - 'neko' Those repositories do not exist (or you do not have the rights to access them). You should edit your configuration to remove those repositories. """) # When with mock_print() as m: with mock.patch("enstaller.cli.utils._display_store_name", return_value="neko"): repository = repository_factory(session, config.indices) # Then self.assertEqual(len(list(repository.iter_packages())), 0) self.assertMultiLineEqual(m.value, r_warning) # When/Then with self.assertRaises(requests.exceptions.HTTPError): repository_factory(session, config.indices, raise_on_error=True)
def test_repository_factory(self): # Given config = Configuration() session = mocked_session_factory(self.tempdir) # When repository = repository_factory(session, config.indices) # Then self.assertEqual(len(list(repository.iter_packages())), 1) self.assertEqual(len(list(repository.find_packages("nose", "1.3.4-1"))), 1)
def test_repository_factory(self): # Given config = Configuration() session = mocked_session_factory(self.tempdir) # When repository = repository_factory(session, config.indices) # Then self.assertEqual(len(list(repository.iter_packages())), 1) self.assertEqual( len(list(repository.find_packages("nose", "1.3.4-1"))), 1)
def test_etag(self): # Given config = Configuration() session = mocked_session_factory(config.repository_cache) # When with session.etag(): pass # Then self.assertFalse( isinstance(session._raw.adapters["http://"], CacheControlAdapter)) self.assertFalse( isinstance(session._raw.adapters["https://"], CacheControlAdapter))
def test_fetch_invalid_md5(self): # Given filename = "nose-1.3.0-1.egg" path = os.path.join(_EGGINST_COMMON_DATA, filename) repository = Repository() package = RepositoryPackageMetadata.from_egg(path) package.md5 = "a" * 32 repository.add_package(package) downloader = _DownloadManager(mocked_session_factory(self.tempdir), repository) with self.assertRaises(InvalidChecksum): downloader.fetch(filename)
def test_etag(self): # Given config = Configuration() session = mocked_session_factory(config.repository_cache) # When with session.etag(): pass # Then self.assertFalse(isinstance(session._raw.adapters["http://"], CacheControlAdapter)) self.assertFalse(isinstance(session._raw.adapters["https://"], CacheControlAdapter))
def test_fetch_egg_refetch(self): # Given egg = "nose-1.3.0-1.egg" repository = self._create_store_and_repository([egg]) # When downloader = _DownloadManager(mocked_session_factory(self.tempdir), repository) downloader.fetch(egg) # Then target = os.path.join(self.tempdir, egg) self.assertTrue(os.path.exists(target))
def test_fetch_simple(self): # Given filename = "nose-1.3.0-1.egg" repository = self._create_store_and_repository([filename]) downloader = _DownloadManager(mocked_session_factory(self.tempdir), repository) downloader.fetch(filename) # Then target = os.path.join(self.tempdir, filename) self.assertTrue(os.path.exists(target)) self.assertEqual(compute_md5(target), repository.find_package("nose", "1.3.0-1").md5)
def test_nested_etag(self): # Given session = mocked_session_factory(self.prefix) # When/Then with session.etag(): self._assert_use_etag_cache_controller(session) with session.etag(): self._assert_use_etag_cache_controller(session) self._assert_use_etag_cache_controller(session) with session.etag(): self._assert_use_etag_cache_controller(session) with session.etag(): self._assert_use_etag_cache_controller(session) self._assert_use_etag_cache_controller(session) self._assert_use_default_adapter(session)
def test_nested_etag(self): # Given session = mocked_session_factory(self.prefix) # When/Then with session.etag(): self._assert_use_etag_cache_controller(session) with session.etag(): self._assert_use_etag_cache_controller(session) self._assert_use_etag_cache_controller(session) with session.etag(): self._assert_use_etag_cache_controller(session) with session.etag(): self._assert_use_etag_cache_controller(session) self._assert_use_etag_cache_controller(session) self._assert_use_default_adapter(session)
def test_multiple_etag(self): # Given config = Configuration() session = mocked_session_factory(config.repository_cache) # When with mock.patch("enstaller.session.CacheControlAdapter") as m: with session.etag(): pass with session.etag(): pass # Then self.assertFalse(isinstance(session._raw.adapters["http://"], CacheControlAdapter)) self.assertFalse(isinstance(session._raw.adapters["https://"], CacheControlAdapter)) self.assertEqual(m.call_count, 4)
def test_multiple_etag(self): # Given config = Configuration() session = mocked_session_factory(config.repository_cache) # When with mock.patch("enstaller.session.CacheControlAdapter") as m: with session.etag(): pass with session.etag(): pass # Then self.assertFalse( isinstance(session._raw.adapters["http://"], CacheControlAdapter)) self.assertFalse( isinstance(session._raw.adapters["https://"], CacheControlAdapter)) self.assertEqual(m.call_count, 4)
def test_fetch_abort(self): # Given filename = "nose-1.3.0-1.egg" repository = self._create_store_and_repository([filename]) downloader = _DownloadManager(mocked_session_factory(self.tempdir), repository) target = os.path.join(self.tempdir, filename) # When context = downloader.iter_fetch(filename) for i, chunk in enumerate(context): if i == 1: context.cancel() break # Then self.assertFalse(os.path.exists(target))
def test_fetch_unauthorized(self): # Given filename = "nose-1.3.0-1.egg" url = "http://api.enthought.com/eggs/yoyo/" repository = Repository() path = os.path.join(_EGGINST_COMMON_DATA, filename) package = RepositoryPackageMetadata.from_egg(path, url) repository.add_package(package) responses.add(responses.GET, url + filename, body='{"error": "forbidden"}', status=403) downloader = _DownloadManager(mocked_session_factory(self.tempdir), repository) # When/Then with self.assertRaises(requests.exceptions.HTTPError): downloader.fetch(filename)
def test_install_not_available(self): # Given config = Configuration() config.update(auth=FAKE_AUTH) nose = dummy_repository_package_factory("nose", "1.3.0", 1) nose.available = False repository = Repository() repository.add_package(nose) enpkg = Enpkg(repository, mocked_session_factory(config.repository_cache), [self.prefix]) enpkg.execute = mock.Mock() # When/Then with mock.patch("enstaller.config.subscription_message") as \ subscription_message: with self.assertRaises(SystemExit) as e: install_req(enpkg, config, "nose", FakeOptions()) subscription_message.assert_called() self.assertEqual(e.exception.code, 1)
def test_install_not_available(self): # Given config = Configuration() config.update(auth=FAKE_AUTH) nose = dummy_repository_package_factory("nose", "1.3.0", 1) nose.available = False repository = Repository() repository.add_package(nose) enpkg = Enpkg(repository, mocked_session_factory(config.repository_cache), [self.prefix]) enpkg.execute = mock.Mock() # When/Then with mock.patch("enstaller.config.subscription_message") as \ subscription_message: with self.assertRaises(SystemExit) as e: install_req(enpkg, config, "nose", FakeOptions()) subscription_message.assert_called() self.assertEqual(e.exception.code, 1)
def test_fetch_egg_refetch_invalid_md5(self): # Given egg = "nose-1.3.0-1.egg" path = os.path.join(_EGGINST_COMMON_DATA, egg) repository = self._create_store_and_repository([egg]) package = repository.find_package("nose", "1.3.0-1") def _corrupt_file(target): with open(target, "wb") as fo: fo.write(b"") # When downloader = _DownloadManager(mocked_session_factory(self.tempdir), repository) downloader.fetch(package) # Then target = os.path.join(self.tempdir, egg) self.assertEqual(compute_md5(target), compute_md5(path)) # When _corrupt_file(target) # Then self.assertNotEqual(compute_md5(target), compute_md5(path)) # When downloader.fetch(package, force=True) # Then self.assertEqual(compute_md5(target), compute_md5(path)) # When/Then # Ensure we deal correctly with force=False when the egg is already # there. downloader.fetch(package, force=False)
def test_fetch_egg_refetch_invalid_md5(self): # Given egg = "nose-1.3.0-1.egg" path = os.path.join(_EGGINST_COMMON_DATA, egg) repository = self._create_store_and_repository([egg]) def _corrupt_file(target): with open(target, "wb") as fo: fo.write(b"") # When downloader = _DownloadManager(mocked_session_factory(self.tempdir), repository) downloader.fetch(egg) # Then target = os.path.join(self.tempdir, egg) self.assertEqual(compute_md5(target), compute_md5(path)) # When _corrupt_file(target) # Then self.assertNotEqual(compute_md5(target), compute_md5(path)) # When downloader.fetch(egg, force=True) # Then self.assertEqual(compute_md5(target), compute_md5(path)) # When/Then # Ensure we deal correctly with force=False when the egg is already # there. downloader.fetch(egg, force=False)
def test_repository_factory(self): self.maxDiff = None # Given store_url = "https://acme.com" config = Configuration(store_url=store_url, use_webservice=False) config.set_repositories_from_names(["enthought/foo"]) responses.add(responses.GET, config.indices[0][0], status=404) session = mocked_session_factory(self.tempdir) r_warning = textwrap.dedent("""\ Warning: Could not fetch the following indices: - 'neko' Those repositories do not exist (or you do not have the rights to access them). You should edit your configuration to remove those repositories. """) # When with mock_print() as m: with mock.patch("enstaller.cli.utils._display_store_name", return_value="neko"): repository = repository_factory(session, config.indices) # Then self.assertEqual(len(list(repository.iter_packages())), 0) self.assertMultiLineEqual(m.value, r_warning) # When/Then with self.assertRaises(requests.exceptions.HTTPError): repository_factory(session, config.indices, raise_on_error=True)