From 4e375497ea3ec6470d2de97feb955c58633212a7 Mon Sep 17 00:00:00 2001 From: "zhesi.huang" Date: Fri, 3 Nov 2017 21:01:06 +0800 Subject: [PATCH] etcdctl/ctlv3: do not modify db file on "restore" --- e2e/ctl_v3_snapshot_test.go | 29 +++++++++++++++++++++++ etcdctl/ctlv3/command/snapshot_command.go | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/e2e/ctl_v3_snapshot_test.go b/e2e/ctl_v3_snapshot_test.go index d2394885d62..68aaf2ed35d 100644 --- a/e2e/ctl_v3_snapshot_test.go +++ b/e2e/ctl_v3_snapshot_test.go @@ -93,6 +93,35 @@ func snapshotCorruptTest(cx ctlCtx) { } } +// This test ensures that the snapshot status does not modify the snapshot file +func TestCtlV3SnapshotStatusBeforeRestore(t *testing.T) { testCtl(t, snapshotStatusBeforeRestoreTest) } + +func snapshotStatusBeforeRestoreTest(cx ctlCtx) { + fpath := "test.snapshot" + defer os.RemoveAll(fpath) + + if err := ctlV3SnapshotSave(cx, fpath); err != nil { + cx.t.Fatalf("snapshotTest ctlV3SnapshotSave error (%v)", err) + } + + // snapshot status on the fresh snapshot file + _, err := getSnapshotStatus(cx, fpath) + if err != nil { + cx.t.Fatalf("snapshotTest getSnapshotStatus error (%v)", err) + } + + defer os.RemoveAll("snap.etcd") + serr := spawnWithExpect( + append(cx.PrefixArgs(), "snapshot", "restore", + "--data-dir", "snap.etcd", + fpath), + "added member") + + if serr != nil { + cx.t.Fatal(serr) + } +} + func ctlV3SnapshotSave(cx ctlCtx, fpath string) error { cmdArgs := append(cx.PrefixArgs(), "snapshot", "save", fpath) return spawnWithExpect(cmdArgs, fmt.Sprintf("Snapshot saved at %s", fpath)) diff --git a/etcdctl/ctlv3/command/snapshot_command.go b/etcdctl/ctlv3/command/snapshot_command.go index 80f68892571..49a668f55d0 100644 --- a/etcdctl/ctlv3/command/snapshot_command.go +++ b/etcdctl/ctlv3/command/snapshot_command.go @@ -407,7 +407,7 @@ func dbStatus(p string) dbstatus { ds := dbstatus{} - db, err := bolt.Open(p, 0400, nil) + db, err := bolt.Open(p, 0400, &bolt.Options{ReadOnly: true}) if err != nil { ExitWithError(ExitError, err) }