Sort slices to avoid flakeyness in tests (#3064)

The `TestGetServicesFor` is flaky because it compares two slices of services which are in a non-deterministic order.

To make this deterministic, we first sort the slices by name.

Signed-off-by: Alex Leong <alex@buoyant.io>
This commit is contained in:
Alex Leong 2019-07-16 16:03:21 -07:00 committed by GitHub
parent 811d173174
commit dc2b96f903
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 3 deletions

View File

@ -1235,6 +1235,8 @@ spec:
t.Fatalf("api.GetServicesFor() unexpected error, expected [%s] got: [%s]", exp.err, err)
}
sort.Sort(byService(k8sResultServices))
sort.Sort(byService(services))
if !reflect.DeepEqual(services, k8sResultServices) {
t.Fatalf("Expected: %+v, Got: %+v", k8sResultServices, services)
}

View File

@ -35,8 +35,12 @@ func NewFakeAPI(configs ...string) (*API, error) {
type byPod []*corev1.Pod
func (s byPod) Len() int { return len(s) }
func (bp byPod) Len() int { return len(bp) }
func (bp byPod) Swap(i, j int) { bp[i], bp[j] = bp[j], bp[i] }
func (bp byPod) Less(i, j int) bool { return bp[i].Name <= bp[j].Name }
func (s byPod) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
type byService []*corev1.Service
func (s byPod) Less(i, j int) bool { return s[i].Name < s[j].Name }
func (bs byService) Len() int { return len(bs) }
func (bs byService) Swap(i, j int) { bs[i], bs[j] = bs[j], bs[i] }
func (bs byService) Less(i, j int) bool { return bs[i].Name <= bs[j].Name }