@@ -17,7 +17,6 @@ package crds_test
1717import (
1818 "bufio"
1919 "bytes"
20- "fmt"
2120 "testing"
2221
2322 "github.com/stretchr/testify/assert"
@@ -108,173 +107,3 @@ func TestSelectVersion(t *testing.T) {
108107 })
109108 }
110109}
111-
112- func TestGetOpenAPIProperties (t * testing.T ) {
113- const kind = "MyTestCRD"
114-
115- happyPathVersion := & apiextensionsv1.CustomResourceDefinitionVersion {
116- Name : "v1" ,
117- Schema : & apiextensionsv1.CustomResourceValidation {
118- OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {
119- Properties : map [string ]apiextensionsv1.JSONSchemaProps {
120- "spec" : {
121- Type : "object" ,
122- Properties : map [string ]apiextensionsv1.JSONSchemaProps {
123- "fieldA" : {Type : "string" },
124- },
125- },
126- },
127- },
128- },
129- }
130-
131- testCases := []struct {
132- name string
133- version * apiextensionsv1.CustomResourceDefinitionVersion
134- wantProperties map [string ]apiextensionsv1.JSONSchemaProps
135- wantErrMsg string
136- }{
137- {
138- name : "should return properties" ,
139- version : happyPathVersion ,
140- wantProperties : happyPathVersion .Schema .OpenAPIV3Schema .Properties ,
141- wantErrMsg : "" ,
142- },
143- {
144- name : "should return error if version is nil" ,
145- version : nil ,
146- wantProperties : nil ,
147- wantErrMsg : fmt .Sprintf ("missing version (nil) from %v spec" , kind ),
148- },
149- {
150- name : "should return error if schema is nil" ,
151- version : & apiextensionsv1.CustomResourceDefinitionVersion {
152- Name : "v1" ,
153- Schema : nil , // The point of failure
154- },
155- wantProperties : nil ,
156- wantErrMsg : fmt .Sprintf ("missing version schema from %v spec" , kind ),
157- },
158- {
159- name : "error - should return error if OpenAPIV3Schema is nil" ,
160- version : & apiextensionsv1.CustomResourceDefinitionVersion {
161- Name : "v1" ,
162- Schema : & apiextensionsv1.CustomResourceValidation {
163- OpenAPIV3Schema : nil , // The point of failure
164- },
165- },
166- wantProperties : nil ,
167- wantErrMsg : fmt .Sprintf ("missing version OpenAPI Schema from %v spec" , kind ),
168- },
169- {
170- name : "should return error if Properties map is nil" ,
171- version : & apiextensionsv1.CustomResourceDefinitionVersion {
172- Name : "v1" ,
173- Schema : & apiextensionsv1.CustomResourceValidation {
174- OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {
175- Properties : nil , // The point of failure
176- },
177- },
178- },
179- wantProperties : nil ,
180- wantErrMsg : fmt .Sprintf ("missing version OpenAPI Properties from %v spec" , kind ),
181- },
182- }
183-
184- for _ , tc := range testCases {
185- t .Run (tc .name , func (t * testing.T ) {
186- gotProperties , err := crds .GetOpenAPIProperties (kind , tc .version )
187- if tc .wantErrMsg != "" {
188- require .Error (t , err )
189- require .EqualError (t , err , tc .wantErrMsg )
190- require .Nil (t , gotProperties )
191- } else {
192- require .NoError (t , err )
193- require .Equal (t , tc .wantProperties , gotProperties )
194- }
195- })
196- }
197- }
198-
199- func TestGetSpecPropertiesFor (t * testing.T ) {
200- const kind = "MyTestCRD"
201-
202- // Reusable properties for the test cases
203- nestedProperties := map [string ]apiextensionsv1.JSONSchemaProps {
204- "replicas" : {Type : "integer" },
205- "image" : {Type : "string" },
206- }
207-
208- testCases := []struct {
209- name string
210- props map [string ]apiextensionsv1.JSONSchemaProps
211- field string
212- wantProperties map [string ]apiextensionsv1.JSONSchemaProps
213- wantErrMsg string
214- }{
215- {
216- name : "should return nested properties" ,
217- props : map [string ]apiextensionsv1.JSONSchemaProps {
218- "spec" : {
219- Type : "object" ,
220- Properties : nestedProperties ,
221- },
222- "status" : {
223- Type : "object" ,
224- },
225- },
226- field : "spec" ,
227- wantProperties : nestedProperties ,
228- wantErrMsg : "" , // Expect no error
229- },
230- {
231- name : "field is missing from properties map" ,
232- props : map [string ]apiextensionsv1.JSONSchemaProps {
233- "status" : {Type : "object" },
234- },
235- field : "spec" , // This field does not exist
236- wantProperties : nil ,
237- wantErrMsg : fmt .Sprintf ("kind %q spec is missing field %q on" , kind , "spec" ),
238- },
239- {
240- name : "field is not of type object" ,
241- props : map [string ]apiextensionsv1.JSONSchemaProps {
242- "spec" : {Type : "string" }, // The point of failure
243- },
244- field : "spec" ,
245- wantProperties : nil ,
246- wantErrMsg : fmt .Sprintf ("kind %q field %q expected to be object but is %v" , kind , "spec" , "string" ),
247- },
248- {
249- name : "field is an object but has no nested properties" ,
250- props : map [string ]apiextensionsv1.JSONSchemaProps {
251- "spec" : {
252- Type : "object" ,
253- Properties : nil , // The nested map is nil
254- },
255- },
256- field : "spec" ,
257- wantProperties : nil , // Expecting a nil map is correct here
258- wantErrMsg : "" ,
259- },
260- }
261-
262- for _ , tc := range testCases {
263- t .Run (tc .name , func (t * testing.T ) {
264- // Act
265- gotProperties , err := crds .GetSpecPropertiesFor (kind , tc .props , tc .field )
266-
267- // Assert
268- if tc .wantErrMsg != "" {
269- // We expect an error
270- require .Error (t , err )
271- require .EqualError (t , err , tc .wantErrMsg )
272- require .Nil (t , gotProperties )
273- } else {
274- // We expect success
275- require .NoError (t , err )
276- require .Equal (t , tc .wantProperties , gotProperties )
277- }
278- })
279- }
280- }
0 commit comments