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:
parent
5c503d85f7
commit
7bb73a9abd
|
@ -27,6 +27,7 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
@ -539,6 +540,7 @@ func testEndpoints(t *testing.T, api *API, testLabelAPI bool) {
|
||||||
query url.Values
|
query url.Values
|
||||||
response interface{}
|
response interface{}
|
||||||
errType errorType
|
errType errorType
|
||||||
|
sorter func(interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
var tests = []test{
|
var tests = []test{
|
||||||
|
@ -1009,6 +1011,12 @@ func testEndpoints(t *testing.T, api *API, testLabelAPI bool) {
|
||||||
Unit: "",
|
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.
|
// Without a matching metric.
|
||||||
{
|
{
|
||||||
|
@ -1162,6 +1170,11 @@ func testEndpoints(t *testing.T, api *API, testLabelAPI bool) {
|
||||||
}
|
}
|
||||||
res := test.endpoint(req.WithContext(ctx))
|
res := test.endpoint(req.WithContext(ctx))
|
||||||
assertAPIError(t, res.err, test.errType)
|
assertAPIError(t, res.err, test.errType)
|
||||||
|
|
||||||
|
if test.sorter != nil {
|
||||||
|
test.sorter(res.data)
|
||||||
|
}
|
||||||
|
|
||||||
assertAPIResponse(t, res.data, test.response)
|
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{}) {
|
func assertAPIResponse(t *testing.T, got interface{}, exp interface{}) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
if !reflect.DeepEqual(exp, got) {
|
if !reflect.DeepEqual(exp, got) {
|
||||||
respJSON, err := json.Marshal(got)
|
respJSON, err := json.Marshal(got)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue