Privilege split 10/11: app-level tests with and without the hardware service

- tests/test_app.py runs the whole app in demo mode (the real HardwareService
  in-process on the demo fakes, reached through the client), in a temp folder
  on a spare port:
  - with the service: the fan groups, fan history, and profile and RAM
    lighting changes through the web API;
  - without it: the app carries on (telemetry, the app's own lighting), the
    state, health, alert, tray line and a 503 from the API all say the
    hardware service is unavailable; when it's back, everything recovers.
- The rest of stage 10 landed with the stages it belongs to: --demo on the
  in-process service (6), the service loop with no client (4), the client
  reconnecting (6) and the settings migration (9).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
paulandClaude Opus 5.5 committed 2026-12-05 19:52:40 +13:00
1 parent 8f91a38695
commit 0d5f470c4a
1 file changed
+103
+103
View File
@@ -0,0 +1,103 @@
import http.client
import json
import os
import socket
import tempfile
import time
import unittest
from unittest.mock import patch
from kurgan import app as kurgan_app
from kurgan import paths
from kurgan.hw.client import HardwareUnavailable
from kurgan.hw.pipe import PipeError
from kurgan.settings import Settings
def free_port() -> int:
with socket.socket() as s:
s.bind(("127.0.0.1", 0))
return s.getsockname()[1]
def wait_for(condition, timeout=10.0):
deadline = time.monotonic() + timeout
while not condition():
if time.monotonic() > deadline:
raise AssertionError("timed out")
time.sleep(0.05)
class AppTest(unittest.TestCase):
"""The whole app in demo mode (the real hardware service in-process, on fakes), in a temp folder."""
def setUp(self):
directory = tempfile.TemporaryDirectory()
self.addCleanup(directory.cleanup)
patcher = patch.object(paths, "DATA_DIR", directory.name) # demo-hw-settings.json goes here
patcher.start()
self.addCleanup(patcher.stop)
self.port = free_port()
args = kurgan_app.parse_args(["--demo", "--no-ai", "--no-tray", "--interval", "0.25"])
self.app = kurgan_app.Kurgan(args, Settings(os.path.join(directory.name, "settings.json"), None))
self.app.hw.poll_seconds = 0.1
self.connect = self.app.hw._connect
self.app.start(self.port, tray=False)
self.addCleanup(self.app.close)
def post(self, path, body):
connection = http.client.HTTPConnection("127.0.0.1", self.port, timeout=10)
try:
connection.request("POST", path, json.dumps(body),
{"Content-Type": "application/json", "X-Kurgan": "1", "Host": f"127.0.0.1:{self.port}"})
response = connection.getresponse()
return response.status, json.loads(response.read())
finally:
connection.close()
def test_the_demo_app_shows_and_drives_the_services_fans(self):
wait_for(lambda: self.app.state()["cooling"]["groups"])
state = self.app.state()
self.assertTrue(state["hardware"]["available"])
self.assertEqual([g["name"] for g in state["cooling"]["groups"]], ["GPU", "CPU"])
wait_for(lambda: self.app.history()["fans"]["rows"])
status, body = self.post("/api/cooling/profile", {"profile": "performance"})
self.assertEqual((status, body["cooling"]["profile"]), (200, "performance"))
status, body = self.post("/api/lighting", {"zone": "memory", "mode": "temperature"})
self.assertEqual(status, 200)
self.assertEqual(next(z for z in body["lighting"]["zones"] if z["key"] == "memory")["mode"], "temperature")
def test_without_the_service_the_app_carries_on_and_says_so(self):
def no_service():
raise PipeError("the hardware service isn't running (no pipe)", 2)
with self.assertLogs("kurgan", level="WARNING"):
self.app.hw._connect = no_service
self.app.hw._drop()
wait_for(lambda: "hardware service" in self.app.telemetry.health())
state = self.app.state()
self.assertFalse(state["hardware"]["available"])
self.assertIn("isn't running", state["hardware"]["error"])
self.assertEqual(state["cooling"]["groups"], [])
self.assertIn("Hardware service unavailable", state["cooling"]["note"])
self.assertIsNotNone(state["telemetry"]["gpu"]["temp"]) # the rest of telemetry carries on
self.assertEqual(list(state["telemetry"]["health"]), ["hardware service"])
states = kurgan_app.alert_states(self.app.hw.cooling(), self.app.ai, self.app.telemetry)
self.assertEqual(list(states), ["hw"])
self.assertIn("Fans: hardware service unavailable", self.app.tray_lines())
with self.assertLogs("kurgan.web.server", level="WARNING"):
status, body = self.post("/api/cooling/profile", {"profile": "quiet"})
self.assertEqual(status, 503)
self.assertIn("isn't running", body["error"])
with self.assertRaises(HardwareUnavailable):
self.app.set_lighting("memory", "chase")
self.app.set_lighting("gpu", "chase") # the app's own lighting still works
with self.assertLogs("kurgan", level="INFO"):
self.app.hw._connect = self.connect # the service is back
wait_for(lambda: self.app.telemetry.health() == {} and self.app.state()["cooling"]["groups"])
self.assertTrue(self.app.state()["hardware"]["available"])
if __name__ == "__main__":
unittest.main()