-
Notifications
You must be signed in to change notification settings - Fork 53
fix: use head-based manifest version check #2429
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
Draft
am9zZWY
wants to merge
1
commit into
pulp:main
Choose a base branch
from
am9zZWY:fix/head-manifest
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+171
−6
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Pull-through manifest resolution now performs a HEAD-based version check and serves locally | ||
| stored manifests without downloading the manifest body. Per Docker Hub's pull definition, version | ||
| checks do not count toward rate limits, substantially reducing 429 errors for repeat tag | ||
| resolutions. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure we need a unit test for this fix. As long as the functional tests pass, I think we are fine. The change uses the same pattern as sync, so I'm confident it'll work. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| """Unit tests for HEAD-based manifest version checking in pull-through caching.""" | ||
|
|
||
| import unittest | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| from aiohttp.client_exceptions import ClientConnectionError, ClientResponseError | ||
| from rest_framework.exceptions import Throttled | ||
|
|
||
| from pulp_container.app.exceptions import BadGateway, GatewayTimeout, ManifestNotFound | ||
| from pulp_container.app.registry_api import Manifests | ||
| from pulpcore.plugin.exceptions import TimeoutException | ||
|
|
||
|
|
||
| def _mock_response(digest): | ||
| response = MagicMock() | ||
| response.headers = {"docker-content-digest": digest} if digest else {} | ||
| return response | ||
|
|
||
|
|
||
| class TestFetchManifest(unittest.TestCase): | ||
| """Exercise the HEAD-first manifest resolution flow.""" | ||
|
|
||
| def setUp(self): | ||
| self.view = Manifests() | ||
| self.remote = MagicMock() | ||
| self.remote.namespaced_upstream_name = "library/test" | ||
| self.remote.url = "https://registry.example/" | ||
|
|
||
| def _set_downloaders(self, *downloaders): | ||
| self.remote.get_downloader = MagicMock(side_effect=list(downloaders)) | ||
|
|
||
| @patch("pulp_container.app.registry_api.get_domain") | ||
| @patch("pulp_container.app.registry_api.models.Manifest.objects") | ||
| def test_local_hit_issues_only_head(self, mock_manifest_objects, mock_get_domain): | ||
| digest = "sha256:" + "a" * 64 | ||
| local_manifest = MagicMock() | ||
| mock_manifest_objects.filter.return_value.first.return_value = local_manifest | ||
|
|
||
| head_downloader = MagicMock() | ||
| head_downloader.fetch.return_value = _mock_response(digest) | ||
| self._set_downloaders(head_downloader) | ||
|
|
||
| manifest, response = self.view.fetch_manifest(self.remote, "latest") | ||
|
|
||
| self.assertIs(manifest, local_manifest) | ||
| self.remote.get_downloader.assert_called_once() | ||
| head_downloader.fetch.assert_called_once() | ||
| _, kwargs = head_downloader.fetch.call_args | ||
| self.assertEqual(kwargs["extra_data"]["http_method"], "head") | ||
|
|
||
| @patch("pulp_container.app.registry_api.get_domain") | ||
| @patch("pulp_container.app.registry_api.models.Manifest.objects") | ||
| def test_local_miss_falls_back_to_get(self, mock_manifest_objects, mock_get_domain): | ||
| digest = "sha256:" + "b" * 64 | ||
| mock_manifest_objects.filter.return_value.first.return_value = None | ||
|
|
||
| head_downloader = MagicMock() | ||
| head_downloader.fetch.return_value = _mock_response(digest) | ||
| get_downloader = MagicMock() | ||
| get_downloader.fetch.return_value = _mock_response(digest) | ||
| self._set_downloaders(head_downloader, get_downloader) | ||
|
|
||
| manifest, response = self.view.fetch_manifest(self.remote, "latest") | ||
|
|
||
| self.assertIsNone(manifest) | ||
| self.assertIs(response, get_downloader.fetch.return_value) | ||
| self.assertEqual(self.remote.get_downloader.call_count, 2) | ||
| head_downloader.fetch.assert_called_once() | ||
| get_downloader.fetch.assert_called_once() | ||
| _, kwargs = get_downloader.fetch.call_args | ||
| self.assertEqual(kwargs["extra_data"]["http_method"], "get") | ||
|
|
||
| @patch("pulp_container.app.registry_api.get_domain") | ||
| @patch("pulp_container.app.registry_api.models.Manifest.objects") | ||
| def test_missing_digest_header_falls_back_to_get(self, mock_manifest_objects, mock_get_domain): | ||
| mock_manifest_objects.filter.return_value.first.return_value = None | ||
|
|
||
| head_downloader = MagicMock() | ||
| head_downloader.fetch.return_value = _mock_response(digest=None) | ||
| get_downloader = MagicMock() | ||
| get_downloader.fetch.return_value = _mock_response("sha256:" + "c" * 64) | ||
| self._set_downloaders(head_downloader, get_downloader) | ||
|
|
||
| self.view.fetch_manifest(self.remote, "latest") | ||
|
|
||
| self.assertEqual(self.remote.get_downloader.call_count, 2) | ||
| get_downloader.fetch.assert_called_once() | ||
|
|
||
| def test_404_on_head_raises_manifest_not_found(self): | ||
| head_downloader = MagicMock() | ||
| head_downloader.fetch.side_effect = ClientResponseError( | ||
| request_info=MagicMock(), history=(), status=404 | ||
| ) | ||
| self._set_downloaders(head_downloader) | ||
|
|
||
| with self.assertRaises(ManifestNotFound): | ||
| self.view.fetch_manifest(self.remote, "missing-tag") | ||
|
|
||
| def test_429_on_head_raises_throttled(self): | ||
| head_downloader = MagicMock() | ||
| head_downloader.fetch.side_effect = ClientResponseError( | ||
| request_info=MagicMock(), history=(), status=429 | ||
| ) | ||
| self._set_downloaders(head_downloader) | ||
|
|
||
| with self.assertRaises(Throttled): | ||
| self.view.fetch_manifest(self.remote, "latest") | ||
|
|
||
| def test_other_status_on_head_raises_bad_gateway(self): | ||
| head_downloader = MagicMock() | ||
| head_downloader.fetch.side_effect = ClientResponseError( | ||
| request_info=MagicMock(), history=(), status=500, message="boom" | ||
| ) | ||
| self._set_downloaders(head_downloader) | ||
|
|
||
| with self.assertRaises(BadGateway): | ||
| self.view.fetch_manifest(self.remote, "latest") | ||
|
|
||
| def test_connection_error_on_head_raises_gateway_timeout(self): | ||
| head_downloader = MagicMock() | ||
| head_downloader.fetch.side_effect = ClientConnectionError() | ||
| self._set_downloaders(head_downloader) | ||
|
|
||
| with self.assertRaises(GatewayTimeout): | ||
| self.view.fetch_manifest(self.remote, "latest") | ||
|
|
||
| def test_timeout_on_head_raises_gateway_timeout(self): | ||
| head_downloader = MagicMock() | ||
| head_downloader.fetch.side_effect = TimeoutException() | ||
| self._set_downloaders(head_downloader) | ||
|
|
||
| with self.assertRaises(GatewayTimeout): | ||
| self.view.fetch_manifest(self.remote, "latest") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you put this on one line and make it more concise?