提交 7bb73a9a 编写于 作者: G gotjosh 提交者: Brian Brazil

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: Ngotjosh <josue@grafana.com>
上级 5c503d85
......@@ -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 {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册