66 "path/filepath"
77 "runtime"
88 "strings"
9- "syscall"
109 "testing"
1110 "time"
1211
@@ -171,7 +170,7 @@ var _ = SynchronizedAfterSuite(func() {
171170})
172171
173172func revertSnapshot (vmIpath string , snapshotName string ) {
174- By (fmt .Sprintf ("Starting VM snapshot.revert: %s \n " , snapshotName ))
173+ By (fmt .Sprintf ("VM snapshot.revert - %s STARTING " , snapshotName ))
175174
176175 snapshotCommand := []string {
177176 "snapshot.revert" ,
@@ -180,36 +179,41 @@ func revertSnapshot(vmIpath string, snapshotName string) {
180179 fmt .Sprintf ("-tls-ca-certs=%s" , pathToCACert ),
181180 snapshotName ,
182181 }
183- revertExitCode := runIgnoringOutput (snapshotCommand )
184- Expect (revertExitCode ).To (Equal (0 ), "Starting VM snapshot.revert failed" )
182+ revertExitCode := cli . Run (snapshotCommand )
183+ Expect (revertExitCode ).To (Equal (0 ), fmt . Sprintf ( " VM snapshot.revert - %s FAILED" , snapshotName ) )
185184
186- By ("Started VM snapshot.revert" )
185+ By (fmt . Sprintf ( " VM snapshot.revert - %s STARTED" , snapshotName ) )
187186}
188187
189188func waitForVmToBeReady (vmIp string , vmUsername string , vmPassword string ) {
190- const vmReadyTimeout = 15 * time .Minute
191- const vmReadySleepInterval = 1 * time .Minute
192-
193- By ("Waiting for VM snapshot.revert ..." )
194- clientFactory := remotemanager .NewWinRmClientFactory (vmIp , vmUsername , vmPassword )
195- rm := remotemanager .NewWinRM (vmIp , vmUsername , vmPassword , clientFactory )
196- Expect (rm ).ToNot (BeNil ())
197-
198- start := time .Now ()
189+ const vmReadyCheckTimeout = 15 * time .Minute
190+ const vmReadyCheckSleepInterval = 30 * time .Second
191+ vmReadyCheckStartTime := time .Now ()
192+
193+ By ("VM snapshot.revert - creating WinRM Remote Manager" )
194+ remoteManager := remotemanager .NewWinRM (
195+ vmIp ,
196+ vmUsername ,
197+ vmPassword ,
198+ remotemanager .NewWinRmClientFactory (vmIp , vmUsername , vmPassword ),
199+ )
200+ Expect (remoteManager ).ToNot (BeNil ())
201+
202+ By (fmt .Sprintf ("VM snapshot.revert - checking VM at %s" , vmIp ))
199203 vmReady := false
200204 for ! vmReady {
201- if time .Since (start ) > vmReadyTimeout {
202- Fail (fmt .Sprintf ("VM at %s failed to start " , vmIp ))
205+ if time .Since (vmReadyCheckStartTime ) > vmReadyCheckTimeout {
206+ Fail (fmt .Sprintf ("VM snapshot.revert - VM at %s not ready after %d minutes " , vmIp , vmReadyCheckTimeout / time . Minute ))
203207 }
204- time .Sleep (vmReadySleepInterval )
205- _ , err := rm .ExecuteCommand (`powershell.exe "ls c:\windows 1>$null"` )
208+ time .Sleep (vmReadyCheckSleepInterval )
209+ _ , err := remoteManager .ExecuteCommand (`powershell.exe "ls c:\windows 1>$null"` )
206210 if err != nil {
207- By (fmt .Sprintf ("VM not yet ready: %v" , err ))
211+ By (fmt .Sprintf ("VM snapshot.revert - VM at %s not ready: %v" , vmIp , err ))
208212 }
209213 vmReady = err == nil
210214 }
211215
212- By ("VM snapshot.revert finished" )
216+ By (fmt . Sprintf ( "VM snapshot.revert - VM at %s is ready" , vmIp ) )
213217}
214218
215219func envMustExist (variableName string ) string {
@@ -237,7 +241,7 @@ func enableWinRM() {
237241 "C:\\ Windows\\ Temp\\ BOSH.WinRM.psm1" ,
238242 }
239243
240- uploadExitCode := runIgnoringOutput (uploadCommand )
244+ uploadExitCode := cli . Run (uploadCommand )
241245 Expect (uploadExitCode ).To (Equal (0 ), fmt .Sprintf ("There was an error uploading %s" , winRMPowershellModule ))
242246 By (fmt .Sprintf ("WinRM '%s' uploaded" , winRMPowershellModule ))
243247
@@ -251,7 +255,7 @@ func enableWinRM() {
251255 `-command` ,
252256 `&{Import-Module C:\Windows\Temp\BOSH.WinRM.psm1; Enable-WinRM}` ,
253257 }
254- enableExitCode := runIgnoringOutput (enableCommand )
258+ enableExitCode := cli . Run (enableCommand )
255259 Expect (enableExitCode ).To (Equal (0 ), "There was an error enabling WinRM." )
256260 By ("WinRM enabled." )
257261}
@@ -283,27 +287,6 @@ func powerOnVM() {
283287 fmt .Sprintf ("-tls-ca-certs=%s" , pathToCACert ),
284288 "-on" ,
285289 }
286- powerOnExitCode := runIgnoringOutput (powerOnCommand )
290+ powerOnExitCode := cli . Run (powerOnCommand )
287291 By (fmt .Sprintf ("VM power-on exited with %d" , powerOnExitCode ))
288292}
289-
290- func runIgnoringOutput (args []string ) int {
291- oldStderr := os .Stderr
292- oldStdout := os .Stdout
293-
294- _ , w , _ := os .Pipe () //nolint:errcheck
295-
296- defer w .Close () //nolint:errcheck
297-
298- os .Stderr = w
299- os .Stdout = w
300-
301- os .Stderr = os .NewFile (uintptr (syscall .Stderr ), "/dev/null" )
302-
303- exitCode := cli .Run (args )
304-
305- os .Stderr = oldStderr
306- os .Stdout = oldStdout
307-
308- return exitCode
309- }
0 commit comments