mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-30 18:25:34 +01:00
Harden RPM smoke lifecycle and cover failure paths
This commit is contained in:
1 parent
76aa6afeb0
commit
bcd1fcac61
4 files changed
+188
-60
No files matched your search
@@ -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()
|
||||
Reference in new issue
Block a user