@@ -2,10 +2,8 @@ package crdupgradesafety
2
2
3
3
import (
4
4
"errors"
5
- "fmt"
6
5
"testing"
7
6
8
- "github.com/stretchr/testify/assert"
9
7
"github.com/stretchr/testify/require"
10
8
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
11
9
"k8s.io/utils/ptr"
@@ -906,182 +904,3 @@ func TestType(t *testing.T) {
906
904
})
907
905
}
908
906
}
909
-
910
- func TestServedVersionValidator (t * testing.T ) {
911
- validationErr1 := errors .New (`version "v1alpha1", field "^" has unknown change, refusing to determine that change is safe` )
912
- validationErr2 := errors .New (`version upgrade "v1alpha1" to "v1alpha2", field "^": fail` )
913
-
914
- for _ , tc := range []struct {
915
- name string
916
- servedVersionValidator * ServedVersionValidator
917
- new apiextensionsv1.CustomResourceDefinition
918
- expectedError error
919
- }{
920
- {
921
- name : "no changes, no error" ,
922
- servedVersionValidator : & ServedVersionValidator {
923
- Validations : []ChangeValidation {
924
- func (_ FieldDiff ) (bool , error ) {
925
- return false , errors .New ("should not run" )
926
- },
927
- },
928
- },
929
- new : apiextensionsv1.CustomResourceDefinition {
930
- Spec : apiextensionsv1.CustomResourceDefinitionSpec {
931
- Versions : []apiextensionsv1.CustomResourceDefinitionVersion {
932
- {
933
- Name : "v1alpha1" ,
934
- Served : true ,
935
- Schema : & apiextensionsv1.CustomResourceValidation {
936
- OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {},
937
- },
938
- },
939
- },
940
- },
941
- },
942
- },
943
- {
944
- name : "changes, validation successful, change is fully handled, no error" ,
945
- servedVersionValidator : & ServedVersionValidator {
946
- Validations : []ChangeValidation {
947
- func (_ FieldDiff ) (bool , error ) {
948
- return true , nil
949
- },
950
- },
951
- },
952
- new : apiextensionsv1.CustomResourceDefinition {
953
- Spec : apiextensionsv1.CustomResourceDefinitionSpec {
954
- Versions : []apiextensionsv1.CustomResourceDefinitionVersion {
955
- {
956
- Name : "v1alpha1" ,
957
- Served : true ,
958
- Schema : & apiextensionsv1.CustomResourceValidation {
959
- OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {},
960
- },
961
- },
962
- {
963
- Name : "v1alpha2" ,
964
- Served : true ,
965
- Schema : & apiextensionsv1.CustomResourceValidation {
966
- OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {
967
- ID : "foo" ,
968
- },
969
- },
970
- },
971
- },
972
- },
973
- },
974
- },
975
- {
976
- name : "changes, validation successful, change not fully handled, error" ,
977
- servedVersionValidator : & ServedVersionValidator {
978
- Validations : []ChangeValidation {
979
- func (_ FieldDiff ) (bool , error ) {
980
- return false , nil
981
- },
982
- },
983
- },
984
- new : apiextensionsv1.CustomResourceDefinition {
985
- Spec : apiextensionsv1.CustomResourceDefinitionSpec {
986
- Versions : []apiextensionsv1.CustomResourceDefinitionVersion {
987
- {
988
- Name : "v1alpha1" ,
989
- Served : true ,
990
- Schema : & apiextensionsv1.CustomResourceValidation {
991
- OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {},
992
- },
993
- },
994
- {
995
- Name : "v1alpha2" ,
996
- Served : true ,
997
- Schema : & apiextensionsv1.CustomResourceValidation {
998
- OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {
999
- ID : "foo" ,
1000
- },
1001
- },
1002
- },
1003
- },
1004
- },
1005
- },
1006
- expectedError : validationErr1 ,
1007
- },
1008
- {
1009
- name : "changes, validation failed, change fully handled, error" ,
1010
- servedVersionValidator : & ServedVersionValidator {
1011
- Validations : []ChangeValidation {
1012
- func (_ FieldDiff ) (bool , error ) {
1013
- return true , errors .New ("fail" )
1014
- },
1015
- },
1016
- },
1017
- new : apiextensionsv1.CustomResourceDefinition {
1018
- Spec : apiextensionsv1.CustomResourceDefinitionSpec {
1019
- Versions : []apiextensionsv1.CustomResourceDefinitionVersion {
1020
- {
1021
- Name : "v1alpha1" ,
1022
- Served : true ,
1023
- Schema : & apiextensionsv1.CustomResourceValidation {
1024
- OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {},
1025
- },
1026
- },
1027
- {
1028
- Name : "v1alpha2" ,
1029
- Served : true ,
1030
- Schema : & apiextensionsv1.CustomResourceValidation {
1031
- OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {
1032
- ID : "foo" ,
1033
- },
1034
- },
1035
- },
1036
- },
1037
- },
1038
- },
1039
- expectedError : validationErr2 ,
1040
- },
1041
- {
1042
- name : "changes, validation failed, change not fully handled, ordered error" ,
1043
- servedVersionValidator : & ServedVersionValidator {
1044
- Validations : []ChangeValidation {
1045
- func (_ FieldDiff ) (bool , error ) {
1046
- return false , errors .New ("fail" )
1047
- },
1048
- func (_ FieldDiff ) (bool , error ) {
1049
- return false , errors .New ("error" )
1050
- },
1051
- },
1052
- },
1053
- new : apiextensionsv1.CustomResourceDefinition {
1054
- Spec : apiextensionsv1.CustomResourceDefinitionSpec {
1055
- Versions : []apiextensionsv1.CustomResourceDefinitionVersion {
1056
- {
1057
- Name : "v1alpha1" ,
1058
- Served : true ,
1059
- Schema : & apiextensionsv1.CustomResourceValidation {
1060
- OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {},
1061
- },
1062
- },
1063
- {
1064
- Name : "v1alpha2" ,
1065
- Served : true ,
1066
- Schema : & apiextensionsv1.CustomResourceValidation {
1067
- OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {
1068
- ID : "foo" ,
1069
- },
1070
- },
1071
- },
1072
- },
1073
- },
1074
- },
1075
- expectedError : fmt .Errorf ("%w\n %s\n %w" , validationErr2 , `version upgrade "v1alpha1" to "v1alpha2", field "^": error` , validationErr1 ),
1076
- },
1077
- } {
1078
- t .Run (tc .name , func (t * testing.T ) {
1079
- err := tc .servedVersionValidator .Validate (apiextensionsv1.CustomResourceDefinition {}, tc .new )
1080
- if tc .expectedError != nil {
1081
- assert .EqualError (t , err , tc .expectedError .Error ())
1082
- } else {
1083
- assert .NoError (t , err )
1084
- }
1085
- })
1086
- }
1087
- }
0 commit comments