@@ -20,6 +20,7 @@ import (
20
20
"testing"
21
21
22
22
"github.com/google/go-cmp/cmp"
23
+ "go.uber.org/multierr"
23
24
24
25
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
25
26
"k8s.io/apimachinery/pkg/util/validation/field"
@@ -440,12 +441,52 @@ func TestCompatibility(t *testing.T) {
440
441
},
441
442
},
442
443
},
444
+ }, {
445
+ desc : "existing has properties, new has neither properties or additionalProperties" ,
446
+ existing : & apiextensionsv1.JSONSchemaProps {
447
+ Type : "object" ,
448
+ Properties : map [string ]apiextensionsv1.JSONSchemaProps {
449
+ "existing" : {Type : "boolean" },
450
+ },
451
+ },
452
+ new : & apiextensionsv1.JSONSchemaProps {
453
+ Type : "array" ,
454
+ Items : & apiextensionsv1.JSONSchemaPropsOrArray {
455
+ Schema : & apiextensionsv1.JSONSchemaProps {
456
+ Type : "object" ,
457
+ Properties : map [string ]apiextensionsv1.JSONSchemaProps {
458
+ "existing" : {Type : "integer" },
459
+ },
460
+ },
461
+ },
462
+ },
463
+ wantErr : multierr .Append (
464
+ field .Invalid (
465
+ field .NewPath ("schema" , "openAPISchema" ).Child ("type" ),
466
+ "array" ,
467
+ `The type changed (was "object", now "array")` ,
468
+ ),
469
+ field .Invalid (
470
+ field .NewPath ("schema" , "openAPISchema" ).Child ("properties" ),
471
+ []string {"existing" },
472
+ "properties value has been completely cleared in an incompatible way" ,
473
+ ),
474
+ ),
443
475
}} {
444
476
t .Run (c .desc , func (t * testing.T ) {
445
477
gotLCD , err := EnsureStructuralSchemaCompatibility (field .NewPath ("schema" , "openAPISchema" ), c .existing , c .new , c .narrowExisting )
446
- if d := cmp .Diff (c .wantErr , err ); d != "" {
447
- t .Errorf ("Error Diff(-want,+got): %s" , d )
478
+ if c .wantErr != nil {
479
+ if err == nil {
480
+ t .Fatalf ("expected err %v but got nil" , c .wantErr )
481
+ }
482
+
483
+ if d := cmp .Diff (c .wantErr .Error (), err .Error ()); d != "" {
484
+ t .Errorf ("Error Diff(-want,+got): %s" , d )
485
+ }
486
+ } else if err != nil {
487
+ t .Fatalf ("unexpected err %v" , err )
448
488
}
489
+
449
490
if d := cmp .Diff (c .wantLCD , gotLCD ); d != "" {
450
491
t .Errorf ("LCD Diff(-want,+got): %s" , d )
451
492
}
0 commit comments