Revert "testutil compareMetricFamilies: make less error-prone (#1424)"
This reverts commit c7c7509669
.
This commit is contained in:
parent
2254d6c308
commit
f3d6c5d959
|
@ -277,15 +277,6 @@ func compareMetricFamilies(got, expected []*dto.MetricFamily, metricNames ...str
|
||||||
if metricNames != nil {
|
if metricNames != nil {
|
||||||
got = filterMetrics(got, metricNames)
|
got = filterMetrics(got, metricNames)
|
||||||
expected = filterMetrics(expected, metricNames)
|
expected = filterMetrics(expected, metricNames)
|
||||||
if len(metricNames) > len(got) {
|
|
||||||
var missingMetricNames []string
|
|
||||||
for _, name := range metricNames {
|
|
||||||
if ok := hasMetricByName(got, name); !ok {
|
|
||||||
missingMetricNames = append(missingMetricNames, name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return fmt.Errorf("expected metric name(s) not found: %v", missingMetricNames)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return compare(got, expected)
|
return compare(got, expected)
|
||||||
|
@ -327,12 +318,3 @@ func filterMetrics(metrics []*dto.MetricFamily, names []string) []*dto.MetricFam
|
||||||
}
|
}
|
||||||
return filtered
|
return filtered
|
||||||
}
|
}
|
||||||
|
|
||||||
func hasMetricByName(metrics []*dto.MetricFamily, name string) bool {
|
|
||||||
for _, mf := range metrics {
|
|
||||||
if mf.GetName() == name {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
|
@ -328,94 +328,51 @@ func TestMetricNotFound(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestScrapeAndCompare(t *testing.T) {
|
func TestScrapeAndCompare(t *testing.T) {
|
||||||
scenarios := map[string]struct {
|
const expected = `
|
||||||
want string
|
|
||||||
metricNames []string
|
|
||||||
// expectedErr if empty, means no fail is expected for the comparison.
|
|
||||||
expectedErr string
|
|
||||||
}{
|
|
||||||
"empty metric Names": {
|
|
||||||
want: `
|
|
||||||
# HELP some_total A value that represents a counter.
|
# HELP some_total A value that represents a counter.
|
||||||
# TYPE some_total counter
|
# TYPE some_total counter
|
||||||
|
|
||||||
some_total{ label1 = "value1" } 1
|
some_total{ label1 = "value1" } 1
|
||||||
`,
|
`
|
||||||
metricNames: []string{},
|
|
||||||
},
|
|
||||||
"one metric": {
|
|
||||||
want: `
|
|
||||||
# HELP some_total A value that represents a counter.
|
|
||||||
# TYPE some_total counter
|
|
||||||
|
|
||||||
some_total{ label1 = "value1" } 1
|
expectedReader := strings.NewReader(expected)
|
||||||
`,
|
|
||||||
metricNames: []string{"some_total"},
|
|
||||||
},
|
|
||||||
"multiple expected": {
|
|
||||||
want: `
|
|
||||||
# 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
|
|
||||||
`,
|
|
||||||
metricNames: []string{"some_total2"},
|
|
||||||
},
|
|
||||||
"expected metric name is not scraped": {
|
|
||||||
want: `
|
|
||||||
# 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
|
|
||||||
`,
|
|
||||||
metricNames: []string{"some_total3"},
|
|
||||||
expectedErr: "expected metric name(s) not found: [some_total3]",
|
|
||||||
},
|
|
||||||
"one of multiple expected metric names is not scraped": {
|
|
||||||
want: `
|
|
||||||
# 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
|
|
||||||
`,
|
|
||||||
metricNames: []string{"some_total1", "some_total3"},
|
|
||||||
expectedErr: "expected metric name(s) not found: [some_total1 some_total3]",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
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) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprintln(w, scenario.want)
|
fmt.Fprintln(w, expected)
|
||||||
}))
|
}))
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
if err := ScrapeAndCompare(ts.URL, expectedReader, scenario.metricNames...); err != nil {
|
|
||||||
if scenario.expectedErr == "" || err.Error() != scenario.expectedErr {
|
if err := ScrapeAndCompare(ts.URL, expectedReader, "some_total"); err != nil {
|
||||||
t.Errorf("unexpected error happened: %s", err)
|
t.Errorf("unexpected scraping result:\n%s", err)
|
||||||
}
|
}
|
||||||
} else if scenario.expectedErr != "" {
|
|
||||||
t.Errorf("expected an error but got nil")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("fetching fail", func(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")
|
err := ScrapeAndCompare("some_url", strings.NewReader("some expectation"), "some_total")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("expected an error but got nil")
|
t.Errorf("expected an error but got nil")
|
||||||
|
@ -423,9 +380,9 @@ func TestScrapeAndCompare(t *testing.T) {
|
||||||
if !strings.HasPrefix(err.Error(), "scraping metrics failed") {
|
if !strings.HasPrefix(err.Error(), "scraping metrics failed") {
|
||||||
t.Errorf("unexpected error happened: %s", err)
|
t.Errorf("unexpected error happened: %s", err)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
t.Run("bad status code", func(t *testing.T) {
|
func TestScrapeAndCompareBadStatusCode(t *testing.T) {
|
||||||
const expected = `
|
const expected = `
|
||||||
# HELP some_total A value that represents a counter.
|
# HELP some_total A value that represents a counter.
|
||||||
# TYPE some_total counter
|
# TYPE some_total counter
|
||||||
|
@ -448,7 +405,6 @@ func TestScrapeAndCompare(t *testing.T) {
|
||||||
if !strings.HasPrefix(err.Error(), "the scraping target returned a status code other than 200") {
|
if !strings.HasPrefix(err.Error(), "the scraping target returned a status code other than 200") {
|
||||||
t.Errorf("unexpected error happened: %s", err)
|
t.Errorf("unexpected error happened: %s", err)
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCollectAndCount(t *testing.T) {
|
func TestCollectAndCount(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue