Files
mesh/scripts/test_gitea_workflow.py
T
waozi 6888d84371 Report coverage in module mode and clear stale CI images
go tool cover lists the profiled packages in vendor mode, which the
liboqs vendor folder fails, so the checks job failed after every test
passed. A cancelled container job also leaves its image behind until the
Mesh runner refuses work below 2 GiB free; each build now removes earlier
job images first.
2026-11-22 18:08:59 -03:00

160 lines
8.7 KiB
Python

#!/usr/bin/env python3
"""Check the Gitea deployment graph, pinned inputs, and runner shell scripts."""
from pathlib import Path
import re
import subprocess
import unittest
import yaml
from verify_build_inputs import EXPECTED_GOLANGCI_LINT_SHA256, EXPECTED_GOLANGCI_LINT_VERSION
ROOT = Path(__file__).resolve().parents[1]
WORKFLOW = ROOT / ".gitea/workflows/ci.yml"
ACTION_REF = re.compile(r"^https://(?:github\.com|git\.waozi\.xyz)/[\w./-]+@[a-f0-9]{40}$")
REQUIRED_JOBS = {"checks", "container"}
SHARED_SLOT_SETUP = ("./.gitea/actions/generate", "./.gitea/actions/liboqs")
def read_yaml(path):
return yaml.load(path.read_text(), Loader=yaml.BaseLoader)
class GiteaWorkflowTest(unittest.TestCase):
def setUp(self):
self.workflow = read_yaml(WORKFLOW)
self.jobs = self.workflow["jobs"]
def test_canonical_deploy_requires_every_gate(self):
self.assertEqual(self.workflow["on"]["push"]["branches"], ["master"])
self.assertEqual(set(self.workflow["on"]), {"push", "workflow_dispatch", "schedule"})
self.assertEqual(self.workflow["permissions"], {"contents": "read"})
deploy = self.jobs["deploy"]
self.assertEqual(set(deploy["needs"]), REQUIRED_JOBS)
self.assertEqual(set(self.jobs), REQUIRED_JOBS | {"deploy"})
for guard in (
"github.server_url == 'https://git.waozi.xyz'",
"github.repository == 'ziranlang/mesh'",
"github.ref == 'refs/heads/master'",
"(github.event_name == 'push' || github.event_name == 'workflow_dispatch')",
"vars.MESH_DEPLOY_READY == 'true'",
"vars.MESH_DEPLOY_RUNNER != ''",
):
self.assertIn(guard, deploy["if"])
self.assertNotIn("environment", deploy)
self.assertEqual(deploy["permissions"], {"contents": "read", "actions": "read"})
self.assertEqual(self.workflow["concurrency"]["cancel-in-progress"], "false")
self.assertEqual(set(read_yaml(ROOT / ".github/workflows/deploy.yml")["on"]), {"workflow_dispatch"})
for filename in ("ci.yml", "security.yml"):
self.assertEqual(set(read_yaml(ROOT / ".github/workflows" / filename)["on"]), {"workflow_call"})
def test_origin_gate_matches_the_deploy_needs(self):
origin = (ROOT / "deploy/gitea-origin.py").read_text()
match = re.search(r"^REQUIRED_JOBS = (\{.*?\})$", origin, re.MULTILINE | re.DOTALL)
self.assertIsNotNone(match)
self.assertEqual(eval(match.group(1), {}), set(self.jobs["deploy"]["needs"]))
# Gitea reports matrix jobs under suffixed names the origin would not match.
for job_id in REQUIRED_JOBS:
self.assertNotIn("strategy", self.jobs[job_id])
def test_shared_slot_sets_up_and_tests_once(self):
steps = self.jobs["checks"]["steps"]
uses = [step.get("uses") for step in steps]
for action in SHARED_SLOT_SETUP:
self.assertEqual(uses.count(action), 1)
runs = [step.get("run", "") for step in steps]
plain_tests = [run for run in runs if re.search(r"make test(\s|$)", run) and "-race" not in run]
self.assertEqual(len(plain_tests), 1)
self.assertIn("-coverprofile=coverage.out", plain_tests[0])
self.assertEqual(sum("-race" in run for run in runs), 1)
coverage = next(step for step in steps if step.get("name") == "Test and report coverage")
self.assertIn("GOFLAGS=-mod=mod go tool cover", coverage["run"])
native = next(step for step in steps if step.get("name") == "Native server suite")
self.assertIn("make sqlite-native microhttpd-native", native["run"])
self.assertIn("make test-ziran-native-server", native["run"])
for name in ("Lint", "Race test", "Scan generated Go and check dependency licenses"):
self.assertIn(name, [step.get("name") for step in steps])
scans = [run for run in runs if "govulncheck -scan=package . ./native" in run]
self.assertEqual(len(scans), 2)
script_tests = re.findall(r"python3 -B (scripts/test_\w+\.py)", "\n".join(runs))
self.assertEqual(len(script_tests), len(set(script_tests)))
setup_go = next(step for step in steps if "actions/setup-go@" in (step.get("uses") or ""))
self.assertEqual(setup_go["with"]["cache"], "false")
names = [step.get("name") for step in steps]
root = names.index("Use the installed Go root")
self.assertEqual(root, steps.index(setup_go) + 1)
self.assertIn('echo "GOROOT=$root" >> "$GITHUB_ENV"', steps[root]["run"])
def test_container_probe_stays_inside_its_network_namespace(self):
steps = self.jobs["container"]["steps"]
verify = next(step for step in steps if step.get("name") == "Verify container starts as a non-root user")
self.assertNotIn(" -p ", verify["run"])
self.assertIn('--network "container:$container"', verify["run"])
self.assertRegex(verify["env"]["PROBE_IMAGE"], r"^[\w./-]+:[\w.-]+@sha256:[0-9a-f]{64}$")
def test_generated_go_reuses_the_runner_package_cache(self):
steps = read_yaml(ROOT / ".gitea/actions/generate/action.yml")["runs"]["steps"]
generate = next(step for step in steps if step.get("name") == "Generate the Go")
self.assertNotIn("XDG_CACHE_HOME", generate.get("env", {}))
self.assertNotIn("runner.temp", str(generate))
env = generate["env"]
rewrites = {env[f"GIT_CONFIG_VALUE_{index}"]: env[f"GIT_CONFIG_KEY_{index}"]
for index in range(int(env["GIT_CONFIG_COUNT"]))}
for organization in ("ziranlang", "kryonlabs"):
self.assertEqual(rewrites[f"https://github.com/{organization}/"],
f"url.https://git.waozi.xyz/{organization}/.insteadOf")
def test_pinned_actions_and_job_shell_syntax(self):
step_groups = [job["steps"] for job in self.jobs.values()]
for action_file in (ROOT / ".gitea/actions").glob("*/action.yml"):
step_groups.append(read_yaml(action_file)["runs"]["steps"])
for steps in step_groups:
for step in steps:
action = step.get("uses")
if action and not action.startswith("./"):
self.assertRegex(action, ACTION_REF)
if "actions/checkout@" in (action or ""):
self.assertEqual(step["with"]["persist-credentials"], "false")
self.assertEqual(step["with"]["submodules"], "recursive")
if "run" in step:
with self.subTest(step=step.get("name")):
bash = re.sub(r"\$\{\{.*?\}\}", "0" * 40, step["run"])
subprocess.run(["bash", "-n"], text=True, input=bash, check=True)
container = self.jobs["container"]["steps"]
build = next(step for step in container if step.get("name") == "Build container image")
scan = next(step for step in container if step.get("name") == "Scan image")
self.assertIn('mesh:ci-$GITHUB_RUN_ID', build["run"])
self.assertEqual(scan["with"]["image-ref"], "mesh:ci-${{ github.run_id }}")
self.assertEqual(scan["with"]["exit-code"], "1")
self.assertEqual(scan["with"]["severity"], "CRITICAL,HIGH")
self.assertEqual(scan["with"]["skip-setup-trivy"], "true")
install = next(step for step in container if step.get("name") == "Install the pinned image scanner")
self.assertLess(container.index(install), container.index(scan))
self.assertRegex(install["env"]["TRIVY_SHA256"], r"^[0-9a-f]{64}$")
self.assertLess(install["run"].index("sha256sum --check --strict"), install["run"].index("tar --extract"))
source = WORKFLOW.read_text()
for job_id in REQUIRED_JOBS:
self.assertNotRegex(str(self.jobs[job_id]), r"\$\{\{\s*secrets\.")
self.assertNotIn("ssh-keyscan", source)
self.assertNotIn("~/.ssh", source)
deploy = self.jobs["deploy"]
origin = next(step for step in deploy["steps"] if step.get("name") == "Deploy the verified Gitea artifact")
self.assertIn("printf '%s' \"$GITEA_JOB_TOKEN\" | ssh", origin["run"])
self.assertIn('deploy "$GITHUB_RUN_ID" "$GITHUB_RUN_ATTEMPT" "$GITHUB_SHA"', origin["run"])
self.assertNotIn("/opt/mesh-src", source)
def test_linter_pins_and_checksum_are_retained(self):
step = next(item for item in self.jobs["checks"]["steps"] if item.get("name") == "Install golangci-lint")
self.assertEqual(step["env"], {
"GOLANGCI_LINT_VERSION": EXPECTED_GOLANGCI_LINT_VERSION,
"GOLANGCI_LINT_SHA256": EXPECTED_GOLANGCI_LINT_SHA256,
})
self.assertIn("sha256sum --check --strict", step["run"])
self.assertLess(step["run"].index("sha256sum"), step["run"].index("tar --extract"))
if __name__ == "__main__":
unittest.main()