force_cluster_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package etcdserver
  14. import (
  15. "reflect"
  16. "testing"
  17. "github.com/coreos/etcd/pkg/pbutil"
  18. "github.com/coreos/etcd/raft/raftpb"
  19. )
  20. func TestGetIDs(t *testing.T) {
  21. addcc := &raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 2}
  22. addEntry := raftpb.Entry{Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(addcc)}
  23. removecc := &raftpb.ConfChange{Type: raftpb.ConfChangeRemoveNode, NodeID: 2}
  24. removeEntry := raftpb.Entry{Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc)}
  25. normalEntry := raftpb.Entry{Type: raftpb.EntryNormal}
  26. tests := []struct {
  27. snap *raftpb.Snapshot
  28. ents []raftpb.Entry
  29. widSet []uint64
  30. }{
  31. {nil, []raftpb.Entry{}, []uint64{}},
  32. {&raftpb.Snapshot{Nodes: []uint64{1}}, []raftpb.Entry{}, []uint64{1}},
  33. {&raftpb.Snapshot{Nodes: []uint64{1}}, []raftpb.Entry{addEntry}, []uint64{1, 2}},
  34. {&raftpb.Snapshot{Nodes: []uint64{1}}, []raftpb.Entry{addEntry, removeEntry}, []uint64{1}},
  35. {&raftpb.Snapshot{Nodes: []uint64{1}}, []raftpb.Entry{addEntry, normalEntry}, []uint64{1, 2}},
  36. {&raftpb.Snapshot{Nodes: []uint64{1}}, []raftpb.Entry{addEntry, removeEntry, normalEntry}, []uint64{1}},
  37. }
  38. for i, tt := range tests {
  39. idSet := getIDs(tt.snap, tt.ents)
  40. if !reflect.DeepEqual(idSet, tt.widSet) {
  41. t.Errorf("#%d: idset = %#v, want %#v", i, idSet, tt.widSet)
  42. }
  43. }
  44. }
  45. func TestCreateConfigChangeEnts(t *testing.T) {
  46. removecc2 := &raftpb.ConfChange{Type: raftpb.ConfChangeRemoveNode, NodeID: 2}
  47. removecc3 := &raftpb.ConfChange{Type: raftpb.ConfChangeRemoveNode, NodeID: 3}
  48. tests := []struct {
  49. ids []uint64
  50. self uint64
  51. term, index uint64
  52. wents []raftpb.Entry
  53. }{
  54. {
  55. []uint64{1},
  56. 1,
  57. 1, 1,
  58. []raftpb.Entry{},
  59. },
  60. {
  61. []uint64{1, 2},
  62. 1,
  63. 1, 1,
  64. []raftpb.Entry{{Term: 1, Index: 2, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc2)}},
  65. },
  66. {
  67. []uint64{1, 2},
  68. 1,
  69. 2, 2,
  70. []raftpb.Entry{{Term: 2, Index: 3, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc2)}},
  71. },
  72. {
  73. []uint64{1, 2, 3},
  74. 1,
  75. 2, 2,
  76. []raftpb.Entry{
  77. {Term: 2, Index: 3, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc2)},
  78. {Term: 2, Index: 4, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc3)},
  79. },
  80. },
  81. {
  82. []uint64{2, 3},
  83. 2,
  84. 2, 2,
  85. []raftpb.Entry{
  86. {Term: 2, Index: 3, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc3)},
  87. },
  88. },
  89. }
  90. for i, tt := range tests {
  91. gents := createConfigChangeEnts(tt.ids, tt.self, tt.term, tt.index)
  92. if !reflect.DeepEqual(gents, tt.wents) {
  93. t.Errorf("#%d: ents = %v, want %v", i, gents, tt.wents)
  94. }
  95. }
  96. }