From 7bb73a9abd9ed9005729e242723bd9dd86fab957 Mon Sep 17 00:00:00 2001 From: gotjosh Date: Mon, 9 Dec 2019 21:36:38 +0000 Subject: [PATCH] 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 --- web/api/v1/api_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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 {