diff --git a/tests/guest-tools/win/__init__.py b/tests/guest-tools/win/__init__.py index 412f04b9..ae04203c 100644 --- a/tests/guest-tools/win/__init__.py +++ b/tests/guest-tools/win/__init__.py @@ -38,20 +38,16 @@ def encode_ps_command(cmd: str): def is_drivers_installed(vm: VM): + cmd = encode_ps_command( + r""" + $ProgressPreference = 'SilentlyContinue'; + Get-PnpDevice -PresentOnly | + Where-Object CompatibleID -icontains 'PCI\VEN_5853' | + Select-Object -ExpandProperty Problem + """ + ) output = vm.ssh( - [ - "powershell.exe", - "-noprofile", - "-noninteractive", - "-encodedcommand", - encode_ps_command( - r""" - Get-PnpDevice -PresentOnly | - Where-Object CompatibleID -icontains 'PCI\VEN_5853' | - Select-Object -ExpandProperty Problem - """ - ), - ] + ["powershell.exe", "-noprofile", "-noninteractive", "-encodedcommand", cmd] ) logging.debug(f"Installed Xen device status: {output}") if output == "CM_PROB_NONE": @@ -69,8 +65,14 @@ def run_command_powershell(vm: VM, program: str, args: List[str]): Backslash-safe. """ args = " ".join(args) - # ProgressPreference is needed to suppress any cli\xml progress output as it's not filtered away from stdout by default, and we're grabbing stdout - cmd = f"$ProgressPreference = 'SilentlyContinue'; Write-Output (Start-Process -Wait -PassThru {program} -ArgumentList '{args}').ExitCode" + # ProgressPreference is needed to suppress any cli\xml progress output + # as it's not filtered away from stdout by default, and we're grabbing stdout + cmd = encode_ps_command( + f""" + $ProgressPreference = 'SilentlyContinue'; + Write-Output (Start-Process -Wait -PassThru {program} -ArgumentList '{args}').ExitCode + """ + ) return int( vm.ssh( [ @@ -79,7 +81,7 @@ def run_command_powershell(vm: VM, program: str, args: List[str]): "-noprofile", "-noninteractive", "-encodedcommand", - encode_ps_command(cmd), + cmd, ] ) ) @@ -91,7 +93,15 @@ def start_background_powershell(vm: VM, cmd: str): Backslash-safe. """ - async_cmd = f"\\'powershell.exe -noprofile -noninteractive -encodedcommand {encode_ps_command(cmd)}\\'" + async_cmd = " ".join( + [ + "powershell.exe", + "-noprofile", + "-noninteractive", + "-encodedcommand", + encode_ps_command(cmd), + ] + ) vm.ssh( [ "powershell.exe", @@ -103,7 +113,7 @@ def start_background_powershell(vm: VM, cmd: str): "-Name", "Create", "-ArgumentList", - async_cmd, + f"\\'{async_cmd}\\'", ], ) diff --git a/tests/guest-tools/win/test_guest_tools_win.py b/tests/guest-tools/win/test_guest_tools_win.py index 0ea98fda..6a268780 100644 --- a/tests/guest-tools/win/test_guest_tools_win.py +++ b/tests/guest-tools/win/test_guest_tools_win.py @@ -162,16 +162,48 @@ def running_windows_vm(imported_vm: VM) -> VM: # no teardown +@pytest.fixture(scope="module") +def vm_prepared(running_windows_vm: VM): + """Unseal VM and get its IP, then shut it down. Cache the unsealed state in a snapshot to save time.""" + vm = running_windows_vm + # vm shutdown is not usable yet (there's no tools) + vm.ssh( + [ + "powershell.exe", + "-noprofile", + "-noninteractive", + "Stop-Computer", + "-Force", + ] + ) + wait_for(vm.is_halted, "Shutdown VM") + snapshot = vm.snapshot() + yield (vm, snapshot) + snapshot.destroy(verify=True) + + +@pytest.fixture +def vm_instance(vm_prepared: tuple[VM, Snapshot]): + (vm, snapshot) = vm_prepared + vm.start() + wait_for_guest_start(vm) + yield vm + snapshot.revert() + + @pytest.mark.multi_vms @pytest.mark.usefixtures("windows_vm") class TestGuestToolsWindows: @pytest.fixture(scope="class") - def vm_install(self, running_windows_vm): - vm = running_windows_vm + def vm_install(self, vm_prepared): + (vm, snapshot) = vm_prepared + vm.start() + wait_for_guest_start(vm) install_tools_noreboot(vm) vm.reboot() wait_for_guest_start(vm) - return vm + yield vm + snapshot.revert() def test_tools_after_reboot(self, vm_install: VM): vm = vm_install @@ -185,33 +217,6 @@ def test_drivers_detected(self, vm_install: VM): @pytest.mark.multi_vms @pytest.mark.usefixtures("windows_vm") class TestGuestToolsWindowsDestructive: - @pytest.fixture(scope="class") - def vm_prepared(self, running_windows_vm: VM): - """Unseal VM and get its IP, then shut it down. Cache the unsealed state in a snapshot to save time.""" - vm = running_windows_vm - # vm shutdown is not usable yet (there's no tools) - vm.ssh( - [ - "powershell.exe", - "-noprofile", - "-noninteractive", - "Stop-Computer", - "-Force", - ] - ) - wait_for(vm.is_halted, "Shutdown VM") - snapshot = vm.snapshot() - yield (vm, snapshot) - snapshot.destroy(verify=True) - - @pytest.fixture - def vm_instance(self, vm_prepared: tuple[VM, Snapshot]): - (vm, snapshot) = vm_prepared - vm.start() - wait_for_guest_start(vm) - yield vm - snapshot.revert() - @pytest.fixture def vm_install(self, vm_instance: VM): install_tools_noreboot(vm_instance)