@@ -176,13 +176,23 @@ def wait_for_vm_running_and_ssh_up(self):
176
176
self .wait_for_os_booted ()
177
177
wait_for (self .is_ssh_up , "Wait for SSH up" )
178
178
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
+ """
180
185
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 ])
183
190
self .ssh (['sync' , filepath ])
184
191
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 ])
186
196
187
197
def suspend (self , verify = False ):
188
198
logging .info ("Suspend VM" )
@@ -403,7 +413,18 @@ def start_background_process(self, cmd: str) -> str:
403
413
return pid
404
414
405
415
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 ])
407
428
408
429
def execute_script (self , script_contents , simple_output = True ):
409
430
with tempfile .NamedTemporaryFile ('w' ) as f :
@@ -443,9 +464,16 @@ def tools_version(self):
443
464
return "{major}.{minor}.{micro}-{build}" .format (** version_dict )
444
465
445
466
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
449
477
450
478
def detect_package_manager (self ):
451
479
""" Heuristic to determine the package manager on a unix distro. """
@@ -472,13 +500,16 @@ def test_snapshot_on_running_vm(self):
472
500
self .wait_for_vm_running_and_ssh_up ()
473
501
snapshot = self .snapshot ()
474
502
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 )
477
508
snapshot .revert ()
478
509
self .start ()
479
510
self .wait_for_vm_running_and_ssh_up ()
480
511
logging .info ("Check file does not exist anymore" )
481
- self .ssh ([ 'test ! -f ' + filepath ] )
512
+ assert not self .file_exists ( filepath )
482
513
finally :
483
514
snapshot .destroy (verify = True )
484
515
@@ -767,15 +798,16 @@ def run_powershell_command(self, program: str, args: str):
767
798
768
799
def start_background_powershell (self , cmd : str ):
769
800
"""
770
- Run command under powershell in the background.
801
+ Run command under powershell in the background. Return the resulting PID.
771
802
772
803
Backslash-safe.
773
804
"""
774
805
assert self .is_windows
775
806
encoded_command = commands .encode_powershell_command (cmd )
776
- self .ssh (
807
+ return self .ssh (
777
808
"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"
779
811
)
780
812
781
813
def is_windows_pv_device_installed (self ):
0 commit comments