forked from letsencrypt/boulder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_test.go
82 lines (77 loc) · 2.35 KB
/
auto_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
package metrics
import (
"testing"
"github.com/prometheus/client_golang/prometheus"
)
func TestPromAdjust(t *testing.T) {
testCases := []struct {
input, output string
}{
{"Foo.Bar", "Foo_Bar"},
{"", ""},
{"FOO-BAR", "FOO_BAR"},
{">CA", "CA"},
{"?CA!- 99 @#$%&()", "CA_99"},
}
for _, tc := range testCases {
t.Run(tc.input, func(t *testing.T) {
if result := promAdjust(tc.input); result != tc.output {
t.Errorf("promAdjust(%q) - expected %q, got %q", tc.input, tc.output, result)
}
})
}
}
func TestAutoProm(t *testing.T) {
var calledWithName string
var madeGauge prometheus.Gauge
recorder := func(s string) prometheus.Collector {
calledWithName = s
madeGauge = prometheus.NewGauge(prometheus.GaugeOpts{Name: "hi", Help: "hi"})
return madeGauge
}
registry := prometheus.NewRegistry()
ap := newAutoProm(registry)
result := ap.get("foo.bar", recorder)
if calledWithName != "foo_bar" {
t.Errorf("expected maker function to be called with foo_bar, got %q", calledWithName)
}
if result != madeGauge {
t.Errorf("got back a different gauge than we made")
}
// Try again, make sure it was memoized again.
result2 := ap.get("foo.bar", recorder)
if result != result2 {
t.Errorf("expected to get same result twice, got a new result")
}
}
func TestAutoRegisterer(t *testing.T) {
registry := prometheus.NewRegistry()
ap := newAutoRegisterer(registry)
gauge := ap.autoGauge("ima_stat")
expected := "Desc{fqName: \"ima_stat\", help: \"auto\", constLabels: {}, variableLabels: []}"
if gauge == nil {
t.Fatal("gauge was nil")
}
gaugeDesc := gauge.Desc().String()
if gaugeDesc != expected {
t.Errorf("gauge description: got %q, expected %q", gaugeDesc, expected)
}
counter := ap.autoCounter("ima_counter")
expected = "Desc{fqName: \"ima_counter\", help: \"auto\", constLabels: {}, variableLabels: []}"
if counter == nil {
t.Fatal("counter was nil")
}
counterDesc := counter.Desc().String()
if counterDesc != expected {
t.Errorf("counter description: got %q, expected %q", counterDesc, expected)
}
summary := ap.autoSummary("ima_summary")
expected = "Desc{fqName: \"ima_summary\", help: \"auto\", constLabels: {}, variableLabels: []}"
if summary == nil {
t.Fatal("summary was nil")
}
summaryDesc := summary.Desc().String()
if summaryDesc != expected {
t.Errorf("summary description: got %q, expected %q", summaryDesc, expected)
}
}