Skip to content

Commit 847a7bb

Browse files
leonnicolasbwplotka
andcommitted
Apply suggestions from code review
Co-authored-by: Bartlomiej Plotka <[email protected]> Signed-off-by: leonnicolas <[email protected]>
1 parent d4090e9 commit 847a7bb

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

prometheus/testutil/testutil.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -255,16 +255,9 @@ func compareMetricFamilies(got, expected []*dto.MetricFamily, metricNames ...str
255255
got = filterMetrics(got, metricNames)
256256
expected = filterMetrics(expected, metricNames)
257257
if len(metricNames) > len(got) {
258-
h := make(map[string]struct{})
259-
for _, mf := range got {
260-
if mf == nil {
261-
continue
262-
}
263-
h[mf.GetName()] = struct{}{}
264-
}
265258
var missingMetricNames []string
266259
for _, name := range metricNames {
267-
if _, ok := h[name]; !ok {
260+
if ok := hasMetricByName(got, name); !ok {
268261
missingMetricNames = append(missingMetricNames, name)
269262
}
270263
}
@@ -275,6 +268,15 @@ func compareMetricFamilies(got, expected []*dto.MetricFamily, metricNames ...str
275268
return compare(got, expected)
276269
}
277270

271+
func hasMetricByName(metrics []*dto.MetricFamily, name string) bool {
272+
for _, mf := range metrics {
273+
if mf.GetName() == name {
274+
return true
275+
}
276+
}
277+
return false
278+
}
279+
278280
// compare encodes both provided slices of metric families into the text format,
279281
// compares their string message, and returns an error if they do not match.
280282
// The error contains the encoded text of both the desired and the actual

prometheus/testutil/testutil_test.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,8 @@ func TestScrapeAndCompare(t *testing.T) {
335335
scenarios := map[string]struct {
336336
want string
337337
metricNames []string
338-
errPrefix string
339-
fail bool
338+
// expectedErrPrefix if empty, means no fail is expected for the comparison.
339+
expectedErrPrefix string
340340
}{
341341
"empty metric Names": {
342342
want: `
@@ -382,9 +382,8 @@ func TestScrapeAndCompare(t *testing.T) {
382382
383383
some_total2{ label2 = "value2" } 1
384384
`,
385-
metricNames: []string{"some_total3"},
386-
errPrefix: "expected metric name(s) not found",
387-
fail: true,
385+
metricNames: []string{"some_total3"},
386+
expectedErrPrefix: "expected metric name(s) not found",
388387
},
389388
"one of multiple expected metric names is not scraped": {
390389
want: `
@@ -398,9 +397,8 @@ func TestScrapeAndCompare(t *testing.T) {
398397
399398
some_total2{ label2 = "value2" } 1
400399
`,
401-
metricNames: []string{"some_total1", "some_total3"},
402-
errPrefix: "expected metric name(s) not found",
403-
fail: true,
400+
metricNames: []string{"some_total1", "some_total3"},
401+
expectedErrPrefix: "expected metric name(s) not found",
404402
},
405403
}
406404
for name, scenario := range scenarios {
@@ -412,10 +410,10 @@ func TestScrapeAndCompare(t *testing.T) {
412410
}))
413411
defer ts.Close()
414412
if err := ScrapeAndCompare(ts.URL, expectedReader, scenario.metricNames...); err != nil {
415-
if !scenario.fail || !strings.HasPrefix(err.Error(), scenario.errPrefix) {
413+
if scenario.expectedErrPrefix == "" || !strings.HasPrefix(err.Error(), scenario.expectedErrPrefix) {
416414
t.Errorf("unexpected error happened: %s", err)
417415
}
418-
} else if scenario.fail {
416+
} else if scenario.expectedErrPrefix != "" {
419417
t.Errorf("expected an error but got nil")
420418
}
421419
})

0 commit comments

Comments
 (0)