- kurgan/hw/install.py (run elevated by setup.ps1): checks the base interpreter can run hw_service.py, creates %ProgramData%\Kurgan or resets its owner and access (Administrators own it; SYSTEM and Administrators full control, users read), moves the fan and RAM settings there, then creates or updates KurganHw through the service manager API (pywin32; sc.exe's quoting from Windows PowerShell 5.1 isn't reliable): LocalSystem, automatic start, restart after 5 s, 10 s and 60 s, the user's SID for the pipe. A running service is stopped first; then it's started and must reach "running". - kurgan/hw/migrate.py: "copy" (elevated) creates hw-settings.json from the user's cooling and lighting.memory, never overwriting an existing file; "strip" (as the user) removes them from settings.json once hw-settings.json exists, keeping a dated backup. An invalid file on either side stops it. - setup.ps1: creates the .venv if needed (the service runs from it), passes the base interpreter, the user's SID and settings path to the elevated part, which installs the service; then strips settings.json, registers and starts the app's task, and prints the service's status. - uninstall.ps1: also stops and deletes the service. - python -m kurgan.hw.client prints the service's status (setup runs it). - The pipe's "isn't running", "refused this user" and "closed the connection" errors no longer repeat the Windows error text. - Tests: migration (move, re-run, defaults, invalid files) and the install module's parts that don't need admin rights. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
87 lines
3.9 KiB
Python
87 lines
3.9 KiB
Python
import json
|
|
import os
|
|
import tempfile
|
|
import unittest
|
|
|
|
from kurgan.hw import migrate
|
|
from kurgan.settings import SettingsError
|
|
|
|
OLD_USER = {
|
|
"cooling": {"profile": "quiet", "gpu_ports": [4, 5, 6], "cpu_ports": [1, 2, 3], "top_channels": [2, 5]},
|
|
"lighting": {"case_fans": "chase", "memory": "temperature", "gpu": "off"},
|
|
"ai": {"active": "strata"},
|
|
"web": {"port": 8080},
|
|
}
|
|
|
|
|
|
class MigrateTest(unittest.TestCase):
|
|
def setUp(self):
|
|
directory = tempfile.TemporaryDirectory()
|
|
self.addCleanup(directory.cleanup)
|
|
self.dir = directory.name
|
|
self.user = os.path.join(self.dir, "settings.json")
|
|
self.hw = os.path.join(self.dir, "ProgramData", "hw-settings.json")
|
|
|
|
def write(self, path, data):
|
|
os.makedirs(os.path.dirname(path), exist_ok=True)
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
f.write(data if isinstance(data, str) else json.dumps(data))
|
|
|
|
def read(self, path):
|
|
with open(path, encoding="utf-8") as f:
|
|
return json.load(f)
|
|
|
|
def test_the_fan_and_ram_settings_move_and_the_rest_stays(self):
|
|
self.write(self.user, OLD_USER)
|
|
self.assertIn("with cooling and lighting.memory", migrate.copy(self.user, self.hw))
|
|
self.assertEqual(self.read(self.hw), {"cooling": OLD_USER["cooling"], "lighting": {"memory": "temperature"}})
|
|
message = migrate.strip(self.user, self.hw, now=0)
|
|
self.assertIn("backup", message)
|
|
user = self.read(self.user)
|
|
self.assertNotIn("cooling", user)
|
|
self.assertEqual(user["lighting"], {"case_fans": "chase", "gpu": "off"})
|
|
self.assertEqual(user["ai"]["active"], "strata")
|
|
backups = [name for name in os.listdir(self.dir) if name.endswith(".bak")]
|
|
self.assertEqual(len(backups), 1)
|
|
self.assertEqual(self.read(os.path.join(self.dir, backups[0])), OLD_USER)
|
|
|
|
def test_running_it_again_changes_nothing(self):
|
|
self.write(self.user, OLD_USER)
|
|
migrate.copy(self.user, self.hw)
|
|
migrate.strip(self.user, self.hw)
|
|
hw, user = self.read(self.hw), self.read(self.user)
|
|
self.write(self.user, {**user, "cooling": {"profile": "full"}}) # stale, e.g. from an old backup
|
|
self.assertIn("already exists", migrate.copy(self.user, self.hw))
|
|
self.assertEqual(self.read(self.hw), hw) # never overwritten
|
|
migrate.strip(self.user, self.hw) # the stale section goes too
|
|
self.assertEqual(self.read(self.user), user)
|
|
self.assertIn("Nothing to move", migrate.strip(self.user, self.hw))
|
|
|
|
def test_no_user_settings_gives_the_defaults(self):
|
|
self.assertIn("with the defaults", migrate.copy(self.user, self.hw))
|
|
self.assertEqual(self.read(self.hw), {"cooling": {"profile": "balanced"}, "lighting": {"memory": "off"}})
|
|
self.assertIn("Nothing to move", migrate.strip(self.user, self.hw))
|
|
|
|
def test_settings_json_is_left_alone_until_hw_settings_exists(self):
|
|
self.write(self.user, OLD_USER)
|
|
self.assertIn("doesn't exist yet", migrate.strip(self.user, self.hw))
|
|
self.assertEqual(self.read(self.user), OLD_USER)
|
|
|
|
def test_an_invalid_file_stops_the_move(self):
|
|
for bad in ("{not json", {"cooling": {"profile": "turbo"}}, {"extra": {}}):
|
|
with self.subTest(bad=bad):
|
|
self.write(self.user, bad)
|
|
with self.assertRaises(SettingsError):
|
|
migrate.copy(self.user, self.hw)
|
|
self.assertFalse(os.path.exists(self.hw))
|
|
self.write(self.hw, {"cooling": {"profile": "balanced"}})
|
|
self.write(self.user, {"cooling": {"profile": "quiet"}, "web": {"port": 80}})
|
|
with self.assertRaises(SettingsError):
|
|
migrate.strip(self.user, self.hw)
|
|
self.assertEqual(self.read(self.user)["web"]["port"], 80) # untouched
|
|
self.assertFalse([name for name in os.listdir(self.dir) if name.endswith(".bak")])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|