"""Tests for M6 runtime tunables (parse, command builders, value validation).""" import unittest from unittest import mock from rigdoctor.core import fixes from rigdoctor.core.fixes import Tunable class ParseTests(unittest.TestCase): def test_bracketed(self): self.assertEqual(fixes._bracketed("always [madvise] never"), (["always", "madvise", "never"], "madvise")) def test_bracketed_none_active(self): self.assertEqual(fixes._bracketed("a b c"), (["a", "b", "c"], None)) class CommandBuilderTests(unittest.TestCase): def test_governor_cmd_writes_value_to_sysfs(self): cmd = fixes._cpu_governor_cmd("performance") self.assertEqual(cmd[:2], ["/bin/sh", "-c"]) self.assertIn("performance", cmd[2]) self.assertIn("scaling_governor", cmd[2]) def test_persistence_cmd(self): self.assertEqual(fixes._nvidia_persistence_cmd("Enabled"), ["nvidia-smi", "-pm", "1"]) self.assertEqual(fixes._nvidia_persistence_cmd("Disabled"), ["nvidia-smi", "-pm", "0"]) def test_swappiness_cmd_targets_procfs(self): self.assertIn("/proc/sys/vm/swappiness", fixes._swappiness_cmd("10")[2]) def test_quoting_is_safe(self): # A value that would be dangerous unquoted stays a single quoted token. cmd = fixes._pcie_aspm_cmd("performance; rm -rf /") self.assertIn("'performance; rm -rf /'", cmd[2]) class ApplyValidationTests(unittest.TestCase): def test_unknown_fix_returns_none(self): self.assertIsNone(fixes.apply_command("does_not_exist", "x")) def test_value_validated_against_live_options(self): fake = Tunable("x", "X", ["a", "b"], "a") with mock.patch.dict(fixes._TUNABLES, {"x": (lambda: fake, lambda v: ["echo", v])}, clear=False): self.assertEqual(fixes.apply_command("x", "a"), ["echo", "a"]) self.assertIsNone(fixes.apply_command("x", "not-an-option")) def test_apply_unknown_is_error(self): rc, _ = fixes.apply("nope", "x") self.assertEqual(rc, 1) class GameenvWiringTests(unittest.TestCase): def test_findings_reference_known_fix_ids(self): from rigdoctor.core import gameenv fix_ids = {f.fix for f in gameenv.run_gameenv_checks() if f.fix} # Whatever fixes the live system surfaces, each must be a real tunable id. self.assertTrue(fix_ids.issubset(set(fixes._TUNABLES))) if __name__ == "__main__": unittest.main()