diff --git a/.github/scripts/release-rpm-smoke.py b/.github/scripts/release-rpm-smoke.py index 73dc871f1d..3d70cca7c1 100644 --- a/.github/scripts/release-rpm-smoke.py +++ b/.github/scripts/release-rpm-smoke.py @@ -7,63 +7,68 @@ import secrets import socket import subprocess import time +import tempfile -results = Path("/results") -requirements = (results / "rpm-requires.txt").read_text() -for capability in ( - "libc.so.6()(64bit)", - "libm.so.6(GLIBC_2.35)(64bit)", - "libgcc_s.so.1()(64bit)", - "libxkbcommon.so.0()(64bit)", - "(libvulkan.so.1()(64bit) or libEGL.so.1()(64bit))", -): - if capability not in requirements.splitlines(): - raise RuntimeError(f"Missing RPM requirement: {capability}") -version = subprocess.check_output(["vectorcraft-cli", "--version"], text=True) -(results / "cli-version.txt").write_text(version) -# Original fixture authored for this package smoke test; no external artwork. -fixture = results / "original-smoke.svg" -fixture.write_text('' - '' - '') -token = secrets.token_hex(32) -env = dict(os.environ, VECTORCRAFT_NO_PREFS="1", XDG_RUNTIME_DIR="/tmp/vectorcraft-runtime", - VECTORCRAFT_CONTROL_TOKEN=token) -Path(env["XDG_RUNTIME_DIR"]).mkdir(mode=0o700, exist_ok=True) -with (results / "gui.log").open("w") as log: - app = subprocess.Popen([ - "vectorcraft", "--control", "32180", "--automation-read-root", str(results), - "--automation-write-root", str(results), str(fixture), - ], stdout=log, stderr=subprocess.STDOUT, env=env) - try: - deadline = time.monotonic() + 90 - while True: - if app.poll() is not None: - raise RuntimeError(f"GUI exited during startup: {app.returncode}") - try: - channel = socket.create_connection(("127.0.0.1", 32180), timeout=5) - break - except OSError: - if time.monotonic() >= deadline: - raise RuntimeError("GUI control channel did not become ready") from None - time.sleep(1) - channel.settimeout(30) - with channel, channel.makefile("rwb") as stream: - for request_id, method, params in ( - (0, "auth", {"token": token}), - (1, "ui.inspect", {}), - ): - stream.write((json.dumps(dict(id=request_id, method=method, params=params)) + "\n").encode()) - stream.flush() - response = json.loads(stream.readline()) - (results / f"{method}.json").write_text(json.dumps(response, indent=2)) - if response.get("id") != request_id or response.get("ok") is not True: - raise RuntimeError(f"GUI request failed: {method}") - finally: - app.terminate() +def run_smoke(results): + requirements = (results / "rpm-requires.txt").read_text() + for capability in ( + "libc.so.6()(64bit)", + "libm.so.6(GLIBC_2.35)(64bit)", + "libgcc_s.so.1()(64bit)", + "libxkbcommon.so.0()(64bit)", + "(libvulkan.so.1()(64bit) or libEGL.so.1()(64bit))", + ): + if capability not in requirements.splitlines(): + raise RuntimeError(f"Missing RPM requirement: {capability}") + + version = subprocess.check_output(["vectorcraft-cli", "--version"], text=True) + (results / "cli-version.txt").write_text(version) + # Original fixture authored for this package smoke test; no external artwork. + fixture = results / "original-smoke.svg" + fixture.write_text('' + '' + '') + token = secrets.token_hex(32) + with tempfile.TemporaryDirectory(prefix="vectorcraft-runtime-") as runtime, (results / "gui.log").open("w") as log: + env = dict(os.environ, VECTORCRAFT_NO_PREFS="1", XDG_RUNTIME_DIR=runtime, + VECTORCRAFT_CONTROL_TOKEN=token) + app = subprocess.Popen([ + "vectorcraft", "--control", "32180", "--automation-read-root", str(results), + "--automation-write-root", str(results), str(fixture), + ], stdout=log, stderr=subprocess.STDOUT, env=env) try: - app.wait(timeout=10) - except subprocess.TimeoutExpired: - app.kill() - app.wait() + deadline = time.monotonic() + 90 + while True: + if app.poll() is not None: + raise RuntimeError(f"GUI exited during startup: {app.returncode}") + try: + channel = socket.create_connection(("127.0.0.1", 32180), timeout=5) + break + except OSError: + if time.monotonic() >= deadline: + raise RuntimeError("GUI control channel did not become ready") from None + time.sleep(1) + channel.settimeout(30) + with channel, channel.makefile("rwb") as stream: + for request_id, method, params in ( + (0, "auth", {"token": token}), + (1, "ui.inspect", {}), + ): + stream.write((json.dumps(dict(id=request_id, method=method, params=params)) + "\n").encode()) + stream.flush() + response = json.loads(stream.readline()) + (results / f"{method}.json").write_text(json.dumps(response, indent=2)) + if response.get("id") != request_id or response.get("ok") is not True: + raise RuntimeError(f"GUI request failed: {method}") + finally: + app.terminate() + try: + app.wait(timeout=10) + except subprocess.TimeoutExpired: + app.kill() + app.wait() + + +if __name__ == "__main__": + run_smoke(Path("/results")) diff --git a/.github/scripts/tests/test_release_rpm_smoke.py b/.github/scripts/tests/test_release_rpm_smoke.py new file mode 100644 index 0000000000..9d92237534 --- /dev/null +++ b/.github/scripts/tests/test_release_rpm_smoke.py @@ -0,0 +1,116 @@ +"""Exercise failures that an installed-package happy-path check cannot reproduce.""" +import importlib.util +import json +from pathlib import Path +import subprocess +import tempfile +import unittest +from unittest.mock import MagicMock, patch + +spec = importlib.util.spec_from_file_location("rpm_smoke", Path(__file__).parents[1] / "release-rpm-smoke.py") +smoke = importlib.util.module_from_spec(spec) +spec.loader.exec_module(smoke) + + +class SmokeLifecycleTests(unittest.TestCase): + def setUp(self): + self.folder = tempfile.TemporaryDirectory() + self.addCleanup(self.folder.cleanup) + self.results = Path(self.folder.name) + (self.results / "rpm-requires.txt").write_text("\n".join([ + "libc.so.6()(64bit)", "libm.so.6(GLIBC_2.35)(64bit)", + "libgcc_s.so.1()(64bit)", "libxkbcommon.so.0()(64bit)", + "(libvulkan.so.1()(64bit) or libEGL.so.1()(64bit))", + ])) + self.app = MagicMock() + self.app.poll.return_value = None + self.channel = MagicMock() + self.stream = self.channel.makefile.return_value.__enter__.return_value + self.stream.readline.side_effect = [ + b'{"id":0,"ok":true}\n', b'{"id":1,"ok":true,"result":{"window":"fixture"}}\n', + ] + self.runtime = None + + def launch(*args, **kwargs): + self.runtime = Path(kwargs["env"]["XDG_RUNTIME_DIR"]) + self.assertTrue(self.runtime.is_dir()) + return self.app + + self.launch = self.start_patch(smoke.subprocess, "Popen", side_effect=launch) + self.start_patch(smoke.subprocess, "check_output", return_value="vectorcraft-cli 0.25.0\n") + self.connect = self.start_patch(smoke.socket, "create_connection", return_value=self.channel) + self.start_patch(smoke.time, "sleep") + + def start_patch(self, owner, name, **kwargs): + patcher = patch.object(owner, name, **kwargs) + result = patcher.start() + self.addCleanup(patcher.stop) + return result + + def assert_cleaned_up(self): + self.app.terminate.assert_called_once() + self.assertFalse(self.runtime.exists()) + + def test_success_records_inspection_and_removes_private_runtime(self): + smoke.run_smoke(self.results) + report = json.loads((self.results / "ui.inspect.json").read_text()) + self.assertEqual(report["result"], {"window": "fixture"}) + requests = [json.loads(call.args[0]) for call in self.stream.write.call_args_list] + self.assertEqual(requests[0]["method"], "auth") + self.assertEqual(requests[0]["params"]["token"], self.launch.call_args.kwargs["env"]["VECTORCRAFT_CONTROL_TOKEN"]) + self.assert_cleaned_up() + + def test_missing_runtime_dependency_prevents_launch(self): + (self.results / "rpm-requires.txt").write_text("") + with self.assertRaisesRegex(RuntimeError, "Missing RPM requirement"): + smoke.run_smoke(self.results) + self.launch.assert_not_called() + + def test_child_exit_during_startup_is_a_failure(self): + self.app.poll.return_value = 1 + self.app.returncode = 1 + with self.assertRaisesRegex(RuntimeError, "exited during startup"): + smoke.run_smoke(self.results) + self.assert_cleaned_up() + + def test_connection_is_retried_until_ready(self): + self.connect.side_effect = [OSError("not ready"), self.channel] + smoke.run_smoke(self.results) + self.assertEqual(self.connect.call_count, 2) + self.assert_cleaned_up() + + def test_startup_timeout_stops_child(self): + self.connect.side_effect = OSError("not ready") + self.start_patch(smoke.time, "monotonic", side_effect=[0, 91]) + with self.assertRaisesRegex(RuntimeError, "did not become ready"): + smoke.run_smoke(self.results) + self.assert_cleaned_up() + + def test_auth_refusal_prevents_inspection(self): + self.stream.readline.side_effect = [b'{"id":0,"ok":false}\n'] + with self.assertRaisesRegex(RuntimeError, "request failed: auth"): + smoke.run_smoke(self.results) + self.assertFalse((self.results / "ui.inspect.json").exists()) + self.assert_cleaned_up() + + def test_response_for_another_request_is_rejected(self): + self.stream.readline.side_effect = [b'{"id":0,"ok":true}\n', b'{"id":99,"ok":true}\n'] + with self.assertRaisesRegex(RuntimeError, "request failed: ui.inspect"): + smoke.run_smoke(self.results) + self.assert_cleaned_up() + + def test_malformed_response_still_stops_child(self): + self.stream.readline.side_effect = [b'not json\n'] + with self.assertRaises(json.JSONDecodeError): + smoke.run_smoke(self.results) + self.assert_cleaned_up() + + def test_unresponsive_child_is_killed_after_termination_timeout(self): + self.app.wait.side_effect = [subprocess.TimeoutExpired("vectorcraft", 10), 0] + smoke.run_smoke(self.results) + self.app.kill.assert_called_once() + self.assert_cleaned_up() + + +if __name__ == "__main__": + unittest.main() diff --git a/.github/workflows/release-rpm-smoke.yml b/.github/workflows/release-rpm-smoke.yml index 4140f57f3d..61948e0f35 100644 --- a/.github/workflows/release-rpm-smoke.yml +++ b/.github/workflows/release-rpm-smoke.yml @@ -11,13 +11,14 @@ on: paths: - .github/workflows/release-rpm-smoke.yml - .github/scripts/release-rpm-smoke.py + - .github/scripts/tests/test_release_rpm_smoke.py -permissions: - contents: read - actions: read +permissions: {} jobs: source: + permissions: + actions: read runs-on: ubuntu-24.04 timeout-minutes: 5 outputs: @@ -43,6 +44,9 @@ jobs: echo '::error::No release run has both Linux package artifacts available' exit 1 smoke: + permissions: + contents: read + actions: read needs: source timeout-minutes: 25 strategy: diff --git a/.github/workflows/sonarcloud.yml b/.github/workflows/sonarcloud.yml index 4cd4eb6387..223859d9b9 100644 --- a/.github/workflows/sonarcloud.yml +++ b/.github/workflows/sonarcloud.yml @@ -21,4 +21,7 @@ jobs: # The reusable workflow defaults build-jobs to 2 so a cold instrumented # archive stays within hosted-runner memory (craft-common-actions#333). uses: storytold/craft-common-actions/.github/workflows/sonarcloud.yml@main + with: + python-coverage-source: .github/scripts + python-coverage-script: .github/scripts/tests/test_release_rpm_smoke.py secrets: inherit