raft_test.go 4.0 KB

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