maintenance_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2017 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 integration
  15. import (
  16. "context"
  17. "testing"
  18. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  19. "github.com/coreos/etcd/integration"
  20. "github.com/coreos/etcd/pkg/testutil"
  21. )
  22. func TestMaintenanceHashKV(t *testing.T) {
  23. defer testutil.AfterTest(t)
  24. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  25. defer clus.Terminate(t)
  26. for i := 0; i < 3; i++ {
  27. if _, err := clus.RandClient().Put(context.Background(), "foo", "bar"); err != nil {
  28. t.Fatal(err)
  29. }
  30. }
  31. var hv uint32
  32. for i := 0; i < 3; i++ {
  33. cli := clus.Client(i)
  34. // ensure writes are replicated
  35. if _, err := cli.Get(context.TODO(), "foo"); err != nil {
  36. t.Fatal(err)
  37. }
  38. hresp, err := cli.HashKV(context.Background(), clus.Members[i].GRPCAddr(), 0)
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. if hv == 0 {
  43. hv = hresp.Hash
  44. continue
  45. }
  46. if hv != hresp.Hash {
  47. t.Fatalf("#%d: hash expected %d, got %d", i, hv, hresp.Hash)
  48. }
  49. }
  50. }
  51. func TestMaintenanceMoveLeader(t *testing.T) {
  52. defer testutil.AfterTest(t)
  53. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  54. defer clus.Terminate(t)
  55. oldLeadIdx := clus.WaitLeader(t)
  56. targetIdx := (oldLeadIdx + 1) % 3
  57. target := uint64(clus.Members[targetIdx].ID())
  58. cli := clus.Client(targetIdx)
  59. _, err := cli.MoveLeader(context.Background(), target)
  60. if err != rpctypes.ErrNotLeader {
  61. t.Fatalf("error expected %v, got %v", rpctypes.ErrNotLeader, err)
  62. }
  63. cli = clus.Client(oldLeadIdx)
  64. _, err = cli.MoveLeader(context.Background(), target)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. leadIdx := clus.WaitLeader(t)
  69. lead := uint64(clus.Members[leadIdx].ID())
  70. if target != lead {
  71. t.Fatalf("new leader expected %d, got %d", target, lead)
  72. }
  73. }