def test_archive_delete():
    conn = GlacierTestsConfig().connection()
    vault = Util.get_new_vault()
    vault_name = vault['Location'].split('/')[-1]
    archive = Util.upload_archive(vault_name, b"hello", None)
    ok_('ArchiveId' in archive)
    archive_id = archive['ArchiveId']
    conn.delete_archive(vault_name, archive_id)
def test_vault_describe_does_not_exist():
    conn = GlacierTestsConfig().connection()
    try:
        conn.describe_vault(GlacierTestsConfig().prefix() + '-doesnotexist')
    except GlacierError as e:
        eq_(e.code, 'ResourceNotFoundException')
        body = json.loads(e.body)
        eq_(body['type'], 'client')
def test_vault_delete():
    conn = GlacierTestsConfig().connection()
    vault = Util.get_new_vault()
    ok_('Location' in vault)
    ok_('RequestId' in vault)
    vault_name = vault['Location'].split('/')[-1]
    _check_in_listing([vault_name])
    conn.delete_vault(vault_name)
    _check_not_in_listing([vault_name])
def test_create_inventory_job():
    vault = Util.get_new_vault()
    vault_name = vault['Location'].split('/')[-1]
    description = "test archive"
    conn = GlacierTestsConfig().connection()
    job_data = {'Type': 'inventory-retrieval'}
    job = conn.initiate_job(vault_name, job_data)
    ok_('JobId' in job)
    description = conn.describe_job(vault_name, job['JobId'])
    date = dateutil.parser.parse(description['CompletionDate'])
    date = dateutil.parser.parse(description['CreationDate'])
    eq_(description['StatusCode'], 'Succeeded')
    eq_(description['Action'], 'inventory-retrieval')
def purge_prefix_vaults():
    conn = GlacierTestsConfig().connection()
    all_vaults = conn.list_vaults()
    jobs = {}
    for vault in all_vaults['VaultList']:
        if vault['VaultName'].startswith(GlacierTestsConfig().prefix()):
            # Try to delete and only schedule an inventory job if delete fails
            try:
                print 'delete vault %s' % vault['VaultName']
                conn.delete_vault(vault['VaultName'])
            except Exception as e:
                print 'delete failed %s' % str(e)
                jobs[vault['VaultName']] = enumerate_vault(vault['VaultName'],
                                                           conn)
    while jobs:
        remaining = {}
        while jobs:
            vault, job_id = jobs.popitem()
            status = conn.describe_job(vault, job_id)
            if status['Completed'] == 'false':
                print 'Waiting for: %s' % job_id
                remaining[vault] = job_id
                continue
            resp = None
            try:
                resp = conn.get_job_output(vault, job_id)
            except Exception as e:
                print "Error: %s" % str(e)
                remaining[vault] = job_id
                continue
            for archive in resp['ArchiveList']:
                conn.delete_archive(vault, archive['ArchiveId'])
        jobs = remaining
        if jobs:
            time.sleep(10)
def test_create_archive_retrieval_job():
    vault = Util.get_new_vault()
    vault_name = vault['Location'].split('/')[-1]
    description = "test archive"
    archive = _setup_test_archive(vault_name, description)
    conn = GlacierTestsConfig().connection()
    job_data = {'Type': 'archive-retrieval',
                'ArchiveId': archive}
    job = conn.initiate_job(vault_name, job_data)
    ok_('JobId' in job)
    description = conn.describe_job(vault_name, job['JobId'])
    date = dateutil.parser.parse(description['CompletionDate'])
    date = dateutil.parser.parse(description['CreationDate'])
    eq_(description['RetrievalByteRange'], '0-4')
    eq_(description['StatusCode'], 'Succeeded')
    eq_(description['Completed'], True)
    eq_(description['ArchiveId'], archive)
    eq_(description['Action'], 'archive-retrieval')
def test_vault_list_all():
    conn = GlacierTestsConfig().connection()
    result = conn.list_vaults()
    ok_('RequestId' in result)
    ok_('VaultList' in result)
    ok_('Marker' in result)
def _get_vault_names():
    conn = GlacierTestsConfig().connection()
    result = conn.list_vaults()
    return [x['VaultName'] for x in result['VaultList']]