From 91fc9da08406fee85409a44addb1863de56a54ea Mon Sep 17 00:00:00 2001 From: Dongsu Park Date: Tue, 15 Mar 2016 17:22:39 +0100 Subject: [PATCH] functional: add a new test TestUnitStartReplace TestUnitStartReplace() tests whether a command "fleetctl start --replace hello.service" works or not. --- functional/unit_action_test.go | 76 ++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/functional/unit_action_test.go b/functional/unit_action_test.go index 07fbcf44c..5b27239fc 100644 --- a/functional/unit_action_test.go +++ b/functional/unit_action_test.go @@ -326,6 +326,82 @@ func TestUnitLoadReplace(t *testing.T) { } } +// TestUnitStartReplace() tests whether a command "fleetctl start --replace +// hello.service" works or not. +func TestUnitStartReplace(t *testing.T) { + cluster, err := platform.NewNspawnCluster("smoke") + if err != nil { + t.Fatal(err) + } + defer cluster.Destroy() + + m, err := cluster.CreateMember() + if err != nil { + t.Fatal(err) + } + _, err = cluster.WaitForNMachines(m, 1) + if err != nil { + t.Fatal(err) + } + + // start a unit and assert it shows up + if _, _, err := cluster.Fleetctl(m, "start", fxtHelloService); err != nil { + t.Fatalf("Unable to start fleet unit: %v", err) + } + stdout, _, err := cluster.Fleetctl(m, "list-units", "--no-legend") + if err != nil { + t.Fatalf("Failed to run list-units: %v", err) + } + units := strings.Split(strings.TrimSpace(stdout), "\n") + if len(units) != 1 { + t.Fatalf("Did not find 1 unit in cluster: \n%s", stdout) + } + + // replace the unit and assert it shows up + if _, _, err := cluster.Fleetctl(m, "start", fxtHelloService); err != nil { + t.Fatalf("Unable to start fleet unit: %v", err) + } + err = genNewFleetService(tmpHelloService, fxtHelloService, "sleep 2", "sleep 1") + if err != nil { + t.Fatalf("Failed to generate a temp fleet service: %v", err) + } + if _, _, err := cluster.Fleetctl(m, "start", "--replace", tmpHelloService); err != nil { + t.Fatalf("Unable to replace fleet unit: %v", err) + } + stdout, _, err = cluster.Fleetctl(m, "list-units", "--no-legend") + if err != nil { + t.Fatalf("Failed to run list-units: %v", err) + } + units = strings.Split(strings.TrimSpace(stdout), "\n") + if len(units) != 1 { + t.Fatalf("Did not find 1 unit in cluster: \n%s", stdout) + } + os.Remove(tmpHelloService) + + // destroy the unit and ensure it disappears from the unit list. + // It could take a little time until the unit gets destroyed. + maxAttempts := 3 + for { + if _, _, err := cluster.Fleetctl(m, "destroy", fxtHelloService); err != nil { + t.Fatalf("Failed to destroy unit: %v", err) + } + _, err = cluster.WaitForNActiveUnits(m, 0) + if err == nil { + break + } else { + maxAttempts-- + } + } + + stdout, _, err = cluster.Fleetctl(m, "list-units", "--no-legend") + if err != nil { + t.Fatalf("Failed to run list-units: %v", err) + } + if strings.TrimSpace(stdout) != "" { + t.Fatalf("Did not find 0 units in cluster: \n%s", stdout) + } +} + func TestUnitSSHActions(t *testing.T) { cluster, err := platform.NewNspawnCluster("smoke") if err != nil {