-
Notifications
You must be signed in to change notification settings - Fork 5
/
groupcollector_test.go
116 lines (102 loc) · 2.45 KB
/
groupcollector_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 main
import (
"context"
"errors"
"regexp"
"testing"
"github.com/hansmi/prometheus-lvm-exporter/lvmreport"
"github.com/prometheus/client_golang/prometheus"
"github.com/sebdah/goldie/v2"
"golang.org/x/sync/errgroup"
)
func TestGroupCollectorCollect(t *testing.T) {
for _, tc := range []struct {
name string
data *lvmreport.ReportData
collector *groupCollector
wantErrMsg []*regexp.Regexp
}{
{
name: "group-collector-with-error",
data: &lvmreport.ReportData{
SEG: []lvmreport.Row{
{"key1": "1", "col1": "123", "count": "true"},
{"key1": "2", "text": "Hello World", "count": "22", "info1": "info:2"},
{"key1": "3", "unknown key": "unknown", "count": "11.5"},
},
},
collector: newGroupCollector(&group{
name: lvmreport.SEG,
infoMetricName: "test_info",
keyFields: []*descriptor{
{
fieldName: "key1",
metricName: "m_key1",
},
},
infoFields: []*descriptor{
{
fieldName: "info1",
metricName: "m_info1",
},
},
metricFields: []*descriptor{
{
fieldName: "col1",
metricName: "m_col1",
metricValue: func(raw string) (float64, error) {
return 0, errors.New("col1 error")
},
},
{
fieldName: "count",
metricName: "m_count",
},
},
}),
wantErrMsg: []*regexp.Regexp{
regexp.MustCompile(`(?m)field col1: col1 error$`),
regexp.MustCompile(`(?m)field count:.*: invalid syntax$`),
},
},
} {
t.Run(tc.name, func(t *testing.T) {
disableLogOutput(t)
c := newEmptyCollector()
c.gc = append(c.gc, tc.collector)
c.load = func(ctx context.Context) (*lvmreport.ReportData, error) {
return tc.data, nil
}
g := goldie.New(t)
g.Assert(t, tc.name, gatherAndFormat(t, c))
{
var g errgroup.Group
ch := make(chan prometheus.Metric)
g.Go(func() error {
defer close(ch)
for _, gc := range c.gc {
if err := gc.collect(ch, tc.data); err != nil {
return err
}
}
return nil
})
for range ch {
}
if err := g.Wait(); len(tc.wantErrMsg) > 0 {
match := (err != nil)
if err != nil {
for _, i := range tc.wantErrMsg {
match = match && i.MatchString(err.Error())
}
}
if !match {
t.Errorf("collector failed with %q, want match for %q", err, tc.wantErrMsg)
}
} else if err != nil {
t.Errorf("collector failed with %v", err)
}
}
})
}
}