@@ -13,6 +13,8 @@ import (
13
13
"testing/quick"
14
14
"time"
15
15
16
+ "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
17
+
16
18
"github.com/sirupsen/logrus"
17
19
"github.com/stretchr/testify/require"
18
20
"golang.org/x/time/rate"
@@ -30,6 +32,7 @@ import (
30
32
"k8s.io/apimachinery/pkg/runtime/schema"
31
33
"k8s.io/apimachinery/pkg/types"
32
34
utilclock "k8s.io/apimachinery/pkg/util/clock"
35
+ k8syaml "k8s.io/apimachinery/pkg/util/yaml"
33
36
utilyaml "k8s.io/apimachinery/pkg/util/yaml"
34
37
"k8s.io/apiserver/pkg/storage/names"
35
38
fakedynamic "k8s.io/client-go/dynamic/fake"
@@ -1284,6 +1287,70 @@ func TestCompetingCRDOwnersExist(t *testing.T) {
1284
1287
}
1285
1288
}
1286
1289
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
+
1287
1354
func fakeConfigMapData () map [string ]string {
1288
1355
data := make (map [string ]string )
1289
1356
yaml , err := yaml .Marshal ([]apiextensionsv1beta1.CustomResourceDefinition {crd ("fake-crd" )})
0 commit comments