|
5 | 5 | "fmt"
|
6 | 6 | "testing"
|
7 | 7 |
|
| 8 | + "github.com/stretchr/testify/assert" |
8 | 9 | "github.com/stretchr/testify/require"
|
9 | 10 | apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
10 | 11 | "k8s.io/utils/ptr"
|
@@ -983,3 +984,182 @@ func TestOrderKappsValidateErr(t *testing.T) {
|
983 | 984 | })
|
984 | 985 | }
|
985 | 986 | }
|
| 987 | + |
| 988 | +func TestServedVersionValidator(t *testing.T) { |
| 989 | + validationErr1 := errors.New(`version "v1alpha1", field "^" has unknown change, refusing to determine that change is safe`) |
| 990 | + validationErr2 := errors.New(`version upgrade "v1alpha1" to "v1alpha2", field "^": fail`) |
| 991 | + |
| 992 | + for _, tc := range []struct { |
| 993 | + name string |
| 994 | + servedVersionValidator *ServedVersionValidator |
| 995 | + new apiextensionsv1.CustomResourceDefinition |
| 996 | + expectedError error |
| 997 | + }{ |
| 998 | + { |
| 999 | + name: "no changes, no error", |
| 1000 | + servedVersionValidator: &ServedVersionValidator{ |
| 1001 | + Validations: []ChangeValidation{ |
| 1002 | + func(_ FieldDiff) (bool, error) { |
| 1003 | + return false, errors.New("should not run") |
| 1004 | + }, |
| 1005 | + }, |
| 1006 | + }, |
| 1007 | + new: apiextensionsv1.CustomResourceDefinition{ |
| 1008 | + Spec: apiextensionsv1.CustomResourceDefinitionSpec{ |
| 1009 | + Versions: []apiextensionsv1.CustomResourceDefinitionVersion{ |
| 1010 | + { |
| 1011 | + Name: "v1alpha1", |
| 1012 | + Served: true, |
| 1013 | + Schema: &apiextensionsv1.CustomResourceValidation{ |
| 1014 | + OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{}, |
| 1015 | + }, |
| 1016 | + }, |
| 1017 | + }, |
| 1018 | + }, |
| 1019 | + }, |
| 1020 | + }, |
| 1021 | + { |
| 1022 | + name: "changes, validation successful, change is fully handled, no error", |
| 1023 | + servedVersionValidator: &ServedVersionValidator{ |
| 1024 | + Validations: []ChangeValidation{ |
| 1025 | + func(_ FieldDiff) (bool, error) { |
| 1026 | + return true, nil |
| 1027 | + }, |
| 1028 | + }, |
| 1029 | + }, |
| 1030 | + new: apiextensionsv1.CustomResourceDefinition{ |
| 1031 | + Spec: apiextensionsv1.CustomResourceDefinitionSpec{ |
| 1032 | + Versions: []apiextensionsv1.CustomResourceDefinitionVersion{ |
| 1033 | + { |
| 1034 | + Name: "v1alpha1", |
| 1035 | + Served: true, |
| 1036 | + Schema: &apiextensionsv1.CustomResourceValidation{ |
| 1037 | + OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{}, |
| 1038 | + }, |
| 1039 | + }, |
| 1040 | + { |
| 1041 | + Name: "v1alpha2", |
| 1042 | + Served: true, |
| 1043 | + Schema: &apiextensionsv1.CustomResourceValidation{ |
| 1044 | + OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{ |
| 1045 | + ID: "foo", |
| 1046 | + }, |
| 1047 | + }, |
| 1048 | + }, |
| 1049 | + }, |
| 1050 | + }, |
| 1051 | + }, |
| 1052 | + }, |
| 1053 | + { |
| 1054 | + name: "changes, validation successful, change not fully handled, error", |
| 1055 | + servedVersionValidator: &ServedVersionValidator{ |
| 1056 | + Validations: []ChangeValidation{ |
| 1057 | + func(_ FieldDiff) (bool, error) { |
| 1058 | + return false, nil |
| 1059 | + }, |
| 1060 | + }, |
| 1061 | + }, |
| 1062 | + new: apiextensionsv1.CustomResourceDefinition{ |
| 1063 | + Spec: apiextensionsv1.CustomResourceDefinitionSpec{ |
| 1064 | + Versions: []apiextensionsv1.CustomResourceDefinitionVersion{ |
| 1065 | + { |
| 1066 | + Name: "v1alpha1", |
| 1067 | + Served: true, |
| 1068 | + Schema: &apiextensionsv1.CustomResourceValidation{ |
| 1069 | + OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{}, |
| 1070 | + }, |
| 1071 | + }, |
| 1072 | + { |
| 1073 | + Name: "v1alpha2", |
| 1074 | + Served: true, |
| 1075 | + Schema: &apiextensionsv1.CustomResourceValidation{ |
| 1076 | + OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{ |
| 1077 | + ID: "foo", |
| 1078 | + }, |
| 1079 | + }, |
| 1080 | + }, |
| 1081 | + }, |
| 1082 | + }, |
| 1083 | + }, |
| 1084 | + expectedError: validationErr1, |
| 1085 | + }, |
| 1086 | + { |
| 1087 | + name: "changes, validation failed, change fully handled, error", |
| 1088 | + servedVersionValidator: &ServedVersionValidator{ |
| 1089 | + Validations: []ChangeValidation{ |
| 1090 | + func(_ FieldDiff) (bool, error) { |
| 1091 | + return true, errors.New("fail") |
| 1092 | + }, |
| 1093 | + }, |
| 1094 | + }, |
| 1095 | + new: apiextensionsv1.CustomResourceDefinition{ |
| 1096 | + Spec: apiextensionsv1.CustomResourceDefinitionSpec{ |
| 1097 | + Versions: []apiextensionsv1.CustomResourceDefinitionVersion{ |
| 1098 | + { |
| 1099 | + Name: "v1alpha1", |
| 1100 | + Served: true, |
| 1101 | + Schema: &apiextensionsv1.CustomResourceValidation{ |
| 1102 | + OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{}, |
| 1103 | + }, |
| 1104 | + }, |
| 1105 | + { |
| 1106 | + Name: "v1alpha2", |
| 1107 | + Served: true, |
| 1108 | + Schema: &apiextensionsv1.CustomResourceValidation{ |
| 1109 | + OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{ |
| 1110 | + ID: "foo", |
| 1111 | + }, |
| 1112 | + }, |
| 1113 | + }, |
| 1114 | + }, |
| 1115 | + }, |
| 1116 | + }, |
| 1117 | + expectedError: validationErr2, |
| 1118 | + }, |
| 1119 | + { |
| 1120 | + name: "changes, validation failed, change not fully handled, ordered error", |
| 1121 | + servedVersionValidator: &ServedVersionValidator{ |
| 1122 | + Validations: []ChangeValidation{ |
| 1123 | + func(_ FieldDiff) (bool, error) { |
| 1124 | + return false, errors.New("fail") |
| 1125 | + }, |
| 1126 | + func(_ FieldDiff) (bool, error) { |
| 1127 | + return false, errors.New("error") |
| 1128 | + }, |
| 1129 | + }, |
| 1130 | + }, |
| 1131 | + new: apiextensionsv1.CustomResourceDefinition{ |
| 1132 | + Spec: apiextensionsv1.CustomResourceDefinitionSpec{ |
| 1133 | + Versions: []apiextensionsv1.CustomResourceDefinitionVersion{ |
| 1134 | + { |
| 1135 | + Name: "v1alpha1", |
| 1136 | + Served: true, |
| 1137 | + Schema: &apiextensionsv1.CustomResourceValidation{ |
| 1138 | + OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{}, |
| 1139 | + }, |
| 1140 | + }, |
| 1141 | + { |
| 1142 | + Name: "v1alpha2", |
| 1143 | + Served: true, |
| 1144 | + Schema: &apiextensionsv1.CustomResourceValidation{ |
| 1145 | + OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{ |
| 1146 | + ID: "foo", |
| 1147 | + }, |
| 1148 | + }, |
| 1149 | + }, |
| 1150 | + }, |
| 1151 | + }, |
| 1152 | + }, |
| 1153 | + expectedError: fmt.Errorf("%w\n%s\n%w", validationErr2, `version upgrade "v1alpha1" to "v1alpha2", field "^": error`, validationErr1), |
| 1154 | + }, |
| 1155 | + } { |
| 1156 | + t.Run(tc.name, func(t *testing.T) { |
| 1157 | + err := tc.servedVersionValidator.Validate(apiextensionsv1.CustomResourceDefinition{}, tc.new) |
| 1158 | + if tc.expectedError != nil { |
| 1159 | + assert.EqualError(t, err, tc.expectedError.Error()) |
| 1160 | + } else { |
| 1161 | + assert.NoError(t, err) |
| 1162 | + } |
| 1163 | + }) |
| 1164 | + } |
| 1165 | +} |
0 commit comments