Skip to content

Commit 295f52d

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 338b0b1 commit 295f52d

File tree

2 files changed

+57
-22
lines changed

2 files changed

+57
-22
lines changed

lib/vm.py

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,22 @@ 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+
On Windows, it uses PowerShell path format and may not update timestamps.
183+
"""
180184
logging.info("Create file on VM (%s)" % filepath)
181-
self.ssh(['touch', filepath])
182-
if not self.is_windows:
185+
if self.is_windows:
186+
self.execute_powershell_script(f'New-Item -ErrorAction SilentlyContinue -Type File -Path {filepath}')
187+
else:
188+
self.ssh(['touch', filepath])
183189
self.ssh(['sync', filepath])
184190
logging.info("Check file created")
185-
self.ssh(['test -f ' + filepath])
191+
if self.is_windows:
192+
assert self.file_exists(filepath)
193+
else:
194+
self.ssh(['test -f ' + filepath])
186195

187196
def suspend(self, verify=False):
188197
logging.info("Suspend VM")
@@ -403,7 +412,18 @@ def start_background_process(self, cmd: str) -> str:
403412
return pid
404413

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

408428
def execute_script(self, script_contents, simple_output=True):
409429
with tempfile.NamedTemporaryFile('w') as f:
@@ -443,9 +463,16 @@ def tools_version(self):
443463
return "{major}.{minor}.{micro}-{build}".format(**version_dict)
444464

445465
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
466+
"""
467+
Returns True if the file exists, otherwise returns False.
468+
469+
regular_file does not apply to Windows.
470+
"""
471+
if self.is_windows:
472+
return strtobool(self.execute_powershell_script(f'Test-Path {filepath}'))
473+
else:
474+
option = '-f' if regular_file else '-e'
475+
return self.ssh_with_result(['test', option, filepath]).returncode == 0
449476

450477
def detect_package_manager(self):
451478
""" Heuristic to determine the package manager on a unix distro. """
@@ -472,13 +499,16 @@ def test_snapshot_on_running_vm(self):
472499
self.wait_for_vm_running_and_ssh_up()
473500
snapshot = self.snapshot()
474501
try:
475-
filepath = '/tmp/%s' % snapshot.uuid
476-
self.ssh_touch_file(filepath)
502+
if self.is_windows:
503+
filepath = fr'$Env:Temp\{snapshot.uuid}'
504+
else:
505+
filepath = '/tmp/%s' % snapshot.uuid
506+
self.touch_file(filepath)
477507
snapshot.revert()
478508
self.start()
479509
self.wait_for_vm_running_and_ssh_up()
480510
logging.info("Check file does not exist anymore")
481-
self.ssh(['test ! -f ' + filepath])
511+
assert not self.file_exists(filepath)
482512
finally:
483513
snapshot.destroy(verify=True)
484514

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

768798
def start_background_powershell(self, cmd: str):
769799
"""
770-
Run command under powershell in the background.
800+
Run command under powershell in the background. Return the resulting PID.
771801
772802
Backslash-safe.
773803
"""
774804
assert self.is_windows
775805
encoded_command = commands.encode_powershell_command(cmd)
776-
self.ssh(
806+
return self.ssh(
777807
"powershell.exe -noprofile -noninteractive Invoke-WmiMethod -Class Win32_Process -Name Create "
778-
f"-ArgumentList \\'powershell.exe -noprofile -noninteractive -encodedcommand {encoded_command}\\'"
808+
f"-ArgumentList \\'powershell.exe -noprofile -noninteractive -encodedcommand {encoded_command}\\' \\|"
809+
"Select-Object -Expand ProcessId"
779810
)
780811

781812
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)