ctl_v3_snapshot_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. leaseID, err := ctlV3LeaseGrant(cx, 100)
  32. if err != nil {
  33. cx.t.Fatalf("snapshot: ctlV3LeaseGrant error (%v)", err)
  34. }
  35. if err = ctlV3Put(cx, "withlease", "withlease", leaseID); err != nil {
  36. cx.t.Fatalf("snapshot: ctlV3Put error (%v)", err)
  37. }
  38. fpath := "test.snapshot"
  39. defer os.RemoveAll(fpath)
  40. if err = ctlV3SnapshotSave(cx, fpath); err != nil {
  41. cx.t.Fatalf("snapshotTest ctlV3SnapshotSave error (%v)", err)
  42. }
  43. st, err := getSnapshotStatus(cx, fpath)
  44. if err != nil {
  45. cx.t.Fatalf("snapshotTest getSnapshotStatus error (%v)", err)
  46. }
  47. if st.Revision != 5 {
  48. cx.t.Fatalf("expected 4, got %d", st.Revision)
  49. }
  50. if st.TotalKey < 4 {
  51. cx.t.Fatalf("expected at least 4, got %d", st.TotalKey)
  52. }
  53. }
  54. func TestCtlV3SnapshotCorrupt(t *testing.T) { testCtl(t, snapshotCorruptTest) }
  55. func snapshotCorruptTest(cx ctlCtx) {
  56. fpath := "test.snapshot"
  57. defer os.RemoveAll(fpath)
  58. if err := ctlV3SnapshotSave(cx, fpath); err != nil {
  59. cx.t.Fatalf("snapshotTest ctlV3SnapshotSave error (%v)", err)
  60. }
  61. // corrupt file
  62. f, oerr := os.OpenFile(fpath, os.O_WRONLY, 0)
  63. if oerr != nil {
  64. cx.t.Fatal(oerr)
  65. }
  66. if _, err := f.Write(make([]byte, 512)); err != nil {
  67. cx.t.Fatal(err)
  68. }
  69. f.Close()
  70. defer os.RemoveAll("snap.etcd")
  71. serr := spawnWithExpect(
  72. append(cx.PrefixArgs(), "snapshot", "restore",
  73. "--data-dir", "snap.etcd",
  74. fpath),
  75. "expected sha256")
  76. if serr != nil {
  77. cx.t.Fatal(serr)
  78. }
  79. }
  80. func ctlV3SnapshotSave(cx ctlCtx, fpath string) error {
  81. cmdArgs := append(cx.PrefixArgs(), "snapshot", "save", fpath)
  82. return spawnWithExpect(cmdArgs, fmt.Sprintf("Snapshot saved at %s", fpath))
  83. }
  84. type snapshotStatus struct {
  85. Hash uint32 `json:"hash"`
  86. Revision int64 `json:"revision"`
  87. TotalKey int `json:"totalKey"`
  88. TotalSize int64 `json:"totalSize"`
  89. }
  90. func getSnapshotStatus(cx ctlCtx, fpath string) (snapshotStatus, error) {
  91. cmdArgs := append(cx.PrefixArgs(), "--write-out", "json", "snapshot", "status", fpath)
  92. proc, err := spawnCmd(cmdArgs)
  93. if err != nil {
  94. return snapshotStatus{}, err
  95. }
  96. var txt string
  97. txt, err = proc.Expect("totalKey")
  98. if err != nil {
  99. return snapshotStatus{}, err
  100. }
  101. if err = proc.Close(); err != nil {
  102. return snapshotStatus{}, err
  103. }
  104. resp := snapshotStatus{}
  105. dec := json.NewDecoder(strings.NewReader(txt))
  106. if err := dec.Decode(&resp); err == io.EOF {
  107. return snapshotStatus{}, err
  108. }
  109. return resp, nil
  110. }