e2e: add tests with environment vars for flags

This commit is contained in:
Gyu-Ho Lee 2016-10-14 11:15:59 -07:00
parent a00ed609c3
commit 8081254498
2 changed files with 39 additions and 5 deletions

View File

@ -25,6 +25,9 @@ func TestCtlV3PutClientTLS(t *testing.T) { testCtl(t, putTest, withCfg(confi
func TestCtlV3PutClientAutoTLS(t *testing.T) { testCtl(t, putTest, withCfg(configClientAutoTLS)) }
func TestCtlV3PutPeerTLS(t *testing.T) { testCtl(t, putTest, withCfg(configPeerTLS)) }
func TestCtlV3PutTimeout(t *testing.T) { testCtl(t, putTest, withDialTimeout(0)) }
func TestCtlV3PutClientTLSFlagByEnv(t *testing.T) {
testCtl(t, putTest, withCfg(configClientTLS), withFlagByEnv())
}
func TestCtlV3Get(t *testing.T) { testCtl(t, getTest) }
func TestCtlV3GetNoTLS(t *testing.T) { testCtl(t, getTest, withCfg(configNoTLS)) }

View File

@ -15,11 +15,13 @@
package e2e
import (
"fmt"
"os"
"strings"
"testing"
"time"
"github.com/coreos/etcd/pkg/flags"
"github.com/coreos/etcd/pkg/testutil"
"github.com/coreos/etcd/version"
)
@ -57,6 +59,8 @@ type ctlCtx struct {
epc *etcdProcessCluster
envMap map[string]struct{}
dialTimeout time.Duration
quorum bool // if true, set up 3-node cluster and linearizable read
@ -105,6 +109,10 @@ func withNoStrictReconfig() ctlOption {
return func(cx *ctlCtx) { cx.noStrictReconfig = true }
}
func withFlagByEnv() ctlOption {
return func(cx *ctlCtx) { cx.envMap = make(map[string]struct{}) }
}
func testCtl(t *testing.T, testFunc func(ctlCtx), opts ...ctlOption) {
defer testutil.AfterTest(t)
@ -133,6 +141,11 @@ func testCtl(t *testing.T, testFunc func(ctlCtx), opts ...ctlOption) {
defer func() {
os.Unsetenv("ETCDCTL_API")
if ret.envMap != nil {
for k := range ret.envMap {
os.Unsetenv(k)
}
}
if errC := ret.epc.Close(); errC != nil {
t.Fatalf("error closing etcd processes (%v)", errC)
}
@ -160,22 +173,40 @@ func (cx *ctlCtx) prefixArgs(eps []string) []string {
panic("v3 proxy not implemented")
}
cmdArgs := []string{ctlBinPath, "--endpoints", strings.Join(eps, ","), "--dial-timeout", cx.dialTimeout.String()}
fmap := make(map[string]string)
fmap["endpoints"] = strings.Join(eps, ",")
fmap["dial-timeout"] = cx.dialTimeout.String()
if cx.epc.cfg.clientTLS == clientTLS {
if cx.epc.cfg.isClientAutoTLS {
cmdArgs = append(cmdArgs, "--insecure-transport=false", "--insecure-skip-tls-verify")
fmap["insecure-transport"] = "false"
fmap["insecure-skip-tls-verify"] = "true"
} else {
cmdArgs = append(cmdArgs, "--cacert", caPath, "--cert", certPath, "--key", privateKeyPath)
fmap["cacert"] = caPath
fmap["cert"] = certPath
fmap["key"] = privateKeyPath
}
}
if cx.user != "" {
cmdArgs = append(cmdArgs, "--user="+cx.user+":"+cx.pass)
fmap["user"] = cx.user + ":" + cx.pass
}
useEnv := cx.envMap != nil
cmdArgs := []string{ctlBinPath}
for k, v := range fmap {
if useEnv {
ek := flags.FlagToEnv("ETCDCTL", k)
os.Setenv(ek, v)
cx.envMap[ek] = struct{}{}
} else {
cmdArgs = append(cmdArgs, fmt.Sprintf("--%s=%s", k, v))
}
}
return cmdArgs
}
// PrefixArgs prefixes etcdctl command.
// Make sure to unset environment variables after tests.
func (cx *ctlCtx) PrefixArgs() []string {
return cx.prefixArgs(cx.epc.grpcEndpoints())
}