瀏覽代碼

*: add snapshot e2e test

Gyu-Ho Lee 9 年之前
父節點
當前提交
a2afb513dd
共有 2 個文件被更改,包括 90 次插入0 次删除
  1. 89 0
      e2e/ctl_v3_snapshot_test.go
  2. 1 0
      etcdctl/ctlv3/command/snapshot_command.go

+ 89 - 0
e2e/ctl_v3_snapshot_test.go

@@ -0,0 +1,89 @@
+// 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 (
+	"encoding/json"
+	"fmt"
+	"io"
+	"os"
+	"strings"
+	"testing"
+)
+
+func TestCtlV3Snapshot(t *testing.T) { testCtl(t, snapshotTest) }
+
+func snapshotTest(cx ctlCtx) {
+	var kvs = []kv{{"key", "val1"}, {"key", "val2"}, {"key", "val3"}}
+	for i := range kvs {
+		if err := ctlV3Put(cx, kvs[i].key, kvs[i].val, ""); err != nil {
+			cx.t.Fatal(err)
+		}
+	}
+
+	fpath := "test.snapshot"
+	defer os.RemoveAll(fpath)
+
+	if err := ctlV3SnapshotSave(cx, fpath); err != nil {
+		cx.t.Fatalf("snapshotTest ctlV3SnapshotSave error (%v)", err)
+	}
+
+	st, err := getSnapshotStatus(cx, fpath)
+	if err != nil {
+		cx.t.Fatalf("snapshotTest getSnapshotStatus error (%v)", err)
+	}
+	if st.Revision != 4 {
+		cx.t.Fatalf("expected 4, got %d", st.Revision)
+	}
+	if st.TotalKey < 3 {
+		cx.t.Fatalf("expected at least 3, got %d", st.TotalKey)
+	}
+}
+
+func ctlV3SnapshotSave(cx ctlCtx, fpath string) error {
+	cmdArgs := append(cx.PrefixArgs(), "snapshot", "save", fpath)
+	return spawnWithExpect(cmdArgs, fmt.Sprintf("Snapshot saved at %s", fpath))
+}
+
+type snapshotStatus struct {
+	Hash      uint32 `json:"hash"`
+	Revision  int64  `json:"revision"`
+	TotalKey  int    `json:"totalKey"`
+	TotalSize int64  `json:"totalSize"`
+}
+
+func getSnapshotStatus(cx ctlCtx, fpath string) (snapshotStatus, error) {
+	cmdArgs := append(cx.PrefixArgs(), "--write-out", "json", "snapshot", "status", fpath)
+
+	proc, err := spawnCmd(cmdArgs)
+	if err != nil {
+		return snapshotStatus{}, err
+	}
+	var txt string
+	txt, err = proc.Expect("totalKey")
+	if err != nil {
+		return snapshotStatus{}, err
+	}
+	if err = proc.Close(); err != nil {
+		return snapshotStatus{}, err
+	}
+
+	resp := snapshotStatus{}
+	dec := json.NewDecoder(strings.NewReader(txt))
+	if err := dec.Decode(&resp); err == io.EOF {
+		return snapshotStatus{}, err
+	}
+	return resp, nil
+}

+ 1 - 0
etcdctl/ctlv3/command/snapshot_command.go

@@ -127,6 +127,7 @@ func snapshotSaveCommandFunc(cmd *cobra.Command, args []string) {
 		exiterr := fmt.Errorf("could not rename %s to %s (%v)", partpath, path, rerr)
 		ExitWithError(ExitIO, exiterr)
 	}
+	fmt.Printf("Snapshot saved at %s\n", path)
 }
 
 func snapshotStatusCommandFunc(cmd *cobra.Command, args []string) {