forked from skx/puppet-summary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_metrics_test.go
104 lines (87 loc) · 1.79 KB
/
cmd_metrics_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
package main
import (
"bytes"
"context"
"os"
"strings"
"testing"
)
func TestMetrics(t *testing.T) {
// Create a fake database
FakeDB()
// Add some hosts.
addFakeNodes()
// Get the metrics
metrics := getMetrics()
// Now test we can find things.
if len(metrics) != 4 {
t.Errorf("Unexpected metrics-size: %v", len(metrics))
}
// Some values
if metrics["state.changed"] != "1" {
t.Errorf("Unexpected metrics value")
}
if metrics["state.unchanged"] != "0" {
t.Errorf("Unexpected metrics value")
}
if metrics["state.failed"] != "1" {
t.Errorf("Unexpected metrics value")
}
if metrics["state.orphaned"] != "0" {
t.Errorf("Unexpected metrics value")
}
//
// Cleanup here because otherwise later tests will
// see an active/valid DB-handle.
//
db.Close()
db = nil
os.RemoveAll(path)
}
//
// Actually attempt to send the metrics to stdout.
//
func TestMetricNop(t *testing.T) {
//
// Fake out the STDOUT
//
bak := out
out = new(bytes.Buffer)
defer func() { out = bak }()
// Create a fake database
FakeDB()
// Add some hosts.
addFakeNodes()
//
// Dump our metrics to STDOUT, due to `nop`, which will end up
// in our faux buffer.
s := metricsCmd{nop: true}
s.Execute(context.TODO(), nil)
//
// Now see what we got.
//
read := out.(*bytes.Buffer).String()
//
// And test it against each of the things we
// expect.
//
// NOTE: We have to do this as the output is ordered
// randomly.
//
desired := []string{".state.changed 0",
".state.failed 0",
".state.orphaned 0",
".state.unchanged 0"}
for _, str := range desired {
if !strings.Contains(read, str) {
t.Errorf("Unexpected metric-output - %s", read)
}
}
//
// Cleanup here because otherwise later tests will
// see an active/valid DB-handle.
//
db.Close()
db = nil
os.RemoveAll(path)
}