Skip to content

Commit e066519

Browse files
committed
fix: added mockery
1 parent 3d80b6f commit e066519

File tree

8 files changed

+944
-102
lines changed

8 files changed

+944
-102
lines changed

.mockery.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ packages:
99
interfaces:
1010
DiscoveryInterface:
1111

12+
k8s.io/apimachinery/pkg/api/meta:
13+
config:
14+
dir: listener/kcp/mocks
15+
outpkg: mocks
16+
interfaces:
17+
RESTMapper:
18+
1219
sigs.k8s.io/controller-runtime/pkg/client:
1320
config:
1421
dir: gateway/resolver/mocks
@@ -51,3 +58,18 @@ packages:
5158
outpkg: mocks
5259
interfaces:
5360
Resolver:
61+
62+
github.com/openmfp/kubernetes-graphql-gateway/listener/controller:
63+
config:
64+
dir: listener/controller/mocks
65+
outpkg: mocks
66+
interfaces:
67+
CRDResolver:
68+
69+
k8s.io/client-go/openapi:
70+
config:
71+
dir: listener/apischema/mocks
72+
outpkg: mocks
73+
interfaces:
74+
GroupVersion:
75+
Client:

listener/apischema/crd_resolver_test.go

Lines changed: 71 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import (
99
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1010
"k8s.io/client-go/openapi"
1111
"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"
1216
)
1317

1418
type fakeGV struct {
@@ -228,30 +232,37 @@ func TestGetSchemaForPath(t *testing.T) {
228232
name: "invalid_path",
229233
preferred: []string{"g/v1"},
230234
path: "noSlash",
231-
gv: fakeGV{},
235+
gv: apischemaMocks.NewMockGroupVersion(t),
232236
wantErr: ErrInvalidPath,
233237
},
234238
{
235239
name: "not_preferred",
236240
preferred: []string{"x/y"},
237241
path: "/g/v1",
238-
gv: fakeGV{},
242+
gv: apischemaMocks.NewMockGroupVersion(t),
239243
wantErr: ErrNotPreferred,
240244
},
241245
{
242246
name: "unmarshal error",
243247
preferred: []string{"g/v1"},
244248
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,
247255
},
248256
{
249257
name: "success",
250258
preferred: []string{"g/v1"},
251259
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,
255266
},
256267
}
257268

@@ -268,30 +279,42 @@ func TestGetSchemaForPath(t *testing.T) {
268279
t.Fatalf("unexpected error: %v", err)
269280
}
270281
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)
272283
}
273284
})
274285
}
275286
}
276287

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.
279290
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+
280299
tests := []struct {
281300
name string
282301
preferredResources []*metav1.APIResourceList
283302
err error
284-
openAPIPaths map[string]openapi.GroupVersion
303+
openAPIPath string
285304
openAPIErr error
286-
wantErr bool
305+
wantErr error
306+
setSchema func(mock openapi.GroupVersion)
287307
}{
288308
{
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,
292315
},
293316
{
294-
name: "successful_schema_resolution",
317+
name: "successful_resolution",
295318
preferredResources: []*metav1.APIResourceList{
296319
{
297320
GroupVersion: "v1",
@@ -304,40 +327,46 @@ func TestResolveSchema(t *testing.T) {
304327
},
305328
},
306329
},
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)
317335
},
318-
wantErr: false,
319336
},
320337
}
321338

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)
332358
}
333359

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)
337363
return
338364
}
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")
341370
}
342371
})
343372
}

listener/apischema/mocks/mock_Client.go

Lines changed: 92 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)