fix: flaky test for api/v1/targets/metadata. (#6436)

* Allows sorting of responses from the API in tests

Fixes flaky test for api/v1/targets/metadata.

Allows sorting of responses from the API. For our tests to be deterministic, we need to ensure the response from the API follows an order. This structure allows us to define one.

Fixes #6431

Signed-off-by: gotjosh <josue@grafana.com>
This commit is contained in:
gotjosh 2019-12-09 21:36:38 +00:00 committed by Brian Brazil
parent 5c503d85f7
commit 7bb73a9abd
1 changed files with 15 additions and 0 deletions

View File

@ -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 {