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

Commit b323371

Browse files
author
Dongsu Park
committed
functional: support running etcdctl, as well as using Cluster.Keyspace
Introduce new helpers for running etcdctl for functional tests. Also export a method Cluster.Keyspace() to get it called by functional tests.
1 parent 0be1141 commit b323371

4 files changed

Lines changed: 48 additions & 3 deletions

File tree

build-env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ eval $(go env)
2121
export PATH="${GOROOT}/bin:${PATH}"
2222
export FLEETD_BIN="$(pwd)/bin/fleetd"
2323
export FLEETCTL_BIN="$(pwd)/bin/fleetctl"
24+
export ETCDCTL_BIN="/usr/bin/etcdctl"

functional/platform/cluster.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ type Cluster interface {
3737
FleetctlWithInput(m Member, input string, args ...string) (string, string, error)
3838
WaitForNActiveUnits(Member, int) (map[string][]util.UnitState, error)
3939
WaitForNMachines(Member, int) ([]string, error)
40+
41+
Keyspace() string
4042
}
4143

4244
func CreateNClusterMembers(cl Cluster, count int) ([]Member, error) {

functional/platform/nspawn.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (nc *nspawnCluster) nextID() string {
8989
return strconv.Itoa(nc.maxID)
9090
}
9191

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

254254
etcd := "http://172.18.0.1:4001"
255-
return util.BuildCloudConfig(userFile, ip, etcd, nc.keyspace())
255+
return util.BuildCloudConfig(userFile, ip, etcd, nc.Keyspace())
256256
}
257257

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

428428
run("ip link del fleet0")
429429

functional/util/util.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
)
2626

2727
var fleetctlBinPath string
28+
var etcdctlBinPath string
2829

2930
func init() {
3031
fleetctlBinPath = os.Getenv("FLEETCTL_BIN")
@@ -36,6 +37,15 @@ func init() {
3637
os.Exit(1)
3738
}
3839

40+
etcdctlBinPath = os.Getenv("ETCDCTL_BIN")
41+
if etcdctlBinPath == "" {
42+
fmt.Println("ETCDCTL_BIN environment variable must be set")
43+
os.Exit(1)
44+
} else if _, err := os.Stat(etcdctlBinPath); err != nil {
45+
fmt.Printf("%v\n", err)
46+
os.Exit(1)
47+
}
48+
3949
if os.Getenv("SSH_AUTH_SOCK") == "" {
4050
fmt.Println("SSH_AUTH_SOCK environment variable must be set")
4151
os.Exit(1)
@@ -76,6 +86,38 @@ func RunFleetctlWithInput(input string, args ...string) (string, string, error)
7686
return stdoutBytes.String(), stderrBytes.String(), err
7787
}
7888

89+
func RunEtcdctl(args ...string) (string, string, error) {
90+
log.Printf("%s %s", etcdctlBinPath, strings.Join(args, " "))
91+
var stdoutBytes, stderrBytes bytes.Buffer
92+
cmd := exec.Command(etcdctlBinPath, args...)
93+
cmd.Stdout = &stdoutBytes
94+
cmd.Stderr = &stderrBytes
95+
err := cmd.Run()
96+
return stdoutBytes.String(), stderrBytes.String(), err
97+
}
98+
99+
func RunEtcdctlWithInput(input string, args ...string) (string, string, error) {
100+
log.Printf("%s %s", etcdctlBinPath, strings.Join(args, " "))
101+
var stdoutBytes, stderrBytes bytes.Buffer
102+
cmd := exec.Command(etcdctlBinPath, args...)
103+
cmd.Stdout = &stdoutBytes
104+
cmd.Stderr = &stderrBytes
105+
stdin, err := cmd.StdinPipe()
106+
if err != nil {
107+
return "", "", err
108+
}
109+
110+
if err = cmd.Start(); err != nil {
111+
return "", "", err
112+
}
113+
114+
stdin.Write([]byte(input))
115+
stdin.Close()
116+
err = cmd.Wait()
117+
118+
return stdoutBytes.String(), stderrBytes.String(), err
119+
}
120+
79121
// ActiveToSingleStates takes a map of active states (such as that returned by
80122
// WaitForNActiveUnits) and ensures that each unit has at most a single active
81123
// state. It returns a mapping of unit name to a single UnitState.

0 commit comments

Comments
 (0)