import importlib.util from pathlib import Path import shlex import unittest from unittest.mock import patch spec = importlib.util.spec_from_file_location("fixed_pci", Path(__file__).parents[1] / "set-fixed-pci.py") policy = importlib.util.module_from_spec(spec) spec.loader.exec_module(policy) class FixedPciPolicyTests(unittest.TestCase): def test_keeps_gpu_identity_and_quoted_arguments(self): old = '-set device.hostpci0.x-pci-sub-vendor-id=0x1558 -set device.hostpci0.x-pci-sub-device-id=0x6557 -name "desktop test"' updated = policy.fixed_args(old) self.assertEqual(shlex.split(updated)[:len(shlex.split(old))], shlex.split(old)) self.assertTrue(updated.startswith(old)) self.assertEqual(policy.fixed_args(updated), updated) self.assertFalse(any("usb" in token.lower() for token in shlex.split(updated))) def test_conflicting_explicit_hotplug_setting_requires_review(self): with self.assertRaisesRegex(ValueError, "Conflicting"): policy.fixed_args('-global pcie-root-port.hotplug=on') def test_online_vm_cannot_be_changed(self): with patch.object(policy, "command", return_value="status: running") as call: with self.assertRaisesRegex(RuntimeError, "normally shut down"): policy.require_stopped(100) call.assert_called_once_with(["qm", "status", "100"]) def test_stopped_vm_can_proceed(self): with patch.object(policy, "command", return_value="status: stopped"): policy.require_stopped(100) if __name__ == '__main__': unittest.main()