From 7163ac9859da60c01561ee23cca61721a3d04b8b Mon Sep 17 00:00:00 2001 From: leonnicolas Date: Thu, 4 Jan 2024 12:17:42 +0100 Subject: [PATCH] testutil compareMetricFamilies: make less error-prone The functions `GatherAndCompare`, `ScrapeAndCompare` and others that use `compareMetricFamilies` under the hood can return no error if `metricNames` includes none of the names found in the scraped/gathered results. To avoid false Positves (an error being the negative case), we can return an error if there is is at least one name in `metricNames` that is not in the filtered results. Fixes: https://github.com/prometheus/client_golang/issues/1351 Signed-off-by: leonnicolas --- prometheus/testutil/testutil.go | 3 + prometheus/testutil/testutil_test.go | 138 ++++++++++++++++++--------- 2 files changed, 95 insertions(+), 46 deletions(-) diff --git a/prometheus/testutil/testutil.go b/prometheus/testutil/testutil.go index 269f56435..3ba78baec 100644 --- a/prometheus/testutil/testutil.go +++ b/prometheus/testutil/testutil.go @@ -254,6 +254,9 @@ func compareMetricFamilies(got, expected []*dto.MetricFamily, metricNames ...str if metricNames != nil { got = filterMetrics(got, metricNames) expected = filterMetrics(expected, metricNames) + if len(metricNames) > len(got) { + return fmt.Errorf("expected metrics name not found") + } } return compare(got, expected) diff --git a/prometheus/testutil/testutil_test.go b/prometheus/testutil/testutil_test.go index f2e1cbaff..0ba3ed053 100644 --- a/prometheus/testutil/testutil_test.go +++ b/prometheus/testutil/testutil_test.go @@ -332,27 +332,46 @@ Diff: } func TestScrapeAndCompare(t *testing.T) { - const expected = ` + scenarios := map[string]struct { + want string + metricNames []string + errPrefix string + fail bool + }{ + "empty metric Names": { + want: ` # HELP some_total A value that represents a counter. # TYPE some_total counter some_total{ label1 = "value1" } 1 - ` + `, + metricNames: []string{}, + }, + "one metric": { + want: ` + # HELP some_total A value that represents a counter. + # TYPE some_total counter - expectedReader := strings.NewReader(expected) + some_total{ label1 = "value1" } 1 + `, + metricNames: []string{"some_total"}, + }, + "multiple expected": { + want: ` + # HELP some_total A value that represents a counter. + # TYPE some_total counter - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintln(w, expected) - })) - defer ts.Close() + some_total{ label1 = "value1" } 1 - if err := ScrapeAndCompare(ts.URL, expectedReader, "some_total"); err != nil { - t.Errorf("unexpected scraping result:\n%s", err) - } -} + # HELP some_total2 A value that represents a counter. + # TYPE some_total2 counter -func TestScrapeAndCompareWithMultipleExpected(t *testing.T) { - const expected = ` + some_total2{ label2 = "value2" } 1 + `, + metricNames: []string{"some_total2"}, + }, + "expected metric name is not scraped": { + want: ` # HELP some_total A value that represents a counter. # TYPE some_total counter @@ -362,53 +381,80 @@ func TestScrapeAndCompareWithMultipleExpected(t *testing.T) { # TYPE some_total2 counter some_total2{ label2 = "value2" } 1 - ` - - expectedReader := strings.NewReader(expected) + `, + metricNames: []string{"some_total3"}, + errPrefix: "expected metrics name not found", + fail: true, + }, + "one of multiple expected metric names is not scraped": { + want: ` + # HELP some_total A value that represents a counter. + # TYPE some_total counter - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintln(w, expected) - })) - defer ts.Close() + some_total{ label1 = "value1" } 1 - if err := ScrapeAndCompare(ts.URL, expectedReader, "some_total2"); err != nil { - t.Errorf("unexpected scraping result:\n%s", err) - } -} + # HELP some_total2 A value that represents a counter. + # TYPE some_total2 counter -func TestScrapeAndCompareFetchingFail(t *testing.T) { - err := ScrapeAndCompare("some_url", strings.NewReader("some expectation"), "some_total") - if err == nil { - t.Errorf("expected an error but got nil") + some_total2{ label2 = "value2" } 1 + `, + metricNames: []string{"some_total1", "some_total3"}, + errPrefix: "expected metrics name not found", + fail: true, + }, } - if !strings.HasPrefix(err.Error(), "scraping metrics failed") { - t.Errorf("unexpected error happened: %s", err) + for name, scenario := range scenarios { + t.Run(name, func(t *testing.T) { + expectedReader := strings.NewReader(scenario.want) + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, scenario.want) + })) + defer ts.Close() + if err := ScrapeAndCompare(ts.URL, expectedReader, scenario.metricNames...); err != nil { + if !scenario.fail || !strings.HasPrefix(err.Error(), scenario.errPrefix) { + t.Errorf("unexpected error happened: %s", err) + } + } else if scenario.fail { + t.Errorf("expected an error but got nil") + } + }) } -} -func TestScrapeAndCompareBadStatusCode(t *testing.T) { - const expected = ` + t.Run("fetching fail", func(t *testing.T) { + err := ScrapeAndCompare("some_url", strings.NewReader("some expectation"), "some_total") + if err == nil { + t.Errorf("expected an error but got nil") + } + if !strings.HasPrefix(err.Error(), "scraping metrics failed") { + t.Errorf("unexpected error happened: %s", err) + } + }) + + t.Run("bad status code", func(t *testing.T) { + const expected = ` # HELP some_total A value that represents a counter. # TYPE some_total counter some_total{ label1 = "value1" } 1 ` - expectedReader := strings.NewReader(expected) + expectedReader := strings.NewReader(expected) - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusBadGateway) - fmt.Fprintln(w, expected) - })) - defer ts.Close() + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusBadGateway) + fmt.Fprintln(w, expected) + })) + defer ts.Close() - err := ScrapeAndCompare(ts.URL, expectedReader, "some_total") - if err == nil { - t.Errorf("expected an error but got nil") - } - if !strings.HasPrefix(err.Error(), "the scraping target returned a status code other than 200") { - t.Errorf("unexpected error happened: %s", err) - } + err := ScrapeAndCompare(ts.URL, expectedReader, "some_total") + if err == nil { + t.Errorf("expected an error but got nil") + } + if !strings.HasPrefix(err.Error(), "the scraping target returned a status code other than 200") { + t.Errorf("unexpected error happened: %s", err) + } + }) } func TestCollectAndCount(t *testing.T) {