etcd/e2e/ctl_v3_test.go

217 lines
5.0 KiB
Go
Raw Normal View History

2016-05-13 06:51:48 +03:00
// Copyright 2016 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package e2e
import (
"fmt"
2016-03-28 20:49:03 +03:00
"os"
"strings"
"testing"
"time"
"github.com/coreos/etcd/pkg/flags"
"github.com/coreos/etcd/pkg/testutil"
"github.com/coreos/etcd/version"
)
2016-04-14 21:42:57 +03:00
func TestCtlV3Version(t *testing.T) { testCtl(t, versionTest) }
2016-04-02 08:45:27 +03:00
2016-04-14 21:42:57 +03:00
func versionTest(cx ctlCtx) {
if err := ctlV3Version(cx); err != nil {
cx.t.Fatalf("versionTest ctlV3Version error (%v)", err)
}
2016-03-23 01:49:08 +03:00
}
2016-04-14 21:42:57 +03:00
func ctlV3Version(cx ctlCtx) error {
cmdArgs := append(cx.PrefixArgs(), "version")
return spawnWithExpect(cmdArgs, version.Version)
}
2016-10-03 21:29:48 +03:00
// TestCtlV3DialWithHTTPScheme ensures that client handles endpoints with HTTPS scheme.
func TestCtlV3DialWithHTTPScheme(t *testing.T) {
testCtl(t, dialWithSchemeTest, withCfg(configClientTLS))
}
func dialWithSchemeTest(cx ctlCtx) {
cmdArgs := append(cx.prefixArgs(cx.epc.endpoints()), "put", "foo", "bar")
if err := spawnWithExpect(cmdArgs, "OK"); err != nil {
cx.t.Fatal(err)
}
}
2016-03-31 21:02:01 +03:00
type ctlCtx struct {
2016-04-20 21:34:30 +03:00
t *testing.T
cfg etcdProcessClusterConfig
quotaBackendBytes int64
noStrictReconfig bool
2016-04-20 21:34:30 +03:00
2016-03-31 21:02:01 +03:00
epc *etcdProcessCluster
envMap map[string]struct{}
2016-03-31 21:02:01 +03:00
dialTimeout time.Duration
quorum bool // if true, set up 3-node cluster and linearizable read
interactive bool
2016-06-09 20:05:26 +03:00
user string
pass string
// for compaction
compactPhysical bool
2016-03-23 01:49:08 +03:00
}
2016-03-31 21:02:01 +03:00
type ctlOption func(*ctlCtx)
func (cx *ctlCtx) applyOpts(opts []ctlOption) {
for _, opt := range opts {
opt(cx)
}
2016-03-23 01:49:08 +03:00
}
2016-03-31 21:02:01 +03:00
func withCfg(cfg etcdProcessClusterConfig) ctlOption {
return func(cx *ctlCtx) { cx.cfg = cfg }
2016-03-23 01:49:08 +03:00
}
2016-03-31 21:02:01 +03:00
func withDialTimeout(timeout time.Duration) ctlOption {
return func(cx *ctlCtx) { cx.dialTimeout = timeout }
}
2016-03-31 21:02:01 +03:00
func withQuorum() ctlOption {
return func(cx *ctlCtx) { cx.quorum = true }
}
2016-03-31 21:02:01 +03:00
func withInteractive() ctlOption {
return func(cx *ctlCtx) { cx.interactive = true }
}
2016-04-20 21:34:30 +03:00
func withQuota(b int64) ctlOption {
return func(cx *ctlCtx) { cx.quotaBackendBytes = b }
}
func withCompactPhysical() ctlOption {
return func(cx *ctlCtx) { cx.compactPhysical = true }
}
func withNoStrictReconfig() ctlOption {
return func(cx *ctlCtx) { cx.noStrictReconfig = true }
}
func withFlagByEnv() ctlOption {
return func(cx *ctlCtx) { cx.envMap = make(map[string]struct{}) }
}
2016-03-31 21:02:01 +03:00
func testCtl(t *testing.T, testFunc func(ctlCtx), opts ...ctlOption) {
defer testutil.AfterTest(t)
2016-03-31 21:02:01 +03:00
ret := ctlCtx{
t: t,
cfg: configAutoTLS,
dialTimeout: 7 * time.Second,
2016-03-31 21:02:01 +03:00
}
ret.applyOpts(opts)
2016-03-28 20:49:03 +03:00
os.Setenv("ETCDCTL_API", "3")
2016-04-14 21:42:57 +03:00
mustEtcdctl(t)
if !ret.quorum {
ret.cfg = *configStandalone(ret.cfg)
}
2016-04-20 21:34:30 +03:00
if ret.quotaBackendBytes > 0 {
ret.cfg.quotaBackendBytes = ret.quotaBackendBytes
}
ret.cfg.noStrictReconfig = ret.noStrictReconfig
2016-04-20 21:34:30 +03:00
2016-04-14 21:42:57 +03:00
epc, err := newEtcdProcessCluster(&ret.cfg)
if err != nil {
t.Fatalf("could not start etcd process cluster (%v)", err)
}
ret.epc = epc
2016-03-31 21:02:01 +03:00
defer func() {
2016-03-28 20:49:03 +03:00
os.Unsetenv("ETCDCTL_API")
if ret.envMap != nil {
for k := range ret.envMap {
os.Unsetenv(k)
}
}
2016-03-31 21:02:01 +03:00
if errC := ret.epc.Close(); errC != nil {
t.Fatalf("error closing etcd processes (%v)", errC)
}
}()
donec := make(chan struct{})
go func() {
defer close(donec)
testFunc(ret)
}()
2016-03-31 21:02:01 +03:00
timeout := 2*ret.dialTimeout + time.Second
if ret.dialTimeout == 0 {
timeout = 30 * time.Second
}
2016-03-31 21:02:01 +03:00
select {
case <-time.After(timeout):
testutil.FatalStack(t, fmt.Sprintf("test timed out after %v", timeout))
case <-donec:
2016-03-31 21:02:01 +03:00
}
}
2016-10-03 21:29:48 +03:00
func (cx *ctlCtx) prefixArgs(eps []string) []string {
if len(cx.epc.proxies()) > 0 { // TODO: add proxy check as in v2
panic("v3 proxy not implemented")
}
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 {
fmap["insecure-transport"] = "false"
fmap["insecure-skip-tls-verify"] = "true"
2016-05-03 18:35:02 +03:00
} else {
fmap["cacert"] = caPath
fmap["cert"] = certPath
fmap["key"] = privateKeyPath
2016-05-03 18:35:02 +03:00
}
}
2016-06-09 20:05:26 +03:00
if cx.user != "" {
fmap["user"] = cx.user + ":" + cx.pass
2016-06-09 20:05:26 +03:00
}
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.
2016-10-03 21:29:48 +03:00
func (cx *ctlCtx) PrefixArgs() []string {
return cx.prefixArgs(cx.epc.grpcEndpoints())
}
func isGRPCTimedout(err error) bool {
return strings.Contains(err.Error(), "grpc: timed out trying to connect")
}