-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdb_test.go
493 lines (413 loc) · 8.27 KB
/
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//
// Basic testing of our DB primitives
//
package main
import (
"database/sql"
"fmt"
"io/ioutil"
"os"
"regexp"
"testing"
"time"
)
//
// Temporary location for database
//
var path string
//
// Create a temporary database
//
func FakeDB() {
p, err := ioutil.TempDir(os.TempDir(), "prefix")
if err == nil {
path = p
}
//
// Setup the tables.
//
SetupDB(p + "/db.sql")
}
//
// Add some fake reports
//
func addFakeReports() {
tx, err := db.Begin()
if err != nil {
panic(err)
}
//
// Add some records
stmt, err := tx.Prepare("INSERT INTO reports(fqdn,environment,yaml_file,executed_at) values(?,?,?,?)")
if err != nil {
panic(err)
}
defer stmt.Close()
count := 0
for count < 30 {
now := time.Now().Unix()
days := int64(60 * 60 * 24 * count)
env := "production"
if count > 2 {
env = "test"
}
fqdn := fmt.Sprintf("node%d.example.com", count)
now -= days
stmt.Exec(fqdn, env, "/../data/valid.yaml", now)
count++
}
tx.Commit()
}
//
// Add some (repeated) nodes in various states
//
func addFakeNodes() {
var n PuppetReport
n.Fqdn = "foo.example.com"
n.State = "changed"
n.Runtime = "3.134"
n.Failed = "0"
n.Total = "1"
n.Changed = "2"
n.Skipped = "3"
addDB(n, "")
n.Fqdn = "bar.example.com"
n.State = "failed"
n.Runtime = "2.718"
n.Failed = "0"
n.Total = "1"
n.Changed = "2"
n.Skipped = "3"
addDB(n, "")
n.Fqdn = "foo.example.com"
n.State = "unchanged"
n.Runtime = "2.718"
n.Failed = "0"
n.Total = "1"
n.Changed = "2"
n.Skipped = "3"
addDB(n, "")
//
// Here we're trying to fake an orphaned node.
//
// When a report is added the exected_at field is set to
// "time.Now().Unix()". To make an orphaned record we need
// to change that to some time >24 days ago.
//
// We do that by finding the last report-ID, and then editing
// the field.
//
var maxID string
row := db.QueryRow("SELECT MAX(id) FROM reports")
err := row.Scan(&maxID)
switch {
case err == sql.ErrNoRows:
case err != nil:
panic("failed to find max report ID")
default:
}
//
// Now we can change the executed_at field of that last
// addition
//
sqlStmt := fmt.Sprintf("UPDATE reports SET executed_at=300 WHERE id=%s",
maxID)
_, err = db.Exec(sqlStmt)
if err != nil {
panic("Failed to change report ")
}
}
//
// Get a valid report ID.
//
func validReportID() (int, error) {
var count int
row := db.QueryRow("SELECT MAX(id) FROM reports")
err := row.Scan(&count)
return count, err
}
//
// Test that functions return errors if setup hasn't been called.
//
func TestMissingInit(t *testing.T) {
//
// Regexp to match the error we expect to receive.
//
reg, _ := regexp.Compile("SetupDB not called")
var x PuppetReport
err := addDB(x, "")
if !reg.MatchString(err.Error()) {
t.Errorf("Got wrong error: %v", err)
}
_, err = countReports()
if !reg.MatchString(err.Error()) {
t.Errorf("Got wrong error: %v", err)
}
_, err = getYAML("", "")
if !reg.MatchString(err.Error()) {
t.Errorf("Got wrong error: %v", err)
}
_, err = getIndexNodes("")
if !reg.MatchString(err.Error()) {
t.Errorf("Got wrong error: %v", err)
}
_, err = getReports("example.com")
if !reg.MatchString(err.Error()) {
t.Errorf("Got wrong error: %v", err)
}
_, err = getHistory("", 60)
if !reg.MatchString(err.Error()) {
t.Errorf("Got wrong error: %v", err)
}
err = pruneReports("", "", 3, false)
if !reg.MatchString(err.Error()) {
t.Errorf("Got wrong error: %v", err)
}
}
//
// Test creating a new DB fails when given a directory.
//
func TestBogusInit(t *testing.T) {
// Create a fake database
FakeDB()
err := SetupDB(path)
if err == nil {
t.Errorf("We should have seen a create-error")
}
//
// Cleanup here because otherwise later tests will
// see an active/valid DB-handle.
//
db.Close()
db = nil
os.RemoveAll(path)
}
//
// Add some nodes and verify they are reaped.
//
func TestPrune(t *testing.T) {
// Create a fake database
FakeDB()
// With some reports.
addFakeReports()
//
// Count records and assume we have some.
//
old, err := countReports()
if err != nil {
t.Errorf("Error counting reports")
}
if old != 30 {
t.Errorf("We have %d reports, not 30", old)
}
//
// Run the prune
//
pruneReports("", "", 5, false)
//
// Count them again
//
new, err := countReports()
if err != nil {
t.Errorf("Error counting reports")
}
if new != 6 {
t.Errorf("We have %d reports, not 6", new)
}
//
// Test pruning of specific environments by pruning all test envs
//
pruneReports("test", "", 0, false)
//
// Final count
//
fnl, err := countReports()
if err != nil {
t.Errorf("Error counting reports")
}
if fnl != 3 {
t.Errorf("We have %d production environment reports, not 3", fnl)
}
//
// Cleanup here because otherwise later tests will
// see an active/valid DB-handle.
//
db.Close()
db = nil
os.RemoveAll(path)
}
//
// Add some nodes and verify they are reaped, if unchanged.
//
func TestPruneUnchanged(t *testing.T) {
// Create a fake database
FakeDB()
// With some reports.
addFakeNodes()
//
// Count records and assume we have some.
//
old, err := countReports()
if err != nil {
t.Errorf("Error counting reports")
}
if old != 3 {
t.Errorf("We have %d reports, not 3", old)
}
//
// Run the prune
//
pruneUnchanged("", "", false)
//
// Count them again
//
new, err := countReports()
if err != nil {
t.Errorf("Error counting reports")
}
//
// The value won't have changed.
//
if new != old {
t.Errorf("We have %d reports, not %d", new, old)
}
//
// But we'll expect that several will have updated
// to show that their paths have been changed to 'reaped'
//
pruned, err := countUnchangedAndReapedReports()
if err != nil {
t.Errorf("Error counting reaped reports")
}
if pruned != 1 {
t.Errorf("We have %d pruned reports, not 1", pruned)
}
//
// Cleanup here because otherwise later tests will
// see an active/valid DB-handle.
//
db.Close()
db = nil
os.RemoveAll(path)
}
//
// Test the index nodes are valid
//
func TestIndex(t *testing.T) {
//
// Create a fake database.
//
FakeDB()
// Add some fake nodes.
addFakeNodes()
//
// We have three fake nodes now, two of which have the
// same hostname.
//
runs, err := getIndexNodes("")
if err != nil {
t.Errorf("getIndexNodes failed: %v", err)
}
//
// Should have two side
//
if len(runs) != 2 {
t.Errorf("getIndexNodes returned wrong number of results: %d", len(runs))
}
//
// But three reports
//
total, err := countReports()
if err != nil {
t.Errorf("Failed to count reports")
}
if total != 3 {
t.Errorf("We found the wrong number of reports, %d", total)
}
//
// Cleanup here because otherwise later tests will
// see an active/valid DB-handle.
//
db.Close()
db = nil
os.RemoveAll(path)
}
//
// Test the report-run are valid
//
func TestMissiongReport(t *testing.T) {
FakeDB()
_, err := getYAML("", "")
reg, _ := regexp.Compile("failed to find report with specified ID")
if !reg.MatchString(err.Error()) {
t.Errorf("Got wrong error: %v", err)
}
//
// Cleanup here because otherwise later tests will
// see an active/valid DB-handle.
//
db.Close()
db = nil
os.RemoveAll(path)
}
//
// Test the report-run are valid
//
func TestReports(t *testing.T) {
//
// Add fake reports.
//
FakeDB()
addFakeNodes()
//
// We have three fake nodes now, two of which have the
// same hostname.
//
runs, err := getReports("foo.example.com")
if err != nil {
t.Errorf("getReports failed: %v", err)
}
//
// Should have two runs against the host
//
if len(runs) != 2 {
t.Errorf("getReports returned wrong number of results: %d", len(runs))
}
//
// Cleanup here because otherwise later tests will
// see an active/valid DB-handle.
//
db.Close()
db = nil
os.RemoveAll(path)
}
//
// Test the report-run are valid
//
func TestHistory(t *testing.T) {
//
// Add fake reports.
//
FakeDB()
addFakeNodes()
//
// We have three fake nodes now, two of which have the same hostname.
//
runs, err := getHistory("", 60)
if err != nil {
t.Errorf("getHistory failed: %v", err)
}
//
// Should have 2 runs, becase we have only one unique date..
//
if len(runs) != 2 {
t.Errorf("getReports returned wrong number of results: %d", len(runs))
}
//
// Cleanup here because otherwise later tests will
// see an active/valid DB-handle.
//
db.Close()
db = nil
os.RemoveAll(path)
}