/
promtest_test.go
116 lines (108 loc) · 2.22 KB
/
promtest_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
package promtest_test
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/mdlayher/promtest"
"github.com/prometheus/client_golang/prometheus"
)
func TestCollect(t *testing.T) {
tests := []struct {
name string
c prometheus.Collector
s string
}{
{
name: "OK",
c: prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Name: "promtest_value",
Help: "A metric for promtest testing.",
},
func() float64 { return 1.0 },
),
s: `# HELP promtest_value A metric for promtest testing.
# TYPE promtest_value gauge
promtest_value 1
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := string(promtest.Collect(t, tt.c))
if diff := cmp.Diff(tt.s, s); diff != "" {
t.Fatalf("unexpected body (-want +got):\n%s", diff)
}
})
}
}
func TestLint(t *testing.T) {
tests := []struct {
name string
s string
ok bool
}{
{
name: "bad",
s: `# HELP
# TYPE promtest_value counter
promtest_value 1
`,
},
{
name: "OK",
s: `# HELP promtest_value A metric for promtest testing.
# TYPE promtest_value gauge
promtest_value 1
`,
ok: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ok := promtest.Lint(t, []byte(tt.s))
if diff := cmp.Diff(tt.ok, ok); diff != "" {
t.Fatalf("unexpected OK value (-want +got):\n%s", diff)
}
})
}
}
func TestMatch(t *testing.T) {
tests := []struct {
name string
s string
metrics []string
ok bool
}{
{
name: "bad",
s: `# HELP promtest_value A metric for promtest testing.
# TYPE promtest_value gauge
promtest_value 1
`,
metrics: []string{`promtest_value 100`},
},
{
name: "OK",
s: `# HELP promtest_value A metric for promtest testing.
# TYPE promtest_value gauge
promtest_value 1
# HELP promtest_labels A metric for promtest testing.
# TYPE promtest_labels gauge
promtest_value{foo="bar"} 2
`,
metrics: []string{
`promtest_value 1`,
`promtest_value{foo="bar"} 2`,
},
ok: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ok := promtest.Match(t, []byte(tt.s), tt.metrics)
if diff := cmp.Diff(tt.ok, ok); diff != "" {
t.Fatalf("unexpected OK value (-want +got):\n%s", diff)
}
})
}
}