Skip to content

Commit

Permalink
Fix TestNewKubernetesSource sporadic
Browse files Browse the repository at this point in the history
- Compare sorted expected and observed slices.

Signed-off-by: Alper Rifat Ulucinar <[email protected]>
  • Loading branch information
ulucinar committed Nov 22, 2024
1 parent ba35c31 commit 3982f4c
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions pkg/migration/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
package migration

import (
"cmp"
"slices"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/crossplane/crossplane-runtime/pkg/test"
gocmp "github.com/google/go-cmp/cmp"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/discovery/cached/memory"
"k8s.io/client-go/dynamic/fake"
fakeclientset "k8s.io/client-go/kubernetes/fake"
Expand Down Expand Up @@ -119,12 +123,34 @@ func TestNewKubernetesSource(t *testing.T) {
}

ks, err := NewKubernetesSource(dynamicClient, memory.NewMemCacheClient(client.Discovery()), WithRegistry(r))
if diff := cmp.Diff(tc.want.err, err); diff != "" {
if diff := gocmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
t.Errorf("\nNext(...): -want, +got:\n%s", diff)
}
if diff := cmp.Diff(tc.want.ks.items, ks.items); diff != "" {
slices.SortFunc(tc.want.ks.items, compareUnstructuredWithMetadata)
slices.SortFunc(ks.items, compareUnstructuredWithMetadata)
if diff := gocmp.Diff(tc.want.ks.items, ks.items); diff != "" {
t.Errorf("\nNext(...): -want, +got:\n%s", diff)
}
})
}
}

// compareUnstructuredWithMetadata is used for sorting UnstructuredWithMetadata
// slices based on their namespaced names and GVKs.
func compareUnstructuredWithMetadata(a, b UnstructuredWithMetadata) int {
switch {
case a.Object.Object == nil && b.Object.Object == nil:
return 0 // not comparable
case a.Object.Object == nil && b.Object.Object != nil:
return -1
case a.Object.Object != nil && b.Object.Object == nil:
return 1
}
fS := func(x *UnstructuredWithMetadata) string {
return types.NamespacedName{
Namespace: x.Object.GetNamespace(),
Name: x.Object.GetName(),
}.String() + ":" + x.Object.GetObjectKind().GroupVersionKind().String()
}
return cmp.Compare(fS(&a), fS(&b))
}

0 comments on commit 3982f4c

Please sign in to comment.