Skip to content

Commit

Permalink
functional: introduce new helpers for testing the replace option
Browse files Browse the repository at this point in the history
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 <cmd> --replace" repeatedly to ensure
the unit is actually removed.
  • Loading branch information
Dongsu Park committed Mar 16, 2016
1 parent 963d001 commit 11031e2
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions functional/unit_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package functional

import (
"fmt"
"io/ioutil"
"strings"
"testing"

Expand Down Expand Up @@ -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
}

0 comments on commit 11031e2

Please sign in to comment.