diff --git a/functional/unit_action_test.go b/functional/unit_action_test.go index 10e7438e9..dbca3bbb5 100644 --- a/functional/unit_action_test.go +++ b/functional/unit_action_test.go @@ -57,6 +57,9 @@ func TestUnitRunnable(t *testing.T) { } } +// TestUnitSubmit checks if a unit becomes submitted and destroyed successfully. +// First it submits a unit, and destroys the unit, verifies it's destroyed, +// finally submits the unit again. func TestUnitSubmit(t *testing.T) { cluster, err := platform.NewNspawnCluster("smoke") if err != nil { @@ -73,48 +76,47 @@ func TestUnitSubmit(t *testing.T) { t.Fatal(err) } + unitFile := "fixtures/units/hello.service" + // submit a unit and assert it shows up - if _, _, err := cluster.Fleetctl(m, "submit", "fixtures/units/hello.service"); err != nil { + _, _, err = cluster.Fleetctl(m, "submit", unitFile) + if err != nil { t.Fatalf("Unable to submit fleet unit: %v", err) } - stdout, _, err := cluster.Fleetctl(m, "list-units", "--no-legend") + + // wait until the unit gets submitted up to 15 seconds + err = runListUnits(cluster, m, 1) 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) - } // submitting the same unit should not fail - if _, _, err = cluster.Fleetctl(m, "submit", "fixtures/units/hello.service"); err != nil { + _, _, err = cluster.Fleetctl(m, "submit", unitFile) + if err != nil { t.Fatalf("Expected no failure when double-submitting unit, got this: %v", err) } // destroy the unit and ensure it disappears from the unit list - if _, _, err := cluster.Fleetctl(m, "destroy", "fixtures/units/hello.service"); err != nil { + _, _, err = cluster.Fleetctl(m, "destroy", unitFile) + if err != nil { t.Fatalf("Failed to destroy unit: %v", err) } - stdout, _, err = cluster.Fleetctl(m, "list-units", "--no-legend") + // wait until the unit gets submitted up to 15 seconds + err = runListUnits(cluster, m, 0) 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) - } // submitting the unit after destruction should succeed - if _, _, err := cluster.Fleetctl(m, "submit", "fixtures/units/hello.service"); err != nil { + _, _, err = cluster.Fleetctl(m, "submit", unitFile) + if err != nil { t.Fatalf("Unable to submit fleet unit: %v", err) } - stdout, _, err = cluster.Fleetctl(m, "list-units", "--no-legend") + // wait until the unit gets submitted up to 15 seconds + err = runListUnits(cluster, m, 1) 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) - } } func TestUnitRestart(t *testing.T) {