@@ -293,3 +293,48 @@ func TestStandaloneUpgradeRollbackOnRestarts(t *testing.T) {
293
293
err = upgradetest .CheckHealthyAndVersion (ctx , startFixture , startVersionInfo .Binary )
294
294
assert .NoError (t , err )
295
295
}
296
+
297
+ func restartAgentNTimes (t * testing.T , noOfRestarts int , sleepBetweenIterations time.Duration ) {
298
+ topPath := paths .Top ()
299
+
300
+ for restartIdx := 0 ; restartIdx < noOfRestarts ; restartIdx ++ {
301
+ time .Sleep (sleepBetweenIterations )
302
+
303
+ t .Logf ("Stopping agent via service to simulate crashing" )
304
+ err := install .StopService (topPath , install .DefaultStopTimeout , install .DefaultStopInterval )
305
+ if err != nil && runtime .GOOS == define .Windows && strings .Contains (err .Error (), "The service has not been started." ) {
306
+ // Due to the quick restarts every 10 seconds its possible that this is faster than Windows
307
+ // can handle. Decrementing restartIdx means that the loop will occur again.
308
+ t .Logf ("Got an allowed error on Windows: %s" , err )
309
+ err = nil
310
+ }
311
+ require .NoError (t , err )
312
+
313
+ // ensure that it's stopped before starting it again
314
+ var status service.Status
315
+ var statusErr error
316
+ require .Eventuallyf (t , func () bool {
317
+ status , statusErr = install .StatusService (topPath )
318
+ if statusErr != nil {
319
+ return false
320
+ }
321
+ return status != service .StatusRunning
322
+ }, 5 * time .Minute , 1 * time .Second , "service never fully stopped (status: %v): %s" , status , statusErr )
323
+ t .Logf ("Stopped agent via service to simulate crashing" )
324
+
325
+ // start it again
326
+ t .Logf ("Starting agent via service to simulate crashing" )
327
+ err = install .StartService (topPath )
328
+ require .NoError (t , err )
329
+
330
+ // ensure that it's started before next loop
331
+ require .Eventuallyf (t , func () bool {
332
+ status , statusErr = install .StatusService (topPath )
333
+ if statusErr != nil {
334
+ return false
335
+ }
336
+ return status == service .StatusRunning
337
+ }, 5 * time .Minute , 1 * time .Second , "service never fully started (status: %v): %s" , status , statusErr )
338
+ t .Logf ("Started agent via service to simulate crashing" )
339
+ }
340
+ }
0 commit comments