@@ -13,6 +13,8 @@ import (
1313 "testing/quick"
1414 "time"
1515
16+ "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
17+
1618 "github.com/sirupsen/logrus"
1719 "github.com/stretchr/testify/require"
1820 "golang.org/x/time/rate"
@@ -30,6 +32,7 @@ import (
3032 "k8s.io/apimachinery/pkg/runtime/schema"
3133 "k8s.io/apimachinery/pkg/types"
3234 utilclock "k8s.io/apimachinery/pkg/util/clock"
35+ k8syaml "k8s.io/apimachinery/pkg/util/yaml"
3336 utilyaml "k8s.io/apimachinery/pkg/util/yaml"
3437 "k8s.io/apiserver/pkg/storage/names"
3538 fakedynamic "k8s.io/client-go/dynamic/fake"
@@ -1284,6 +1287,70 @@ func TestCompetingCRDOwnersExist(t *testing.T) {
12841287 }
12851288}
12861289
1290+ func TestValidateExistingCRs (t * testing.T ) {
1291+ unstructuredForFile := func (file string ) * unstructured.Unstructured {
1292+ data , err := ioutil .ReadFile (file )
1293+ require .NoError (t , err )
1294+ dec := k8syaml .NewYAMLOrJSONDecoder (strings .NewReader (string (data )), 30 )
1295+ k8sFile := & unstructured.Unstructured {}
1296+ require .NoError (t , dec .Decode (k8sFile ))
1297+ return k8sFile
1298+ }
1299+
1300+ unversionedCRDForV1beta1File := func (file string ) * apiextensions.CustomResourceDefinition {
1301+ data , err := ioutil .ReadFile (file )
1302+ require .NoError (t , err )
1303+ dec := k8syaml .NewYAMLOrJSONDecoder (strings .NewReader (string (data )), 30 )
1304+ k8sFile := & apiextensionsv1beta1.CustomResourceDefinition {}
1305+ require .NoError (t , dec .Decode (k8sFile ))
1306+ convertedCRD := & apiextensions.CustomResourceDefinition {}
1307+ require .NoError (t , apiextensionsv1beta1 .Convert_v1beta1_CustomResourceDefinition_To_apiextensions_CustomResourceDefinition (k8sFile , convertedCRD , nil ))
1308+ return convertedCRD
1309+ }
1310+
1311+ tests := []struct {
1312+ name string
1313+ existingObjects []runtime.Object
1314+ gvr schema.GroupVersionResource
1315+ newCRD * apiextensions.CustomResourceDefinition
1316+ want error
1317+ }{
1318+ {
1319+ name : "label validation" ,
1320+ existingObjects : []runtime.Object {
1321+ unstructuredForFile ("testdata/hivebug/cr.yaml" ),
1322+ },
1323+ gvr : schema.GroupVersionResource {
1324+ Group : "hive.openshift.io" ,
1325+ Version : "v1" ,
1326+ Resource : "machinepools" ,
1327+ },
1328+ newCRD : unversionedCRDForV1beta1File ("testdata/hivebug/crd.yaml" ),
1329+ },
1330+ {
1331+ name : "fail validation" ,
1332+ existingObjects : []runtime.Object {
1333+ unstructuredForFile ("testdata/hivebug/fail.yaml" ),
1334+ },
1335+ gvr : schema.GroupVersionResource {
1336+ Group : "hive.openshift.io" ,
1337+ Version : "v1" ,
1338+ Resource : "machinepools" ,
1339+ },
1340+ newCRD : unversionedCRDForV1beta1File ("testdata/hivebug/crd.yaml" ),
1341+ want : fmt .Errorf ("error validating custom resource against new schema for MachinePool /test: [[].spec.clusterDeploymentRef: Invalid value: \" null\" : spec.clusterDeploymentRef in body must be of type object: \" null\" , [].spec.name: Required value, [].spec.platform: Required value]" ),
1342+ },
1343+ }
1344+ for _ , tt := range tests {
1345+ t .Run (tt .name , func (t * testing.T ) {
1346+ client := fakedynamic .NewSimpleDynamicClientWithCustomListKinds (runtime .NewScheme (), map [schema.GroupVersionResource ]string {
1347+ tt .gvr : "UnstructuredList" ,
1348+ }, tt .existingObjects ... )
1349+ require .Equal (t , tt .want , validateExistingCRs (client , tt .gvr , tt .newCRD ))
1350+ })
1351+ }
1352+ }
1353+
12871354func fakeConfigMapData () map [string ]string {
12881355 data := make (map [string ]string )
12891356 yaml , err := yaml .Marshal ([]apiextensionsv1beta1.CustomResourceDefinition {crd ("fake-crd" )})
0 commit comments