This repository has been archived by the owner on Mar 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_integration_test.go
253 lines (204 loc) · 6.5 KB
/
node_integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package raftify
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"testing"
"time"
)
func TestSingleNodeClusterWithNoPeers(t *testing.T) {
if testing.Short() {
t.Skip("Skipping TestSingleNodeClusterWithNoPeers in short mode")
}
// Reserve ports for this test
ports := reservePorts(1)
config := Config{
ID: "Node_TestSingleNodeClusterWithNoPeers",
MaxNodes: 1,
Expect: 1,
BindPort: ports[0],
}
// Initialize node
pwd, _ := os.Getwd()
logger := log.New(os.Stderr, "", 0)
os.MkdirAll(pwd+"/testing/TestSingleNodeClusterWithNoPeers", 0755)
defer os.RemoveAll(pwd + "/testing")
nodesBytes, _ := json.Marshal(config)
ioutil.WriteFile(pwd+"/testing/TestSingleNodeClusterWithNoPeers/raftify.json", nodesBytes, 0755)
node, _ := InitNode(logger, pwd+"/testing/TestSingleNodeClusterWithNoPeers")
if node.GetState() != Leader {
t.Logf("Expected node in single-node cluster to switch to leader immediately, instead it's in the %v state", node.state.toString())
t.FailNow()
}
if err := node.Shutdown(); err != nil {
t.Logf("Expected successful shutdown of Node_TestSingleNodeClusterWithNoPeers, instead got error: %v", err.Error())
t.FailNow()
}
}
func TestSingleNodeClusterWithPeers(t *testing.T) {
if testing.Short() {
t.Skip("Skipping TestSingleNodeClusterWithPeers in short mode")
}
// Reserve ports for this test
ports := reservePorts(2)
config := Config{
ID: "Node_TestSingleNodeClusterWithPeers",
MaxNodes: 2,
Expect: 1,
BindPort: ports[0],
PeerList: []string{
fmt.Sprintf("127.0.0.1:%v", ports[1]),
},
}
// Initialize node
pwd, _ := os.Getwd()
logger := log.New(os.Stderr, "", 0)
os.MkdirAll(pwd+"/testing/TestSingleNodeClusterWithPeers", 0755)
defer os.RemoveAll(pwd + "/testing")
nodesBytes, _ := json.Marshal(config)
ioutil.WriteFile(pwd+"/testing/TestSingleNodeClusterWithPeers/raftify.json", nodesBytes, 0755)
node, _ := InitNode(logger, pwd+"/testing/TestSingleNodeClusterWithPeers")
if node.GetState() == Leader {
t.Log("Expected node in single-node cluster not to switch to leader immediately, instead it's in the leader state right away")
t.FailNow()
}
if err := node.Shutdown(); err != nil {
t.Logf("Expected successful shutdown of Node_TestSingleNodeClusterWithPeers, instead got error: %v", err.Error())
t.FailNow()
}
}
func TestNode(t *testing.T) {
if testing.Short() {
t.Skip("Skipping TestNode in short mode")
}
// Reserve ports for this test
ports := reservePorts(3)
config := Config{
ID: "Node_TestNode",
MaxNodes: 3,
Expect: 3,
}
// Populate peerlist
for i := 0; i < config.MaxNodes; i++ {
config.PeerList = append(config.PeerList, fmt.Sprintf("127.0.0.1:%v", ports[i]))
}
// Initialize all nodes
pwd, _ := os.Getwd()
logger := log.New(os.Stderr, "", 0)
nodes := []*Node{}
for i := 0; i < config.MaxNodes; i++ {
os.MkdirAll(fmt.Sprintf("%v/testing/TestNode-%v", pwd, i), 0755)
defer os.RemoveAll(fmt.Sprintf("%v/testing", pwd))
config.ID = fmt.Sprintf("TestNode-%v", i)
config.BindPort = ports[i]
nodesBytes, _ := json.Marshal(config)
ioutil.WriteFile(fmt.Sprintf("%v/testing/TestNode-%v/raftify.json", pwd, i), nodesBytes, 0755)
go func(pwd string, i int) {
node, _ := InitNode(logger, fmt.Sprintf("%v/testing/TestNode-%v", pwd, i))
nodes = append(nodes, node)
}(pwd, i)
time.Sleep(time.Second)
}
// Wait for bootstrap to kick in for a leader to be elected
time.Sleep(2 * time.Second)
// Check if every node is out of bootstrap mode
for i, node := range nodes {
if node.state == Bootstrap {
t.Logf("Expected TestNode-%v to be bootstrapped, instead it is still in bootstrap state", i)
t.FailNow()
}
}
// Test leader leave event
for i, node := range nodes {
if node.state == Leader {
if err := node.Shutdown(); err != nil {
t.Logf("Expected successful shutdown of TestNode-%v, instead got error: %v", i, err.Error())
t.FailNow()
}
nodes = append(nodes[:i], nodes[i+1:]...)
break
}
if i == len(nodes)-1 {
t.Log("Expected to find a leader and shut it down, instead couldn't find a leader")
t.FailNow()
}
}
// Wait for a new leader to be elected
time.Sleep(2 * time.Second)
// Check if a new leader exists
for i, node := range nodes {
if node.state == Leader {
break
}
if i == len(nodes)-1 {
t.Log("Expected a new leader to be elected after previous one left, instead couldn't find a leader")
t.FailNow()
}
}
for i, node := range nodes {
if err := node.Shutdown(); err != nil {
t.Logf("Expected successful shutdown of TestNode-%v, instead got error: %v", i, err.Error())
t.FailNow()
}
}
}
func TestNodeRejoin(t *testing.T) {
if testing.Short() {
t.Skip("Skipping TestNodeRejoin in short mode")
}
// Reserve ports for this test and configure nodes
ports := reservePorts(3)
config := Config{
ID: "Node_TestNodeRejoin",
MaxNodes: 3,
Expect: 2,
}
// Populate peerlist
for i := 0; i < config.MaxNodes; i++ {
config.PeerList = append(config.PeerList, fmt.Sprintf("127.0.0.1:%v", ports[i]))
}
// Initialize all nodes
pwd, _ := os.Getwd()
logger := log.New(os.Stderr, "", 0)
nodes := []*Node{}
for i := 0; i < config.MaxNodes; i++ {
os.MkdirAll(fmt.Sprintf("%v/testing/TestNodeRejoin-%v", pwd, i), 0755)
defer os.RemoveAll(fmt.Sprintf("%v/testing", pwd))
config.ID = fmt.Sprintf("TestNodeRejoin-%v", i)
config.BindPort = ports[i]
nodesBytes, _ := json.Marshal(config)
ioutil.WriteFile(fmt.Sprintf("%v/testing/TestNodeRejoin-%v/raftify.json", pwd, i), nodesBytes, 0755)
go func(pwd string, i int) {
node, _ := InitNode(logger, fmt.Sprintf("%v/testing/TestNodeRejoin-%v", pwd, i))
nodes = append(nodes, node)
}(pwd, i)
time.Sleep(time.Second)
}
// Wait for bootstrap to kick in for a leader to be elected
time.Sleep(2 * time.Second)
// Make leader trigger transition into rejoin state
for _, node := range nodes {
if node.state == Leader {
node.toRejoin()
break
}
}
// Wait for the rejoin to be resolved
time.Sleep(3 * time.Second)
// Check if rejoin has been resolved
for _, node := range nodes {
if node.state != Follower && node.state != PreCandidate && node.state != Candidate && node.state != Leader {
t.Logf("Expected rejoin situation to be resolved, instead found %v to be in the %v state", node.config.ID, node.state.toString())
t.FailNow()
}
}
// Shut down all nodes
for _, node := range nodes {
if err := node.Shutdown(); err != nil {
t.Logf("Expected successful shutdown of Node_TestNode, instead got error: %v", err.Error())
t.FailNow()
}
}
}