ctl_v3_snapshot_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2016 CoreOS, Inc.
  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 ctlV3SnapshotSave(cx ctlCtx, fpath string) error {
  48. cmdArgs := append(cx.PrefixArgs(), "snapshot", "save", fpath)
  49. return spawnWithExpect(cmdArgs, fmt.Sprintf("Snapshot saved at %s", fpath))
  50. }
  51. type snapshotStatus struct {
  52. Hash uint32 `json:"hash"`
  53. Revision int64 `json:"revision"`
  54. TotalKey int `json:"totalKey"`
  55. TotalSize int64 `json:"totalSize"`
  56. }
  57. func getSnapshotStatus(cx ctlCtx, fpath string) (snapshotStatus, error) {
  58. cmdArgs := append(cx.PrefixArgs(), "--write-out", "json", "snapshot", "status", fpath)
  59. proc, err := spawnCmd(cmdArgs)
  60. if err != nil {
  61. return snapshotStatus{}, err
  62. }
  63. var txt string
  64. txt, err = proc.Expect("totalKey")
  65. if err != nil {
  66. return snapshotStatus{}, err
  67. }
  68. if err = proc.Close(); err != nil {
  69. return snapshotStatus{}, err
  70. }
  71. resp := snapshotStatus{}
  72. dec := json.NewDecoder(strings.NewReader(txt))
  73. if err := dec.Decode(&resp); err == io.EOF {
  74. return snapshotStatus{}, err
  75. }
  76. return resp, nil
  77. }