Files
Kamino/tests/scripts/test_bump_image_versions.py
T
Defame1297andClaude Sonnet 5.5 ec15abf400 feat(scripts): add bump_image_versions.py for the image version bump
Factor the changed-path listing and the per-image input detection out of
check_image_versions.py (changed_paths, changed_images) and reuse them in a
new script that bumps the PATCH version of every image whose inputs changed.
It is meant to run later as a Renovate postUpgradeTasks command.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
2026-10-25 21:06:33 +00:00

150 lines
5.9 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 bump_image_versions as biv # noqa: E402
IMAGE_YML = """\
# header comment
jobs:
image:
strategy:
matrix:
include:
- name: mol
version: {mol} # keep me
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 ./\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_next_patch(self):
self.assertEqual(biv.next_patch("0.2.9"), "0.2.10")
self.assertEqual(biv.next_patch("1.0.0-rc.1"), "1.0.1")
with self.assertRaises(ValueError):
biv.next_patch("latest")
def test_rewrite_changes_only_the_version_line(self):
text = IMAGE_YML.format(mol="0.1.0", ci="0.2.0")
out = biv.rewrite(text, {"mol": "0.1.1", "ci": "0.2.1"})
self.assertEqual(out, IMAGE_YML.format(mol="0.1.1", ci="0.2.1"))
self.assertIn("version: 0.1.1 # keep me\n", out)
self.assertIn('version: "0.2.1"\n', out)
def test_rewrite_unknown_image_fails(self):
with self.assertRaises(biv.civ.CheckError):
biv.rewrite(IMAGE_YML.format(mol="0.1.0", ci="0.2.0"), {"nope": "1.0.0"})
class RepoTest(unittest.TestCase):
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 script(self, name, *args):
proc = subprocess.run([sys.executable, str(SCRIPTS / name), *args], cwd=self.repo,
capture_output=True, text=True, check=False)
return proc.returncode, proc.stdout, proc.stderr
def bump(self, *args):
return self.script("bump_image_versions.py", *(args or ("main",)))
def versions(self):
return (self.repo / ".gitea/workflows/image.yml").read_text()
def test_nothing_changed(self):
code, out, _ = self.bump()
self.assertEqual((code, out), (0, "image versions: nothing to bump\n"))
self.assertEqual(self.versions(), IMAGE_YML.format(mol="0.1.0", ci="0.2.0"))
def test_bumps_exactly_the_changed_image_and_check_passes(self):
self.write("requirements-dev.txt", "a==2\n") # an input of ci only
code, out, err = self.bump()
self.assertEqual(code, 0, err)
self.assertEqual(out, "image versions: bumped ci 0.2.0 -> 0.2.1\n")
self.assertEqual(self.versions(), IMAGE_YML.format(mol="0.1.0", ci="0.2.1"))
self.commit("deps and bump")
self.assertEqual(self.script("check_image_versions.py", "bump", "main")[0], 0)
def test_check_fails_without_the_bump(self):
self.write("images/mol/Dockerfile", "FROM y\n")
self.commit("input only")
self.assertEqual(self.script("check_image_versions.py", "bump", "main")[0], 1)
self.assertEqual(self.bump()[0], 0)
self.commit("bump")
self.assertEqual(self.script("check_image_versions.py", "bump", "main")[0], 0)
def test_idempotent_and_committed_changes_count(self):
self.write("images/mol/Dockerfile", "FROM y\n")
self.commit("input")
first = self.bump()
self.assertEqual(first[1], "image versions: bumped mol 0.1.0 -> 0.1.1\n")
second = self.bump()
self.assertEqual(second[1], "image versions: nothing to bump\n")
self.assertEqual(self.versions(), IMAGE_YML.format(mol="0.1.1", ci="0.2.0"))
def test_higher_version_already_present_is_kept(self):
self.write("images/mol/Dockerfile", "FROM y\n")
self.write(".gitea/workflows/image.yml", IMAGE_YML.format(mol="0.3.0", ci="0.2.0"))
self.assertEqual(self.bump()[1], "image versions: nothing to bump\n")
self.assertEqual(self.versions(), IMAGE_YML.format(mol="0.3.0", ci="0.2.0"))
def test_both_images_and_untracked_input(self):
self.write("images/mol/new-file", "x\n") # untracked
self.write("requirements-dev.txt", "a==2\n")
out = self.bump()[1]
self.assertEqual(out, "image versions: bumped ci 0.2.0 -> 0.2.1, mol 0.1.0 -> 0.1.1\n")
def test_bad_base_ref_is_a_clean_failure(self):
for ref in ("--output=x", "nope"):
code, _, err = self.bump(ref)
self.assertEqual(code, 1)
self.assertNotIn("Traceback", err)
self.assertEqual(len(err.strip().splitlines()), 1, err)
def test_usage(self):
self.assertEqual(self.bump("a", "b")[0], 2)
if __name__ == "__main__":
unittest.main()