From 11031e2cb9a4a3b26832f218f5705c77db19c1cb Mon Sep 17 00:00:00 2001 From: Dongsu Park Date: Tue, 15 Mar 2016 17:18:07 +0100 Subject: [PATCH] functional: introduce new helpers for testing the replace option Add new helpers genNewFleetService() and destroyUnitRetrying() to prepare the new functional tests for the replace options. genNewFleetService() is a helper to replace a string with a new one. It's necessary for the next functional tests. destroyUnitRetrying() runs "fleetctl --replace" repeatedly to ensure the unit is actually removed. --- functional/unit_action_test.go | 54 ++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/functional/unit_action_test.go b/functional/unit_action_test.go index 78a150fd7..0eb8cecd6 100644 --- a/functional/unit_action_test.go +++ b/functional/unit_action_test.go @@ -15,6 +15,8 @@ package functional import ( + "fmt" + "io/ioutil" "strings" "testing" @@ -224,3 +226,55 @@ func TestUnitSSHActions(t *testing.T) { t.Errorf("Could not find expected string in journal output:\n%s", stdout) } } + +// genNewFleetService() is a helper for generating a temporary fleet service +// that reads from oldFile, replaces oldVal with newVal, and stores the result +// to newFile. +func genNewFleetService(newFile, oldFile, newVal, oldVal string) error { + input, err := ioutil.ReadFile(oldFile) + if err != nil { + return err + } + lines := strings.Split(string(input), "\n") + + for i, line := range lines { + if strings.Contains(line, oldVal) { + lines[i] = strings.Replace(line, oldVal, newVal, len(oldVal)) + } + } + output := strings.Join(lines, "\n") + err = ioutil.WriteFile(newFile, []byte(output), 0644) + if err != nil { + return err + } + return nil +} + +// destroyUnitRetrying() destroys the unit and ensure it disappears from the +// unit list. It could take a little time until the unit gets destroyed. +func destroyUnitRetrying(cluster platform.Cluster, m platform.Member, serviceFile string) error { + maxAttempts := 3 + found := false + var stdout string + var err error + for { + if _, _, err := cluster.Fleetctl(m, "destroy", serviceFile); err != nil { + return fmt.Errorf("Failed to destroy unit: %v", err) + } + stdout, _, err = cluster.Fleetctl(m, "list-units", "--no-legend") + if err != nil { + return fmt.Errorf("Failed to run list-units: %v", err) + } + if strings.TrimSpace(stdout) == "" || maxAttempts == 0 { + found = true + break + } + maxAttempts-- + } + + if !found { + return fmt.Errorf("Did not find 0 units in cluster: \n%s", stdout) + } + + return nil +}