"""Execute the package workflow's configuration and publishing shell steps.""" import os from pathlib import Path import shlex import subprocess import tempfile import textwrap import unittest ROOT = Path(__file__).resolve().parents[1] WORKFLOW = ROOT / ".github/workflows/packages.yml" def script(name, workflow=WORKFLOW): lines = workflow.read_text().splitlines() start = lines.index(" - name: " + name) + 1 end = next((index for index in range(start, len(lines)) if lines[index].startswith(" - ")), len(lines)) block = lines[start:end] run = block.index(" run: |") + 1 return textwrap.dedent("\n".join(block[run:])) + "\n" class PackageWorkflowTest(unittest.TestCase): def test_node_app_package_paths_are_confined_to_the_release(self): workflow = ROOT / ".github/workflows/node-app-release.yml" environment = os.environ | { "APP": "photos", "REPOSITORY": "waozixyz/photos", "COMMIT": "1" * 40, "TAG": "node-photos-1.0.6", "SHA256": "2" * 64, "PACKAGE_PATH": ".", "LEDGER_PATH": "apps/versions.json", } for package_path, ledger_path in ((".", "apps/versions.json"), ("portable", "versions.json"), ("modules/photos", "apps/versions.json")): with self.subTest(package=package_path, ledger=ledger_path): subprocess.run(["bash", "-eu"], input=script("Check the request", workflow), text=True, capture_output=True, env=environment | {"PACKAGE_PATH": package_path, "LEDGER_PATH": ledger_path}, check=True) for variable in ("PACKAGE_PATH", "LEDGER_PATH"): for value in ("../photos", "/portable", "portable/../private", "", "$(false)", "portable\nprivate"): with self.subTest(variable=variable, value=value): result = subprocess.run( ["bash", "-eu"], input=script("Check the request", workflow), text=True, capture_output=True, env=environment | {variable: value}) self.assertNotEqual(result.returncode, 0) def test_node_app_signing_uses_the_selected_package_ledger(self): workflow = ROOT / ".github/workflows/node-app-release.yml" for package_path, ledger_path in ((".", "apps/versions.json"), ("portable", "versions.json")): with self.subTest(package=package_path), tempfile.TemporaryDirectory() as temporary: log = Path(temporary) / "commands" stub = "python3() { printf '%s\\n' \"$*\" >> " + shlex.quote(str(log)) + "; }\n" environment = os.environ | { "RUNNER_TEMP": temporary, "APP": "photos", "KEY_ID": "test", "TARGET": "test@example.invalid", "STORE": "/test/packages", "PACKAGE_PATH": package_path, "LEDGER_PATH": ledger_path, } subprocess.run(["bash", "-eu"], input=stub + script( "Sign, publish to Waozi's Mesh node and verify", workflow), text=True, capture_output=True, env=environment, check=True) command = log.read_text() self.assertIn(f"--app-ledger {temporary}/node-app/source/{package_path}/{ledger_path}", command) self.assertIn("--node-app photos", command) self.assertIn("--verify-origin https://api.waozi.xyz", command) def test_package_runtime_fixture_repairs_start_the_workflow(self): trigger = WORKFLOW.read_text().split(" push:\n", 1)[1].split(" workflow_dispatch:\n", 1)[0] for path in ("tests/package_release_test.py", "tests/package_manager_host.c", "tests/package_manager_behavior.zi"): self.assertIn(" - '" + path + "'", trigger) def test_private_node_configuration_is_masked_by_github(self): workflow = WORKFLOW.read_text() for name in ("INBE_NODE_KNOWN_HOSTS", "INBE_NODE_SSH_TARGET", "INBE_NODE_PACKAGE_STORE"): self.assertIn("secrets." + name, workflow) self.assertNotIn("vars." + name, workflow) def test_configuration_branches_and_private_files(self): cases = (({}, False, False), ({"SIGNING_KEY": "test-private-key", "KEY_ID": "test"}, True, False), ({"SIGNING_KEY": "test-private-key", "KEY_ID": "test", "SSH_KEY": "test-ssh-key", "SSH_HOSTS": "test-host", "TARGET": "test@example.invalid", "STORE": "/test/packages"}, True, True)) for values, signing, configured in cases: with self.subTest(signing=signing, configured=configured), tempfile.TemporaryDirectory() as temporary: directory = Path(temporary) output = directory / "output" env = {key: value for key, value in os.environ.items() if key not in ("SIGNING_KEY", "KEY_ID", "SSH_KEY", "SSH_HOSTS", "TARGET", "STORE")} env.update(values, RUNNER_TEMP=temporary, GITHUB_OUTPUT=str(output)) result = subprocess.run(["bash", "-eu"], input=script("Configure publisher"), text=True, capture_output=True, env=env, check=True) self.assertEqual(result.stderr, "") self.assertNotIn("test-private-key", result.stdout) self.assertNotIn("test-ssh-key", result.stdout) self.assertEqual(output.read_text(), f"signing={str(signing).lower()}\nconfigured={str(configured).lower()}\n") publisher = directory / "inbe-publisher" self.assertEqual(publisher.exists(), signing) if signing: self.assertEqual(publisher.stat().st_mode & 0o777, 0o700) expected = {"publisher.pem": "test-private-key\n"} if configured: expected.update({"node.key": "test-ssh-key\n", "known_hosts": "test-host\n"}) self.assertEqual({path.name for path in publisher.iterdir()}, set(expected)) for filename, content in expected.items(): path = publisher / filename self.assertEqual(path.read_text(), content) self.assertEqual(path.stat().st_mode & 0o777, 0o600) def test_sign_and_publish_command_steps(self): # Crypto/payload publication has a separate real integration test. # Here the actual workflow shell must invoke the correct commands and # must not execute a misplaced heredoc delimiter as a shell command. for name in ("Sign app releases", "Sign and publish to Waozi's Mesh node"): with self.subTest(step=name), tempfile.TemporaryDirectory() as temporary: log = Path(temporary) / "commands" stub = "python3() { printf '%s\\n' \"$*\" >> " + shlex.quote(str(log)) + "; }\n" env = os.environ | {"RUNNER_TEMP": temporary, "KEY_ID": "test", "TARGET": "test@example.invalid", "STORE": "/test/packages"} result = subprocess.run(["bash", "-eu"], input=stub + script(name), text=True, capture_output=True, env=env, check=True) self.assertEqual(result.stderr, "") commands = log.read_text().splitlines() self.assertTrue(commands[-1].startswith("scripts/package-release.py --store build/package-releases ")) self.assertIn("--key-id test", commands[-1]) self.assertIn(f"--key {temporary}/inbe-publisher/publisher.pem", commands[-1]) if name == "Sign app releases": self.assertEqual(commands[0], "scripts/check-package-publishers.py") else: self.assertIn("--ssh-target test@example.invalid --remote-store /test/packages", commands[-1]) def test_apps_the_node_offers_publish_with_the_upload_key(self): with tempfile.TemporaryDirectory() as temporary: log = Path(temporary) / "commands" stub = ("python3() { printf '%s %s %s\\n' \"$INBE_SSH_IDENTITY\" \"$INBE_SSH_KNOWN_HOSTS\" \"$*\" >> " + shlex.quote(str(log)) + "; }\n") env = os.environ | {"RUNNER_TEMP": temporary, "KEY_ID": "test", "TARGET": "test@example.invalid", "STORE": "/test/packages"} result = subprocess.run(["bash", "-eu"], input=stub + script("Publish new releases of the apps the node offers"), text=True, capture_output=True, env=env, check=True) self.assertEqual(result.stderr, "") command = log.read_text().splitlines()[-1] self.assertTrue(command.startswith(f"{temporary}/inbe-publisher/node.key " f"{temporary}/inbe-publisher/known_hosts " "scripts/package-release.py --node-apps ")) for argument in ("--key-id test", "--ssh-target test@example.invalid", "--remote-store /test/packages", "--verify-origin https://api.waozi.xyz"): self.assertIn(argument, command) def test_actions_are_pinned_to_commits(self): for path in (WORKFLOW, ROOT / ".github/actions/ziran-packages/action.yml"): for line in path.read_text().splitlines(): if "uses: actions/" in line: reference = line.split("@", 1)[1].split()[0] self.assertRegex(reference, r"\A[0-9a-f]{40}\Z", line) if __name__ == "__main__": unittest.main()