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()