Skip to content

Commit

Permalink
fix: handle empty bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
roleyfoley committed Feb 23, 2021
1 parent 10880e4 commit dca4716
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
12 changes: 6 additions & 6 deletions s3-age-metric/src/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ def lambda_handler(event, context):
logger.fatal(str(e))
raise e

s3_contents = sorted(s3_objects["Contents"], key= lambda i: i['LastModified'] )

oldest_item_in_set = s3_contents[0]['LastModified']
if oldest_item_in_set < oldest_item:
oldest_item = oldest_item_in_set
oldest_item_key = s3_contents[0]['Key']
if 'Contents' in s3_objects:
s3_contents = sorted(s3_objects['Contents'], key= lambda i: i['LastModified'] )
oldest_item_in_set = s3_contents[0]['LastModified']
if oldest_item_in_set < oldest_item:
oldest_item = oldest_item_in_set
oldest_item_key = s3_contents[0]['Key']

if s3_objects['IsTruncated']:
continuation_token = s3_objects['NextContinuationToken']
Expand Down
53 changes: 53 additions & 0 deletions s3-age-metric/src/test_lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@

from lambda_function import lambda_handler

@mock_cloudwatch
@mock_s3
def test_no_object_in_bucket():

bucket_name = 'test_bucket'
bucket_prefix = ''
oldest_object_key = 'single_object'

s3_client = boto3.client('s3', region_name='us-east-1')
s3_client.create_bucket(Bucket=bucket_name)

with mock.patch.dict(os.environ, {'BUCKET_NAME': bucket_name, 'BUCKET_PREFIX': bucket_prefix}) as env:
result = lambda_handler({}, {})

assert result['bucket_name'] == 'test_bucket'
assert result['bucket_prefix'] == ''
assert result['oldest_item_key'] == None

@mock_cloudwatch
@mock_s3
def test_single_object_in_bucket():
Expand Down Expand Up @@ -37,6 +55,41 @@ def test_single_object_in_bucket():
assert result['oldest_item_key'] == 'single_object'


@mock_cloudwatch
@mock_s3
def test_small_set_of_objects_in_bucket():

bucket_name = 'test_bucket'
bucket_prefix = ''

s3_client = boto3.client('s3', region_name='us-east-1')
s3_client.create_bucket(Bucket=bucket_name)

# Create a single item first which should be the oldest item
oldest_object_key = 'oldest_object'
s3_client.put_object(
Body='test content',
Bucket=bucket_name,
Key=oldest_object_key
)

time.sleep(1)

# Create a lot of items to ensure pagination works as expected
for i in range(10):
s3_client.put_object(
Body='test content',
Bucket=bucket_name,
Key=f'object_{i}'
)

with mock.patch.dict(os.environ, {'BUCKET_NAME': bucket_name, 'BUCKET_PREFIX': bucket_prefix}) as env:
result = lambda_handler({}, {})

assert result['bucket_name'] == 'test_bucket'
assert result['bucket_prefix'] == ''
assert result['oldest_item_key'] == 'oldest_object'

@mock_cloudwatch
@mock_s3
def test_large_set_of_objects_in_bucket():
Expand Down

0 comments on commit dca4716

Please sign in to comment.