Skip to content

Commit

Permalink
etcdctlv3: add dial timeout flag
Browse files Browse the repository at this point in the history
  • Loading branch information
gyuho committed Mar 22, 2016
1 parent 2b44df5 commit c480f48
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 5 deletions.
4 changes: 4 additions & 0 deletions e2e/etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ type etcdProcessClusterConfig struct {
isPeerTLS bool
isPeerAutoTLS bool
initialToken string
isV3 bool
}

// newEtcdProcessCluster launches a new cluster from etcd processes, returning
Expand Down Expand Up @@ -283,6 +284,9 @@ func (cfg *etcdProcessClusterConfig) etcdProcessConfigs() []*etcdProcessConfig {
"--initial-cluster-token", cfg.initialToken,
"--data-dir", dataDirPath,
}
if cfg.isV3 {
args = append(args, "--experimental-v3demo")
}

args = append(args, cfg.tlsArgs()...)

Expand Down
135 changes: 135 additions & 0 deletions e2e/etcdctlv3_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Copyright 2016 CoreOS, Inc.
//
// 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 (
"strings"
"testing"
"time"

"github.com/coreos/etcd/pkg/fileutil"
"github.com/coreos/etcd/pkg/testutil"
)

func TestCtlV3SetQuorum(t *testing.T) {
testCtlV3Set(t, &configNoTLS, 3*time.Second, true)
}

func TestCtlV3SetQuorumTimeout(t *testing.T) {
testCtlV3Set(t, &configNoTLS, time.Nanosecond, true)
}

func TestCtlV3SetPeerTLSQuorum(t *testing.T) {
testCtlV3Set(t, &configPeerTLS, 3*time.Second, true)
}

func testCtlV3Set(t *testing.T, cfg *etcdProcessClusterConfig, dialTimeout time.Duration, quorum bool) {
defer testutil.AfterTest(t)

epc := setupCtlV3Test(t, cfg, quorum)
defer func() {
if errC := epc.Close(); errC != nil {
t.Fatalf("error closing etcd processes (%v)", errC)
}
}()

key, value := "foo", "bar"

if err := ctlV3Put(epc, key, value, dialTimeout); err != nil {
if dialTimeout > 0 && dialTimeout <= time.Nanosecond && isGRPCTimedout(err) { // timeout expected
return
}
t.Fatalf("put error (%v)", err)
}
if err := ctlV3Get(epc, key, value, dialTimeout, quorum); err != nil {
if dialTimeout > 0 && dialTimeout <= time.Nanosecond && isGRPCTimedout(err) { // timeout expected
return
}
t.Fatalf("get error (%v)", err)
}
}

func ctlV3PrefixArgs(clus *etcdProcessCluster, dialTimeout time.Duration) []string {
if len(clus.proxies()) > 0 { // TODO: add proxy check as in v2
panic("v3 proxy not implemented")
}

endpoints := ""
if backends := clus.backends(); len(backends) != 0 {
es := []string{}
for _, b := range backends {
es = append(es, stripSchema(b.cfg.acurl.String()))
}
endpoints = strings.Join(es, ",")
}
cmdArgs := []string{"../bin/etcdctlv3", "--endpoints", endpoints, "--dial-timeout", dialTimeout.String()}
if clus.cfg.isClientTLS {
cmdArgs = append(cmdArgs, "--cacert", caPath, "--cert", certPath, "--key", privateKeyPath)
}
return cmdArgs
}

func ctlV3Put(clus *etcdProcessCluster, key, value string, dialTimeout time.Duration) error {
cmdArgs := append(ctlV3PrefixArgs(clus, dialTimeout), "put", key, value)
return spawnWithExpectedString(cmdArgs, "OK")
}

func ctlV3Get(clus *etcdProcessCluster, key, value string, dialTimeout time.Duration, quorum bool) error {
if !quorum { // TODO: add serialized option
panic("serialized option is not implemented")
}

cmdArgs := append(ctlV3PrefixArgs(clus, dialTimeout), "get", key)

// TODO: match by value. Currently it prints out both key and value in multi-lines.
return spawnWithExpectedString(cmdArgs, key)
}

func mustCtlV3(t *testing.T) {
if !fileutil.Exist("../bin/etcdctlv3") {
t.Fatalf("could not find etcdctlv3 binary")
}
}

func setupCtlV3Test(t *testing.T, cfg *etcdProcessClusterConfig, quorum bool) *etcdProcessCluster {
if !quorum { // TODO: add serialized option
panic("serialized option is not implemented")
}

mustCtlV3(t)
if !quorum {
cfg = configStandalone(*cfg)
}
cfg.isV3 = true
epc, err := newEtcdProcessCluster(cfg)
if err != nil {
t.Fatalf("could not start etcd process cluster (%v)", err)
}
return epc
}

func isGRPCTimedout(err error) bool {
return strings.Contains(err.Error(), "grpc: timed out trying to connect")
}

func stripSchema(s string) string {
if strings.HasPrefix(s, "http://") {
s = strings.Replace(s, "http://", "", -1)
}
if strings.HasPrefix(s, "https://") {
s = strings.Replace(s, "https://", "", -1)
}
return s
}
19 changes: 15 additions & 4 deletions etcdctlv3/command/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import (
// GlobalFlags are flags that defined globally
// and are inherited to all sub-commands.
type GlobalFlags struct {
Endpoints []string
Endpoints []string
DialTimeout time.Duration

TLS transport.TLSInfo

Expand All @@ -44,6 +45,8 @@ func mustClientFromCmd(cmd *cobra.Command) *clientv3.Client {
ExitWithError(ExitError, err)
}

dialTimeout := dialTimeoutFromCmd(cmd)

var cert, key, cacert string
if cert, err = cmd.Flags().GetString("cert"); err != nil {
ExitWithError(ExitBadArgs, err)
Expand All @@ -69,10 +72,10 @@ func mustClientFromCmd(cmd *cobra.Command) *clientv3.Client {
ExitWithError(ExitBadFeature, errors.New("unsupported output format"))
}

return mustClient(endpoints, cert, key, cacert)
return mustClient(endpoints, dialTimeout, cert, key, cacert)
}

func mustClient(endpoints []string, cert, key, cacert string) *clientv3.Client {
func mustClient(endpoints []string, dialTimeout time.Duration, cert, key, cacert string) *clientv3.Client {
// set tls if any one tls option set
var cfgtls *transport.TLSInfo
tls := transport.TLSInfo{}
Expand All @@ -94,7 +97,7 @@ func mustClient(endpoints []string, cert, key, cacert string) *clientv3.Client {

cfg := clientv3.Config{
Endpoints: endpoints,
DialTimeout: 20 * time.Second,
DialTimeout: dialTimeout,
}
if cfgtls != nil {
clientTLS, err := cfgtls.ClientConfig()
Expand Down Expand Up @@ -122,3 +125,11 @@ func argOrStdin(args []string, stdin io.Reader, i int) (string, error) {
}
return string(bytes), nil
}

func dialTimeoutFromCmd(cmd *cobra.Command) time.Duration {
dialTimeout, err := cmd.Flags().GetDuration("dial-timeout")
if err != nil {
ExitWithError(ExitError, err)
}
return dialTimeout
}
4 changes: 3 additions & 1 deletion etcdctlv3/command/make_mirror_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ func makeMirrorCommandFunc(cmd *cobra.Command, args []string) {
ExitWithError(ExitBadArgs, errors.New("make-mirror takes one destination arguement."))
}

dc := mustClient([]string{args[0]}, mmcert, mmkey, mmcacert)
dialTimeout := dialTimeoutFromCmd(cmd)

dc := mustClient([]string{args[0]}, dialTimeout, mmcert, mmkey, mmcacert)
c := mustClientFromCmd(cmd)

err := makeMirror(context.TODO(), c, dc)
Expand Down
5 changes: 5 additions & 0 deletions etcdctlv3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package main

import (
"text/tabwriter"
"time"

"github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
"github.com/coreos/etcd/etcdctlv3/command"
Expand All @@ -25,6 +26,8 @@ import (
const (
cliName = "etcdctlv3"
cliDescription = "A simple command line client for etcd3."

defaultDialTimeout = 2 * time.Second
)

var (
Expand All @@ -46,6 +49,8 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&globalFlags.OutputFormat, "write-out", "w", "simple", "set the output format (simple, json, protobuf)")
rootCmd.PersistentFlags().BoolVar(&globalFlags.IsHex, "hex", false, "print byte strings as hex encoded strings")

rootCmd.PersistentFlags().DurationVar(&globalFlags.DialTimeout, "dial-timeout", defaultDialTimeout, "dial timeout for client connections")

rootCmd.PersistentFlags().StringVar(&globalFlags.TLS.CertFile, "cert", "", "identify secure client using this TLS certificate file")
rootCmd.PersistentFlags().StringVar(&globalFlags.TLS.KeyFile, "key", "", "identify secure client using this TLS key file")
rootCmd.PersistentFlags().StringVar(&globalFlags.TLS.CAFile, "cacert", "", "verify certificates of TLS-enabled secure servers using this CA bundle")
Expand Down

0 comments on commit c480f48

Please sign in to comment.