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

Commit

Permalink
functional: support running etcdctl, as well as using Cluster.Keyspace
Browse files Browse the repository at this point in the history
Introduce new helpers for running etcdctl for functional tests.
Also export a method Cluster.Keyspace() to get it called by functional
tests.
  • Loading branch information
Dongsu Park committed Aug 12, 2016
1 parent cbb77da commit 789b688
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
1 change: 1 addition & 0 deletions build-env
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ export PATH="${GOROOT}/bin:${PATH}"
export FLEETD_BIN="${CDIR}/bin/fleetd"
export FLEETCTL_BIN="${CDIR}/bin/fleetctl"
export FLEETD_TEST_ENV="enable_grpc=false"
export ETCDCTL_BIN="/usr/bin/etcdctl"
2 changes: 2 additions & 0 deletions functional/platform/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type Cluster interface {
WaitForNActiveUnits(Member, int) (map[string][]util.UnitState, error)
WaitForNUnitFiles(Member, int) (map[string][]util.UnitFileState, error)
WaitForNMachines(Member, int) ([]string, error)

Keyspace() string
}

func CreateNClusterMembers(cl Cluster, count int) ([]Member, error) {
Expand Down
6 changes: 3 additions & 3 deletions functional/platform/nspawn.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (nc *nspawnCluster) nextID() string {
return strconv.Itoa(nc.maxID)
}

func (nc *nspawnCluster) keyspace() string {
func (nc *nspawnCluster) Keyspace() string {
// TODO(jonboulle): generate this dynamically with atomic in order keys?
return fmt.Sprintf("/fleet_functional/%s", nc.name)
}
Expand Down Expand Up @@ -367,7 +367,7 @@ func (nc *nspawnCluster) buildConfigDrive(dir, ip string) error {
defer userFile.Close()

etcd := "http://172.18.0.1:4001"
return util.BuildCloudConfig(userFile, ip, etcd, nc.keyspace())
return util.BuildCloudConfig(userFile, ip, etcd, nc.Keyspace())
}

func (nc *nspawnCluster) Members() []Member {
Expand Down Expand Up @@ -601,7 +601,7 @@ func (nc *nspawnCluster) Destroy(t *testing.T) error {
// TODO(bcwaldon): This returns 4 on success, but we can't easily
// ignore just that return code. Ignore the returned error
// altogether until this is fixed.
run("etcdctl rm --recursive " + nc.keyspace())
run("etcdctl rm --recursive " + nc.Keyspace())

run("ip link del fleet0")

Expand Down
42 changes: 42 additions & 0 deletions functional/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
)

var fleetctlBinPath string
var etcdctlBinPath string

func init() {
fleetctlBinPath = os.Getenv("FLEETCTL_BIN")
Expand All @@ -39,6 +40,15 @@ func init() {
os.Exit(1)
}

etcdctlBinPath = os.Getenv("ETCDCTL_BIN")
if etcdctlBinPath == "" {
fmt.Println("ETCDCTL_BIN environment variable must be set")
os.Exit(1)
} else if _, err := os.Stat(etcdctlBinPath); err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}

if os.Getenv("SSH_AUTH_SOCK") == "" {
fmt.Println("SSH_AUTH_SOCK environment variable must be set")
os.Exit(1)
Expand Down Expand Up @@ -79,6 +89,38 @@ func RunFleetctlWithInput(input string, args ...string) (string, string, error)
return stdoutBytes.String(), stderrBytes.String(), err
}

func RunEtcdctl(args ...string) (string, string, error) {
log.Printf("%s %s", etcdctlBinPath, strings.Join(args, " "))
var stdoutBytes, stderrBytes bytes.Buffer
cmd := exec.Command(etcdctlBinPath, args...)
cmd.Stdout = &stdoutBytes
cmd.Stderr = &stderrBytes
err := cmd.Run()
return stdoutBytes.String(), stderrBytes.String(), err
}

func RunEtcdctlWithInput(input string, args ...string) (string, string, error) {
log.Printf("%s %s", etcdctlBinPath, strings.Join(args, " "))
var stdoutBytes, stderrBytes bytes.Buffer
cmd := exec.Command(etcdctlBinPath, args...)
cmd.Stdout = &stdoutBytes
cmd.Stderr = &stderrBytes
stdin, err := cmd.StdinPipe()
if err != nil {
return "", "", err
}

if err = cmd.Start(); err != nil {
return "", "", err
}

stdin.Write([]byte(input))
stdin.Close()
err = cmd.Wait()

return stdoutBytes.String(), stderrBytes.String(), err
}

// ActiveToSingleStates takes a map of active states (such as that returned by
// WaitForNActiveUnits) and ensures that each unit has at most a single active
// state. It returns a mapping of unit name to a single UnitState.
Expand Down

0 comments on commit 789b688

Please sign in to comment.