Repository navigation
fix(ns8): normalise the repository argument, and test that it cannot drift #154
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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,121 @@ | ||
| """Tests for the repository-argument handling in assert-multiarc.py. | ||
|
|
||
| Run offline, so they are safe to enforce on every push: | ||
|
|
||
| python3 -m unittest discover -s packaging/ns8 -p 'test_*.py' -v | ||
|
|
||
| The registry round-trip tests are opt-in, because CI should not depend on | ||
| ghcr's availability to decide whether a source change is valid: | ||
|
|
||
| NS8_REGISTRY_TESTS=1 python3 -m unittest discover -s packaging/ns8 -p 'test_*.py' -v | ||
|
|
||
| The four spellings below are the ones that matter. Three resolve and one does | ||
| not, and the one that does not is the exact string ns8-module.yml passes, which | ||
| made main red while the image itself was present and correct. | ||
| """ | ||
|
|
||
| import importlib.util | ||
| import os | ||
| import pathlib | ||
| import unittest | ||
|
|
||
| _SPEC = importlib.util.spec_from_file_location( | ||
| "assert_multiarc", | ||
| pathlib.Path(__file__).resolve().parent / "assert-multiarc.py", | ||
| ) | ||
| am = importlib.util.module_from_spec(_SPEC) | ||
| _SPEC.loader.exec_module(am) | ||
|
|
||
|
|
||
| class NormalizeRepo(unittest.TestCase): | ||
| def test_the_four_spellings_agree(self): | ||
| """Every spelling of one repository must reduce to one canonical form. | ||
|
|
||
| This is the regression. `ghcr.io/Calnode/ns8-calnode` used to 404 while | ||
| the other three resolved, because a redundant leading host segment makes | ||
| GitHub's edge router match the segments after it case-sensitively. | ||
| """ | ||
| forms = [ | ||
| "calnode/ns8-calnode", | ||
| "Calnode/ns8-calnode", | ||
| "ghcr.io/calnode/ns8-calnode", | ||
| "ghcr.io/Calnode/ns8-calnode", | ||
| ] | ||
| canonical = {am.normalize_repo(f) for f in forms} | ||
| self.assertEqual(canonical, {"calnode/ns8-calnode"}) | ||
|
|
||
| def test_host_prefix_is_case_insensitive(self): | ||
| for host in ("ghcr.io", "GHCR.IO", "Ghcr.io"): | ||
| self.assertEqual( | ||
| am.normalize_repo(f"{host}/Calnode/ns8-calnode"), | ||
| "calnode/ns8-calnode", | ||
| ) | ||
|
|
||
| def test_dotted_owner_is_not_mistaken_for_a_host(self): | ||
| """A dotted first segment is a legal owner, not a registry host. | ||
|
|
||
| Stripping it would silently rewrite `my.org/team/image` into a different | ||
| repository, which is far worse than the 404 this function prevents. | ||
| """ | ||
| self.assertEqual(am.normalize_repo("my.org/team/image"), "my.org/team/image") | ||
| self.assertEqual(am.normalize_repo("My.Org/Team/Image"), "my.org/team/image") | ||
|
|
||
| def test_two_segment_names_are_untouched_apart_from_case(self): | ||
| self.assertEqual(am.normalize_repo("Calnode/Ns8-Calnode"), "calnode/ns8-calnode") | ||
|
|
||
| def test_whitespace_is_trimmed(self): | ||
| self.assertEqual(am.normalize_repo(" calnode/ns8-calnode\n"), "calnode/ns8-calnode") | ||
|
|
||
| def test_normalisation_is_idempotent(self): | ||
| once = am.normalize_repo("ghcr.io/Calnode/ns8-calnode") | ||
| self.assertEqual(am.normalize_repo(once), once) | ||
|
|
||
| def test_every_fetch_path_normalises(self): | ||
| """get(), get_blob() and token() must all agree, not just one of them.""" | ||
| for name in ("get", "get_blob"): | ||
| src = getattr(am, name).__doc__ or "" | ||
| self.assertIn("normalize_repo", __import__("inspect").getsource(getattr(am, name))) | ||
| del src | ||
| self.assertIn("normalize_repo", __import__("inspect").getsource(am.token)) | ||
|
|
||
|
|
||
| class RegistryRoundTrip(unittest.TestCase): | ||
| """Opt-in: proves the four spellings produce identical live outcomes.""" | ||
|
|
||
| REPO = "calnode/ns8-calnode" | ||
| TAG = os.environ.get("NS8_REGISTRY_TEST_TAG", "latest") | ||
|
|
||
| def setUp(self): | ||
| if not os.environ.get("NS8_REGISTRY_TESTS"): | ||
| self.skipTest("set NS8_REGISTRY_TESTS=1 to run against ghcr.io") | ||
|
|
||
| def test_all_four_spellings_resolve_to_the_same_index(self): | ||
| bodies = {} | ||
| for form in ( | ||
| self.REPO, | ||
| "Calnode/ns8-calnode", | ||
| f"ghcr.io/{self.REPO}", | ||
| f"ghcr.io/Calnode/ns8-calnode", | ||
| ): | ||
| body, _ = am.get(form, self.TAG) | ||
| bodies[form] = body | ||
| digests = {form: body["manifests"][0]["digest"] for form, body in bodies.items()} | ||
| self.assertEqual( | ||
| len(set(digests.values())), | ||
| 1, | ||
| f"the four spellings resolved to different images: {digests}", | ||
| ) | ||
|
|
||
| def test_every_spelling_passes_the_real_check(self): | ||
| for form in ( | ||
| self.REPO, | ||
| "Calnode/ns8-calnode", | ||
| f"ghcr.io/{self.REPO}", | ||
| f"ghcr.io/Calnode/ns8-calnode", | ||
| ): | ||
| with self.subTest(repo=form): | ||
| am.check_tag(form, self.TAG, ["amd64", "arm64"], False, None) | ||
|
|
||
|
|
||
| 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.
srcis assigned and then deleted without ever being read, andinspectis reached through an inline__import__("inspect")twice when the module already imports four other stdlib modules at the top. This is the one test that enforces "the three paths cannot drift", so it should read as the deliberate check it is. Addimport inspectto the import block and delete the dead pair.Technical details