Skip to content

Commit 37fe9df

Browse files
authored
fix: skip cleanup if we can't find anything to delete (#313)
1 parent 2a3a376 commit 37fe9df

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

lambda_functions/cleanup_sentinel2_granules.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ def handler(event: dict, context: dict):
5151
Prefix=prefix,
5252
)
5353

54+
if "Contents" not in response:
55+
print(f"Found 0 granule zip files for prefix={prefix}, aborting.")
56+
return
57+
5458
granule_zips = [obj["Key"] for obj in response["Contents"]]
5559

5660
# We have three possible cases,

lambda_functions/tests/test_cleanup_sentinel2_granules.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,25 @@ def test_bad_granule_id_inputs(mock_delete_object):
7777
handler({"granule": "thisiswronggranuleid,obviouslynotagranuleid"}, {})
7878
mock_list_objects.assert_not_called()
7979
mock_delete_object.assert_not_called()
80+
81+
82+
def test_no_granules_found_skips(mock_delete_object, capsys):
83+
"""Ensure we skip if no granules are found on S3
84+
85+
Ref: https://github.com/NASA-IMPACT/hls_development/issues/342
86+
"""
87+
from lambda_functions.cleanup_sentinel2_granules import handler, s3
88+
89+
with (
90+
patch.object(
91+
s3,
92+
"list_objects_v2",
93+
return_value={},
94+
) as mock_list_objects,
95+
):
96+
handler({"granule": "one12345"}, {})
97+
mock_list_objects.assert_called()
98+
mock_delete_object.assert_not_called()
99+
100+
captured = capsys.readouterr()
101+
assert "Found 0 granule zip files for prefix" in captured.out

0 commit comments

Comments
 (0)