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 from functional tests
Browse files Browse the repository at this point in the history
Introduce new helpers for running etcdctl.
  • Loading branch information
Dongsu Park committed Mar 22, 2016
1 parent 0be1141 commit cd81091
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions build-env
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ eval $(go env)
export PATH="${GOROOT}/bin:${PATH}"
export FLEETD_BIN="$(pwd)/bin/fleetd"
export FLEETCTL_BIN="$(pwd)/bin/fleetctl"
export ETCDCTL_BIN="/usr/bin/etcdctl"
42 changes: 42 additions & 0 deletions functional/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
)

var fleetctlBinPath string
var etcdctlBinPath string

func init() {
fleetctlBinPath = os.Getenv("FLEETCTL_BIN")
Expand All @@ -36,6 +37,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 @@ -76,6 +86,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 cd81091

Please sign in to comment.