ctl_v3_defrag_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 "testing"
  16. func TestCtlV3Defrag(t *testing.T) { testCtl(t, defragTest) }
  17. func TestCtlV3DefragWithAuth(t *testing.T) { testCtl(t, defragTestWithAuth) }
  18. func maintenanceInitKeys(cx ctlCtx) {
  19. var kvs = []kv{{"key", "val1"}, {"key", "val2"}, {"key", "val3"}}
  20. for i := range kvs {
  21. if err := ctlV3Put(cx, kvs[i].key, kvs[i].val, ""); err != nil {
  22. cx.t.Fatal(err)
  23. }
  24. }
  25. }
  26. func defragTest(cx ctlCtx) {
  27. maintenanceInitKeys(cx)
  28. if err := ctlV3Compact(cx, 4, cx.compactPhysical); err != nil {
  29. cx.t.Fatal(err)
  30. }
  31. if err := ctlV3Defrag(cx); err != nil {
  32. cx.t.Fatalf("defragTest ctlV3Defrag error (%v)", err)
  33. }
  34. }
  35. func defragTestWithAuth(cx ctlCtx) {
  36. maintenanceInitKeys(cx)
  37. if err := authEnable(cx); err != nil {
  38. cx.t.Fatal(err)
  39. }
  40. cx.user, cx.pass = "root", "root"
  41. authSetupTestUser(cx)
  42. // ordinal user cannot defrag
  43. cx.user, cx.pass = "test-user", "pass"
  44. if err := ctlV3Defrag(cx); err == nil {
  45. cx.t.Fatal("ordinal user should not be able to issue a defrag request")
  46. }
  47. // root can defrag
  48. cx.user, cx.pass = "root", "root"
  49. if err := ctlV3Defrag(cx); err != nil {
  50. cx.t.Fatal(err)
  51. }
  52. }
  53. func ctlV3Defrag(cx ctlCtx) error {
  54. cmdArgs := append(cx.PrefixArgs(), "defrag")
  55. lines := make([]string, cx.epc.cfg.clusterSize)
  56. for i := range lines {
  57. lines[i] = "Finished defragmenting etcd member"
  58. }
  59. return spawnWithExpects(cmdArgs, lines...)
  60. }