def load_bundle_configuration_from_cache(cache_dir, uri): # When the supplied uri points to a local file, don't load from cache so file can be used as is. if is_local_file(uri): return False, None, None else: log = logging.getLogger(__name__) try: urn, org, repo, package_name, tag, digest = bundle_shorthand.parse_bundle_configuration( uri) log.info( log_message('Loading bundle configuration from cache', org, repo, package_name, tag, digest)) bintray_auth = load_bintray_credentials(raise_error=False) resolved_version = bintray_resolve_version(bintray_auth, org, repo, package_name, tag, digest) if resolved_version: return uri_resolver.load_bundle_from_cache( cache_dir, resolved_version['download_url']) else: return False, None, None except MalformedBundleUriError: return False, None, None except HTTPError as http_error: return handle_http_error(http_error, org, repo) except ConnectionError: return False, None, None
def test_file(self): exists_mock = MagicMock() with patch('os.path.exists', exists_mock): is_resolved, bundle_name, bundle_file = uri_resolver.load_bundle_from_cache('/cache-dir', '/tmp/bundle.zip') self.assertFalse(is_resolved) self.assertIsNone(bundle_name) self.assertIsNone(bundle_file) exists_mock.assert_not_called()
def test_uri_not_found(self): exists_mock = MagicMock(return_value=False) with patch('os.path.exists', exists_mock): is_resolved, bundle_name, bundle_file = uri_resolver.load_bundle_from_cache( '/cache-dir', 'http://site.com/path/bundle-file.zip') self.assertFalse(is_resolved) self.assertIsNone(bundle_name) self.assertIsNone(bundle_file) exists_mock.assert_called_with('/cache-dir/bundle-file.zip')
def test_file(self): exists_mock = MagicMock() with patch('os.path.exists', exists_mock): is_resolved, bundle_name, bundle_file = uri_resolver.load_bundle_from_cache( '/cache-dir', '/tmp/bundle.zip') self.assertFalse(is_resolved) self.assertIsNone(bundle_name) self.assertIsNone(bundle_file) exists_mock.assert_not_called()
def load_bundle_from_cache(cache_dir, uri): # When the supplied uri points to a local file, don't load from cache so file can be used as is. if is_local_file(uri): return False, None, None else: log = logging.getLogger(__name__) try: urn, org, repo, package_name, compatibility_version, digest = bundle_shorthand.parse_bundle(uri) log.info(log_message('Loading bundle from cache', org, repo, package_name, compatibility_version, digest)) bundle_download_url, auth = bintray_download_details(org, repo, package_name, compatibility_version, digest) if bundle_download_url: return uri_resolver.load_bundle_from_cache(cache_dir, bundle_download_url) else: return False, None, None except MalformedBundleUriError: return False, None, None except HTTPError: return False, None, None
def test_uri_found(self): exists_mock = MagicMock(return_value=True) get_logger_mock, log_mock = create_mock_logger() with patch('os.path.exists', exists_mock), \ patch('logging.getLogger', get_logger_mock): is_resolved, bundle_name, bundle_file = uri_resolver.load_bundle_from_cache( '/cache-dir', 'http://site.com/path/bundle-file.zip') self.assertTrue(is_resolved) self.assertEqual('bundle-file.zip', bundle_name) self.assertEqual('/cache-dir/bundle-file.zip', bundle_file) exists_mock.assert_called_with('/cache-dir/bundle-file.zip') get_logger_mock.assert_called_with('conductr_cli.resolvers.uri_resolver') log_mock.info.assert_called_with('Retrieving from cache /cache-dir/bundle-file.zip')
def test_uri_found(self): exists_mock = MagicMock(return_value=True) get_logger_mock, log_mock = create_mock_logger() with patch('os.path.exists', exists_mock), \ patch('logging.getLogger', get_logger_mock): is_resolved, bundle_name, bundle_file = uri_resolver.load_bundle_from_cache( '/cache-dir', 'http://site.com/path/bundle-file.zip') self.assertTrue(is_resolved) self.assertEqual('bundle-file.zip', bundle_name) self.assertEqual('/cache-dir/bundle-file.zip', bundle_file) exists_mock.assert_called_with('/cache-dir/bundle-file.zip') get_logger_mock.assert_called_with( 'conductr_cli.resolvers.uri_resolver') log_mock.info.assert_called_with( 'Retrieving from cache /cache-dir/bundle-file.zip')