Compare text strings directly in testutil

Signed-off-by: PhilipGough <philip.p.gough@gmail.com>
This commit is contained in:
PhilipGough 2018-11-14 11:55:09 +00:00
parent 30503fe98e
commit f9739b3d97
No known key found for this signature in database
GPG Key ID: 114C620CE7C50D52
1 changed files with 10 additions and 13 deletions

View File

@ -138,20 +138,13 @@ func GatherAndCompare(g prometheus.Gatherer, expected io.Reader, metricNames ...
} }
want := internal.NormalizeMetricFamilies(wantRaw) want := internal.NormalizeMetricFamilies(wantRaw)
if len(got) != len(want) { return compare(got, want)
return notMatchingError(got, want)
}
for i := range got {
if got[i].String() != want[i].String() {
return notMatchingError(got, want)
}
}
return nil
} }
// notMatchingError encodes both provided slices of metric families into the // compare encodes both provided slices of metric families into the text format,
// text format and creates a readable error message from the result. // compares their string message, and returns an error if they do not match.
func notMatchingError(got, want []*dto.MetricFamily) error { // The error contains the encoded text, of both the desired, and actual result.
func compare(got, want []*dto.MetricFamily) error {
var gotBuf, wantBuf bytes.Buffer var gotBuf, wantBuf bytes.Buffer
enc := expfmt.NewEncoder(&gotBuf, expfmt.FmtText) enc := expfmt.NewEncoder(&gotBuf, expfmt.FmtText)
for _, mf := range got { for _, mf := range got {
@ -166,6 +159,7 @@ func notMatchingError(got, want []*dto.MetricFamily) error {
} }
} }
if wantBuf.String() != gotBuf.String() {
return fmt.Errorf(` return fmt.Errorf(`
metric output does not match expectation; want: metric output does not match expectation; want:
@ -175,6 +169,9 @@ got:
%s %s
`, wantBuf.String(), gotBuf.String()) `, wantBuf.String(), gotBuf.String())
}
return nil
} }
func filterMetrics(metrics []*dto.MetricFamily, names []string) []*dto.MetricFamily { func filterMetrics(metrics []*dto.MetricFamily, names []string) []*dto.MetricFamily {