forked from hansmi/prometheus-lvm-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector_test.go
81 lines (64 loc) · 1.69 KB
/
collector_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
package main
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"log"
"testing"
"github.com/hansmi/prometheus-lvm-exporter/lvmreport"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/prometheus/common/expfmt"
"github.com/sebdah/goldie/v2"
)
func disableLogOutput(t *testing.T) {
t.Helper()
previous := log.Writer()
log.SetOutput(ioutil.Discard)
t.Cleanup(func() {
log.SetOutput(previous)
})
}
func gatherAndFormat(t *testing.T, c prometheus.Collector) []byte {
t.Helper()
reg := prometheus.NewPedanticRegistry()
if err := prometheus.WrapRegistererWithPrefix(metricPrefix, reg).Register(c); err != nil {
t.Fatalf("registering collector failed: %v", err)
}
if problems, err := testutil.GatherAndLint(reg); !(err == nil || len(problems) > 0) {
t.Errorf("GatherAndLint() failed: %v\n%v", err, problems)
}
families, err := reg.Gather()
if err != nil {
t.Fatalf("Gathering failed: %v", err)
}
var buf bytes.Buffer
for _, mf := range families {
if _, err := expfmt.MetricFamilyToText(&buf, mf); err != nil {
t.Fatalf("MetricFamilyToText(%v) failed: %v", mf, err)
}
}
return buf.Bytes()
}
func TestCollector(t *testing.T) {
disableLogOutput(t)
for _, tc := range []struct {
name string
}{
{name: "vgdata-loop"},
{name: "vgdata-cached"},
{name: "multivg"},
{name: "mirrored"},
{name: "mirrored-unhealthy"},
} {
t.Run(tc.name, func(t *testing.T) {
c := newCollector()
c.load = func(ctx context.Context) (*lvmreport.ReportData, error) {
return lvmreport.FromFile(fmt.Sprintf("testdata/%s.json", tc.name))
}
g := goldie.New(t)
g.Assert(t, tc.name, gatherAndFormat(t, c))
})
}
}