Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clusterdeployment tests for DeepEquals #1410

Merged
merged 1 commit into from Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -15,14 +15,12 @@ import (

batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"

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/apimachinery/pkg/util/diff"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/utils/pointer"
Expand Down Expand Up @@ -223,17 +221,14 @@ func TestClusterDeploymentReconcile(t *testing.T) {
validate: func(c client.Client, t *testing.T) {
cd := getCD(c)
if assert.NotNil(t, cd, "no clusterdeployment found") {
if e, a := testClusterDeploymentWithDefaultConditions(testClusterDeploymentWithInitializedConditions(
testClusterDeploymentWithProvision())), cd; !assert.True(t, apiequality.Semantic.DeepEqual(e, a),
"unexpected change in clusterdeployment") {
t.Logf("diff = %s", diff.ObjectReflectDiff(e, a))
}
e := testClusterDeploymentWithDefaultConditions(testClusterDeploymentWithInitializedConditions(
testClusterDeploymentWithProvision()))
testassert.AssertEqualWhereItCounts(t, e, cd, "unexpected change in clusterdeployment")
}
provisions := getProvisions(c)
if assert.Len(t, provisions, 1, "expected provision to exist") {
if e, a := testProvision(), provisions[0]; !assert.True(t, apiequality.Semantic.DeepEqual(e, a), "unexpected change in provision") {
t.Logf("diff = %s", diff.ObjectReflectDiff(e, a))
}
e := testProvision()
testassert.AssertEqualWhereItCounts(t, e, provisions[0], "unexpected change in provision")
}
},
},
Expand Down Expand Up @@ -2491,11 +2486,10 @@ func testEmptyClusterDeployment() *hivev1.ClusterDeployment {
Kind: "ClusterDeployment",
},
ObjectMeta: metav1.ObjectMeta{
Name: testName,
Namespace: testNamespace,
Finalizers: []string{hivev1.FinalizerDeprovision},
UID: types.UID("1234"),
ResourceVersion: "999",
Name: testName,
Namespace: testNamespace,
Finalizers: []string{hivev1.FinalizerDeprovision},
UID: types.UID("1234"),
},
}
return cd
Expand Down Expand Up @@ -2709,7 +2703,6 @@ func testProvision() *hivev1.ClusterProvision {
Labels: map[string]string{
constants.ClusterDeploymentNameLabel: testName,
},
ResourceVersion: "999",
},
Spec: hivev1.ClusterProvisionSpec{
ClusterDeploymentRef: corev1.LocalObjectReference{
Expand Down
26 changes: 26 additions & 0 deletions pkg/test/assert/assertions.go
Expand Up @@ -5,9 +5,13 @@ import (
"testing"
"time"

"github.com/google/go-cmp/cmp"
testifyassert "github.com/stretchr/testify/assert"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"

hivev1 "github.com/openshift/hive/apis/hive/v1"
)
Expand Down Expand Up @@ -71,3 +75,25 @@ func AssertConditions(t *testing.T, cd *hivev1.ClusterDeployment, expectedCondit
}
}
}

// AssertEqualWhereItCounts compares two runtime.Objects, ignoring their ResourceVersion and TypeMeta, asserting that they
// are otherwise equal.
// This and cleanRVAndTypeMeta were borrowed/adapted from:
// https://github.com/openshift/ci-tools/blob/179a0edb6c003aa95ae1332692c5b4c79e58f674/pkg/testhelper/testhelper.go#L157-L175
func AssertEqualWhereItCounts(t *testing.T, x, y runtime.Object, msg string) {
xCopy := x.DeepCopyObject()
yCopy := y.DeepCopyObject()
cleanRVAndTypeMeta(xCopy)
cleanRVAndTypeMeta(yCopy)
diff := cmp.Diff(xCopy, yCopy)
testifyassert.Empty(t, diff, msg)
}

func cleanRVAndTypeMeta(r runtime.Object) {
if metaObject, ok := r.(metav1.Object); ok {
metaObject.SetResourceVersion("")
}
if typeObject, ok := r.(interface{ SetGroupVersionKind(schema.GroupVersionKind) }); ok {
typeObject.SetGroupVersionKind(schema.GroupVersionKind{})
}
}