def test_prepare_for_cleanup(make_stubber, make_unique_name): s3_stubber = make_stubber(batch_versioning.s3.meta.client) bucket_name = make_unique_name('bucket') obj_prefix = 'test-prefix' versions = [ s3_stubber.make_version(f'key {index}', f'version-{index}', True) for index in range(5) ] delete_markers = [ s3_stubber.make_version(f'key {index}', f'version-{index}', True) for index in range(5) ] s3_stubber.stub_list_object_versions(bucket_name, f'{obj_prefix}stanza', versions=versions, delete_markers=delete_markers) manifest_lines = batch_versioning.prepare_for_cleanup( batch_versioning.s3.Bucket(bucket_name), obj_prefix, [MagicMock(), MagicMock(), MagicMock()]) assert manifest_lines == [ f"{bucket_name},{parse.quote(ver['Key'])},{ver['VersionId']}" for ver in delete_markers ]
def test_prepare_for_cleanup_failure(make_stubber, make_unique_name, fail_at): s3_stubber = make_stubber(batch_versioning.s3.meta.client) bucket_name = make_unique_name('bucket') obj_prefix = 'test-prefix' stanzas = [MagicMock(), MagicMock(), MagicMock()] if fail_at == 'put_object': stanzas[0].put = MagicMock(side_effect=ClientError( {'Error': { 'Code': 'TestException', 'Message': 'hi' }}, 'test-op')) elif fail_at == 'list_versions': s3_stubber.stub_list_object_versions(bucket_name, f'{obj_prefix}stanza', error_code='TestException') with pytest.raises(ClientError) as exc_info: batch_versioning.prepare_for_cleanup( batch_versioning.s3.Bucket(bucket_name), obj_prefix, stanzas) assert exc_info.value.response['Error']['Code'] == 'TestException'