Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
functional: add new tests for the replace option
Browse files Browse the repository at this point in the history
TestUnit{Submit,Load,Start}Replace() tests whether a command "fleetctl
{submit,load,start} --replace hello.service" works respectively.
As most of the test sequences are identical, the common part is split
into replaceUnitCommon().
  • Loading branch information
Dongsu Park authored and Djalal Harouni committed Mar 17, 2016
1 parent b6ccdcb commit 6338b3e
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions functional/unit_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ package functional
import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"

"github.com/coreos/fleet/functional/platform"
)

const (
tmpHelloService = "/tmp/hello.service"
fxtHelloService = "fixtures/units/hello.service"
)

// TestUnitRunnable is the simplest test possible, deplying a single-node
// cluster and ensuring a unit can enter an 'active' state
func TestUnitRunnable(t *testing.T) {
Expand Down Expand Up @@ -169,6 +175,30 @@ func TestUnitRestart(t *testing.T) {

}

// TestUnitSubmitReplace() tests whether a command "fleetctl submit --replace
// hello.service" works or not.
func TestUnitSubmitReplace(t *testing.T) {
if err := replaceUnitCommon("submit"); err != nil {
t.Fatal(err)
}
}

// TestUnitLoadReplace() tests whether a command "fleetctl load --replace
// hello.service" works or not.
func TestUnitLoadReplace(t *testing.T) {
if err := replaceUnitCommon("load"); err != nil {
t.Fatal(err)
}
}

// TestUnitStartReplace() tests whether a command "fleetctl start --replace
// hello.service" works or not.
func TestUnitStartReplace(t *testing.T) {
if err := replaceUnitCommon("start"); err != nil {
t.Fatal(err)
}
}

func TestUnitSSHActions(t *testing.T) {
cluster, err := platform.NewNspawnCluster("smoke")
if err != nil {
Expand Down Expand Up @@ -227,6 +257,74 @@ func TestUnitSSHActions(t *testing.T) {
}
}

// replaceUnitCommon() tests whether a command "fleetctl {submit,load,start}
// --replace hello.service" works or not.
func replaceUnitCommon(cmd string) error {
// check if cmd is one of the supported commands.
listCmds := []string{"submit", "load", "start"}
found := false
for _, ccmd := range listCmds {
if ccmd == cmd {
found = true
}
}
if !found {
return fmt.Errorf("invalid command %s", cmd)
}

cluster, err := platform.NewNspawnCluster("smoke")
if err != nil {
return fmt.Errorf("%v", err)
}
defer cluster.Destroy()

m, err := cluster.CreateMember()
if err != nil {
return fmt.Errorf("%v", err)
}
_, err = cluster.WaitForNMachines(m, 1)
if err != nil {
return fmt.Errorf("%v", err)
}

// run a command for a unit and assert it shows up
if _, _, err := cluster.Fleetctl(m, cmd, fxtHelloService); err != nil {
return fmt.Errorf("Unable to %s fleet unit: %v", cmd, err)
}
stdout, _, err := cluster.Fleetctl(m, "list-units", "--no-legend")
if err != nil {
return fmt.Errorf("Failed to run list-units: %v", err)
}
units := strings.Split(strings.TrimSpace(stdout), "\n")
if len(units) != 1 {
return fmt.Errorf("Did not find 1 unit in cluster: \n%s", stdout)
}

// replace the unit and assert it shows up
err = genNewFleetService(tmpHelloService, fxtHelloService, "sleep 2", "sleep 1")
if err != nil {
return fmt.Errorf("Failed to generate a temp fleet service: %v", err)
}
if _, _, err := cluster.Fleetctl(m, cmd, "--replace", tmpHelloService); err != nil {
return fmt.Errorf("Unable to replace fleet 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)
}
units = strings.Split(strings.TrimSpace(stdout), "\n")
if len(units) != 1 {
return fmt.Errorf("Did not find 1 unit in cluster: \n%s", stdout)
}
os.Remove(tmpHelloService)

if err := destroyUnitRetrying(cluster, m, fxtHelloService); err != nil {
return fmt.Errorf("Cannot destroy unit %v", fxtHelloService)
}

return nil
}

// copyFile()
func copyFile(newFile, oldFile string) error {
input, err := ioutil.ReadFile(oldFile)
Expand Down

0 comments on commit 6338b3e

Please sign in to comment.