Filter expected metrics as well in CollectAndCompare (#1143)

* Filter expected metrics as well in CollectAndCompare

Signed-off-by: Daria Danilenko <daria.danilenko@fluxninja.com>

* Add testcase for multiple expected metrics
Signed-off-by: Daria Danilenko <daria.danilenko@fluxninja.com>

* Change test values for filtering multiple expected metrics
Signed-off-by: Daria Danilenko <daria.danilenko@fluxninja.com>

---------

Signed-off-by: Daria Danilenko <daria.danilenko@fluxninja.com>
This commit is contained in:
Daria Bialobrzeska 2023-03-22 11:45:45 +01:00 committed by GitHub
parent 5c7821d84a
commit eb339714f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions

View File

@ -238,6 +238,7 @@ func convertReaderToMetricFamily(reader io.Reader) ([]*dto.MetricFamily, error)
func compareMetricFamilies(got, expected []*dto.MetricFamily, metricNames ...string) error {
if metricNames != nil {
got = filterMetrics(got, metricNames)
expected = filterMetrics(expected, metricNames)
}
return compare(got, expected)

View File

@ -331,6 +331,31 @@ func TestScrapeAndCompare(t *testing.T) {
}
}
func TestScrapeAndCompareWithMultipleExpected(t *testing.T) {
const expected = `
# HELP some_total A value that represents a counter.
# TYPE some_total counter
some_total{ label1 = "value1" } 1
# HELP some_total2 A value that represents a counter.
# TYPE some_total2 counter
some_total2{ label2 = "value2" } 1
`
expectedReader := strings.NewReader(expected)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, expected)
}))
defer ts.Close()
if err := ScrapeAndCompare(ts.URL, expectedReader, "some_total2"); err != nil {
t.Errorf("unexpected scraping result:\n%s", err)
}
}
func TestScrapeAndCompareFetchingFail(t *testing.T) {
err := ScrapeAndCompare("some_url", strings.NewReader("some expectation"), "some_total")
if err == nil {