Skip to content

Commit

Permalink
etcdctl/ctlv3: etcd v3.4 makes ETCDCTL_API=3 by default
Browse files Browse the repository at this point in the history
  • Loading branch information
vimalk78 committed May 29, 2018
1 parent 476c9cb commit 25bc657
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 18 deletions.
9 changes: 1 addition & 8 deletions etcdctl/ctlv2/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/urfave/cli"
)

func Start(apiv string) {
func Start() {
app := cli.NewApp()
app.Name = "etcdctl"
app.Version = version.Version
Expand All @@ -36,13 +36,6 @@ func Start(apiv string) {
}
app.Usage = "A simple command line client for etcd."

if apiv == "" {
app.Usage += "\n\n" +
"WARNING:\n" +
" Environment variable ETCDCTL_API is not set; defaults to etcdctl v2.\n" +
" Set environment variable ETCDCTL_API=3 to use v3 API or ETCDCTL_API=2 to use v2 API."
}

app.Flags = []cli.Flag{
cli.BoolFlag{Name: "debug", Usage: "output cURL commands which can be used to reproduce the request"},
cli.BoolFlag{Name: "no-sync", Usage: "don't synchronize cluster information before sending request"},
Expand Down
9 changes: 8 additions & 1 deletion etcdctl/ctlv3/ctl_cov.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@ import (
"github.com/coreos/etcd/etcdctl/ctlv3/command"
)

func Start() {
func Start(apiv string) {
// ETCDCTL_ARGS=etcdctl_test arg1 arg2...
// SetArgs() takes arg1 arg2...
if apiv == "" {
rootCmd.Short += "\n\n" +
"WARNING:\n" +
" Environment variable ETCDCTL_API is not set; defaults to etcdctl v3.\n" +
" Set environment variable ETCDCTL_API=2 to use v2 API or ETCDCTL_API=3 to use v3 API."

}
rootCmd.SetArgs(strings.Split(os.Getenv("ETCDCTL_ARGS"), "\xe7\xcd")[1:])
os.Unsetenv("ETCDCTL_ARGS")
if err := rootCmd.Execute(); err != nil {
Expand Down
9 changes: 8 additions & 1 deletion etcdctl/ctlv3/ctl_nocov.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ package ctlv3

import "github.com/coreos/etcd/etcdctl/ctlv3/command"

func Start() {
func Start(apiv string) {
if apiv == "" {
rootCmd.Short += "\n\n" +
"WARNING:\n" +
" Environment variable ETCDCTL_API is not set; defaults to etcdctl v3.\n" +
" Set environment variable ETCDCTL_API=2 to use v2 API or ETCDCTL_API=3 to use v3 API."

}
rootCmd.SetUsageFunc(usageFunc)
// Make help just show the usage
rootCmd.SetHelpTemplate(`{{.UsageString}}`)
Expand Down
8 changes: 4 additions & 4 deletions etcdctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ func main() {
apiv := os.Getenv(apiEnv)
// unset apiEnv to avoid side-effect for future env and flag parsing.
os.Unsetenv(apiEnv)
if len(apiv) == 0 || apiv == "2" {
ctlv2.Start(apiv)
if len(apiv) == 0 || apiv == "3" {
ctlv3.Start(apiv)
return
}

if apiv == "3" {
ctlv3.Start()
if apiv == "2" {
ctlv2.Start()
return
}

Expand Down
4 changes: 2 additions & 2 deletions functional/cmd/etcd-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ $ make build-etcd-proxy
$ ./bin/etcd-proxy --help
$ ./bin/etcd-proxy --from localhost:23790 --to localhost:2379 --http-port 2378 --verbose
$ ETCDCTL_API=3 ./bin/etcdctl --endpoints localhost:2379 put foo bar
$ ETCDCTL_API=3 ./bin/etcdctl --endpoints localhost:23790 put foo bar`)
$ ./bin/etcdctl --endpoints localhost:2379 put foo bar
$ ./bin/etcdctl --endpoints localhost:23790 put foo bar`)
flag.PrintDefaults()
}

Expand Down
22 changes: 22 additions & 0 deletions tests/e2e/ctl_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func TestCtlV2SetClientTLS(t *testing.T) { testCtlV2Set(t, &configClientTLS, fal
func TestCtlV2SetPeerTLS(t *testing.T) { testCtlV2Set(t, &configPeerTLS, false) }
func TestCtlV2SetTLS(t *testing.T) { testCtlV2Set(t, &configTLS, false) }
func testCtlV2Set(t *testing.T, cfg *etcdProcessClusterConfig, quorum bool) {
os.Setenv("ETCDCTL_API", "2")
defer os.Unsetenv("ETCDCTL_API")
defer testutil.AfterTest(t)

epc := setupEtcdctlTest(t, cfg, quorum)
Expand All @@ -55,6 +57,8 @@ func TestCtlV2Mk(t *testing.T) { testCtlV2Mk(t, &configNoTLS, false) }
func TestCtlV2MkQuorum(t *testing.T) { testCtlV2Mk(t, &configNoTLS, true) }
func TestCtlV2MkTLS(t *testing.T) { testCtlV2Mk(t, &configTLS, false) }
func testCtlV2Mk(t *testing.T, cfg *etcdProcessClusterConfig, quorum bool) {
os.Setenv("ETCDCTL_API", "2")
defer os.Unsetenv("ETCDCTL_API")
defer testutil.AfterTest(t)

epc := setupEtcdctlTest(t, cfg, quorum)
Expand All @@ -81,6 +85,8 @@ func testCtlV2Mk(t *testing.T, cfg *etcdProcessClusterConfig, quorum bool) {
func TestCtlV2Rm(t *testing.T) { testCtlV2Rm(t, &configNoTLS) }
func TestCtlV2RmTLS(t *testing.T) { testCtlV2Rm(t, &configTLS) }
func testCtlV2Rm(t *testing.T, cfg *etcdProcessClusterConfig) {
os.Setenv("ETCDCTL_API", "2")
defer os.Unsetenv("ETCDCTL_API")
defer testutil.AfterTest(t)

epc := setupEtcdctlTest(t, cfg, true)
Expand Down Expand Up @@ -108,6 +114,8 @@ func TestCtlV2Ls(t *testing.T) { testCtlV2Ls(t, &configNoTLS, false) }
func TestCtlV2LsQuorum(t *testing.T) { testCtlV2Ls(t, &configNoTLS, true) }
func TestCtlV2LsTLS(t *testing.T) { testCtlV2Ls(t, &configTLS, false) }
func testCtlV2Ls(t *testing.T, cfg *etcdProcessClusterConfig, quorum bool) {
os.Setenv("ETCDCTL_API", "2")
defer os.Unsetenv("ETCDCTL_API")
defer testutil.AfterTest(t)

epc := setupEtcdctlTest(t, cfg, quorum)
Expand All @@ -132,6 +140,8 @@ func TestCtlV2Watch(t *testing.T) { testCtlV2Watch(t, &configNoTLS, false) }
func TestCtlV2WatchTLS(t *testing.T) { testCtlV2Watch(t, &configTLS, false) }

func testCtlV2Watch(t *testing.T, cfg *etcdProcessClusterConfig, noSync bool) {
os.Setenv("ETCDCTL_API", "2")
defer os.Unsetenv("ETCDCTL_API")
defer testutil.AfterTest(t)

epc := setupEtcdctlTest(t, cfg, true)
Expand All @@ -158,6 +168,8 @@ func testCtlV2Watch(t *testing.T, cfg *etcdProcessClusterConfig, noSync bool) {
}

func TestCtlV2GetRoleUser(t *testing.T) {
os.Setenv("ETCDCTL_API", "2")
defer os.Unsetenv("ETCDCTL_API")
defer testutil.AfterTest(t)

epc := setupEtcdctlTest(t, &configNoTLS, false)
Expand Down Expand Up @@ -191,6 +203,8 @@ func TestCtlV2GetRoleUser(t *testing.T) {
func TestCtlV2UserListUsername(t *testing.T) { testCtlV2UserList(t, "username") }
func TestCtlV2UserListRoot(t *testing.T) { testCtlV2UserList(t, "root") }
func testCtlV2UserList(t *testing.T, username string) {
os.Setenv("ETCDCTL_API", "2")
defer os.Unsetenv("ETCDCTL_API")
defer testutil.AfterTest(t)

epc := setupEtcdctlTest(t, &configNoTLS, false)
Expand All @@ -209,6 +223,8 @@ func testCtlV2UserList(t *testing.T, username string) {
}

func TestCtlV2RoleList(t *testing.T) {
os.Setenv("ETCDCTL_API", "2")
defer os.Unsetenv("ETCDCTL_API")
defer testutil.AfterTest(t)

epc := setupEtcdctlTest(t, &configNoTLS, false)
Expand All @@ -233,6 +249,8 @@ func TestCtlV2BackupV3(t *testing.T) { testCtlV2Backup(t, 0, true) }
func TestCtlV2BackupV3Snapshot(t *testing.T) { testCtlV2Backup(t, 1, true) }

func testCtlV2Backup(t *testing.T, snapCount int, v3 bool) {
os.Setenv("ETCDCTL_API", "2")
defer os.Unsetenv("ETCDCTL_API")
defer testutil.AfterTest(t)

backupDir, err := ioutil.TempDir("", "testbackup0.etcd")
Expand Down Expand Up @@ -305,6 +323,8 @@ func testCtlV2Backup(t *testing.T, snapCount int, v3 bool) {
}

func TestCtlV2AuthWithCommonName(t *testing.T) {
os.Setenv("ETCDCTL_API", "2")
defer os.Unsetenv("ETCDCTL_API")
defer testutil.AfterTest(t)

copiedCfg := configClientTLS
Expand Down Expand Up @@ -341,6 +361,8 @@ func TestCtlV2AuthWithCommonName(t *testing.T) {
}

func TestCtlV2ClusterHealth(t *testing.T) {
os.Setenv("ETCDCTL_API", "2")
defer os.Unsetenv("ETCDCTL_API")
defer testutil.AfterTest(t)
epc := setupEtcdctlTest(t, &configNoTLS, true)
defer func() {
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/ctl_v3_migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func TestCtlV3Migrate(t *testing.T) {
keys[i] = fmt.Sprintf("foo_%d", i)
vals[i] = fmt.Sprintf("bar_%d", i)
}
os.Setenv("ETCDCTL_API", "2")
for i := range keys {
if err := etcdctlSet(epc, keys[i], vals[i]); err != nil {
t.Fatal(err)
Expand All @@ -52,8 +53,7 @@ func TestCtlV3Migrate(t *testing.T) {
t.Fatalf("error closing etcd processes (%v)", err)
}

os.Setenv("ETCDCTL_API", "3")
defer os.Unsetenv("ETCDCTL_API")
os.Unsetenv("ETCDCTL_API")
cx := ctlCtx{
t: t,
cfg: configNoTLS,
Expand Down
3 changes: 3 additions & 0 deletions tests/e2e/v2_curl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package e2e
import (
"fmt"
"math/rand"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -64,6 +65,8 @@ func testCurlPutGet(t *testing.T, cfg *etcdProcessClusterConfig) {
}

func TestV2CurlIssue5182(t *testing.T) {
os.Setenv("ETCDCTL_API", "2")
defer os.Unsetenv("ETCDCTL_API")
defer testutil.AfterTest(t)

epc := setupEtcdctlTest(t, &configNoTLS, false)
Expand Down

0 comments on commit 25bc657

Please sign in to comment.