ctl_v3_compact_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. "strconv"
  17. "strings"
  18. "testing"
  19. )
  20. func TestCtlV3Compact(t *testing.T) { testCtl(t, compactTest) }
  21. func TestCtlV3CompactPhysical(t *testing.T) { testCtl(t, compactTest, withCompactPhysical()) }
  22. func compactTest(cx ctlCtx) {
  23. compactPhysical := cx.compactPhysical
  24. if err := ctlV3Compact(cx, 2, compactPhysical); err != nil {
  25. if !strings.Contains(err.Error(), "required revision is a future revision") {
  26. cx.t.Fatal(err)
  27. }
  28. } else {
  29. cx.t.Fatalf("expected '...future revision' error, got <nil>")
  30. }
  31. var kvs = []kv{{"key", "val1"}, {"key", "val2"}, {"key", "val3"}}
  32. for i := range kvs {
  33. if err := ctlV3Put(cx, kvs[i].key, kvs[i].val, ""); err != nil {
  34. cx.t.Fatalf("compactTest #%d: ctlV3Put error (%v)", i, err)
  35. }
  36. }
  37. if err := ctlV3Get(cx, []string{"key", "--rev", "3"}, kvs[1:2]...); err != nil {
  38. cx.t.Errorf("compactTest: ctlV3Get error (%v)", err)
  39. }
  40. if err := ctlV3Compact(cx, 4, compactPhysical); err != nil {
  41. cx.t.Fatal(err)
  42. }
  43. if err := ctlV3Get(cx, []string{"key", "--rev", "3"}, kvs[1:2]...); err != nil {
  44. if !strings.Contains(err.Error(), "required revision has been compacted") {
  45. cx.t.Errorf("compactTest: ctlV3Get error (%v)", err)
  46. }
  47. } else {
  48. cx.t.Fatalf("expected '...has been compacted' error, got <nil>")
  49. }
  50. if err := ctlV3Compact(cx, 2, compactPhysical); err != nil {
  51. if !strings.Contains(err.Error(), "required revision has been compacted") {
  52. cx.t.Fatal(err)
  53. }
  54. } else {
  55. cx.t.Fatalf("expected '...has been compacted' error, got <nil>")
  56. }
  57. }
  58. func ctlV3Compact(cx ctlCtx, rev int64, physical bool) error {
  59. rs := strconv.FormatInt(rev, 10)
  60. cmdArgs := append(cx.PrefixArgs(), "compact", rs)
  61. if physical {
  62. cmdArgs = append(cmdArgs, "--physical")
  63. }
  64. return spawnWithExpect(cmdArgs, "compacted revision "+rs)
  65. }