From 239928b2af3c3beea3ae2727309fc8fdb539f1b0 Mon Sep 17 00:00:00 2001 From: Tu Dinh Date: Mon, 4 Nov 2024 14:48:35 +0100 Subject: [PATCH] guest-tools/win: take PowerAction in install_guest_tools --- tests/guest-tools/win/test_guest_tools_win.py | 92 +++++++++++-------- 1 file changed, 52 insertions(+), 40 deletions(-) diff --git a/tests/guest-tools/win/test_guest_tools_win.py b/tests/guest-tools/win/test_guest_tools_win.py index 6a268780..1b1eb7d2 100644 --- a/tests/guest-tools/win/test_guest_tools_win.py +++ b/tests/guest-tools/win/test_guest_tools_win.py @@ -14,19 +14,42 @@ ERROR_SUCCESS_REBOOT_REQUIRED = 3010 -def install_guest_tools(vm: VM, check: bool = True): - exitcode = run_command_powershell( - vm, - "msiexec.exe", - [ - "-i", - "C:\\XenDrivers-x64.msi", - "-log", - "C:\\tools_install.log", - "-passive", - "-norestart", - ], - ) +class PowerAction(enum.Enum): + Nothing = "nothing" + Shutdown = "shutdown" + Reboot = "reboot" + + +def install_guest_tools(vm: VM, action: PowerAction, check: bool = True): + msiexec_args = [ + "-i", + "C:\\XenDrivers-x64.msi", + "-log", + "C:\\tools_install.log", + "-passive", + "-norestart", + ] + + if action == PowerAction.Nothing: + exitcode = run_command_powershell(vm, "msiexec.exe", msiexec_args) + else: + if check: + raise Exception(f"Cannot check exit code with {action} action") + install_cmd = [ + # when powershell runs msiexec it doesn't wait for it to end unlike ssh + # it only waits for stdin closing so we need Start-Process -Wait here + f"Start-Process -Wait msiexec.exe -ArgumentList '{" ".join(msiexec_args)}'" + ] + if action != PowerAction.Nothing: + install_cmd += ["Stop-Computer -Force"] + start_background_powershell(vm, ";".join(install_cmd)) + if action != PowerAction.Nothing: + wait_for(vm.is_halted, "Wait for VM halted") + if action == PowerAction.Reboot: + vm.start() + wait_for_guest_start(vm) + exitcode = None + if check: assert exitcode in [ ERROR_SUCCESS, @@ -36,21 +59,8 @@ def install_guest_tools(vm: VM, check: bool = True): return exitcode -class PowerAction(enum.Enum): - Nothing = "nothing" - Shutdown = "shutdown" - Reboot = "reboot" - - def uninstall_guest_tools(vm: VM, action: PowerAction): - """ - Unlike install_guest_tools, uninstall_guest_tools is asynchronous since it disconnects the VM's networking. - For that reason, you must specify whether to reboot or shutdown after installing. - """ uninstall_cmd = [ - "Start-Sleep -Seconds 5", - # when powershell runs msiexec it doesn't wait for it to end unlike ssh - # it only waits for stdin closing so we need Start-Process -Wait here ( "Start-Process -Wait msiexec.exe -ArgumentList '" "/x C:\\XenDrivers-x64.msi /l* C:\\tools_uninstall.log /passive /norestart" @@ -67,7 +77,7 @@ def uninstall_guest_tools(vm: VM, action: PowerAction): wait_for_guest_start(vm) -def install_tools_noreboot(vm: VM, check: bool = True): +def install_cert_and_tools(vm: VM, action: PowerAction, check: bool = True): vm.insert_cd("guest-tools-win.iso") # wait a small amount of time just to ensure the device is available time.sleep(5) @@ -107,7 +117,7 @@ def install_tools_noreboot(vm: VM, check: bool = True): vm.eject_cd() logging.info("Install Windows PV drivers") - return install_guest_tools(vm, check=check) + return install_guest_tools(vm, action=action, check=check) def install_other_drivers(vm: VM, name: str, is_msi: bool): @@ -199,9 +209,7 @@ 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) + install_cert_and_tools(vm, PowerAction.Reboot, check=False) yield vm snapshot.revert() @@ -219,7 +227,7 @@ def test_drivers_detected(self, vm_install: VM): class TestGuestToolsWindowsDestructive: @pytest.fixture def vm_install(self, vm_instance: VM): - install_tools_noreboot(vm_instance) + install_cert_and_tools(vm_instance, PowerAction.Nothing) return vm_instance @pytest.fixture @@ -232,7 +240,7 @@ def vm_install_citrix_tools(self, vm_instance: VM): return vm_instance @pytest.fixture - def vm_install_xcpng_tools(self, vm_instance: VM): + def vm_install_xcpng_v8_tools(self, vm_instance: VM): install_other_drivers( vm_instance, "xcp-ng-8.2.2.200\\managementagentx64.msi", @@ -274,19 +282,23 @@ def test_reinstall_tools_early(self, vm_install: VM): wait_for_guest_start(vm) logging.info("Uninstall Windows PV drivers before reinstalling") uninstall_guest_tools(vm, action=PowerAction.Nothing) - install_guest_tools(vm) - vm.reboot() - wait_for_guest_start(vm) + install_guest_tools(vm, action=PowerAction.Reboot, check=False) assert is_drivers_installed(vm) def test_install_with_citrix_tools(self, vm_install_citrix_tools: VM): - exitcode = install_tools_noreboot(vm_install_citrix_tools, check=False) + exitcode = install_cert_and_tools( + vm_install_citrix_tools, PowerAction.Nothing, check=False + ) assert exitcode == ERROR_INSTALL_FAILURE - def test_install_with_xcpng_tools(self, vm_install_xcpng_tools: VM): - exitcode = install_tools_noreboot(vm_install_xcpng_tools, check=False) + def test_install_with_xcpng_v8_tools(self, vm_install_xcpng_v8_tools: VM): + exitcode = install_cert_and_tools( + vm_install_xcpng_v8_tools, PowerAction.Nothing, check=False + ) assert exitcode == ERROR_INSTALL_FAILURE def test_install_with_citrix_wu(self, vm_install_wu_drivers: VM): - exitcode = install_tools_noreboot(vm_install_wu_drivers, check=False) + exitcode = install_cert_and_tools( + vm_install_wu_drivers, PowerAction.Nothing, check=False + ) assert exitcode == ERROR_INSTALL_FAILURE