@@ -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 ):
0 commit comments