From 74274f441757e067eba1a7b705fa6b9204efb3a6 Mon Sep 17 00:00:00 2001 From: Piotr Tabor Date: Mon, 11 Jan 2021 00:20:16 +0100 Subject: [PATCH] e2e: Adding better diagnostic and location for temporary files to Snapshot tests. --- .../api/v2store/node_extern_test.go | 6 +++ tests/e2e/ctl_v3_snapshot_test.go | 53 +++++++++++-------- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/server/etcdserver/api/v2store/node_extern_test.go b/server/etcdserver/api/v2store/node_extern_test.go index 067cdc42d..f5f99951a 100644 --- a/server/etcdserver/api/v2store/node_extern_test.go +++ b/server/etcdserver/api/v2store/node_extern_test.go @@ -102,7 +102,13 @@ func TestNodeExternClone(t *testing.T) { } func sameSlice(a, b []*NodeExtern) bool { +<<<<<<< HEAD ah := (*reflect.SliceHeader)(unsafe.Pointer(&a)) bh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) return *ah == *bh +======= + va := reflect.ValueOf(a) + vb := reflect.ValueOf(b) + return va.Len() == vb.Len() && va.Pointer() == vb.Pointer() +>>>>>>> 6f8273a2a (fixup! e2e: Adding better diagnostic and location for temporary files to Snapshot tests.) } diff --git a/tests/e2e/ctl_v3_snapshot_test.go b/tests/e2e/ctl_v3_snapshot_test.go index 5e508d27d..74cb4ee50 100644 --- a/tests/e2e/ctl_v3_snapshot_test.go +++ b/tests/e2e/ctl_v3_snapshot_test.go @@ -18,7 +18,7 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" + "math/rand" "os" "path/filepath" "strings" @@ -32,6 +32,13 @@ import ( func TestCtlV3Snapshot(t *testing.T) { testCtl(t, snapshotTest) } +// TODO: Replace with testing.T.TestDir() in golang-1.15. +func tempDir(tb testing.TB) string { + dir := filepath.Join(os.TempDir(), tb.Name(), fmt.Sprint(rand.Int())) + os.MkdirAll(dir, 0700) + return dir +} + func snapshotTest(cx ctlCtx) { maintenanceInitKeys(cx) @@ -43,7 +50,7 @@ func snapshotTest(cx ctlCtx) { cx.t.Fatalf("snapshot: ctlV3Put error (%v)", err) } - fpath := "test1.snapshot" + fpath := filepath.Join(tempDir(cx.t), "snapshot") defer os.RemoveAll(fpath) if err = ctlV3SnapshotSave(cx, fpath); err != nil { @@ -65,7 +72,7 @@ func snapshotTest(cx ctlCtx) { func TestCtlV3SnapshotCorrupt(t *testing.T) { testCtl(t, snapshotCorruptTest) } func snapshotCorruptTest(cx ctlCtx) { - fpath := "test2.snapshot" + fpath := filepath.Join(tempDir(cx.t), "snapshot") defer os.RemoveAll(fpath) if err := ctlV3SnapshotSave(cx, fpath); err != nil { @@ -82,10 +89,12 @@ func snapshotCorruptTest(cx ctlCtx) { } f.Close() - defer os.RemoveAll("snap.etcd") + datadir := filepath.Join(tempDir(cx.t), "data") + defer os.RemoveAll(datadir) + serr := spawnWithExpect( append(cx.PrefixArgs(), "snapshot", "restore", - "--data-dir", "snap.etcd", + "--data-dir", datadir, fpath), "expected sha256") @@ -98,7 +107,7 @@ func snapshotCorruptTest(cx ctlCtx) { func TestCtlV3SnapshotStatusBeforeRestore(t *testing.T) { testCtl(t, snapshotStatusBeforeRestoreTest) } func snapshotStatusBeforeRestoreTest(cx ctlCtx) { - fpath := "test3.snapshot" + fpath := filepath.Join(tempDir(cx.t), "snapshot") defer os.RemoveAll(fpath) if err := ctlV3SnapshotSave(cx, fpath); err != nil { @@ -111,10 +120,11 @@ func snapshotStatusBeforeRestoreTest(cx ctlCtx) { cx.t.Fatalf("snapshotTest getSnapshotStatus error (%v)", err) } - defer os.RemoveAll("snap.etcd") + dataDir := filepath.Join(tempDir(cx.t), "data") + defer os.RemoveAll(dataDir) serr := spawnWithExpect( append(cx.PrefixArgs(), "snapshot", "restore", - "--data-dir", "snap.etcd", + "--data-dir", dataDir, fpath), "added member") if serr != nil { @@ -183,7 +193,7 @@ func TestIssue6361(t *testing.T) { dialTimeout := 10 * time.Second prefixArgs := []string{ctlBinPath, "--endpoints", strings.Join(epc.EndpointsV3(), ","), "--dial-timeout", dialTimeout.String()} - // write some keys + t.Log("Writing some keys...") kvs := []kv{{"foo1", "val1"}, {"foo2", "val2"}, {"foo3", "val3"}} for i := range kvs { if err = spawnWithExpect(append(prefixArgs, "put", kvs[i].key, kvs[i].val), "OK"); err != nil { @@ -191,28 +201,29 @@ func TestIssue6361(t *testing.T) { } } - fpath := filepath.Join(os.TempDir(), "test.snapshot") + fpath := filepath.Join(tempDir(t), "snapshot") defer os.RemoveAll(fpath) - // etcdctl save snapshot + t.Log("etcdctl saving snapshot...") if err = spawnWithExpect(append(prefixArgs, "snapshot", "save", fpath), fmt.Sprintf("Snapshot saved at %s", fpath)); err != nil { t.Fatal(err) } + t.Log("Stopping the original server...") if err = epc.procs[0].Stop(); err != nil { t.Fatal(err) } - newDataDir := filepath.Join(os.TempDir(), "test.data") + newDataDir := tempDir(t) defer os.RemoveAll(newDataDir) - // etcdctl restore the snapshot + t.Log("etcdctl restoring the snapshot...") err = spawnWithExpect([]string{ctlBinPath, "snapshot", "restore", fpath, "--name", epc.procs[0].Config().name, "--initial-cluster", epc.procs[0].Config().initialCluster, "--initial-cluster-token", epc.procs[0].Config().initialToken, "--initial-advertise-peer-urls", epc.procs[0].Config().purl.String(), "--data-dir", newDataDir}, "added member") if err != nil { t.Fatal(err) } - // start the etcd member using the restored snapshot + t.Log("Start the etcd member using the restored snapshot...") epc.procs[0].Config().dataDirPath = newDataDir for i := range epc.procs[0].Config().args { if epc.procs[0].Config().args[i] == "--data-dir" { @@ -223,14 +234,14 @@ func TestIssue6361(t *testing.T) { t.Fatal(err) } - // ensure the restored member has the correct data + t.Log("Ensuring the restored member has the correct data...") for i := range kvs { if err = spawnWithExpect(append(prefixArgs, "get", kvs[i].key), kvs[i].val); err != nil { t.Fatal(err) } } - // add a new member into the cluster + t.Log("Adding new member into the cluster") clientURL := fmt.Sprintf("http://localhost:%d", etcdProcessBasePort+30) peerURL := fmt.Sprintf("http://localhost:%d", etcdProcessBasePort+31) err = spawnWithExpect(append(prefixArgs, "member", "add", "newmember", fmt.Sprintf("--peer-urls=%s", peerURL)), " added to cluster ") @@ -238,16 +249,13 @@ func TestIssue6361(t *testing.T) { t.Fatal(err) } - var newDataDir2 string - newDataDir2, err = ioutil.TempDir("", "newdata2") - if err != nil { - t.Fatal(err) - } + newDataDir2 := filepath.Join(tempDir(t), "newdata") defer os.RemoveAll(newDataDir2) name2 := "infra2" initialCluster2 := epc.procs[0].Config().initialCluster + fmt.Sprintf(",%s=%s", name2, peerURL) + t.Log("Starting new member") // start the new member var nepc *expect.ExpectProcess nepc, err = spawnCmd([]string{epc.procs[0].Config().execPath, "--name", name2, @@ -263,13 +271,14 @@ func TestIssue6361(t *testing.T) { prefixArgs = []string{ctlBinPath, "--endpoints", clientURL, "--dial-timeout", dialTimeout.String()} - // ensure added member has data from incoming snapshot + t.Log("Ensuring added member has data from incoming snapshot...") for i := range kvs { if err = spawnWithExpect(append(prefixArgs, "get", kvs[i].key), kvs[i].val); err != nil { t.Fatal(err) } } + t.Log("Stopping the second member") if err = nepc.Stop(); err != nil { t.Fatal(err) }