Ejemplo n.º 1
0
    def test_resolve_not_found(self):
        os_path_exists_mock = MagicMock(side_effect=[True, False])
        cache_path_mock = MagicMock(return_value='/bundle-cached-path')
        urlretrieve_mock = MagicMock(side_effect=URLError('no_such.bundle'))
        get_url_mock = MagicMock(return_value=('bundle-name',
                                               '/bundle-url-resolved'))

        get_logger_mock, log_mock = create_mock_logger()

        with patch('os.path.exists', os_path_exists_mock), \
                patch('conductr_cli.resolvers.uri_resolver.cache_path', cache_path_mock), \
                patch('conductr_cli.resolvers.uri_resolver.get_url', get_url_mock), \
                patch('conductr_cli.resolvers.uri_resolver.urlretrieve', urlretrieve_mock), \
                patch('logging.getLogger', get_logger_mock):
            is_resolved, bundle_name, bundle_file = uri_resolver.resolve_bundle(
                '/cache-dir', '/bundle-url')
            self.assertFalse(is_resolved)
            self.assertIsNone(bundle_name)
            self.assertIsNone(bundle_file)

        self.assertEqual([call('/cache-dir'),
                          call('/bundle-cached-path.tmp')],
                         os_path_exists_mock.call_args_list)
        cache_path_mock.assert_called_with('/cache-dir', '/bundle-url')
        get_url_mock.assert_called_with('/bundle-url')
        urlretrieve_mock.assert_called_with('/bundle-url-resolved',
                                            '/bundle-cached-path.tmp')

        get_logger_mock.assert_called_with(
            'conductr_cli.resolvers.uri_resolver')
        log_mock.info.assert_called_with('Retrieving /bundle-url-resolved')
Ejemplo n.º 2
0
def resolve_bundle(cache_dir, uri):
    log = logging.getLogger(__name__)
    try:
        bintray_username, bintray_password = load_bintray_credentials()
        urn, org, repo, package_name, compatibility_version, digest = bundle_shorthand.parse(
            uri)
        log.info(
            log_message('Resolving bundle', org, repo, package_name,
                        compatibility_version, digest))
        bundle_download_url = bintray_download_url(bintray_username,
                                                   bintray_password, org, repo,
                                                   package_name,
                                                   compatibility_version,
                                                   digest)
        if bundle_download_url:
            auth = (BINTRAY_DOWNLOAD_REALM, bintray_username,
                    bintray_password) if bintray_username else None
            return uri_resolver.resolve_bundle(cache_dir, bundle_download_url,
                                               auth)
        else:
            return False, None, None
    except MalformedBundleUriError:
        return False, None, None
    except HTTPError:
        return False, None, None
Ejemplo n.º 3
0
    def test_resolve_not_found(self):
        os_path_exists_mock = MagicMock(side_effect=[True, False])
        cache_path_mock = MagicMock(return_value='/bundle-cached-path')
        urlretrieve_mock = MagicMock(side_effect=URLError('no_such.bundle'))
        get_url_mock = MagicMock(return_value=('bundle-name', '/bundle-url-resolved'))

        get_logger_mock, log_mock = create_mock_logger()

        with patch('os.path.exists', os_path_exists_mock), \
                patch('conductr_cli.resolvers.uri_resolver.cache_path', cache_path_mock), \
                patch('conductr_cli.resolvers.uri_resolver.get_url', get_url_mock), \
                patch('conductr_cli.resolvers.uri_resolver.urlretrieve', urlretrieve_mock), \
                patch('logging.getLogger', get_logger_mock):
            is_resolved, bundle_name, bundle_file = uri_resolver.resolve_bundle('/cache-dir', '/bundle-url')
            self.assertFalse(is_resolved)
            self.assertIsNone(bundle_name)
            self.assertIsNone(bundle_file)

        self.assertEqual([
            call('/cache-dir'),
            call('/bundle-cached-path.tmp')
        ], os_path_exists_mock.call_args_list)
        cache_path_mock.assert_called_with('/cache-dir', '/bundle-url')
        get_url_mock.assert_called_with('/bundle-url')
        urlretrieve_mock.assert_called_with('/bundle-url-resolved', '/bundle-cached-path.tmp')

        get_logger_mock.assert_called_with('conductr_cli.resolvers.uri_resolver')
        log_mock.info.assert_called_with('Retrieving /bundle-url-resolved')
Ejemplo n.º 4
0
    def test_show_progress_bar(self):
        os_path_exists_mock = MagicMock(side_effect=[True, True])
        os_remove_mock = MagicMock()
        os_chmod_mock = MagicMock()
        file_move_mock = MagicMock()
        cache_path_mock = MagicMock(return_value='/bundle-cached-path')
        get_url_mock = MagicMock(
            return_value=('bundle-name',
                          'http://site.com/bundle-url-resolved'))
        urlretrieve_mock = MagicMock()
        report_hook_mock = MagicMock()
        show_progress_mock = MagicMock(return_value=report_hook_mock)

        get_logger_mock, log_mock = create_mock_logger()

        with patch('os.path.exists', os_path_exists_mock), \
                patch('os.remove', os_remove_mock), \
                patch('os.chmod', os_chmod_mock), \
                patch('shutil.move', file_move_mock), \
                patch('conductr_cli.resolvers.uri_resolver.cache_path', cache_path_mock), \
                patch('conductr_cli.resolvers.uri_resolver.get_url', get_url_mock), \
                patch('conductr_cli.resolvers.uri_resolver.urlretrieve', urlretrieve_mock), \
                patch('conductr_cli.resolvers.uri_resolver.show_progress', show_progress_mock), \
                patch('logging.getLogger', get_logger_mock):
            is_resolved, bundle_name, bundle_file = uri_resolver.resolve_bundle(
                '/cache-dir', 'http://site.com/bundle-url')
            self.assertTrue(is_resolved)
            self.assertEqual('bundle-name', bundle_name)
            self.assertEqual('/bundle-cached-path', bundle_file)

        self.assertEqual([call('/cache-dir'),
                          call('/bundle-cached-path.tmp')],
                         os_path_exists_mock.call_args_list)
        cache_path_mock.assert_called_with('/cache-dir',
                                           'http://site.com/bundle-url')
        get_url_mock.assert_called_with('http://site.com/bundle-url')
        os_remove_mock.assert_called_with('/bundle-cached-path.tmp')
        urlretrieve_mock.assert_called_with(
            'http://site.com/bundle-url-resolved',
            '/bundle-cached-path.tmp',
            reporthook=report_hook_mock)
        file_move_mock.assert_called_with('/bundle-cached-path.tmp',
                                          '/bundle-cached-path')

        get_logger_mock.assert_called_with(
            'conductr_cli.resolvers.uri_resolver')
        log_mock.info.assert_called_with(
            'Retrieving http://site.com/bundle-url-resolved')
Ejemplo n.º 5
0
    def test_show_progress_bar(self):
        os_path_exists_mock = MagicMock(side_effect=[True, True])
        os_remove_mock = MagicMock()
        os_chmod_mock = MagicMock()
        file_move_mock = MagicMock()
        cache_path_mock = MagicMock(return_value='/bundle-cached-path')
        get_url_mock = MagicMock(return_value=('bundle-name', 'http://site.com/bundle-url-resolved'))
        urlretrieve_mock = MagicMock()
        report_hook_mock = MagicMock()
        show_progress_mock = MagicMock(return_value=report_hook_mock)

        get_logger_mock, log_mock = create_mock_logger()

        with patch('os.path.exists', os_path_exists_mock), \
                patch('os.remove', os_remove_mock), \
                patch('os.chmod', os_chmod_mock), \
                patch('shutil.move', file_move_mock), \
                patch('conductr_cli.resolvers.uri_resolver.cache_path', cache_path_mock), \
                patch('conductr_cli.resolvers.uri_resolver.get_url', get_url_mock), \
                patch('conductr_cli.resolvers.uri_resolver.urlretrieve', urlretrieve_mock), \
                patch('conductr_cli.resolvers.uri_resolver.show_progress', show_progress_mock), \
                patch('logging.getLogger', get_logger_mock):
            is_resolved, bundle_name, bundle_file = uri_resolver.resolve_bundle('/cache-dir',
                                                                                'http://site.com/bundle-url')
            self.assertTrue(is_resolved)
            self.assertEqual('bundle-name', bundle_name)
            self.assertEqual('/bundle-cached-path', bundle_file)

        self.assertEqual([
            call('/cache-dir'),
            call('/bundle-cached-path.tmp')
        ], os_path_exists_mock.call_args_list)
        cache_path_mock.assert_called_with('/cache-dir', 'http://site.com/bundle-url')
        get_url_mock.assert_called_with('http://site.com/bundle-url')
        os_remove_mock.assert_called_with('/bundle-cached-path.tmp')
        urlretrieve_mock.assert_called_with('http://site.com/bundle-url-resolved',
                                            '/bundle-cached-path.tmp', reporthook=report_hook_mock)
        file_move_mock.assert_called_with('/bundle-cached-path.tmp', '/bundle-cached-path')

        get_logger_mock.assert_called_with('conductr_cli.resolvers.uri_resolver')
        log_mock.info.assert_called_with('Retrieving http://site.com/bundle-url-resolved')
Ejemplo n.º 6
0
    def test_resolve_success_create_cache_dir(self):
        os_path_exists_mock = MagicMock(side_effect=[False, False])
        file_move_mock = MagicMock()
        os_chmod_mock = MagicMock()
        os_mkdirs_mock = MagicMock(return_value=())
        cache_path_mock = MagicMock(return_value='/bundle-cached-path')
        get_url_mock = MagicMock(return_value=('bundle-name',
                                               '/bundle-url-resolved'))
        urlretrieve_mock = MagicMock()

        get_logger_mock, log_mock = create_mock_logger()

        with patch('os.path.exists', os_path_exists_mock), \
                patch('os.makedirs', os_mkdirs_mock), \
                patch('os.chmod', os_chmod_mock), \
                patch('shutil.move', file_move_mock), \
                patch('conductr_cli.resolvers.uri_resolver.cache_path', cache_path_mock), \
                patch('conductr_cli.resolvers.uri_resolver.get_url', get_url_mock), \
                patch('conductr_cli.resolvers.uri_resolver.urlretrieve', urlretrieve_mock), \
                patch('logging.getLogger', get_logger_mock):
            is_resolved, bundle_name, bundle_file = uri_resolver.resolve_bundle(
                '/cache-dir', '/bundle-url')
            self.assertTrue(is_resolved)
            self.assertEqual('bundle-name', bundle_name)
            self.assertEqual('/bundle-cached-path', bundle_file)

        self.assertEqual([call('/cache-dir'),
                          call('/bundle-cached-path.tmp')],
                         os_path_exists_mock.call_args_list)
        os_mkdirs_mock.assert_called_with('/cache-dir', mode=448)
        cache_path_mock.assert_called_with('/cache-dir', '/bundle-url')
        get_url_mock.assert_called_with('/bundle-url')
        urlretrieve_mock.assert_called_with('/bundle-url-resolved',
                                            '/bundle-cached-path.tmp')
        file_move_mock.assert_called_with('/bundle-cached-path.tmp',
                                          '/bundle-cached-path')

        get_logger_mock.assert_called_with(
            'conductr_cli.resolvers.uri_resolver')
        log_mock.info.assert_called_with('Retrieving /bundle-url-resolved')
Ejemplo n.º 7
0
    def test_resolve_success_create_cache_dir(self):
        os_path_exists_mock = MagicMock(side_effect=[False, False])
        file_move_mock = MagicMock()
        os_chmod_mock = MagicMock()
        os_mkdirs_mock = MagicMock(return_value=())
        cache_path_mock = MagicMock(return_value='/bundle-cached-path')
        get_url_mock = MagicMock(return_value=('bundle-name', '/bundle-url-resolved'))
        urlretrieve_mock = MagicMock()

        get_logger_mock, log_mock = create_mock_logger()

        with patch('os.path.exists', os_path_exists_mock), \
                patch('os.makedirs', os_mkdirs_mock), \
                patch('os.chmod', os_chmod_mock), \
                patch('shutil.move', file_move_mock), \
                patch('conductr_cli.resolvers.uri_resolver.cache_path', cache_path_mock), \
                patch('conductr_cli.resolvers.uri_resolver.get_url', get_url_mock), \
                patch('conductr_cli.resolvers.uri_resolver.urlretrieve', urlretrieve_mock), \
                patch('logging.getLogger', get_logger_mock):
            is_resolved, bundle_name, bundle_file = uri_resolver.resolve_bundle('/cache-dir', '/bundle-url')
            self.assertTrue(is_resolved)
            self.assertEqual('bundle-name', bundle_name)
            self.assertEqual('/bundle-cached-path', bundle_file)

        self.assertEqual([
            call('/cache-dir'),
            call('/bundle-cached-path.tmp')
        ], os_path_exists_mock.call_args_list)
        os_mkdirs_mock.assert_called_with('/cache-dir', mode=448)
        cache_path_mock.assert_called_with('/cache-dir', '/bundle-url')
        get_url_mock.assert_called_with('/bundle-url')
        urlretrieve_mock.assert_called_with('/bundle-url-resolved', '/bundle-cached-path.tmp')
        file_move_mock.assert_called_with('/bundle-cached-path.tmp', '/bundle-cached-path')

        get_logger_mock.assert_called_with('conductr_cli.resolvers.uri_resolver')
        log_mock.info.assert_called_with('Retrieving /bundle-url-resolved')
Ejemplo n.º 8
0
def bintray_download(cache_dir, 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.resolve_bundle(cache_dir, bundle_download_url, auth)
    else:
        return False, None, None