def test_index_missing_url(bundle_archive):
    cut = HTTPBundleLoader('index_url')
    with successful_get(
        {'test_bundle': {
            '1': {
                'hashes': {
                    'sha224': 'deadbeef'
                }
            }
        }}):
        with pytest.raises(LoadFailed, match=re.compile('valid.*url', re.I)):
            cut.load('test_bundle')
def test_load_cachedir(bundle_archive, tempdir):
    cut = HTTPBundleLoader('index_url', cachedir=tempdir)
    cut.base_directory = 'bdir'
    with open(bundle_archive.archive_path, 'rb') as bf:
        bundle_hash = hashlib.sha224(bf.read()).hexdigest()
    with successful_get({'test_bundle': {'1': {'url': 'http://some_host',
                                               'hashes': {'sha224': bundle_hash}}}}) as get, \
            patch('owmeta_core.bundle.loaders.http.Unarchiver') as Unarchiver:
        with open(bundle_archive.archive_path, 'rb') as bf:
            get().iter_content.return_value = [bf.read()]
        cut.load('test_bundle')
        Unarchiver().unpack.assert_called_with(ANY, 'bdir')
def test_load_no_cachedir():
    from io import BytesIO
    cut = HTTPBundleLoader('index_url')
    cut.base_directory = 'bdir'
    bundle_contents = b'bytes bytes bytes'
    bundle_hash = hashlib.sha224(bundle_contents).hexdigest()
    with successful_get({'test_bundle': {'1': {'url': 'http://some_host',
                                               'hashes': {'sha224': bundle_hash}}}}) as get, \
            patch('owmeta_core.bundle.loaders.http.Unarchiver') as Unarchiver:
        get().raw.read.return_value = bundle_contents
        cut.load('test_bundle')
        Unarchiver().unpack.assert_called_with(
            MatchingBytesIO(BytesIO(b'bytes bytes bytes')), 'bdir')
def test_load_fail_no_valid_bundle_url():
    cut = HTTPBundleLoader('index_url')
    with successful_get({'test_bundle': {'1': {'url': 'down'}}}):
        with pytest.raises(LoadFailed, match=re.compile('valid url', re.I)):
            cut.load('test_bundle')
def test_load_fail_no_valid_release_number():
    cut = HTTPBundleLoader('index_url')
    with successful_get({'test_bundle': {'smack': 'down'}}):
        with pytest.raises(LoadFailed,
                           match=re.compile('no releases found', re.I)):
            cut.load('test_bundle')
def test_load_fail_wrong_type_of_info():
    cut = HTTPBundleLoader('index_url')
    with successful_get({'test_bundle': ["wut"]}):
        with pytest.raises(LoadFailed, match='type.*bundle info'):
            cut.load('test_bundle')
def test_load_fail_no_info():
    cut = HTTPBundleLoader('index_url')
    with successful_get({}):
        with pytest.raises(LoadFailed, match='not in.*index'):
            cut.load('test_bundle')