@@ -9,6 +9,10 @@ import (
9
9
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10
10
"k8s.io/client-go/openapi"
11
11
"k8s.io/kube-openapi/pkg/validation/spec"
12
+
13
+ apischemaMocks "github.com/openmfp/kubernetes-graphql-gateway/listener/apischema/mocks"
14
+ kcpMocks "github.com/openmfp/kubernetes-graphql-gateway/listener/kcp/mocks"
15
+ "github.com/stretchr/testify/assert"
12
16
)
13
17
14
18
type fakeGV struct {
@@ -228,30 +232,37 @@ func TestGetSchemaForPath(t *testing.T) {
228
232
name : "invalid_path" ,
229
233
preferred : []string {"g/v1" },
230
234
path : "noSlash" ,
231
- gv : fakeGV {} ,
235
+ gv : apischemaMocks . NewMockGroupVersion ( t ) ,
232
236
wantErr : ErrInvalidPath ,
233
237
},
234
238
{
235
239
name : "not_preferred" ,
236
240
preferred : []string {"x/y" },
237
241
path : "/g/v1" ,
238
- gv : fakeGV {} ,
242
+ gv : apischemaMocks . NewMockGroupVersion ( t ) ,
239
243
wantErr : ErrNotPreferred ,
240
244
},
241
245
{
242
246
name : "unmarshal error" ,
243
247
preferred : []string {"g/v1" },
244
248
path : "/g/v1" ,
245
- gv : fakeGV {data : []byte ("bad json" ), err : nil },
246
- wantErr : ErrUnmarshalSchemaForPath ,
249
+ gv : func () openapi.GroupVersion {
250
+ mock := apischemaMocks .NewMockGroupVersion (t )
251
+ mock .EXPECT ().Schema ("application/json" ).Return ([]byte ("bad json" ), nil )
252
+ return mock
253
+ }(),
254
+ wantErr : ErrUnmarshalSchemaForPath ,
247
255
},
248
256
{
249
257
name : "success" ,
250
258
preferred : []string {"g/v1" },
251
259
path : "/g/v1" ,
252
- gv : fakeGV {data : validJSON , err : nil },
253
- wantErr : nil ,
254
- wantCount : len (validSchemas ),
260
+ gv : func () openapi.GroupVersion {
261
+ mock := apischemaMocks .NewMockGroupVersion (t )
262
+ mock .EXPECT ().Schema ("application/json" ).Return (validJSON , nil )
263
+ return mock
264
+ }(),
265
+ wantCount : 1 ,
255
266
},
256
267
}
257
268
@@ -268,30 +279,42 @@ func TestGetSchemaForPath(t *testing.T) {
268
279
t .Fatalf ("unexpected error: %v" , err )
269
280
}
270
281
if len (got ) != tc .wantCount {
271
- t .Errorf ("schema count: got %d, want %d" , len (got ), tc .wantCount )
282
+ t .Fatalf ("schema count: got %d, want %d" , len (got ), tc .wantCount )
272
283
}
273
284
})
274
285
}
275
286
}
276
287
277
- // TestResolveSchema tests the resolveSchema function. It checks if the function correctly
278
- // resolves the schema for a given path and handles various error cases.
288
+ // TestResolveSchema tests the resolveSchema function. It checks if the function
289
+ // correctly resolves the schema for a given CRD and handles various error cases.
279
290
func TestResolveSchema (t * testing.T ) {
291
+ // prepare a valid schemaResponse JSON
292
+ validSchemas := map [string ]* spec.Schema {"a.v1.K" : {}}
293
+ resp := schemaResponse {Components : schemasComponentsWrapper {Schemas : validSchemas }}
294
+ validJSON , err := json .Marshal (& resp )
295
+ if err != nil {
296
+ t .Fatalf ("failed to marshal valid response: %v" , err )
297
+ }
298
+
280
299
tests := []struct {
281
300
name string
282
301
preferredResources []* metav1.APIResourceList
283
302
err error
284
- openAPIPaths map [ string ]openapi. GroupVersion
303
+ openAPIPath string
285
304
openAPIErr error
286
- wantErr bool
305
+ wantErr error
306
+ setSchema func (mock openapi.GroupVersion )
287
307
}{
288
308
{
289
- name : "discovery error" ,
290
- err : ErrGetServerPreferred ,
291
- wantErr : true ,
309
+ name : "discovery_error" ,
310
+ err : ErrGetServerPreferred ,
311
+ openAPIPath : "/api/v1" ,
312
+ openAPIErr : nil ,
313
+ wantErr : ErrGetServerPreferred ,
314
+ setSchema : nil ,
292
315
},
293
316
{
294
- name : "successful_schema_resolution " ,
317
+ name : "successful_resolution " ,
295
318
preferredResources : []* metav1.APIResourceList {
296
319
{
297
320
GroupVersion : "v1" ,
@@ -304,40 +327,46 @@ func TestResolveSchema(t *testing.T) {
304
327
},
305
328
},
306
329
},
307
- openAPIPaths : map [string ]openapi.GroupVersion {
308
- "/api/v1" : fakeGV {},
309
- },
310
- wantErr : false ,
311
- },
312
- {
313
- name : "empty resources list" ,
314
- preferredResources : []* metav1.APIResourceList {},
315
- openAPIPaths : map [string ]openapi.GroupVersion {
316
- "/api/v1" : fakeGV {},
330
+ openAPIPath : "/v1" ,
331
+ openAPIErr : nil ,
332
+ wantErr : nil ,
333
+ setSchema : func (mock openapi.GroupVersion ) {
334
+ mock .(* apischemaMocks.MockGroupVersion ).EXPECT ().Schema ("application/json" ).Return (validJSON , nil )
317
335
},
318
- wantErr : false ,
319
336
},
320
337
}
321
338
322
- for _ , tt := range tests {
323
- t .Run (tt .name , func (t * testing.T ) {
324
- resolver := & MockCRDResolver {
325
- CRDResolver : & CRDResolver {},
326
- preferredResources : tt .preferredResources ,
327
- err : tt .err ,
328
- openAPIClient : & mockOpenAPIClient {
329
- paths : tt .openAPIPaths ,
330
- err : tt .openAPIErr ,
331
- },
339
+ for _ , tc := range tests {
340
+ t .Run (tc .name , func (t * testing.T ) {
341
+ dc := kcpMocks .NewMockDiscoveryInterface (t )
342
+ rm := kcpMocks .NewMockRESTMapper (t )
343
+
344
+ // First call in resolveSchema
345
+ dc .EXPECT ().ServerPreferredResources ().Return (tc .preferredResources , tc .err )
346
+
347
+ if tc .err == nil {
348
+ mockGV := apischemaMocks .NewMockGroupVersion (t )
349
+ if tc .setSchema != nil {
350
+ tc .setSchema (mockGV )
351
+ }
352
+ openAPIPaths := map [string ]openapi.GroupVersion {
353
+ tc .openAPIPath : mockGV ,
354
+ }
355
+ openAPIClient := apischemaMocks .NewMockClient (t )
356
+ openAPIClient .EXPECT ().Paths ().Return (openAPIPaths , tc .openAPIErr )
357
+ dc .EXPECT ().OpenAPIV3 ().Return (openAPIClient )
332
358
}
333
359
334
- got , err := resolveSchema (resolver , resolver )
335
- if ( err != nil ) != tt . wantErr {
336
- t . Errorf ( "resolveSchema() error = %v, wantErr %v" , err , tt .wantErr )
360
+ got , err := resolveSchema (dc , rm )
361
+ if tc . wantErr != nil {
362
+ assert . ErrorIs ( t , err , tc .wantErr )
337
363
return
338
364
}
339
- if ! tt .wantErr && got == nil {
340
- t .Error ("resolveSchema() returned nil result when no error expected" )
365
+ if err != nil {
366
+ t .Fatalf ("unexpected error: %v" , err )
367
+ }
368
+ if len (got ) == 0 {
369
+ t .Fatal ("expected non-empty schema map" )
341
370
}
342
371
})
343
372
}
0 commit comments