fix(ci): make the image pin check fail closed on odd spellings and Dockerfile sources
Pin mentions are matched case-insensitively with an optional port, a nested path and no tag, so a mis-spelled or untagged pin is reported instead of ignored. The Dockerfile COPY/ADD reader raises an error naming file and line on a source with a variable, quote or comment character and on a RUN --mount of the build context, and joins continuation lines with trailing blanks. A non-UTF-8 diff path and an empty image.yml get their own messages, lint.yml is a required consumer, the success line lists the consumers checked, and the docstring states the known limits. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
This commit is contained in:
1 parent
4fabe1358e
commit
c3cd04e013
3 files changed
+204
-20
No files matched your search
@@ -84,6 +84,67 @@ class PureTest(unittest.TestCase):
|
||||
self.assertEqual(len(civ.check_pins(m, {"f": "git.rkdr.net/mortis/ci:0.2.0"})), 1)
|
||||
self.assertEqual(len(civ.check_pins(m, {"f": "git.rkdr.net/mortis/ci@sha256:abc"})), 1)
|
||||
|
||||
def test_pin_spellings_are_reported(self):
|
||||
m = self.matrix()
|
||||
spellings = [
|
||||
f"Git.rkdr.net/mortis/ci:0.1.0@{DIGEST}",
|
||||
f"git.rkdr.net/Mortis/ci:0.1.0@{DIGEST}",
|
||||
f"git.rkdr.net:443/mortis/ci:0.1.0@{DIGEST}",
|
||||
f"git.rkdr.net/mortis/ci/sub:0.1.0@{DIGEST}",
|
||||
f"GIT.RKDR.NET/mortis/CI:0.1.0@{DIGEST}",
|
||||
"git.rkdr.net/mortis/ci",
|
||||
"git.rkdr.net/mortis/ci:0.1.0",
|
||||
]
|
||||
for spelling in spellings:
|
||||
errors = civ.check_pins(m, {"f": f"image: {spelling}\n"})
|
||||
self.assertEqual(len(errors), 1, f"{spelling}: {errors}")
|
||||
self.assertIn("not a <name>:<version>@sha256", errors[0])
|
||||
valid = f"image: git.rkdr.net/mortis/ci:0.1.0@{DIGEST}\n"
|
||||
self.assertEqual(civ.check_pins(m, {"f": valid}), [])
|
||||
|
||||
def test_urls_and_ssh_remotes_are_not_pin_mentions(self):
|
||||
m = self.matrix()
|
||||
text = ("url: https://git.rkdr.net/mortis/kamino\nurl2: http://git.rkdr.net/mortis/kamino.git\n"
|
||||
"ssh: [email protected]:mortis/kamino.git\n")
|
||||
self.assertEqual(civ.check_pins(m, {"f": text}), [])
|
||||
|
||||
def test_copy_source_fails_closed(self):
|
||||
cases = {
|
||||
"COPY $FILE /dst/\n": "'$'",
|
||||
'COPY "a b" /dst/\n': "'\"'",
|
||||
"COPY 'a' /dst/\n": "\"'\"",
|
||||
"COPY a#b /dst/\n": "'#'",
|
||||
'COPY ["$X", "/dst/"]\n': "'$'",
|
||||
"COPY [\"a\", unquoted]\n": "JSON form",
|
||||
"RUN --mount=type=bind,source=src,target=/s make\n": "RUN --mount binds the build context",
|
||||
"RUN --mount=source=src,target=/s make\n": "RUN --mount binds the build context",
|
||||
"FROM x\nRUN --mount=type=bind,target=/s \\\n make\n": "Dockerfile:2:",
|
||||
}
|
||||
for text, expected in cases.items():
|
||||
with self.assertRaises(civ.CheckError, msg=text) as ctx:
|
||||
civ.copy_sources(text, "images/ci/Dockerfile")
|
||||
message = str(ctx.exception)
|
||||
self.assertIn(expected, message, text)
|
||||
self.assertIn("images/ci/Dockerfile:", message)
|
||||
self.assertNotIn("\n", message)
|
||||
|
||||
def test_copy_source_ok_cases_still_pass(self):
|
||||
ok = ("RUN --mount=type=cache,target=/c make\nRUN --mount=type=bind,from=b,source=/x,target=/y make\n"
|
||||
"# COPY $IGNORED /x\nCOPY --chown=1:1 a b/ /dst/\nCOPY https://example.org/x /dst/\n")
|
||||
self.assertEqual(civ.copy_sources(ok), ["a", "b/"])
|
||||
|
||||
def test_continuation_with_trailing_blanks_is_joined(self):
|
||||
text = "COPY a.txt \\ \n b.txt /dst/\n"
|
||||
self.assertEqual(civ.copy_sources(text), ["a.txt", "b.txt"])
|
||||
text = "COPY a.txt \\\n# a comment\n b.txt /dst/\n"
|
||||
self.assertEqual(civ.copy_sources(text), ["a.txt", "b.txt"])
|
||||
|
||||
def test_empty_or_non_mapping_matrix_file(self):
|
||||
for text in ("", "# only a comment\n", "- a\n- b\n", "just a string\n"):
|
||||
with self.assertRaises(civ.CheckError, msg=repr(text)) as ctx:
|
||||
civ.matrix_of(text)
|
||||
self.assertIn("image.yml is empty or not a mapping", str(ctx.exception))
|
||||
|
||||
def test_semver_is_strict(self):
|
||||
for bad in ("0.1.0\n", "01.0.0", "1.02.0", "1.0.00", "1.0.0-01", "1.0.0-rc.01", "1.0.0 ", "v1.0.0"):
|
||||
with self.assertRaises(ValueError, msg=repr(bad)):
|
||||
@@ -229,6 +290,38 @@ class GitTest(unittest.TestCase):
|
||||
self.assertIn("image 'mol'", err)
|
||||
self.assertIn("caf\u00e9", err)
|
||||
|
||||
def test_non_utf8_path_in_the_diff_is_named(self):
|
||||
path = os.fsencode(str(self.repo)) + b"/images/mol/caf\xe9"
|
||||
try:
|
||||
with open(path, "wb") as handle:
|
||||
handle.write(b"x\n")
|
||||
except OSError:
|
||||
self.skipTest("the file system does not accept a non-UTF-8 name")
|
||||
self.commit("non-utf8 input")
|
||||
self.assert_clean_failure(self.bump(), "path is not valid UTF-8")
|
||||
|
||||
def test_empty_image_yml_says_so(self):
|
||||
self.write(".gitea/workflows/image.yml", "")
|
||||
self.commit("empty matrix")
|
||||
self.assert_clean_failure(self.bump(), "image.yml is empty or not a mapping")
|
||||
|
||||
def test_unsupported_dockerfile_source_fails_with_file_and_line(self):
|
||||
self.write("images/ci/Dockerfile", "FROM x\nCOPY $SRC ./\n")
|
||||
self.commit("variable source")
|
||||
self.assert_clean_failure(self.bump(), "images/ci/Dockerfile:2:")
|
||||
|
||||
def test_pins_mode_requires_lint_yml_and_lists_checked_consumers(self):
|
||||
proc = subprocess.run([sys.executable, str(SCRIPTS / "check_image_versions.py"), "pins"],
|
||||
cwd=self.repo, capture_output=True, text=True, check=False)
|
||||
self.assertEqual(proc.returncode, 1, proc.stdout)
|
||||
self.assertIn("lint.yml is missing", proc.stderr)
|
||||
self.write(".gitea/workflows/lint.yml", f"image: git.rkdr.net/mortis/ci:0.1.0@{DIGEST}\n")
|
||||
proc = subprocess.run([sys.executable, str(SCRIPTS / "check_image_versions.py"), "pins"],
|
||||
cwd=self.repo, capture_output=True, text=True, check=False)
|
||||
self.assertEqual(proc.returncode, 0, proc.stderr)
|
||||
self.assertIn("(checked: .gitea/workflows/lint.yml)", proc.stdout)
|
||||
self.assertNotIn("build.yml", proc.stdout)
|
||||
|
||||
def test_unrelated_change_passes(self):
|
||||
self.write("README.md", "x\n")
|
||||
self.commit("docs")
|
||||
|
||||
Reference in new issue
Block a user