Skip to content

Commit

Permalink
guest-tools/win: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Tu Dinh committed Oct 31, 2024
1 parent 1d52a23 commit f5f2c36
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 48 deletions.
46 changes: 28 additions & 18 deletions tests/guest-tools/win/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -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(
[
Expand All @@ -79,7 +81,7 @@ def run_command_powershell(vm: VM, program: str, args: List[str]):
"-noprofile",
"-noninteractive",
"-encodedcommand",
encode_ps_command(cmd),
cmd,
]
)
)
Expand All @@ -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",
Expand All @@ -103,7 +113,7 @@ def start_background_powershell(vm: VM, cmd: str):
"-Name",
"Create",
"-ArgumentList",
async_cmd,
f"\\'{async_cmd}\\'",
],
)

Expand Down
65 changes: 35 additions & 30 deletions tests/guest-tools/win/test_guest_tools_win.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit f5f2c36

Please sign in to comment.