raft_test.go 4.0 KB

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