Files
MUA-Mikes-Unraid-Agent/scripts/test-release.py
T
Jean-Luc Picode 4add04e94d release: MUA 2026.11.05.r034
Publish optional native HTTPS and explicit Unraid WebGUI PEM reuse from 1677894. Synchronize all release versions and TXZ/manifest/checksum, use transport-aware authenticated local post-install readiness, preserve r033, and document operator-only installation/trust/renewal. Verified 68 source tests, 31 extracted-r034 transport tests, 52 PHP checks and all 7 release/archive/hook gates; final independent fresh-context AI review passed without blocking security/logic findings. No production installation or configuration changes.
2026-11-05 17:50:41 +01:00

153 lines
8.1 KiB
Python

#!/usr/bin/env python3
"""Release/manifest checks without installing or touching a production host.
Run: python3 scripts/test-release.py
After building, also set MUA_RELEASE_ARCHIVE=/absolute/path/to/the.txz.
Only the post-install INLINE is executed, with its service path redirected to
an isolated test stub. Real rc.mua/TLS/MCP integration is covered by tls.test.ts.
"""
import hashlib
import json
import os
from pathlib import Path, PurePosixPath
import re
import subprocess
import tarfile
import tempfile
import unittest
import xml.etree.ElementTree as ET
ROOT = Path(__file__).resolve().parent.parent
BASE_URL = "https://git.casaderoll.de/michael/MUA-Mikes-Unraid-Agent/raw/branch/main"
def manifest():
# ElementTree expands this manifest's local internal entities, not remote DTDs.
return ET.fromstring((ROOT / "plugin/mua.plg").read_text())
def post_install():
return next(node.text for node in manifest().findall("FILE/INLINE")
if "rc.mua restart" in (node.text or ""))
def version():
return json.loads((ROOT / "package.json").read_text())["version"]
class ReleaseTests(unittest.TestCase):
def test_version_is_consistent(self):
current = version()
self.assertRegex(current, r"^\d{4}\.\d{2}\.\d{2}\.r\d{3}$")
self.assertEqual(manifest().attrib["version"], current)
self.assertEqual(json.loads((ROOT / "plugin/plugin.json").read_text())["version"], current)
for path in ("src/helpers.ts", "mcp/server.php", "mcp/helpers.php"):
with self.subTest(path=path):
declared = re.search(r'MUA_VERSION\s*=\s*[\'\"]([^\'\"]+)', (ROOT / path).read_text())
self.assertIsNotNone(declared)
self.assertEqual(declared[1], current)
self.assertTrue((manifest().findtext("CHANGES") or "").strip().startswith("### " + current))
def test_plugin_has_expected_https_download_and_install_path(self):
plugin = manifest()
self.assertEqual(plugin.attrib["pluginURL"], BASE_URL + "/plugin/mua.plg")
self.assertEqual(plugin.attrib["min"], "7.0.0")
payloads = [node for node in plugin.findall("FILE") if node.find("URL") is not None]
self.assertEqual(len(payloads), 1)
payload = payloads[0]
name = f"mua-{version()}-x86_64-1.txz"
self.assertEqual(payload.findtext("URL"), BASE_URL + "/dist/" + name)
self.assertEqual(payload.attrib["Name"], "/boot/config/plugins/mua/" + name)
self.assertEqual(payload.attrib["Run"], "upgradepkg --install-new")
self.assertRegex(payload.findtext("SHA256") or "", r"^[a-f0-9]{64}$")
def run_isolated_hook(self, healthy, scheme):
with tempfile.TemporaryDirectory(prefix="mua-release-hook-") as tmp:
directory = Path(tmp)
service = directory / "rc.mua"
service.write_text('#!/bin/sh\nprintf "%s\\n" "$1" >> "$CALLS"\n'
'if [ "$1" = health ]; then\n'
' [ "$HEALTHY" = 1 ] || exit 1\n'
' printf \'{"status":"ok","scheme":"%s"}\\n\' "$SCHEME"\n'
'fi\n')
service.chmod(0o755)
# Never contact even a local production service: shadow obsolete curl.
for name, body in {"sleep": "exit 0", "curl": 'printf "UNEXPECTED_CURL\\n" >> "$CALLS"; exit 22',
"hostname": "printf '127.0.0.1\\n'"}.items():
command = directory / name
command.write_text("#!/bin/sh\n" + body + "\n")
command.chmod(0o755)
calls = directory / "calls"
script = post_install().replace("/etc/rc.d/rc.mua", str(service))
result = subprocess.run(["bash"], input=script, text=True, capture_output=True, timeout=5,
env={**os.environ, "PATH": str(directory) + ":" + os.environ["PATH"],
"CALLS": str(calls), "HEALTHY": "1" if healthy else "0", "SCHEME": scheme})
return result, calls.read_text().splitlines()
def test_post_install_uses_transport_aware_readiness_for_http_and_https(self):
for scheme in ("http", "https"):
with self.subTest(scheme=scheme):
result, calls = self.run_isolated_hook(True, scheme)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(calls, ["restart", "health"])
self.assertIn('"scheme":"' + scheme + '"', result.stdout)
self.assertNotIn("WARNUNG", result.stdout)
self.assertNotIn("http://", result.stdout)
def test_post_install_reports_failed_readiness(self):
result, calls = self.run_isolated_hook(False, "https")
self.assertEqual(result.returncode, 0) # Installer displays the actionable warning.
self.assertEqual(calls, ["restart", "health"])
self.assertIn("WARNUNG", result.stdout)
self.assertNotIn("Bereitschaft: OK", result.stdout)
def test_all_inline_hooks_are_valid_bash_without_executing_them(self):
for index, node in enumerate(manifest().findall("FILE/INLINE")):
with self.subTest(hook=index):
result = subprocess.run(["bash", "-n"], input=node.text, text=True, capture_output=True)
self.assertEqual(result.returncode, 0, result.stderr)
def test_r033_archive_is_not_overwritten(self):
archive = ROOT / "dist/mua-2026.11.01.r033-x86_64-1.txz"
self.assertEqual(hashlib.sha256(archive.read_bytes()).hexdigest(),
"ee85aa33230dbb6fe561f8c0000cb854bd542b0da5ad6c1a9100320fc2d5267f")
@unittest.skipUnless(os.environ.get("MUA_RELEASE_ARCHIVE"), "set MUA_RELEASE_ARCHIVE after building")
def test_built_archive_matches_manifest_and_expected_files(self):
archive = Path(os.environ["MUA_RELEASE_ARCHIVE"])
payload = next(node for node in manifest().findall("FILE") if node.find("URL") is not None)
self.assertEqual(archive.name, PurePosixPath(payload.attrib["Name"]).name)
self.assertEqual(hashlib.sha256(archive.read_bytes()).hexdigest(), payload.findtext("SHA256"))
with tarfile.open(archive, "r:xz") as package:
for entry in package.getmembers():
with self.subTest(member=entry.name):
self.assertEqual((entry.uid, entry.gid), (0, 0))
self.assertFalse(entry.name.startswith("/"))
self.assertNotIn("..", PurePosixPath(entry.name).parts)
self.assertNotIn(entry.name, (".", "./"))
self.assertFalse(entry.issym() or entry.islnk() or entry.isdev())
self.assertTrue(PurePosixPath(entry.name).parts[0] in ("etc", "install", "usr"))
self.assertFalse(entry.name.endswith((".key", ".pem", ".conf")))
paths = {
"etc/rc.d/rc.mua": "scripts/rc.mua",
"usr/local/emhttp/plugins/mua/mua.page": "scripts/mua.page",
"usr/local/bin/unraid-docker-mcp-helper.php": "scripts/unraid-docker-mcp-helper.php",
"usr/local/bin/unraid-mcp-status-helper.php": "scripts/unraid-mcp-status-helper.php",
"usr/local/bin/unraid-admin-helper.php": "scripts/unraid-admin-helper.php",
f"usr/doc/mua-{version()}/HTTPS.md": "docs/HTTPS.md",
f"usr/doc/mua-{version()}/HERMES.md": "docs/HERMES.md",
}
for packaged, source in paths.items():
self.assertEqual(package.extractfile(packaged).read(), (ROOT / source).read_bytes())
for executable in ("usr/local/bin/mua", "etc/rc.d/rc.mua", "install/doinst.sh"):
self.assertEqual(package.getmember(executable).mode, 0o755)
hook = package.extractfile("install/doinst.sh").read().decode()
self.assertIn("rc.mua health", hook)
self.assertNotIn("http://127.0.0.1:3002", hook)
result = subprocess.run(["bash", "-n"], input=hook, text=True, capture_output=True)
self.assertEqual(result.returncode, 0, result.stderr)
if __name__ == "__main__":
unittest.main(verbosity=2)