Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ctlv2: backup --with-v3 #8479

Merged
merged 3 commits into from
Sep 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 44 additions & 6 deletions e2e/ctl_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,13 @@ func TestCtlV2RoleList(t *testing.T) {
}
}

func TestCtlV2Backup(t *testing.T) { // For https://github.com/coreos/etcd/issues/5360
func TestCtlV2Backup(t *testing.T) { testCtlV2Backup(t, 0, false) }
func TestCtlV2BackupSnapshot(t *testing.T) { testCtlV2Backup(t, 1, false) }

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) {
defer testutil.AfterTest(t)

backupDir, err := ioutil.TempDir("", "testbackup0.etcd")
Expand All @@ -235,15 +241,29 @@ func TestCtlV2Backup(t *testing.T) { // For https://github.com/coreos/etcd/issue
}
defer os.RemoveAll(backupDir)

epc1 := setupEtcdctlTest(t, &configNoTLS, false)
if err := etcdctlSet(epc1, "foo1", "bar"); err != nil {
etcdCfg := configNoTLS
etcdCfg.snapCount = snapCount
epc1 := setupEtcdctlTest(t, &etcdCfg, false)

// v3 put before v2 set so snapshot happens after v3 operations to confirm
// v3 data is preserved after snapshot.
if err := ctlV3Put(ctlCtx{t: t, epc: epc1}, "v3key", "123", ""); err != nil {
t.Fatal(err)
}

if err := etcdctlBackup(epc1, epc1.procs[0].Config().dataDirPath, backupDir); err != nil {
if err := etcdctlSet(epc1, "foo1", "bar"); err != nil {
t.Fatal(err)
}

if v3 {
// v3 must lock the db to backup, so stop process
if err := epc1.Stop(); err != nil {
t.Fatal(err)
}
}
if err := etcdctlBackup(epc1, epc1.procs[0].Config().dataDirPath, backupDir, v3); err != nil {
t.Fatal(err)
}
if err := epc1.Close(); err != nil {
t.Fatalf("error closing etcd processes (%v)", err)
}
Expand All @@ -260,6 +280,17 @@ func TestCtlV2Backup(t *testing.T) { // For https://github.com/coreos/etcd/issue
t.Fatal(err)
}

ctx2 := ctlCtx{t: t, epc: epc2}
if v3 {
if err := ctlV3Get(ctx2, []string{"v3key"}, kv{"v3key", "123"}); err != nil {
t.Fatal(err)
}
} else {
if err := ctlV3Get(ctx2, []string{"v3key"}); err != nil {
t.Fatal(err)
}
}

// check if it can serve client requests
if err := etcdctlSet(epc2, "foo2", "bar"); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -451,9 +482,16 @@ func etcdctlAuthEnable(clus *etcdProcessCluster) error {
return spawnWithExpect(cmdArgs, "Authentication Enabled")
}

func etcdctlBackup(clus *etcdProcessCluster, dataDir, backupDir string) error {
func etcdctlBackup(clus *etcdProcessCluster, dataDir, backupDir string, v3 bool) error {
cmdArgs := append(etcdctlPrefixArgs(clus), "backup", "--data-dir", dataDir, "--backup-dir", backupDir)
return spawnWithExpects(cmdArgs)
if v3 {
cmdArgs = append(cmdArgs, "--with-v3")
}
proc, err := spawnCmd(cmdArgs)
if err != nil {
return err
}
return proc.Close()
}

func mustEtcdctl(t *testing.T) {
Expand Down
4 changes: 1 addition & 3 deletions e2e/ctl_v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ func testCtl(t *testing.T, testFunc func(ctlCtx), opts ...ctlOption) {
}
ret.applyOpts(opts)

os.Setenv("ETCDCTL_API", "3")
mustEtcdctl(t)
if !ret.quorum {
ret.cfg = *configStandalone(ret.cfg)
Expand All @@ -140,7 +139,6 @@ func testCtl(t *testing.T, testFunc func(ctlCtx), opts ...ctlOption) {
ret.epc = epc

defer func() {
os.Unsetenv("ETCDCTL_API")
if ret.envMap != nil {
for k := range ret.envMap {
os.Unsetenv(k)
Expand Down Expand Up @@ -192,7 +190,7 @@ func (cx *ctlCtx) prefixArgs(eps []string) []string {

useEnv := cx.envMap != nil

cmdArgs := []string{ctlBinPath}
cmdArgs := []string{ctlBinPath + "3"}
for k, v := range fmap {
if useEnv {
ek := flags.FlagToEnv("ETCDCTL", k)
Expand Down
6 changes: 5 additions & 1 deletion e2e/etcd_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import (
"github.com/coreos/etcd/pkg/fileutil"
)

var etcdServerReadyLines = []string{"enabled capabilities for version", "published"}
var (
etcdServerReadyLines = []string{"enabled capabilities for version", "published"}
binPath string
ctlBinPath string
)

// etcdProcess is a process that serves etcd requests.
type etcdProcess interface {
Expand Down
19 changes: 11 additions & 8 deletions e2e/etcd_spawn_cov.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,24 @@ func spawnCmd(args []string) (*expect.ExpectProcess, error) {
if args[0] == binPath {
return spawnEtcd(args)
}
if args[0] == ctlBinPath || args[0] == ctlBinPath+"3" {
// avoid test flag conflicts in coverage enabled etcdctl by putting flags in ETCDCTL_ARGS
env := []string{
// was \xff, but that's used for testing boundary conditions; 0xe7cd should be safe
"ETCDCTL_ARGS=" + strings.Join(args, "\xe7\xcd"),
}
if args[0] == ctlBinPath+"3" {
env = append(env, "ETCDCTL_API=3")
}

if args[0] == ctlBinPath {
covArgs, err := getCovArgs()
if err != nil {
return nil, err
}
// avoid test flag conflicts in coverage enabled etcdctl by putting flags in ETCDCTL_ARGS
ctl_cov_env := []string{
// was \xff, but that's used for testing boundary conditions; 0xe7cd should be safe
"ETCDCTL_ARGS=" + strings.Join(args, "\xe7\xcd"),
}
// when withFlagByEnv() is used in testCtl(), env variables for ctl is set to os.env.
// they must be included in ctl_cov_env.
ctl_cov_env = append(ctl_cov_env, os.Environ()...)
ep, err := expect.NewExpectWithEnv(binDir+"/etcdctl_test", covArgs, ctl_cov_env)
env = append(env, os.Environ()...)
ep, err := expect.NewExpectWithEnv(binDir+"/etcdctl_test", covArgs, env)
if err != nil {
return nil, err
}
Expand Down
10 changes: 9 additions & 1 deletion e2e/etcd_spawn_nocov.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@

package e2e

import "github.com/coreos/etcd/pkg/expect"
import (
"os"

"github.com/coreos/etcd/pkg/expect"
)

const noOutputLineCount = 0 // regular binaries emit no extra lines

func spawnCmd(args []string) (*expect.ExpectProcess, error) {
if args[0] == ctlBinPath+"3" {
env := append(os.Environ(), "ETCDCTL_API=3")
return expect.NewExpectWithEnv(ctlBinPath, args[1:], env)
}
return expect.NewExpect(args[0], args[1:]...)
}
2 changes: 0 additions & 2 deletions e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ var (
binDir string
certDir string

binPath string
ctlBinPath string
certPath string
privateKeyPath string
caPath string
Expand Down
Loading