Add test for pruning unknown fields of CRDs
@@ -18,13 +18,13 @@ package crd
import (
"context"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "reflect"
"testing"
+ "github.com/google/go-cmp/cmp"
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
crdclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
crdclientfake "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var (
@@ -36,6 +36,7 @@ var (
singular: "test",
plural: "tests",
}
+ trueValue = true
)
func TestCreateOrUpdateCRD(t *testing.T) {
@@ -59,6 +60,19 @@ func TestCreateOrUpdateCRD(t *testing.T) {
return nil
},
},
+ {
+ desc: "Update CRD when pruning is not enabled",
+ initFunc: func(clientset crdclient.Interface) error {
+ crd := crd(crdMeta)
+ if crd.Spec.Validation != nil {
+ crd.Spec.PreserveUnknownFields = &trueValue
+ }
+ if _, err := clientset.ApiextensionsV1beta1().CustomResourceDefinitions().Create(context.TODO(), crd, metav1.CreateOptions{}); err != nil {
+ return err
+ }
+ return nil
+ },
+ },
}
for _, tc := range testCases {
@@ -75,10 +89,19 @@ func TestCreateOrUpdateCRD(t *testing.T) {
t.Errorf("%s: Unexpected error in createOrUpdateCRD(): %v", tc.desc, err)
}
+ if crd.Spec.Validation != nil {
+ if crd.Spec.PreserveUnknownFields == nil {
+ t.Fatalf("%s: Unexpected CRD returned, PreserveUnknownFields not set: %v", tc.desc, crd)
+ }
+ if *crd.Spec.PreserveUnknownFields {
+ t.Errorf("%s: Unexpected CRD returned, PreserveUnknownFields set to true: %v", tc.desc, crd)
+ }
+ }
+
// Nuke CRD status before comparing.
crd.Status = apiextensionsv1beta1.CustomResourceDefinitionStatus{}
- if !reflect.DeepEqual(crd, expectedCRD) {
- t.Errorf("%s: Unexpected CRD returned: got %v, want %v", tc.desc, crd, expectedCRD)
+ if diff := cmp.Diff(expectedCRD, crd); diff != "" {
+ t.Errorf("%s: Unexpected CRD returned (-want +got):\n%s", tc.desc, diff)
}
}
