Skip to content

Commit e58b6c2

Browse files
committed
Rework test_checkpoint
This test used the Git Bash environment and therefore often failed on Windows. Use PowerShell instead, which required adapting several VM methods to use the PowerShell alternatives, as well as some behavior changes. Signed-off-by: Tu Dinh <[email protected]>
1 parent b285d79 commit e58b6c2

File tree

2 files changed

+58
-22
lines changed

2 files changed

+58
-22
lines changed

lib/vm.py

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,23 @@ def wait_for_vm_running_and_ssh_up(self):
176176
self.wait_for_os_booted()
177177
wait_for(self.is_ssh_up, "Wait for SSH up")
178178

179-
def ssh_touch_file(self, filepath):
179+
def touch_file(self, filepath):
180+
"""
181+
Make sure that a file exists. Do not change file contents.
182+
183+
On Windows, it uses PowerShell path format and may not update timestamps.
184+
"""
180185
logging.info("Create file on VM (%s)" % filepath)
181-
self.ssh(['touch', filepath])
182-
if not self.is_windows:
186+
if self.is_windows:
187+
self.execute_powershell_script(f'New-Item -ErrorAction SilentlyContinue -Type File -Path {filepath}')
188+
else:
189+
self.ssh(['touch', filepath])
183190
self.ssh(['sync', filepath])
184191
logging.info("Check file created")
185-
self.ssh(['test -f ' + filepath])
192+
if self.is_windows:
193+
assert self.file_exists(filepath)
194+
else:
195+
self.ssh(['test -f ' + filepath])
186196

187197
def suspend(self, verify=False):
188198
logging.info("Suspend VM")
@@ -403,7 +413,18 @@ def start_background_process(self, cmd: str) -> str:
403413
return pid
404414

405415
def pid_exists(self, pid):
406-
return self.ssh_with_result(['kill', '-s', '0', pid]).returncode == 0
416+
if self.is_windows:
417+
return strtobool(
418+
self.execute_powershell_script(f'$null -ne (Get-Process -Id {pid} -ErrorAction SilentlyContinue)')
419+
)
420+
else:
421+
return self.ssh_with_result(['kill', '-s', '0', pid]).returncode == 0
422+
423+
def kill_process(self, pid):
424+
if self.is_windows:
425+
self.execute_powershell_script(f'Get-Process -Id {pid} | Stop-Process')
426+
else:
427+
self.ssh(['kill ' + pid])
407428

408429
def execute_script(self, script_contents, simple_output=True):
409430
with tempfile.NamedTemporaryFile('w') as f:
@@ -443,9 +464,16 @@ def tools_version(self):
443464
return "{major}.{minor}.{micro}-{build}".format(**version_dict)
444465

445466
def file_exists(self, filepath, regular_file=True):
446-
"""Returns True if the file exists, otherwise returns False."""
447-
option = '-f' if regular_file else '-e'
448-
return self.ssh_with_result(['test', option, filepath]).returncode == 0
467+
"""
468+
Returns True if the file exists, otherwise returns False.
469+
470+
regular_file does not apply to Windows.
471+
"""
472+
if self.is_windows:
473+
return strtobool(self.execute_powershell_script(f'Test-Path {filepath}'))
474+
else:
475+
option = '-f' if regular_file else '-e'
476+
return self.ssh_with_result(['test', option, filepath]).returncode == 0
449477

450478
def detect_package_manager(self):
451479
""" Heuristic to determine the package manager on a unix distro. """
@@ -472,13 +500,16 @@ def test_snapshot_on_running_vm(self):
472500
self.wait_for_vm_running_and_ssh_up()
473501
snapshot = self.snapshot()
474502
try:
475-
filepath = '/tmp/%s' % snapshot.uuid
476-
self.ssh_touch_file(filepath)
503+
if self.is_windows:
504+
filepath = fr'$Env:Temp\{snapshot.uuid}'
505+
else:
506+
filepath = '/tmp/%s' % snapshot.uuid
507+
self.touch_file(filepath)
477508
snapshot.revert()
478509
self.start()
479510
self.wait_for_vm_running_and_ssh_up()
480511
logging.info("Check file does not exist anymore")
481-
self.ssh(['test ! -f ' + filepath])
512+
assert not self.file_exists(filepath)
482513
finally:
483514
snapshot.destroy(verify=True)
484515

@@ -767,15 +798,16 @@ def run_powershell_command(self, program: str, args: str):
767798

768799
def start_background_powershell(self, cmd: str):
769800
"""
770-
Run command under powershell in the background.
801+
Run command under powershell in the background. Return the resulting PID.
771802
772803
Backslash-safe.
773804
"""
774805
assert self.is_windows
775806
encoded_command = commands.encode_powershell_command(cmd)
776-
self.ssh(
807+
return self.ssh(
777808
"powershell.exe -noprofile -noninteractive Invoke-WmiMethod -Class Win32_Process -Name Create "
778-
f"-ArgumentList \\'powershell.exe -noprofile -noninteractive -encodedcommand {encoded_command}\\'"
809+
f"-ArgumentList \\'powershell.exe -noprofile -noninteractive -encodedcommand {encoded_command}\\' \\|"
810+
"Select-Object -Expand ProcessId"
779811
)
780812

781813
def is_windows_pv_device_installed(self):

tests/misc/test_vm_basic_operations.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,28 @@ def test_snapshot(self, running_vm: VM):
2323
vm = running_vm
2424
vm.test_snapshot_on_running_vm()
2525

26-
# When using a windows VM the background ssh process is never terminated
27-
# This results in a ResourceWarning
28-
@pytest.mark.filterwarnings("ignore::ResourceWarning")
2926
def test_checkpoint(self, running_vm: VM):
3027
vm = running_vm
3128
logging.info("Start a 'sleep' process on VM through SSH")
32-
pid = vm.start_background_process('sleep 10000')
29+
if vm.is_windows:
30+
pid = vm.start_background_powershell('while ($true) {Start-Sleep -Seconds 10000}')
31+
else:
32+
pid = vm.start_background_process('sleep 10000')
33+
logging.info(f"Background command PID: {pid}")
3334
snapshot = vm.checkpoint()
34-
filepath = '/tmp/%s' % snapshot.uuid
35-
vm.ssh_touch_file(filepath)
35+
if vm.is_windows:
36+
filepath = fr'$Env:Temp\{snapshot.uuid}'
37+
else:
38+
filepath = '/tmp/%s' % snapshot.uuid
39+
vm.touch_file(filepath)
3640
snapshot.revert()
3741
vm.resume()
3842
vm.wait_for_vm_running_and_ssh_up()
3943
logging.info("Check file does not exist anymore")
40-
vm.ssh(['test ! -f ' + filepath])
44+
assert not vm.file_exists(filepath)
4145
logging.info("Check 'sleep' process is still running")
4246
assert vm.pid_exists(pid)
4347
logging.info("Kill background process")
44-
vm.ssh(['kill ' + pid])
48+
vm.kill_process(pid)
4549
wait_for_not(lambda: vm.pid_exists(pid), "Wait for process %s not running anymore" % pid)
4650
snapshot.destroy(verify=True)

0 commit comments

Comments
 (0)