forked from nats-io/graft
-
Notifications
You must be signed in to change notification settings - Fork 3
/
log_test.go
195 lines (172 loc) · 5.26 KB
/
log_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
// Copyright 2013-2018 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package graft
import (
"encoding/json"
"io/ioutil"
"os"
"testing"
"time"
)
func TestLogPermissions(t *testing.T) {
ci := ClusterInfo{Name: "foo", Size: 3}
hand, rpc, log := genNodeArgs(t)
// remove it
os.Remove(log)
tmpDir, err := ioutil.TempDir("", "_grafty")
if err != nil {
t.Fatal("Could not create tmp dir")
}
file, err := ioutil.TempFile(tmpDir, "_log")
os.Chmod(tmpDir, 0400)
defer file.Close()
defer os.RemoveAll(tmpDir)
defer os.Chmod(tmpDir, 0770)
// Test we get correct error
if _, err := New(ci, hand, rpc, file.Name()); err == nil {
t.Fatal("Expected an error with bad permissions")
}
}
func TestLogCleanupOnClose(t *testing.T) {
ci := ClusterInfo{Name: "foo", Size: 3}
hand, rpc, log := genNodeArgs(t)
node, err := New(ci, hand, rpc, log)
if err != nil {
t.Fatalf("Expected no error, got: %v", err)
}
node.Close()
if _, err := os.Stat(log); !os.IsNotExist(err) {
t.Fatal("Expected log to be removed on Close()")
}
}
func TestLogPresenceOnNew(t *testing.T) {
// Make sure to clean us up from wonly state
defer mockResetPeers()
ci := ClusterInfo{Name: "p", Size: 1}
hand, rpc, log := genNodeArgs(t)
node, err := New(ci, hand, rpc, log)
if err != nil {
t.Fatalf("Expected no error, got: %v", err)
}
defer node.Close()
// Set some non default values
node.setTerm(10)
node.setVote("fake")
// Force writing the state
if err := node.writeState(); err != nil {
t.Fatalf("Unexpected error writing state: %v", err)
}
// Wait to become leader..
if state := waitForState(node, LEADER); state != LEADER {
t.Fatalf("Expected Node to be Leader, got %s", state)
}
// Create another with the same log..
node2, err := New(ci, hand, rpc, log)
if err != nil {
t.Fatalf("Expected no error, got: %v", err)
}
defer node2.Close()
if node.term != node2.term {
t.Fatalf("Terms did not match %d vs %d\n", node.term, node2.term)
}
if node.vote != node2.vote {
t.Fatalf("Votes did not match %s vs %s\n", node.vote, node2.vote)
}
}
func TestLogCreationOnNew(t *testing.T) {
ci := ClusterInfo{Name: "foo", Size: 3}
hand, rpc, log := genNodeArgs(t)
node, err := New(ci, hand, rpc, log)
if err != nil {
t.Fatalf("Expected no error, got: %v", err)
}
defer node.Close()
fake := fakeNode("fake")
mockRegisterPeer(fake)
defer mockUnregisterPeer(fake.Id())
// Should move to candidate state
if state := waitForState(node, CANDIDATE); state != CANDIDATE {
t.Fatalf("Expected node to move to Candidate state, got: %s", state)
}
// After this point, we only have the guarantee that the node's state
// changed to Candidate, but it is possible that the runAsCandidate()
// loop has not started yet, or is in progress but before the state was
// written. We know that the state is written before sending a vote request,
// so look for that vote request as the indication that the state should
// have been written.
<-fake.VoteRequests
// We should have written our state.
testStateOfNode(t, node)
}
func TestCorruption(t *testing.T) {
ci := ClusterInfo{Name: "foo", Size: 3}
hand, rpc, log := genNodeArgs(t)
node, err := New(ci, hand, rpc, log)
if err != nil {
t.Fatalf("Expected no error, got: %v", err)
}
defer node.Close()
// Delay elections
node.mu.Lock()
node.electTimer.Reset(10 * time.Second)
node.mu.Unlock()
node.setTerm(1)
node.setVote("foo")
// Force writing the state
node.writeState()
// We should have written our state.
testStateOfNode(t, node)
// Now introduce some corruption
buf, err := ioutil.ReadFile(node.logPath)
if err != nil {
t.Fatalf("Could not read logfile: %v", err)
}
env := &envelope{}
if err := json.Unmarshal(buf, env); err != nil {
t.Fatalf("Error unmarshalling envelope: %v", err)
}
env.Data = []byte("ZZZZ")
toWrite, err := json.Marshal(env)
if err != nil {
t.Fatalf("Error Marshalling envelope: %v", err)
}
if err := ioutil.WriteFile(node.logPath, toWrite, 0660); err != nil {
t.Fatalf("Error writing envelope: %v", err)
}
// Make sure we get the corruptError
_, err = node.readState(node.logPath)
if err == nil {
t.Fatal("Expected an error reading corrupt state")
}
if err != LogCorruptErr {
t.Fatalf("Expected corrupt error, got %q", err)
}
}
// This will test that we have the correct saved state at any point in time.
func testStateOfNode(t *testing.T, node *Node) {
if node == nil {
stackFatalf(t, "Expected a non-nil Node")
}
ps, err := node.readState(node.logPath)
if err != nil {
stackFatalf(t, "Err reading state: %q\n", err)
}
if ps.CurrentTerm != node.CurrentTerm() {
stackFatalf(t, "Expected CurrentTerm of %d, got %d\n",
node.CurrentTerm(), ps.CurrentTerm)
}
if ps.VotedFor != node.CurrentVote() {
stackFatalf(t, "Expected a vote for %q, got %q\n",
node.CurrentVote(), ps.VotedFor)
}
}