-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_test.go
331 lines (311 loc) · 8.63 KB
/
merge_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package main
import (
"context"
"fmt"
"regexp"
"runtime"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"google.golang.org/protobuf/testing/protocmp"
dto "github.com/prometheus/client_model/go"
)
func TestMergeFamily(t *testing.T) {
for _, tc := range []struct {
name string
dst *dto.MetricFamily
src *dto.MetricFamily
want *dto.MetricFamily
wantErr *regexp.Regexp
}{
{
name: "empty",
dst: &dto.MetricFamily{},
src: &dto.MetricFamily{},
want: &dto.MetricFamily{},
},
{
name: "empty dst",
dst: nil,
src: &dto.MetricFamily{Name: newString("src")},
want: &dto.MetricFamily{Name: newString("src")},
},
{
name: "name mismatch",
dst: &dto.MetricFamily{Name: newString("foo")},
src: &dto.MetricFamily{Name: newString("bar")},
wantErr: regexp.MustCompile(`^name mismatch\b`),
},
{
name: "type mismatch",
dst: &dto.MetricFamily{Name: newString("size"), Type: dto.MetricType_GAUGE.Enum()},
src: &dto.MetricFamily{Name: newString("size"), Type: dto.MetricType_COUNTER.Enum()},
wantErr: regexp.MustCompile(`^type mismatch\b`),
},
{
name: "help swap aa,bb",
dst: &dto.MetricFamily{Name: newString("count"), Help: newString("aa")},
src: &dto.MetricFamily{Name: newString("count"), Help: newString("bb")},
want: &dto.MetricFamily{Name: newString("count"), Help: newString("aa")},
},
{
name: "help swap bb,aa",
dst: &dto.MetricFamily{Name: newString("count"), Help: newString("bb")},
src: &dto.MetricFamily{Name: newString("count"), Help: newString("aa")},
want: &dto.MetricFamily{Name: newString("count"), Help: newString("aa")},
},
{
name: "empty src help",
dst: &dto.MetricFamily{Name: newString("count"), Help: newString("bb")},
src: &dto.MetricFamily{Name: newString("count")},
want: &dto.MetricFamily{Name: newString("count"), Help: newString("bb")},
},
{
name: "append metrics",
dst: &dto.MetricFamily{Name: newString("xyz"), Metric: []*dto.Metric{
{Gauge: &dto.Gauge{Value: newFloat64(1)}},
}},
src: &dto.MetricFamily{Name: newString("xyz"), Metric: []*dto.Metric{
{Gauge: &dto.Gauge{Value: newFloat64(9)}},
{Gauge: &dto.Gauge{Value: newFloat64(8)}},
}},
want: &dto.MetricFamily{Name: newString("xyz"), Metric: []*dto.Metric{
{Gauge: &dto.Gauge{Value: newFloat64(1)}},
{Gauge: &dto.Gauge{Value: newFloat64(9)}},
{Gauge: &dto.Gauge{Value: newFloat64(8)}},
}},
},
} {
t.Run(tc.name, func(t *testing.T) {
got, err := mergeFamily(tc.dst, tc.src)
if tc.wantErr != nil {
if err == nil || !tc.wantErr.MatchString(err.Error()) {
t.Errorf("mergeFamily() failed with %v, want match for %q", err, tc.wantErr.String())
}
} else if err != nil {
t.Errorf("mergeFamily() failed with %v", err)
}
if err == nil {
if diff := cmp.Diff(got, tc.want, protocmp.Transform(), cmpopts.EquateEmpty()); diff != "" {
t.Errorf("mergeFamily() difference (-got +want):\n%s", diff)
}
}
})
}
}
func TestMergedInputsWrite(t *testing.T) {
for _, tc := range []struct {
name string
names bool
inputs mergedInputs
want string
}{
{
name: "empty",
},
{
name: "empty with names",
names: true,
want: "# Sources:\n\n",
},
{
name: "two with names",
names: true,
inputs: mergedInputs{
names: []string{"aaa.txt", "zzz.txt", "bbb.txt"},
families: []*dto.MetricFamily{
{
Name: newString("first"),
Type: dto.MetricType_COUNTER.Enum(),
Help: newString("this is a test"),
Metric: []*dto.Metric{
{
Counter: &dto.Counter{Value: newFloat64(13621)},
},
},
},
{
Name: newString("requests"),
Type: dto.MetricType_COUNTER.Enum(),
Metric: []*dto.Metric{
{
Label: []*dto.LabelPair{
{Name: newString("kind"), Value: newString("post")},
},
Counter: &dto.Counter{Value: newFloat64(14955)},
},
{
Label: []*dto.LabelPair{
{Name: newString("kind"), Value: newString("get")},
},
Counter: &dto.Counter{Value: newFloat64(18193)},
},
},
},
},
},
want: `# Sources:
# aaa.txt
# zzz.txt
# bbb.txt
# HELP first this is a test
# TYPE first counter
first 13621
# TYPE requests counter
requests{kind="post"} 14955
requests{kind="get"} 18193
`,
},
} {
t.Run(tc.name, func(t *testing.T) {
var buf strings.Builder
err := tc.inputs.write(&buf, tc.names)
if err != nil {
t.Errorf("mergedInputs() failed: %v", err)
} else if diff := cmp.Diff(buf.String(), tc.want); diff != "" {
t.Errorf("mergedInputs() difference (-got +want):\n%s", diff)
}
})
}
}
func TestReadAndMerge(t *testing.T) {
for _, tc := range []struct {
name string
inputs []inputWrapper
want *mergedInputs
wantErr *regexp.Regexp
}{
{
name: "empty",
want: &mergedInputs{},
},
{
name: "single gauge",
inputs: []inputWrapper{
newReaderInputWrapper(newFakeReaderWithName("a.txt",
"# TYPE size GAUGE\nsize{kind=\"a\"} 1\n")),
},
want: &mergedInputs{
names: []string{"a.txt"},
families: []*dto.MetricFamily{
{
Name: newString("size"),
Type: dto.MetricType_GAUGE.Enum(),
Metric: []*dto.Metric{
{
Label: []*dto.LabelPair{
{Name: newString("kind"), Value: newString("a")},
},
Gauge: &dto.Gauge{Value: newFloat64(1)},
},
},
},
},
},
},
{
name: "multiple inputs",
inputs: []inputWrapper{
newReaderInputWrapper(newFakeReaderWithName("a.txt",
"# TYPE requests COUNTER\nrequests{kind=\"a\"} 26191\n")),
newReaderInputWrapper(newFakeReaderWithName("b.txt",
"# TYPE requests COUNTER\nrequests{kind=\"b\"} 15123\n"+
"# TYPE size GAUGE\nsize 11477\n"+
"# TYPE aborted COUNTER\naborted 5694\n")),
},
want: &mergedInputs{
names: []string{"a.txt", "b.txt"},
families: []*dto.MetricFamily{
{
Name: newString("aborted"),
Type: dto.MetricType_COUNTER.Enum(),
Metric: []*dto.Metric{
{
Counter: &dto.Counter{Value: newFloat64(5694)},
},
},
},
{
Name: newString("requests"),
Type: dto.MetricType_COUNTER.Enum(),
Metric: []*dto.Metric{
{
Label: []*dto.LabelPair{
{Name: newString("kind"), Value: newString("a")},
},
Counter: &dto.Counter{Value: newFloat64(26191)},
},
{
Label: []*dto.LabelPair{
{Name: newString("kind"), Value: newString("b")},
},
Counter: &dto.Counter{Value: newFloat64(15123)},
},
},
},
{
Name: newString("size"),
Type: dto.MetricType_GAUGE.Enum(),
Metric: []*dto.Metric{
{
Gauge: &dto.Gauge{Value: newFloat64(11477)},
},
},
},
},
},
},
{
name: "type mismatch",
inputs: []inputWrapper{
newReaderInputWrapper(newFakeReaderWithName("a.txt",
"# TYPE wrong GAUGE\nwrong 1\n")),
newReaderInputWrapper(newFakeReaderWithName("b.txt",
"# TYPE wrong COUNTER\nwrong 2\n")),
},
wantErr: regexp.MustCompile(`^family "wrong" from "b.txt": type mismatch\b`),
},
} {
t.Run(tc.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
got, err := readAndMerge(ctx, tc.inputs)
if tc.wantErr != nil {
if err == nil || !tc.wantErr.MatchString(err.Error()) {
t.Errorf("readAndMerge() failed with %v, want match for %q", err, tc.wantErr.String())
}
} else if err != nil {
t.Errorf("readAndMerge() failed with %v", err)
}
if err == nil {
if diff := cmp.Diff(got, tc.want, protocmp.Transform(), cmp.AllowUnexported(mergedInputs{}), cmpopts.EquateEmpty(), cmpopts.EquateApprox(0, 0.0001)); diff != "" {
t.Errorf("readAndMerge() difference (-got +want):\n%s", diff)
}
}
})
}
}
func TestReadAndMergeMany(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
var inputs []inputWrapper
var wantNames []string
var count = 100 + runtime.GOMAXPROCS(0)
for i := 0; i < count; i++ {
name := fmt.Sprintf("input%d", i)
wantNames = append(wantNames, name)
inputs = append(inputs, newReaderInputWrapper(newFakeReaderWithName(name,
fmt.Sprintf("# TYPE test%[1]d GAUGE\ntest%[1]d %[1]d\n", i))))
}
got, err := readAndMerge(ctx, inputs)
if err != nil {
t.Errorf("readAndMerge() failed: %v", err)
}
if diff := cmp.Diff(got.names, wantNames); diff != "" {
t.Errorf("names difference (-got +want):\n%s", diff)
}
if len(got.families) != count {
t.Errorf("Result contained %d families, not %d: %v", len(got.families), count, got)
}
}