lib/promscrape/discovery/kubernetes: compare sorted sets of labels in tests

This should deflake tests where the order of labels isn't stable
This commit is contained in:
Aliaksandr Valialkin 2021-02-28 14:10:17 +02:00
parent 64b57c3ed3
commit e003453941

View file

@ -2,6 +2,8 @@ package kubernetes
import (
"reflect"
"sort"
"strconv"
"testing"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
@ -276,7 +278,7 @@ func TestParseNodeListSuccess(t *testing.T) {
"__meta_kubernetes_node_address_Hostname": "m01",
}),
}
if !reflect.DeepEqual(sortedLabelss, expectedLabelss) {
if !areEqualLabelss(sortedLabelss, expectedLabelss) {
t.Fatalf("unexpected labels:\ngot\n%v\nwant\n%v", sortedLabelss, expectedLabelss)
}
}
@ -291,3 +293,26 @@ func getSortedLabelss(objectsByKey map[string]object) [][]prompbmarshal.Label {
}
return result
}
func areEqualLabelss(a, b [][]prompbmarshal.Label) bool {
sortLabelss(a)
sortLabelss(b)
return reflect.DeepEqual(a, b)
}
func sortLabelss(a [][]prompbmarshal.Label) {
sort.Slice(a, func(i, j int) bool {
return marshalLabels(a[i]) < marshalLabels(a[j])
})
}
func marshalLabels(a []prompbmarshal.Label) string {
var b []byte
for _, label := range a {
b = strconv.AppendQuote(b, label.Name)
b = append(b, ':')
b = strconv.AppendQuote(b, label.Value)
b = append(b, ',')
}
return string(b)
}