@@ -326,6 +326,82 @@ func TestUnitLoadReplace(t *testing.T) {
326326 }
327327}
328328
329+ // TestUnitStartReplace() tests whether a command "fleetctl start --replace
330+ // hello.service" works or not.
331+ func TestUnitStartReplace (t * testing.T ) {
332+ cluster , err := platform .NewNspawnCluster ("smoke" )
333+ if err != nil {
334+ t .Fatal (err )
335+ }
336+ defer cluster .Destroy ()
337+
338+ m , err := cluster .CreateMember ()
339+ if err != nil {
340+ t .Fatal (err )
341+ }
342+ _ , err = cluster .WaitForNMachines (m , 1 )
343+ if err != nil {
344+ t .Fatal (err )
345+ }
346+
347+ // start a unit and assert it shows up
348+ if _ , _ , err := cluster .Fleetctl (m , "start" , fxtHelloService ); err != nil {
349+ t .Fatalf ("Unable to start fleet unit: %v" , err )
350+ }
351+ stdout , _ , err := cluster .Fleetctl (m , "list-units" , "--no-legend" )
352+ if err != nil {
353+ t .Fatalf ("Failed to run list-units: %v" , err )
354+ }
355+ units := strings .Split (strings .TrimSpace (stdout ), "\n " )
356+ if len (units ) != 1 {
357+ t .Fatalf ("Did not find 1 unit in cluster: \n %s" , stdout )
358+ }
359+
360+ // replace the unit and assert it shows up
361+ if _ , _ , err := cluster .Fleetctl (m , "start" , fxtHelloService ); err != nil {
362+ t .Fatalf ("Unable to start fleet unit: %v" , err )
363+ }
364+ err = genNewFleetService (tmpHelloService , fxtHelloService , "sleep 2" , "sleep 1" )
365+ if err != nil {
366+ t .Fatalf ("Failed to generate a temp fleet service: %v" , err )
367+ }
368+ if _ , _ , err := cluster .Fleetctl (m , "start" , "--replace" , tmpHelloService ); err != nil {
369+ t .Fatalf ("Unable to replace fleet unit: %v" , err )
370+ }
371+ stdout , _ , err = cluster .Fleetctl (m , "list-units" , "--no-legend" )
372+ if err != nil {
373+ t .Fatalf ("Failed to run list-units: %v" , err )
374+ }
375+ units = strings .Split (strings .TrimSpace (stdout ), "\n " )
376+ if len (units ) != 1 {
377+ t .Fatalf ("Did not find 1 unit in cluster: \n %s" , stdout )
378+ }
379+ os .Remove (tmpHelloService )
380+
381+ // destroy the unit and ensure it disappears from the unit list.
382+ // It could take a little time until the unit gets destroyed.
383+ maxAttempts := 3
384+ for {
385+ if _ , _ , err := cluster .Fleetctl (m , "destroy" , fxtHelloService ); err != nil {
386+ t .Fatalf ("Failed to destroy unit: %v" , err )
387+ }
388+ _ , err = cluster .WaitForNActiveUnits (m , 0 )
389+ if err == nil {
390+ break
391+ } else {
392+ maxAttempts --
393+ }
394+ }
395+
396+ stdout , _ , err = cluster .Fleetctl (m , "list-units" , "--no-legend" )
397+ if err != nil {
398+ t .Fatalf ("Failed to run list-units: %v" , err )
399+ }
400+ if strings .TrimSpace (stdout ) != "" {
401+ t .Fatalf ("Did not find 0 units in cluster: \n %s" , stdout )
402+ }
403+ }
404+
329405func TestUnitSSHActions (t * testing.T ) {
330406 cluster , err := platform .NewNspawnCluster ("smoke" )
331407 if err != nil {
0 commit comments