rawnode_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // Copyright 2015 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 raft
  15. import (
  16. "bytes"
  17. "reflect"
  18. "testing"
  19. "github.com/coreos/etcd/raft/raftpb"
  20. )
  21. // TestRawNodeStep ensures that RawNode.Step ignore local message.
  22. func TestRawNodeStep(t *testing.T) {
  23. for i, msgn := range raftpb.MessageType_name {
  24. s := NewMemoryStorage()
  25. rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, s), []Peer{{ID: 1}})
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. msgt := raftpb.MessageType(i)
  30. err = rawNode.Step(raftpb.Message{Type: msgt})
  31. // LocalMsg should be ignored.
  32. if IsLocalMsg(msgt) {
  33. if err != ErrStepLocalMsg {
  34. t.Errorf("%d: step should ignore %s", msgt, msgn)
  35. }
  36. }
  37. }
  38. }
  39. // TestNodeStepUnblock from node_test.go has no equivalent in rawNode because there is
  40. // no goroutine in RawNode.
  41. // TestRawNodeProposeAndConfChange ensures that RawNode.Propose and RawNode.ProposeConfChange
  42. // send the given proposal and ConfChange to the underlying raft.
  43. func TestRawNodeProposeAndConfChange(t *testing.T) {
  44. s := NewMemoryStorage()
  45. var err error
  46. rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, s), []Peer{{ID: 1}})
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. rd := rawNode.Ready()
  51. s.Append(rd.Entries)
  52. rawNode.Advance(rd)
  53. rawNode.Campaign()
  54. proposed := false
  55. var (
  56. lastIndex uint64
  57. ccdata []byte
  58. )
  59. for {
  60. rd = rawNode.Ready()
  61. s.Append(rd.Entries)
  62. // Once we are the leader, propose a command and a ConfChange.
  63. if !proposed && rd.SoftState.Lead == rawNode.raft.id {
  64. rawNode.Propose([]byte("somedata"))
  65. cc := raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 1}
  66. ccdata, err = cc.Marshal()
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. rawNode.ProposeConfChange(cc)
  71. proposed = true
  72. }
  73. rawNode.Advance(rd)
  74. // Exit when we have four entries: one ConfChange, one no-op for the election,
  75. // our proposed command and proposed ConfChange.
  76. lastIndex, err = s.LastIndex()
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. if lastIndex >= 4 {
  81. break
  82. }
  83. }
  84. entries, err := s.Entries(lastIndex-1, lastIndex+1, noLimit)
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. if len(entries) != 2 {
  89. t.Fatalf("len(entries) = %d, want %d", len(entries), 2)
  90. }
  91. if !bytes.Equal(entries[0].Data, []byte("somedata")) {
  92. t.Errorf("entries[0].Data = %v, want %v", entries[0].Data, []byte("somedata"))
  93. }
  94. if entries[1].Type != raftpb.EntryConfChange {
  95. t.Fatalf("type = %v, want %v", entries[1].Type, raftpb.EntryConfChange)
  96. }
  97. if !bytes.Equal(entries[1].Data, ccdata) {
  98. t.Errorf("data = %v, want %v", entries[1].Data, ccdata)
  99. }
  100. }
  101. // TestBlockProposal from node_test.go has no equivalent in rawNode because there is
  102. // no leader check in RawNode.
  103. // TestNodeTick from node_test.go has no equivalent in rawNode because
  104. // it reaches into the raft object which is not exposed.
  105. // TestNodeStop from node_test.go has no equivalent in rawNode because there is
  106. // no goroutine in RawNode.
  107. // TestRawNodeStart ensures that a node can be started correctly. The node should
  108. // start with correct configuration change entries, and can accept and commit
  109. // proposals.
  110. func TestRawNodeStart(t *testing.T) {
  111. cc := raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 1}
  112. ccdata, err := cc.Marshal()
  113. if err != nil {
  114. t.Fatalf("unexpected marshal error: %v", err)
  115. }
  116. wants := []Ready{
  117. {
  118. HardState: raftpb.HardState{Term: 1, Commit: 1, Vote: 0},
  119. Entries: []raftpb.Entry{
  120. {Type: raftpb.EntryConfChange, Term: 1, Index: 1, Data: ccdata},
  121. },
  122. CommittedEntries: []raftpb.Entry{
  123. {Type: raftpb.EntryConfChange, Term: 1, Index: 1, Data: ccdata},
  124. },
  125. },
  126. {
  127. HardState: raftpb.HardState{Term: 2, Commit: 3, Vote: 1},
  128. Entries: []raftpb.Entry{{Term: 2, Index: 3, Data: []byte("foo")}},
  129. CommittedEntries: []raftpb.Entry{{Term: 2, Index: 3, Data: []byte("foo")}},
  130. },
  131. }
  132. storage := NewMemoryStorage()
  133. rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, storage), []Peer{{ID: 1}})
  134. if err != nil {
  135. t.Fatal(err)
  136. }
  137. rd := rawNode.Ready()
  138. t.Logf("rd %v", rd)
  139. if !reflect.DeepEqual(rd, wants[0]) {
  140. t.Fatalf("#%d: g = %+v,\n w %+v", 1, rd, wants[0])
  141. } else {
  142. storage.Append(rd.Entries)
  143. rawNode.Advance(rd)
  144. }
  145. storage.Append(rd.Entries)
  146. rawNode.Advance(rd)
  147. rawNode.Campaign()
  148. rd = rawNode.Ready()
  149. storage.Append(rd.Entries)
  150. rawNode.Advance(rd)
  151. rawNode.Propose([]byte("foo"))
  152. if rd = rawNode.Ready(); !reflect.DeepEqual(rd, wants[1]) {
  153. t.Errorf("#%d: g = %+v,\n w %+v", 2, rd, wants[1])
  154. } else {
  155. storage.Append(rd.Entries)
  156. rawNode.Advance(rd)
  157. }
  158. if rawNode.HasReady() {
  159. t.Errorf("unexpected Ready: %+v", rawNode.Ready())
  160. }
  161. }
  162. func TestRawNodeRestart(t *testing.T) {
  163. entries := []raftpb.Entry{
  164. {Term: 1, Index: 1},
  165. {Term: 1, Index: 2, Data: []byte("foo")},
  166. }
  167. st := raftpb.HardState{Term: 1, Commit: 1}
  168. want := Ready{
  169. HardState: emptyState,
  170. // commit up to commit index in st
  171. CommittedEntries: entries[:st.Commit],
  172. }
  173. storage := NewMemoryStorage()
  174. storage.SetHardState(st)
  175. storage.Append(entries)
  176. rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, storage), nil)
  177. if err != nil {
  178. t.Fatal(err)
  179. }
  180. rd := rawNode.Ready()
  181. if !reflect.DeepEqual(rd, want) {
  182. t.Errorf("g = %+v,\n w %+v", rd, want)
  183. }
  184. rawNode.Advance(rd)
  185. if rawNode.HasReady() {
  186. t.Errorf("unexpected Ready: %+v", rawNode.Ready())
  187. }
  188. }
  189. func TestRawNodeRestartFromSnapshot(t *testing.T) {
  190. snap := raftpb.Snapshot{
  191. Metadata: raftpb.SnapshotMetadata{
  192. ConfState: raftpb.ConfState{Nodes: []uint64{1, 2}},
  193. Index: 2,
  194. Term: 1,
  195. },
  196. }
  197. entries := []raftpb.Entry{
  198. {Term: 1, Index: 3, Data: []byte("foo")},
  199. }
  200. st := raftpb.HardState{Term: 1, Commit: 3}
  201. want := Ready{
  202. HardState: emptyState,
  203. // commit up to commit index in st
  204. CommittedEntries: entries,
  205. }
  206. s := NewMemoryStorage()
  207. s.SetHardState(st)
  208. s.ApplySnapshot(snap)
  209. s.Append(entries)
  210. rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, s), nil)
  211. if err != nil {
  212. t.Fatal(err)
  213. }
  214. if rd := rawNode.Ready(); !reflect.DeepEqual(rd, want) {
  215. t.Errorf("g = %+v,\n w %+v", rd, want)
  216. } else {
  217. rawNode.Advance(rd)
  218. }
  219. if rawNode.HasReady() {
  220. t.Errorf("unexpected Ready: %+v", rawNode.HasReady())
  221. }
  222. }
  223. // TestNodeAdvance from node_test.go has no equivalent in rawNode because there is
  224. // no dependency check between Ready() and Advance()
  225. func TestRawNodeStatus(t *testing.T) {
  226. storage := NewMemoryStorage()
  227. rawNode, err := NewRawNode(newTestConfig(1, nil, 10, 1, storage), []Peer{{ID: 1}})
  228. if err != nil {
  229. t.Fatal(err)
  230. }
  231. status := rawNode.Status()
  232. if status == nil {
  233. t.Errorf("expected status struct, got nil")
  234. }
  235. }