force_cluster_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. "encoding/json"
  16. "reflect"
  17. "testing"
  18. "github.com/coreos/etcd/pkg/pbutil"
  19. "github.com/coreos/etcd/pkg/types"
  20. "github.com/coreos/etcd/raft/raftpb"
  21. )
  22. func TestGetIDs(t *testing.T) {
  23. addcc := &raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 2}
  24. addEntry := raftpb.Entry{Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(addcc)}
  25. removecc := &raftpb.ConfChange{Type: raftpb.ConfChangeRemoveNode, NodeID: 2}
  26. removeEntry := raftpb.Entry{Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc)}
  27. normalEntry := raftpb.Entry{Type: raftpb.EntryNormal}
  28. tests := []struct {
  29. snap *raftpb.Snapshot
  30. ents []raftpb.Entry
  31. widSet []uint64
  32. }{
  33. {nil, []raftpb.Entry{}, []uint64{}},
  34. {&raftpb.Snapshot{Nodes: []uint64{1}}, []raftpb.Entry{}, []uint64{1}},
  35. {&raftpb.Snapshot{Nodes: []uint64{1}}, []raftpb.Entry{addEntry}, []uint64{1, 2}},
  36. {&raftpb.Snapshot{Nodes: []uint64{1}}, []raftpb.Entry{addEntry, removeEntry}, []uint64{1}},
  37. {&raftpb.Snapshot{Nodes: []uint64{1}}, []raftpb.Entry{addEntry, normalEntry}, []uint64{1, 2}},
  38. {&raftpb.Snapshot{Nodes: []uint64{1}}, []raftpb.Entry{addEntry, removeEntry, normalEntry}, []uint64{1}},
  39. }
  40. for i, tt := range tests {
  41. idSet := getIDs(tt.snap, tt.ents)
  42. if !reflect.DeepEqual(idSet, tt.widSet) {
  43. t.Errorf("#%d: idset = %#v, want %#v", i, idSet, tt.widSet)
  44. }
  45. }
  46. }
  47. func TestCreateConfigChangeEnts(t *testing.T) {
  48. m := Member{
  49. ID: types.ID(1),
  50. RaftAttributes: RaftAttributes{PeerURLs: []string{"http://localhost:7001", "http://localhost:2380"}},
  51. }
  52. ctx, err := json.Marshal(m)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. addcc1 := &raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 1, Context: ctx}
  57. removecc2 := &raftpb.ConfChange{Type: raftpb.ConfChangeRemoveNode, NodeID: 2}
  58. removecc3 := &raftpb.ConfChange{Type: raftpb.ConfChangeRemoveNode, NodeID: 3}
  59. tests := []struct {
  60. ids []uint64
  61. self uint64
  62. term, index uint64
  63. wents []raftpb.Entry
  64. }{
  65. {
  66. []uint64{1},
  67. 1,
  68. 1, 1,
  69. []raftpb.Entry{},
  70. },
  71. {
  72. []uint64{1, 2},
  73. 1,
  74. 1, 1,
  75. []raftpb.Entry{{Term: 1, Index: 2, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc2)}},
  76. },
  77. {
  78. []uint64{1, 2},
  79. 1,
  80. 2, 2,
  81. []raftpb.Entry{{Term: 2, Index: 3, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc2)}},
  82. },
  83. {
  84. []uint64{1, 2, 3},
  85. 1,
  86. 2, 2,
  87. []raftpb.Entry{
  88. {Term: 2, Index: 3, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc2)},
  89. {Term: 2, Index: 4, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc3)},
  90. },
  91. },
  92. {
  93. []uint64{2, 3},
  94. 2,
  95. 2, 2,
  96. []raftpb.Entry{
  97. {Term: 2, Index: 3, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc3)},
  98. },
  99. },
  100. {
  101. []uint64{2, 3},
  102. 1,
  103. 2, 2,
  104. []raftpb.Entry{
  105. {Term: 2, Index: 3, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc2)},
  106. {Term: 2, Index: 4, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc3)},
  107. {Term: 2, Index: 5, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(addcc1)},
  108. },
  109. },
  110. }
  111. for i, tt := range tests {
  112. gents := createConfigChangeEnts(tt.ids, tt.self, tt.term, tt.index)
  113. if !reflect.DeepEqual(gents, tt.wents) {
  114. t.Errorf("#%d: ents = %v, want %v", i, gents, tt.wents)
  115. }
  116. }
  117. }