diff --git a/web/api/v1/api_test.go b/web/api/v1/api_test.go index acaff7d4c..00ad825f2 100644 --- a/web/api/v1/api_test.go +++ b/web/api/v1/api_test.go @@ -27,6 +27,7 @@ import ( "net/url" "os" "reflect" + "sort" "strings" "testing" "time" @@ -539,6 +540,7 @@ func testEndpoints(t *testing.T, api *API, testLabelAPI bool) { query url.Values response interface{} errType errorType + sorter func(interface{}) } var tests = []test{ @@ -1009,6 +1011,12 @@ func testEndpoints(t *testing.T, api *API, testLabelAPI bool) { Unit: "", }, }, + sorter: func(m interface{}) { + sort.Slice(m.([]metricMetadata), func(i, j int) bool { + s := m.([]metricMetadata) + return s[i].Metric < s[j].Metric + }) + }, }, // Without a matching metric. { @@ -1162,6 +1170,11 @@ func testEndpoints(t *testing.T, api *API, testLabelAPI bool) { } res := test.endpoint(req.WithContext(ctx)) assertAPIError(t, res.err, test.errType) + + if test.sorter != nil { + test.sorter(res.data) + } + assertAPIResponse(t, res.data, test.response) } } @@ -1185,6 +1198,8 @@ func assertAPIError(t *testing.T, got *apiError, exp errorType) { } func assertAPIResponse(t *testing.T, got interface{}, exp interface{}) { + t.Helper() + if !reflect.DeepEqual(exp, got) { respJSON, err := json.Marshal(got) if err != nil {