Add scripts/check_image_versions.py (pins mode, and bump mode on pull requests) with unit tests, and run both plus the scripts' tests in lint.yml. tests/scripts is added to galaxy.yml build_ignore. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
150 lines
6.1 KiB
Python
150 lines
6.1 KiB
Python
# SPDX-FileCopyrightText: Defame1297 and contributors
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
"""Run with: python3 -m unittest discover -s tests/scripts"""
|
|
import os
|
|
import pathlib
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
SCRIPTS = pathlib.Path(__file__).resolve().parents[2] / "scripts"
|
|
sys.path.insert(0, str(SCRIPTS))
|
|
import check_image_versions as civ # noqa: E402
|
|
|
|
DIGEST = "sha256:" + "a" * 64
|
|
IMAGE_YML = """\
|
|
jobs:
|
|
image:
|
|
strategy:
|
|
matrix:
|
|
include:
|
|
- name: mol
|
|
version: {mol}
|
|
subpath: images/mol
|
|
dockerfile: Dockerfile
|
|
- name: ci
|
|
version: {ci}
|
|
subpath: ""
|
|
dockerfile: images/ci/Dockerfile
|
|
"""
|
|
CI_DOCKERFILE = "FROM x AS b\nCOPY requirements-dev.txt .pre-commit-config.yaml ./\nCOPY --from=b /a /b\n"
|
|
|
|
|
|
def run(cwd, *cmd):
|
|
return subprocess.run(cmd, cwd=cwd, check=True, capture_output=True, text=True,
|
|
env={**os.environ, "GIT_CONFIG_GLOBAL": "/dev/null", "GIT_CONFIG_SYSTEM": "/dev/null"})
|
|
|
|
|
|
class PureTest(unittest.TestCase):
|
|
def test_copy_sources_skip_from_stage(self):
|
|
self.assertEqual(civ.copy_sources(CI_DOCKERFILE), ["requirements-dev.txt", ".pre-commit-config.yaml"])
|
|
|
|
def test_copy_sources_json_form_and_directories(self):
|
|
self.assertEqual(civ.copy_sources('COPY ["a.txt", "b/", "/dst/"]\n'), ["a.txt", "b/"])
|
|
self.assertEqual(civ.touched(["conf/x.cfg", "other"], [], ["conf/"]), ["conf/x.cfg"])
|
|
self.assertEqual(civ.touched(["a.txt", "b.txt", "c.md"], [], ["*.txt"]), ["a.txt", "b.txt"])
|
|
self.assertEqual(civ.touched(["anything"], [], ["."]), ["anything"])
|
|
|
|
def test_version_order(self):
|
|
self.assertGreater(civ.parse_version("0.10.0"), civ.parse_version("0.9.0"))
|
|
self.assertGreater(civ.parse_version("1.0.0"), civ.parse_version("1.0.0-rc.1"))
|
|
with self.assertRaises(ValueError):
|
|
civ.parse_version("1.0.0+build")
|
|
|
|
def matrix(self, mol="0.1.0", ci="0.2.0"):
|
|
return civ.matrix_of(IMAGE_YML.format(mol=mol, ci=ci))
|
|
|
|
def test_pins_ok_and_lagging(self):
|
|
pin = f"image: git.rkdr.net/mortis/ci:0.1.0@{DIGEST}\n"
|
|
self.assertEqual(civ.check_pins(self.matrix(), {"lint.yml": pin}), [])
|
|
|
|
def test_pin_ahead_of_matrix_unknown_image_and_malformed(self):
|
|
m = self.matrix()
|
|
self.assertEqual(len(civ.check_pins(m, {"f": f"git.rkdr.net/mortis/ci:0.3.0@{DIGEST}"})), 1)
|
|
self.assertEqual(len(civ.check_pins(m, {"f": f"git.rkdr.net/mortis/zzz:0.1.0@{DIGEST}"})), 1)
|
|
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_bump_logic(self):
|
|
base, read = self.matrix(), lambda p: CI_DOCKERFILE
|
|
same = self.matrix()
|
|
for path in ("images/mol/Dockerfile", "images/ci/Dockerfile", "requirements-dev.txt", ".pre-commit-config.yaml"):
|
|
self.assertTrue(civ.check_bump(base, same, [path], read), path)
|
|
self.assertEqual(civ.check_bump(base, same, ["README.md", "images/other/x", ".gitea/workflows/lint.yml"], read), [])
|
|
self.assertEqual(civ.check_bump(base, self.matrix(ci="0.2.1"), ["requirements-dev.txt"], read), [])
|
|
self.assertTrue(civ.check_bump(base, self.matrix(ci="0.1.9"), [], read)) # backwards
|
|
|
|
|
|
class GitTest(unittest.TestCase):
|
|
"""Run the script as CI does, in a throwaway repository."""
|
|
|
|
def setUp(self):
|
|
self.tmp = tempfile.TemporaryDirectory()
|
|
self.repo = pathlib.Path(self.tmp.name)
|
|
self.addCleanup(self.tmp.cleanup)
|
|
run(self.repo, "git", "init", "-q", "-b", "main")
|
|
run(self.repo, "git", "config", "user.email", "[email protected]")
|
|
run(self.repo, "git", "config", "user.name", "t")
|
|
self.write(".gitea/workflows/image.yml", IMAGE_YML.format(mol="0.1.0", ci="0.2.0"))
|
|
self.write("images/ci/Dockerfile", CI_DOCKERFILE)
|
|
self.write("images/mol/Dockerfile", "FROM x\n")
|
|
self.write("requirements-dev.txt", "a==1\n")
|
|
self.write(".pre-commit-config.yaml", "repos: []\n")
|
|
self.commit("base")
|
|
run(self.repo, "git", "checkout", "-q", "-b", "feature")
|
|
|
|
def write(self, rel, text):
|
|
p = self.repo / rel
|
|
p.parent.mkdir(parents=True, exist_ok=True)
|
|
p.write_text(text)
|
|
|
|
def commit(self, msg):
|
|
run(self.repo, "git", "add", "-A")
|
|
run(self.repo, "git", "commit", "-q", "-m", msg)
|
|
|
|
def bump(self):
|
|
proc = subprocess.run([sys.executable, str(SCRIPTS / "check_image_versions.py"), "bump", "main"],
|
|
cwd=self.repo, capture_output=True, text=True)
|
|
return proc.returncode, proc.stderr
|
|
|
|
def test_unrelated_change_passes(self):
|
|
self.write("README.md", "x\n")
|
|
self.commit("docs")
|
|
self.assertEqual(self.bump()[0], 0)
|
|
|
|
def test_pinned_input_without_bump_fails(self):
|
|
self.write("requirements-dev.txt", "a==2\n")
|
|
self.commit("deps")
|
|
code, err = self.bump()
|
|
self.assertEqual(code, 1)
|
|
self.assertIn("image 'ci'", err)
|
|
self.assertNotIn("image 'mol'", err)
|
|
|
|
def test_pinned_input_with_bump_passes(self):
|
|
self.write("requirements-dev.txt", "a==2\n")
|
|
self.write(".gitea/workflows/image.yml", IMAGE_YML.format(mol="0.1.0", ci="0.2.1"))
|
|
self.commit("deps and bump")
|
|
self.assertEqual(self.bump()[0], 0)
|
|
|
|
def test_dockerfile_change_of_molecule_image_fails(self):
|
|
self.write("images/mol/Dockerfile", "FROM y\n")
|
|
self.commit("mol")
|
|
code, err = self.bump()
|
|
self.assertEqual(code, 1)
|
|
self.assertIn("image 'mol'", err)
|
|
|
|
def test_bump_survives_main_moving_on(self):
|
|
run(self.repo, "git", "checkout", "-q", "main")
|
|
self.write("README.md", "newer main\n")
|
|
self.commit("main moves")
|
|
run(self.repo, "git", "checkout", "-q", "feature")
|
|
self.write("images/mol/Dockerfile", "FROM y\n")
|
|
self.write(".gitea/workflows/image.yml", IMAGE_YML.format(mol="0.2.0", ci="0.2.0"))
|
|
self.commit("mol with bump")
|
|
self.assertEqual(self.bump()[0], 0)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|