Skip to content

Commit 8d829ec

Browse files
fix: add constraint to the age of import log range (GoogleCloudPlatform#10633)
ensure that import range does not include dates older than 29 days from now. Fixes GoogleCloudPlatform#10632 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 3968a28 commit 8d829ec

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

logging/import-logs/main.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,21 @@ def _day(blob_name: str) -> int:
7979
return int(blob_name[offset : offset + 2])
8080

8181

82+
def _is_valid_import_range() -> bool:
83+
"""Check the import range dates to ensure that
84+
- start date is earlier than end date
85+
- no dates in the range is older than 29 days
86+
(for reason see https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#FIELDS.timestamp)
87+
"""
88+
if START_DATE > END_DATE:
89+
eprint("Start date of the import time range should be earlier than end date")
90+
return False
91+
if (date.today() - START_DATE).days > 29:
92+
eprint("Import range includes dates older than 29 days from today.")
93+
return False
94+
return True
95+
96+
8297
def calc_import_range() -> Tuple[date, date]:
8398
"""Calculate import range for the task based on full import range and number of tasks"""
8499
if TASK_COUNT == 1:
@@ -201,12 +216,10 @@ def import_logs(
201216

202217
def main() -> None:
203218
"""Imports logs from Cloud Storage to Cloud Logging"""
204-
205219
if not START_DATE or not END_DATE or not LOG_ID or not BUCKET_NAME:
206220
eprint("Missing some of required parameters")
207221
sys.exit(1)
208-
if START_DATE > END_DATE:
209-
eprint("Start date of the import time range should be earlier than end date")
222+
if not _is_valid_import_range():
210223
sys.exit(1)
211224

212225
start_date, end_date = calc_import_range()

logging/import-logs/main_test.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# pylint: disable=too-many-arguments
1818
# pylint: disable=too-many-return-statements
1919

20-
from datetime import date
20+
from datetime import date, timedelta
2121
import json
2222
import os
2323
import sys
@@ -53,6 +53,34 @@ def _setup_environment(
5353
main._LOGS_MAX_SIZE_BYTES = max_size # pylint: disable=protected-access
5454

5555

56+
@pytest.mark.parametrize(
57+
"start_date, end_date, expected_validity",
58+
[
59+
(date.today() - timedelta(days=29), date.today() - timedelta(days=10), True),
60+
(date.today() - timedelta(days=10), date.today() - timedelta(days=29), False),
61+
(date.today() - timedelta(days=30), date.today(), False),
62+
("07/01/2023", "06/01/2023", False),
63+
],
64+
ids=[
65+
"valid range",
66+
"invalid range: start later than end",
67+
"invalid range: start older than 29 days",
68+
"invalid range: end older than 29 days",
69+
],
70+
)
71+
def test_import_range_validation(
72+
start_date: str, end_date: str, expected_validity: bool
73+
) -> None:
74+
_setup_environment(
75+
start_date=start_date,
76+
end_date=end_date,
77+
)
78+
isvalid = main._is_valid_import_range() # pylint: disable=protected-access
79+
assert (
80+
isvalid == expected_validity
81+
), f"import range ({start_date} -> {end_date}) validation failed: expected {expected_validity} and got {isvalid}"
82+
83+
5684
@pytest.mark.parametrize(
5785
"start_date, end_date, task_index, task_count, expected_first_day, expected_last_day",
5886
[

0 commit comments

Comments
 (0)