Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when determining archive age, fall-back to updated time #977

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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