ctl_v3_snapshot_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package e2e
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "io"
  19. "os"
  20. "strings"
  21. "testing"
  22. )
  23. func TestCtlV3Snapshot(t *testing.T) { testCtl(t, snapshotTest) }
  24. func snapshotTest(cx ctlCtx) {
  25. var kvs = []kv{{"key", "val1"}, {"key", "val2"}, {"key", "val3"}}
  26. for i := range kvs {
  27. if err := ctlV3Put(cx, kvs[i].key, kvs[i].val, ""); err != nil {
  28. cx.t.Fatal(err)
  29. }
  30. }
  31. fpath := "test.snapshot"
  32. defer os.RemoveAll(fpath)
  33. if err := ctlV3SnapshotSave(cx, fpath); err != nil {
  34. cx.t.Fatalf("snapshotTest ctlV3SnapshotSave error (%v)", err)
  35. }
  36. st, err := getSnapshotStatus(cx, fpath)
  37. if err != nil {
  38. cx.t.Fatalf("snapshotTest getSnapshotStatus error (%v)", err)
  39. }
  40. if st.Revision != 4 {
  41. cx.t.Fatalf("expected 4, got %d", st.Revision)
  42. }
  43. if st.TotalKey < 3 {
  44. cx.t.Fatalf("expected at least 3, got %d", st.TotalKey)
  45. }
  46. }
  47. func TestCtlV3SnapshotCorrupt(t *testing.T) { testCtl(t, snapshotCorruptTest) }
  48. func snapshotCorruptTest(cx ctlCtx) {
  49. fpath := "test.snapshot"
  50. defer os.RemoveAll(fpath)
  51. if err := ctlV3SnapshotSave(cx, fpath); err != nil {
  52. cx.t.Fatalf("snapshotTest ctlV3SnapshotSave error (%v)", err)
  53. }
  54. // corrupt file
  55. f, oerr := os.OpenFile(fpath, os.O_WRONLY, 0)
  56. if oerr != nil {
  57. cx.t.Fatal(oerr)
  58. }
  59. if _, err := f.Write(make([]byte, 512)); err != nil {
  60. cx.t.Fatal(err)
  61. }
  62. f.Close()
  63. defer os.RemoveAll("snap.etcd")
  64. serr := spawnWithExpect(
  65. append(cx.PrefixArgs(), "snapshot", "restore",
  66. "--data-dir", "snap.etcd",
  67. fpath),
  68. "expected sha256")
  69. if serr != nil {
  70. cx.t.Fatal(serr)
  71. }
  72. }
  73. func ctlV3SnapshotSave(cx ctlCtx, fpath string) error {
  74. cmdArgs := append(cx.PrefixArgs(), "snapshot", "save", fpath)
  75. return spawnWithExpect(cmdArgs, fmt.Sprintf("Snapshot saved at %s", fpath))
  76. }
  77. type snapshotStatus struct {
  78. Hash uint32 `json:"hash"`
  79. Revision int64 `json:"revision"`
  80. TotalKey int `json:"totalKey"`
  81. TotalSize int64 `json:"totalSize"`
  82. }
  83. func getSnapshotStatus(cx ctlCtx, fpath string) (snapshotStatus, error) {
  84. cmdArgs := append(cx.PrefixArgs(), "--write-out", "json", "snapshot", "status", fpath)
  85. proc, err := spawnCmd(cmdArgs)
  86. if err != nil {
  87. return snapshotStatus{}, err
  88. }
  89. var txt string
  90. txt, err = proc.Expect("totalKey")
  91. if err != nil {
  92. return snapshotStatus{}, err
  93. }
  94. if err = proc.Close(); err != nil {
  95. return snapshotStatus{}, err
  96. }
  97. resp := snapshotStatus{}
  98. dec := json.NewDecoder(strings.NewReader(txt))
  99. if err := dec.Decode(&resp); err == io.EOF {
  100. return snapshotStatus{}, err
  101. }
  102. return resp, nil
  103. }