Skip to content
Open
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
8 changes: 8 additions & 0 deletions CHANGES/2717.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Fixed a 500 (``UnboundLocalError: cannot access local variable '_'``) in the content app
``pulpcore/content/handler.py`` ``_match_and_stream``. Patch 0080 used a bare ``_`` as the
throwaway target of three tuple unpacks, which made ``_`` a function-local name and shadowed
gettext's ``_``. Requests for a distribution with no repository, publication, or remote reached
the "Distribution is not pointing to..." branch without executing any of those unpacks, so the
gettext call raised ``UnboundLocalError`` and the content app returned 500 instead of 404
(PULP-2717). The same shadowing turned upstream fetch failures into
``TypeError: 'QuerySet' object is not callable``. The unpacks now use ``_lazy_queryset``.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
- )
- .filter(
- content__in=publication.repository_version.content,
+ rows, _ = await self._retry_content_artifact_query(
+ rows, _lazy_queryset = await self._retry_content_artifact_query(
+ lambda: sync_to_async(content_query_experiment)(
+ "pass_through_file_lookup",
+ publication.repository_version,
Expand All @@ -65,7 +65,7 @@
- lambda: ContentArtifact.objects.filter(
- content__in=repo_version.content, relative_path=index_path
- ).aexists()
+ probe_rows, _ = await self._retry_content_artifact_query(
+ probe_rows, _lazy_queryset = await self._retry_content_artifact_query(
+ lambda: sync_to_async(content_query_experiment)(
+ "index_probe",
+ repo_version,
Expand All @@ -86,7 +86,7 @@
- lambda: ContentArtifact.objects.select_related(
- "artifact", "artifact__pulp_domain"
- ).aget(content__in=repo_version.content, relative_path=original_rel_path)
+ rows, _ = await self._retry_content_artifact_query(
+ rows, _lazy_queryset = await self._retry_content_artifact_query(
+ lambda: sync_to_async(content_query_experiment)(
+ "file_lookup",
+ repo_version,
Expand Down
22 changes: 22 additions & 0 deletions pulp_service/pulp_service/tests/functional/test_content_handler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import uuid

import requests

Expand All @@ -24,3 +25,24 @@ def test_artifact_size_header_on_pull_through_cache(
response = requests.get(f"{distro.base_url}{package_filename}")

assert response.headers.get("X-PULP-ARTIFACT-SIZE")


def test_empty_distribution_returns_404(file_bindings, gen_object_with_cleanup):
"""A distribution with no repository, publication, or remote must 404, not 500.

Regression test for PULP-2717. Patch 0080 used a bare ``_`` as the throwaway target of
three tuple unpacks in ``Handler._match_and_stream``, which made ``_`` a function-local
name and shadowed gettext's ``_``. A distribution with nothing configured reaches the
"Distribution is not pointing to..." branch without executing any of those unpacks, so
the gettext call raised ``UnboundLocalError`` and the content app answered 500. In
production this hit every request for a stale COPR pass-through distribution.
"""
distro = gen_object_with_cleanup(
file_bindings.DistributionsFileApi,
{"name": str(uuid.uuid4()), "base_path": str(uuid.uuid4())},
)

response = requests.get(f"{distro.base_url}missing/path", timeout=30)

assert response.status_code == 404, response.text
assert "Distribution is not pointing to" in response.reason

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Correct 404 response fails assertion

When the handler correctly returns a 404 with the distribution message in the response body, test_empty_distribution_returns_404 fails at line 48: requests.Response.reason is the HTTP status phrase (Not Found), not the response body containing the distribution message, so the assertion rejects the correct 404.

Check response.text for the distribution message instead of response.reason.

Prompt for AI agents
In `pulp_service/pulp_service/tests/functional/test_content_handler.py` at line 48:

**Correct 404 response fails assertion**

When the handler correctly returns a 404 with the distribution message in the response body, `test_empty_distribution_returns_404` fails at line 48: `requests.Response.reason` is the HTTP status phrase (`Not Found`), not the response body containing the distribution message, so the assertion rejects the correct 404.

Check `response.text` for the distribution message instead of `response.reason`.

Loading