Files
NODEDC_DEVICE_CORE/infra/ci/test_qualify.py
DCCONSTRUCTIONS 341d1f87e6
Device Core qualification / qualify (push) Successful in 5m54s
Qualify the standalone Core runtime lock used by Docker
2026-09-30 17:51:38 +03:00

193 lines
9.7 KiB
Python

import importlib.util
import io
import json
from contextlib import redirect_stdout
from pathlib import Path
import subprocess
import tempfile
import unittest
from unittest import mock
SPEC = importlib.util.spec_from_file_location("qualify", Path(__file__).with_name("qualify.py"))
CI = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(CI)
class QualificationTests(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.root = Path(self.temp.name) / "source"
self.root.mkdir()
self.git("init", "--quiet")
self.git("config", "user.name", "CI Test")
self.git("config", "user.email", "ci@example.invalid")
(self.root / "source.txt").write_text("committed\n")
self.git("add", "source.txt")
self.git("commit", "--quiet", "-m", "fixture")
self.commit = self.git("rev-parse", "HEAD").strip()
def git(self, *args):
return subprocess.check_output(["git", *args], cwd=self.root, text=True)
def test_clean_exact_commit_is_accepted(self):
CI.verify_checkout(self.root, self.commit)
def test_stale_commit_is_rejected(self):
with self.assertRaisesRegex(ValueError, "source_commit_mismatch"):
CI.verify_checkout(self.root, "a" * 40)
def test_branch_name_is_not_a_release_identity(self):
with self.assertRaisesRegex(ValueError, "full_commit_sha_required"):
CI.verify_checkout(self.root, "main")
def test_local_edits_cannot_be_credited_to_a_commit(self):
(self.root / "source.txt").write_text("uncommitted\n")
with self.assertRaisesRegex(ValueError, "dirty_source_checkout"):
CI.verify_checkout(self.root, self.commit)
def test_untracked_source_is_rejected(self):
(self.root / "extra.mjs").write_text("export default 1")
with self.assertRaisesRegex(ValueError, "dirty_source_checkout"):
CI.verify_checkout(self.root, self.commit)
def test_parent_git_repository_is_not_accepted_as_a_dependency(self):
child = self.root / "child"
child.mkdir()
with self.assertRaisesRegex(ValueError, "independent_checkout_required"):
CI.verify_checkout(child, self.commit)
def test_existing_operator_dependencies_are_never_overwritten(self):
dependency = self.root.parent / "NODEDC_DESIGN_GUIDELINE"
dependency.mkdir()
marker = dependency / "local-work"
marker.write_text("preserve")
with self.assertRaisesRegex(ValueError, "dependency_destination_exists"):
CI.fetch_dependencies(self.root, {})
self.assertEqual(marker.read_text(), "preserve")
def test_lock_cannot_fetch_a_different_repository(self):
lock = json.loads(Path(__file__).with_name("sources.lock.json").read_text())
lock["dependencies"]["platform"]["url"] = "https://example.invalid/other.git"
(self.root / "infra/ci").mkdir(parents=True)
(self.root / "infra/ci/sources.lock.json").write_text(json.dumps(lock))
with self.assertRaisesRegex(ValueError, "invalid_dependency_identity:platform"):
CI.read_lock(self.root)
def test_digest_is_path_independent_and_changes_with_content(self):
a, b = self.root / "a", self.root / "b"
for directory in (a, b):
directory.mkdir()
(directory / "index.html").write_text("same")
self.assertEqual(CI.tree_digest(a), CI.tree_digest(b))
(b / "index.html").write_text("different")
self.assertNotEqual(CI.tree_digest(a), CI.tree_digest(b))
def test_build_cannot_include_external_symlinks(self):
output = self.root / "dist"
output.mkdir()
(output / "index.html").symlink_to(self.root / "source.txt")
with self.assertRaisesRegex(ValueError, "symlink_in_build_output"):
CI.tree_digest(output)
def test_failed_step_never_writes_success_report_or_runs_later_steps(self):
report = self.root.parent / "result.json"
lock = {"node": "v22.21.0", "npm": "10.9.4"}
with mock.patch.object(CI, "verify_sources"), mock.patch.object(
CI, "run", side_effect=["v22.21.0\n", "10.9.4\n", subprocess.CalledProcessError(1, "npm")]
) as execute:
with self.assertRaises(subprocess.CalledProcessError):
CI.qualify(self.root, self.commit, lock, report)
self.assertEqual(execute.call_count, 3)
self.assertFalse(report.exists())
def test_existing_report_cannot_be_mistaken_for_new_success(self):
report = self.root.parent / "result.json"
report.write_text('{"sourceCommit":"old"}')
with self.assertRaisesRegex(ValueError, "qualification_report_already_exists"):
CI.qualify(self.root, self.commit, {}, report)
self.assertEqual(json.loads(report.read_text())["sourceCommit"], "old")
def test_network_install_error_recovers_using_the_same_frozen_command(self):
failure = subprocess.CalledProcessError(152, "npm", output="npm error code ECONNRESET\n")
with mock.patch.object(CI, "run", side_effect=[failure, "installed\n"]) as execute, \
mock.patch.object(CI.time, "sleep") as pause, redirect_stdout(io.StringIO()):
CI.install_dependencies(self.root)
self.assertEqual(execute.call_count, 2)
self.assertEqual(execute.call_args_list[0], execute.call_args_list[1])
self.assertEqual(execute.call_args.args[0], ["npm", "ci", "--ignore-scripts", "--no-audit", "--no-fund"])
pause.assert_called_once_with(5)
def test_core_runtime_uses_its_own_lock_outside_the_workspace(self):
source = self.root / "services/device-control-core"
source.mkdir(parents=True)
for name, value in (("package.json", '{"name":"core"}'),
("package-lock.json", '{"name":"standalone-lock"}')):
(source / name).write_text(value)
(self.root / "package-lock.json").write_text('{"name":"workspace-lock"}')
temporary_paths = []
def inspect_install(path, *, production):
self.assertTrue(production)
self.assertFalse(path.is_relative_to(self.root))
self.assertEqual((path / "package-lock.json").read_text(), '{"name":"standalone-lock"}')
self.assertEqual(sorted(p.name for p in path.iterdir()), ["package-lock.json", "package.json"])
temporary_paths.append(path)
with mock.patch.object(CI, "install_dependencies", side_effect=inspect_install), \
mock.patch.object(CI, "run") as execute:
CI.qualify_core_runtime(self.root)
self.assertIn("await import('oauth4webapi')", execute.call_args.args[0][-1])
self.assertFalse(temporary_paths[0].exists())
def test_core_runtime_lock_failure_stops_before_imports(self):
source = self.root / "services/device-control-core"
source.mkdir(parents=True)
for name in ("package.json", "package-lock.json"):
(source / name).write_text('{}')
failure = subprocess.CalledProcessError(1, "npm", output="npm error code EUSAGE\n")
with mock.patch.object(CI, "install_dependencies", side_effect=failure), \
mock.patch.object(CI, "run") as execute:
with self.assertRaises(subprocess.CalledProcessError):
CI.qualify_core_runtime(self.root)
execute.assert_not_called()
def test_permanent_mixed_and_unclassified_install_failures_never_retry(self):
for output in ("npm error code EINTEGRITY\n", "npm error code E401\n",
"npm error code EUSAGE\n", "download ECONNRESET in explanatory text\n",
"npm error code ECONNRESET\nnpm error code E403\n", ""):
with self.subTest(output=output), mock.patch.object(
CI, "run", side_effect=subprocess.CalledProcessError(1, "npm", output=output)
) as execute, mock.patch.object(CI.time, "sleep") as pause, redirect_stdout(io.StringIO()):
with self.assertRaises(subprocess.CalledProcessError):
CI.install_dependencies(self.root)
execute.assert_called_once()
pause.assert_not_called()
def test_install_retries_are_bounded_and_do_not_publish_a_report(self):
report = self.root.parent / "result.json"
failure = subprocess.CalledProcessError(1, "npm", output="npm error code ETIMEDOUT\n")
with mock.patch.object(CI, "verify_sources"), mock.patch.object(
CI, "run", side_effect=["v22.21.0\n", "10.9.4\n", failure, failure, failure]
) as execute, mock.patch.object(CI.time, "sleep") as pause, redirect_stdout(io.StringIO()):
with self.assertRaises(subprocess.CalledProcessError):
CI.qualify(self.root, self.commit, {"node": "v22.21.0", "npm": "10.9.4"}, report)
self.assertEqual(execute.call_count, 5)
self.assertEqual(pause.call_args_list, [mock.call(5), mock.call(10)])
self.assertFalse(report.exists())
def test_build_failure_is_not_retried_as_an_install(self):
report = self.root.parent / "result.json"
failure = subprocess.CalledProcessError(1, "npm", output="npm error code ECONNRESET\n")
with mock.patch.object(CI, "verify_sources"), mock.patch.object(
CI, "run", side_effect=["v22.21.0\n", "10.9.4\n", "installed\n", failure]
) as execute, mock.patch.object(CI.time, "sleep") as pause, redirect_stdout(io.StringIO()):
with self.assertRaises(subprocess.CalledProcessError):
CI.qualify(self.root, self.commit, {"node": "v22.21.0", "npm": "10.9.4"}, report)
self.assertEqual(execute.call_count, 4)
pause.assert_not_called()
self.assertFalse(report.exists())
if __name__ == "__main__":
unittest.main()