Skip to content

Commit

Permalink
guest-tools/win: take PowerAction in install_guest_tools
Browse files Browse the repository at this point in the history
  • Loading branch information
Tu Dinh committed Nov 4, 2024
1 parent f5f2c36 commit 239928b
Showing 1 changed file with 52 additions and 40 deletions.
92 changes: 52 additions & 40 deletions tests/guest-tools/win/test_guest_tools_win.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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()

Expand All @@ -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
Expand All @@ -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",
Expand Down Expand Up @@ -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

0 comments on commit 239928b

Please sign in to comment.