-
Notifications
You must be signed in to change notification settings - Fork 5
/
serve_db_test.go
349 lines (286 loc) · 7.66 KB
/
serve_db_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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package main
import (
"io/ioutil"
"net/url"
"os"
"reflect"
"testing"
)
type fixturePair struct {
json []byte
triplets []serveRecord
}
func (f *fixturePair) check(t *testing.T, sdb *serveDb) {
for _, triplet := range f.triplets {
rec, ok := sdb.identToServe[sKey{I: triplet.I, P: triplet.P}]
if !ok {
t.Fatalf("Expected to find identifier %q", triplet.I)
}
if !reflect.DeepEqual(triplet.u, rec.u) {
t.Fatalf("Expected to resolve to %+v, "+
"but got %v instead", triplet.u, rec.u)
}
}
}
func mustParseURL(us string) url.URL {
u, err := url.Parse(us)
if err != nil {
panic(err)
}
return *u
}
var fixtures = []fixturePair{
{
json: []byte(`{"serves": ` +
`[{"i": "apple", "url": "https://token:chocolate@localhost", ` +
`"p": "/p1/log.sock"}, ` +
`{"i": "banana", "url": "https://token:vanilla@localhost", ` +
`"p": "/p2/log.sock"}]}`),
triplets: []serveRecord{
{sKey{I: "apple", P: "/p1/log.sock"},
mustParseURL(
"https://token:chocolate@localhost"),
"brown"},
{sKey{I: "banana", P: "/p2/log.sock"},
mustParseURL(
"https://token:vanilla@localhost"),
"white"},
},
},
{
json: []byte(`{"serves": ` +
`[{"i": "bed", ` +
`"url": "https://token:pillow@localhost", ` +
`"p": "/p1/log.sock"}, ` +
`{"i": "nightstand", ` +
`"url": "https://token:alarm-clock@localhost", ` +
`"p": "/p2/log.sock"}]}`),
triplets: []serveRecord{
{sKey{I: "bed", P: "/p1/log.sock"},
mustParseURL(
"https://token:pillow@localhost"),
"white"},
{sKey{I: "nightstand", P: "/p2/log.sock"},
mustParseURL(
"https://token:alarm-clock@localhost"),
"black"},
},
},
}
func newTmpDb(t *testing.T) string {
name, err := ioutil.TempDir("", "test_")
if err != nil {
t.Fatalf("Could not create temporary directory for test: %v",
err)
}
return name
}
func TestEmptyDB(t *testing.T) {
name := newTmpDb(t)
defer os.RemoveAll(name)
sdb := newServeDb(name)
updates, err := sdb.Poll()
if err != nil {
t.Fatalf("Poll on an empty directory should succeed, "+
"instead failed: %v", err)
}
if !updates {
t.Fatal("Expect updates for first poll in an empty database")
}
updates, err = sdb.Poll()
if err != nil {
t.Fatalf("Poll on an empty directory should succeed, "+
"instead failed: %v", err)
}
if updates {
t.Fatal("Expect no updates for second poll")
}
}
func TestMultipleLoad(t *testing.T) {
name := newTmpDb(t)
defer os.RemoveAll(name)
sdb := newServeDb(name)
for i := range fixtures {
fixture := &fixtures[i]
ioutil.WriteFile(sdb.newPath(), fixture.json, 0400)
if _, err := sdb.Poll(); err != nil {
t.Fatalf("Poll should succeed with valid input, "+
"instead: %v", err)
}
_, err := os.Stat(sdb.loadedPath())
if err != nil {
t.Fatalf("Input should be successfully loaded to %v, "+
"but the file could not be stat()ed for some "+
"reason: %v", sdb.loadedPath(), err)
}
fixture.check(t, sdb)
}
}
// Write out some valid input to serves.new.
func writeLoadFixture(t *testing.T, sdb *serveDb, fixture *fixturePair) {
ioutil.WriteFile(sdb.newPath(), fixture.json, 0400)
if _, err := sdb.Poll(); err != nil {
t.Fatalf("Poll should succeed with valid input, "+
"instead: %v", err)
}
}
func TestIntermixedGoodBadInput(t *testing.T) {
name := newTmpDb(t)
defer os.RemoveAll(name)
sdb := newServeDb(name)
fixture := &fixtures[0]
writeLoadFixture(t, sdb, fixture)
// Write a bad serves.new file.
ioutil.WriteFile(sdb.newPath(), []byte(`{}`), 0400)
if _, err := sdb.Poll(); err != nil {
t.Fatalf("Poll should succeed with invalid input, "+
"instead: %v", err)
}
// Confirm that the original, good fixture's data is still in
// place.
fixture.check(t, sdb)
// Confirm that the serves.rej and last_error file have been
// made.
_, err := os.Stat(sdb.errPath())
if err != nil {
t.Fatalf("last_error file should exist: %v", err)
}
_, err = os.Stat(sdb.rejPath())
if err != nil {
t.Fatalf("serves.rej should exist: %v", err)
}
// Submit a new set of good input, to see if the last_error
// and serves.rej are unlinked.
secondFixture := &fixtures[1]
writeLoadFixture(t, sdb, secondFixture)
// Make sure new data was loaded properly.
secondFixture.check(t, sdb)
// Check that the old reject file and error file are gone.
_, err = os.Stat(sdb.errPath())
if err == nil || !os.IsNotExist(err) {
t.Fatalf("last_error file shouldn't exist: %v", err)
}
_, err = os.Stat(sdb.rejPath())
if err == nil || !os.IsNotExist(err) {
t.Fatalf("serves.rej shouldn't exist: %v", err)
}
}
func TestFirstTimeLoadPoll(t *testing.T) {
name := newTmpDb(t)
defer os.RemoveAll(name)
sdb := newServeDb(name)
// Write directly to the serves.loaded file, which is not the
// normal way thing are done; Poll() should move things around
// outside a test environment.
fixture := &fixtures[0]
ioutil.WriteFile(sdb.loadedPath(), fixture.json, 0400)
if _, err := sdb.Poll(); err != nil {
t.Fatalf("Poll should succeed with valid input, "+
"instead: %v", err)
}
fixture.check(t, sdb)
}
func TestEmptyPoll(t *testing.T) {
name := newTmpDb(t)
defer os.RemoveAll(name)
sdb := newServeDb(name)
_, err := sdb.Poll()
if err != nil {
t.Fatalf("An empty database should not cause an error, "+
"but got: %v", err)
}
if sdb.identToServe == nil {
t.Fatal("An empty database should yield an " +
"empty routing table.")
}
}
func TestFirstLoadBad(t *testing.T) {
name := newTmpDb(t)
defer os.RemoveAll(name)
sdb := newServeDb(name)
// Write a bad serves.new file.
ioutil.WriteFile(sdb.newPath(), []byte(`{}`), 0400)
if _, err := sdb.Poll(); err != nil {
t.Fatalf("Poll should succeed with invalid input, "+
"instead: %v", err)
}
_, err := sdb.Poll()
if err != nil {
t.Fatalf("Rejected input should not cause an error, "+
"but got: %v", err)
}
// Confirm that the serves.rej and last_error file have been
// made.
_, err = os.Stat(sdb.errPath())
if err != nil {
t.Fatalf("last_error file should exist: %v", err)
}
_, err = os.Stat(sdb.rejPath())
if err != nil {
t.Fatalf("serves.rej should exist: %v", err)
}
}
func TestSnapshot(t *testing.T) {
name := newTmpDb(t)
defer os.RemoveAll(name)
sdb := newServeDb(name)
snap := sdb.Snapshot()
if len(snap) != 0 {
t.Fatalf("Expect snapshot to have be empty")
}
fix := &fixtures[0]
writeLoadFixture(t, sdb, fix)
snap = sdb.Snapshot()
if len(snap) != len(fix.triplets) {
t.Fatalf("Expect snapshot to be filled, got %v", snap)
}
}
func TestSnapReload(t *testing.T) {
// Sketch how to use Poll() and Snapshot() together with
// goroutines.
name := newTmpDb(t)
defer os.RemoveAll(name)
sdb := newServeDb(name)
writeLoadFixture(t, sdb, &fixtures[0])
die := make(chan struct{})
deaths := make(chan bool)
justWait := func(die <-chan struct{}, r serveRecord) {
t.Logf("justWait started with %v", r)
for {
t.Logf("justWait 'running' with %v", r)
select {
case <-die:
t.Logf("justWait 'dies' with %v", r)
deaths <- true
return
default:
break
}
}
}
snapLoad := func(fix *fixturePair) {
writeLoadFixture(t, sdb, fix)
snap := sdb.Snapshot()
for i := range snap {
go justWait(die, snap[i])
}
close(die)
// Confirm everyone dies
for _ = range snap {
<-deaths
}
}
// Simulates a case where one calls Poll() repeatedly -- in
// fact, writeLoadFixture used above does do this and checks
// the results.
for i := range fixtures {
snapLoad(&fixtures[i])
// Have to make a new 'die' channel for each
// generation of goroutines.
die = make(chan struct{})
}
// If anyone gorooutines are left and trying to write to the
// deaths channel, (due to a programming error), try to force
// a panic by closing the death channel.
close(deaths)
}