Skip to content

Commit

Permalink
when determining archive age, fall-back to updated time
Browse files Browse the repository at this point in the history
The created field in a request can potentially be None. If so, fall back
to using the updated field in order to determine the age of a request.

Signed-off-by: Taylor Madore <[email protected]>
  • Loading branch information
taylormadore committed Feb 7, 2024
1 parent b7c47b4 commit 0f73ead
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
14 changes: 11 additions & 3 deletions cachito/workers/prune_archives.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,19 @@ def _resolve_source_archive(parsed_archive: _ParsedArchive) -> Optional[_Resolve
log.debug("Archive %s could not be resolved via the API.", parsed_archive.path)
return None

request_age = latest_request.get("created") or latest_request.get("updated")
if request_age is None:
# This should be impossible
log.debug(
"Unable to determine the age of %s with latest request_id=%s",
parsed_archive.path,
latest_request["id"],
)
return None

return _ResolvedArchive(
parsed_archive.path,
datetime.strptime(latest_request["created"], "%Y-%m-%dT%H:%M:%S.%f").replace(
tzinfo=timezone.utc
),
datetime.strptime(request_age, "%Y-%m-%dT%H:%M:%S.%f").replace(tzinfo=timezone.utc),
latest_request["id"],
)

Expand Down
37 changes: 37 additions & 0 deletions tests/test_workers/test_prune_archives.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,43 @@ def test_resolve_source_archive_not_found(mock_request: mock.Mock):
assert resolved_archive is None


@mock.patch("cachito.workers.prune_archives._get_latest_request")
def test_resolve_source_archive_no_created_date(mock_request: mock.Mock):
"""Tests resolving a ParsedArchive when request data is missing a created date."""
path = Path("my-org/my-project/ce60002604554992203f2afe17f23724f674b411.tar.gz")
repo_name = path.parent.as_posix()
ref = path.name[:40]
updated = datetime(2024, 1, 1, tzinfo=timezone.utc)
latest_request_id = 1

mock_request.return_value = {
"created": None,
"updated": datetime.strftime(updated, format="%Y-%m-%dT%H:%M:%S.%f"),
"id": latest_request_id,
}
parsed_archive = _ParsedArchive(path, repo_name, ref)
expected_resolved_archive = _ResolvedArchive(path, updated, latest_request_id)

resolved_archive = _resolve_source_archive(parsed_archive)
assert resolved_archive == expected_resolved_archive


@mock.patch("cachito.workers.prune_archives._get_latest_request")
def test_resolve_source_archive_no_age(mock_request: mock.Mock):
"""Tests when we cannot resolve a ParsedArchive because we can't determine the age."""
path = Path("my-org/my-project/ce60002604554992203f2afe17f23724f674b411.tar.gz")
repo_name = path.parent.as_posix()
ref = path.name[:40]

mock_request.return_value = {
"id": 1,
}
parsed_archive = _ParsedArchive(path, repo_name, ref)

resolved_archive = _resolve_source_archive(parsed_archive)
assert resolved_archive is None


@mock.patch("cachito.workers.prune_archives._resolve_source_archive")
@mock.patch("cachito.workers.prune_archives._get_parsed_source_archives")
def test_get_stale_archives(
Expand Down

0 comments on commit 0f73ead

Please sign in to comment.