Skip to content

Commit f640bd9

Browse files
committed
fix: changed package name for tests and updated test files
1 parent 759eefa commit f640bd9

File tree

5 files changed

+115
-49
lines changed

5 files changed

+115
-49
lines changed

listener/apischema/builder_test.go

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
package apischema
1+
package apischema_test
22

33
import (
44
"errors"
55
"testing"
66

77
"github.com/openmfp/kubernetes-graphql-gateway/common"
8+
apischema "github.com/openmfp/kubernetes-graphql-gateway/listener/apischema"
89
apischemaMocks "github.com/openmfp/kubernetes-graphql-gateway/listener/apischema/mocks"
910
"github.com/stretchr/testify/assert"
1011
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
@@ -33,7 +34,7 @@ func TestGetOpenAPISchemaKey(t *testing.T) {
3334
}
3435

3536
for _, tc := range tests {
36-
got := getOpenAPISchemaKey(tc.gvk)
37+
got := apischema.GetOpenAPISchemaKey(tc.gvk)
3738
assert.Equal(t, tc.want, got, "getOpenAPISchemaKey(%+v) result mismatch", tc.gvk)
3839
}
3940
}
@@ -68,13 +69,13 @@ func TestGetCRDGroupVersionKind(t *testing.T) {
6869
Names: apiextensionsv1.CustomResourceDefinitionNames{Kind: "Bar"},
6970
},
7071
want: nil,
71-
wantErr: ErrCRDNoVersions,
72+
wantErr: apischema.ErrCRDNoVersions,
7273
},
7374
}
7475

7576
for _, tc := range tests {
7677
t.Run(tc.name, func(t *testing.T) {
77-
got, err := getCRDGroupVersionKind(tc.spec)
78+
got, err := apischema.GetCRDGroupVersionKind(tc.spec)
7879
assert.Equal(t, tc.wantErr, err, "error value mismatch")
7980
if tc.wantErr != nil {
8081
return
@@ -127,21 +128,21 @@ func TestNewSchemaBuilder(t *testing.T) {
127128
mock.EXPECT().Paths().Return(nil, errors.New("paths error"))
128129
return mock
129130
}(),
130-
wantErr: ErrGetOpenAPIPaths,
131+
wantErr: apischema.ErrGetOpenAPIPaths,
131132
},
132133
}
133134

134135
for _, tc := range tests {
135136
t.Run(tc.name, func(t *testing.T) {
136-
b := NewSchemaBuilder(tc.client, []string{"v1"})
137+
b := apischema.NewSchemaBuilder(tc.client, []string{"v1"})
137138
if tc.wantErr != nil {
138-
assert.NotNil(t, b.err, "expected error, got nil")
139-
assert.Equal(t, 0, len(b.schemas), "expected 0 schemas on error")
139+
assert.NotNil(t, b.GetError(), "expected error, got nil")
140+
assert.Equal(t, 0, len(b.GetSchemas()), "expected 0 schemas on error")
140141
return
141142
}
142-
assert.Equal(t, tc.wantLen, len(b.schemas), "schema count mismatch")
143+
assert.Equal(t, tc.wantLen, len(b.GetSchemas()), "schema count mismatch")
143144
if tc.wantKey != "" {
144-
_, ok := b.schemas[tc.wantKey]
145+
_, ok := b.GetSchemas()[tc.wantKey]
145146
assert.True(t, ok, "schema key %s not found in builder.schemas", tc.wantKey)
146147
}
147148
})
@@ -190,11 +191,14 @@ func TestWithCRDCategories(t *testing.T) {
190191
}
191192
for _, tc := range tests {
192193
t.Run(tc.name, func(t *testing.T) {
193-
b := &SchemaBuilder{schemas: map[string]*spec.Schema{
194+
mock := apischemaMocks.NewMockClient(t)
195+
mock.EXPECT().Paths().Return(map[string]openapi.GroupVersion{}, nil)
196+
b := apischema.NewSchemaBuilder(mock, nil)
197+
b.SetSchemas(map[string]*spec.Schema{
194198
tc.key: {VendorExtensible: spec.VendorExtensible{Extensions: map[string]interface{}{}}},
195-
}}
199+
})
196200
b.WithCRDCategories(tc.crd)
197-
ext, found := b.schemas[tc.key].VendorExtensible.Extensions[common.CategoriesExtensionKey]
201+
ext, found := b.GetSchemas()[tc.key].VendorExtensible.Extensions[common.CategoriesExtensionKey]
198202
if tc.wantCats == nil {
199203
assert.False(t, found, "expected no categories")
200204
return
@@ -239,11 +243,14 @@ func TestWithApiResourceCategories(t *testing.T) {
239243
}
240244
for _, tc := range tests {
241245
t.Run(tc.name, func(t *testing.T) {
242-
b := &SchemaBuilder{schemas: map[string]*spec.Schema{
246+
mock := apischemaMocks.NewMockClient(t)
247+
mock.EXPECT().Paths().Return(map[string]openapi.GroupVersion{}, nil)
248+
b := apischema.NewSchemaBuilder(mock, nil)
249+
b.SetSchemas(map[string]*spec.Schema{
243250
tc.key: {VendorExtensible: spec.VendorExtensible{Extensions: map[string]interface{}{}}},
244-
}}
251+
})
245252
b.WithApiResourceCategories(tc.list)
246-
ext, found := b.schemas[tc.key].VendorExtensible.Extensions[common.CategoriesExtensionKey]
253+
ext, found := b.GetSchemas()[tc.key].VendorExtensible.Extensions[common.CategoriesExtensionKey]
247254
if tc.wantCats == nil {
248255
assert.False(t, found, "expected no categories")
249256
return
@@ -272,11 +279,12 @@ func TestWithScope(t *testing.T) {
272279
},
273280
}
274281

275-
b := &SchemaBuilder{
276-
schemas: map[string]*spec.Schema{
277-
"g.v1.K": s,
278-
},
279-
}
282+
mock := apischemaMocks.NewMockClient(t)
283+
mock.EXPECT().Paths().Return(map[string]openapi.GroupVersion{}, nil)
284+
b := apischema.NewSchemaBuilder(mock, nil)
285+
b.SetSchemas(map[string]*spec.Schema{
286+
"g.v1.K": s,
287+
})
280288

281289
// Create RESTMapper and mark GVK as namespaced
282290
mapper := meta.NewDefaultRESTMapper([]schema.GroupVersion{gvk.GroupVersion()})
@@ -285,6 +293,6 @@ func TestWithScope(t *testing.T) {
285293
b.WithScope(mapper)
286294

287295
// Validate
288-
scope := b.schemas["g.v1.K"].VendorExtensible.Extensions[common.ScopeExtensionKey]
296+
scope := b.GetSchemas()["g.v1.K"].VendorExtensible.Extensions[common.ScopeExtensionKey]
289297
assert.Equal(t, apiextensionsv1.NamespaceScoped, scope, "scope value mismatch")
290298
}

listener/apischema/crd_resolver_test.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package apischema
1+
package apischema_test
22

33
import (
44
"encoding/json"
@@ -9,6 +9,7 @@ import (
99
"k8s.io/client-go/openapi"
1010
"k8s.io/kube-openapi/pkg/validation/spec"
1111

12+
apischema "github.com/openmfp/kubernetes-graphql-gateway/listener/apischema"
1213
apischemaMocks "github.com/openmfp/kubernetes-graphql-gateway/listener/apischema/mocks"
1314
kcpMocks "github.com/openmfp/kubernetes-graphql-gateway/listener/kcp/mocks"
1415
"github.com/stretchr/testify/assert"
@@ -58,7 +59,7 @@ func TestGetCRDGroupKindVersions(t *testing.T) {
5859
}
5960
for _, tc := range tests {
6061
t.Run(tc.name, func(t *testing.T) {
61-
gkv := getCRDGroupKindVersions(tc.spec)
62+
gkv := apischema.GetCRDGroupKindVersions(tc.spec)
6263
assert.Equal(t, tc.wantG, gkv.Group, "Group mismatch")
6364
assert.Equal(t, tc.wantKind, gkv.Kind, "Kind mismatch")
6465
assert.Equal(t, len(tc.wantVers), len(gkv.Versions), "Versions length mismatch")
@@ -72,13 +73,13 @@ func TestGetCRDGroupKindVersions(t *testing.T) {
7273
func TestIsCRDKindIncluded(t *testing.T) {
7374
tests := []struct {
7475
name string
75-
gkv *GroupKindVersions
76+
gkv *apischema.GroupKindVersions
7677
apiList *metav1.APIResourceList
7778
want bool
7879
}{
7980
{
8081
name: "kind_present",
81-
gkv: &GroupKindVersions{
82+
gkv: &apischema.GroupKindVersions{
8283
GroupKind: &metav1.GroupKind{
8384
Group: "g",
8485
Kind: "KindA",
@@ -96,7 +97,7 @@ func TestIsCRDKindIncluded(t *testing.T) {
9697
},
9798
{
9899
name: "kind_absent",
99-
gkv: &GroupKindVersions{
100+
gkv: &apischema.GroupKindVersions{
100101
GroupKind: &metav1.GroupKind{
101102
Group: "g",
102103
Kind: "KindA",
@@ -114,7 +115,7 @@ func TestIsCRDKindIncluded(t *testing.T) {
114115
}
115116
for _, tc := range tests {
116117
t.Run(tc.name, func(t *testing.T) {
117-
got := isCRDKindIncluded(tc.gkv, tc.apiList)
118+
got := apischema.IsCRDKindIncluded(tc.gkv, tc.apiList)
118119
assert.Equal(t, tc.want, got, "isCRDKindIncluded result mismatch")
119120
})
120121
}
@@ -123,7 +124,7 @@ func TestIsCRDKindIncluded(t *testing.T) {
123124
// TestErrorIfCRDNotInPreferredApiGroups tests the errorIfCRDNotInPreferredApiGroups function.
124125
// It checks if the function correctly identifies if a CRD is not in the preferred API groups.
125126
func TestErrorIfCRDNotInPreferredApiGroups(t *testing.T) {
126-
gkv := &GroupKindVersions{
127+
gkv := &apischema.GroupKindVersions{
127128
GroupKind: &metav1.GroupKind{Group: "g", Kind: "K"},
128129
Versions: []string{"v1", "v2"},
129130
}
@@ -158,13 +159,13 @@ func TestErrorIfCRDNotInPreferredApiGroups(t *testing.T) {
158159
},
159160
},
160161
},
161-
wantErr: ErrGVKNotPreferred,
162+
wantErr: apischema.ErrGVKNotPreferred,
162163
},
163164
}
164165

165166
for _, tc := range cases {
166167
t.Run(tc.name, func(t *testing.T) {
167-
groups, err := errorIfCRDNotInPreferredApiGroups(gkv, tc.lists)
168+
groups, err := apischema.ErrorIfCRDNotInPreferredApiGroups(gkv, tc.lists)
168169
if tc.wantErr != nil {
169170
assert.ErrorIs(t, err, tc.wantErr)
170171
return
@@ -181,7 +182,7 @@ func TestErrorIfCRDNotInPreferredApiGroups(t *testing.T) {
181182
func TestGetSchemaForPath(t *testing.T) {
182183
// prepare a valid schemaResponse JSON
183184
validSchemas := map[string]*spec.Schema{"a.v1.K": {}}
184-
resp := schemaResponse{Components: schemasComponentsWrapper{Schemas: validSchemas}}
185+
resp := apischema.SchemaResponse{Components: apischema.SchemasComponentsWrapper{Schemas: validSchemas}}
185186
validJSON, err := json.Marshal(&resp)
186187
assert.NoError(t, err, "failed to marshal valid response")
187188

@@ -198,14 +199,14 @@ func TestGetSchemaForPath(t *testing.T) {
198199
preferred: []string{"g/v1"},
199200
path: "noSlash",
200201
gv: apischemaMocks.NewMockGroupVersion(t),
201-
wantErr: ErrInvalidPath,
202+
wantErr: apischema.ErrInvalidPath,
202203
},
203204
{
204205
name: "not_preferred",
205206
preferred: []string{"x/y"},
206207
path: "/g/v1",
207208
gv: apischemaMocks.NewMockGroupVersion(t),
208-
wantErr: ErrNotPreferred,
209+
wantErr: apischema.ErrNotPreferred,
209210
},
210211
{
211212
name: "unmarshal_error",
@@ -216,7 +217,7 @@ func TestGetSchemaForPath(t *testing.T) {
216217
mock.EXPECT().Schema("application/json").Return([]byte("bad json"), nil)
217218
return mock
218219
}(),
219-
wantErr: ErrUnmarshalSchemaForPath,
220+
wantErr: apischema.ErrUnmarshalSchemaForPath,
220221
},
221222
{
222223
name: "success",
@@ -233,7 +234,7 @@ func TestGetSchemaForPath(t *testing.T) {
233234

234235
for _, tc := range tests {
235236
t.Run(tc.name, func(t *testing.T) {
236-
got, err := getSchemaForPath(tc.preferred, tc.path, tc.gv)
237+
got, err := apischema.GetSchemaForPath(tc.preferred, tc.path, tc.gv)
237238
if tc.wantErr != nil {
238239
assert.ErrorIs(t, err, tc.wantErr)
239240
return
@@ -249,7 +250,7 @@ func TestGetSchemaForPath(t *testing.T) {
249250
func TestResolveSchema(t *testing.T) {
250251
// prepare a valid schemaResponse JSON
251252
validSchemas := map[string]*spec.Schema{"a.v1.K": {}}
252-
resp := schemaResponse{Components: schemasComponentsWrapper{Schemas: validSchemas}}
253+
resp := apischema.SchemaResponse{Components: apischema.SchemasComponentsWrapper{Schemas: validSchemas}}
253254
validJSON, err := json.Marshal(&resp)
254255
assert.NoError(t, err, "failed to marshal valid response")
255256

@@ -264,10 +265,10 @@ func TestResolveSchema(t *testing.T) {
264265
}{
265266
{
266267
name: "discovery_error",
267-
err: ErrGetServerPreferred,
268+
err: apischema.ErrGetServerPreferred,
268269
openAPIPath: "/api/v1",
269270
openAPIErr: nil,
270-
wantErr: ErrGetServerPreferred,
271+
wantErr: apischema.ErrGetServerPreferred,
271272
setSchema: nil,
272273
},
273274
{
@@ -317,7 +318,7 @@ func TestResolveSchema(t *testing.T) {
317318
dc.EXPECT().OpenAPIV3().Return(openAPIClient)
318319
}
319320

320-
got, err := resolveSchema(dc, rm)
321+
got, err := apischema.ResolveSchema(dc, rm)
321322
if tc.wantErr != nil {
322323
assert.ErrorIs(t, err, tc.wantErr)
323324
return

listener/apischema/export_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package apischema
2+
3+
import (
4+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
5+
"k8s.io/apimachinery/pkg/api/meta"
6+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
7+
"k8s.io/client-go/discovery"
8+
"k8s.io/client-go/openapi"
9+
"k8s.io/kube-openapi/pkg/validation/spec"
10+
)
11+
12+
func GetCRDGroupKindVersions(spec apiextensionsv1.CustomResourceDefinitionSpec) *GroupKindVersions {
13+
return getCRDGroupKindVersions(spec)
14+
}
15+
16+
func IsCRDKindIncluded(gkv *GroupKindVersions, apiList *metav1.APIResourceList) bool {
17+
return isCRDKindIncluded(gkv, apiList)
18+
}
19+
20+
func ErrorIfCRDNotInPreferredApiGroups(gkv *GroupKindVersions, lists []*metav1.APIResourceList) ([]string, error) {
21+
return errorIfCRDNotInPreferredApiGroups(gkv, lists)
22+
}
23+
24+
func GetSchemaForPath(preferred []string, path string, gv openapi.GroupVersion) (map[string]*spec.Schema, error) {
25+
return getSchemaForPath(preferred, path, gv)
26+
}
27+
28+
func ResolveSchema(dc discovery.DiscoveryInterface, rm meta.RESTMapper) ([]byte, error) {
29+
return resolveSchema(dc, rm)
30+
}
31+
32+
func GetOpenAPISchemaKey(gvk metav1.GroupVersionKind) string {
33+
return getOpenAPISchemaKey(gvk)
34+
}
35+
36+
func GetCRDGroupVersionKind(spec apiextensionsv1.CustomResourceDefinitionSpec) (*metav1.GroupVersionKind, error) {
37+
return getCRDGroupVersionKind(spec)
38+
}
39+
40+
type (
41+
SchemaResponse = schemaResponse
42+
SchemasComponentsWrapper = schemasComponentsWrapper
43+
)
44+
45+
func (b *SchemaBuilder) GetSchemas() map[string]*spec.Schema {
46+
return b.schemas
47+
}
48+
49+
func (b *SchemaBuilder) GetError() error {
50+
return b.err
51+
}
52+
53+
func (b *SchemaBuilder) SetSchemas(schemas map[string]*spec.Schema) {
54+
b.schemas = schemas
55+
}

listener/apischema/json_converter_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
package apischema
1+
package apischema_test
22

33
import (
44
"encoding/json"
55
"testing"
66

7+
"github.com/openmfp/kubernetes-graphql-gateway/listener/apischema"
78
"github.com/stretchr/testify/assert"
89
)
910

1011
func TestConvertJSON_InvalidInput(t *testing.T) {
11-
_, err := ConvertJSON([]byte("not a json"))
12-
assert.ErrorIs(t, err, ErrUnmarshalJSON)
12+
_, err := apischema.ConvertJSON([]byte("not a json"))
13+
assert.ErrorIs(t, err, apischema.ErrUnmarshalJSON)
1314
}
1415

1516
func TestConvertJSON_Transforms(t *testing.T) {
@@ -42,7 +43,7 @@ func TestConvertJSON_Transforms(t *testing.T) {
4243
}
4344
}`
4445

45-
out, err := ConvertJSON(input)
46+
out, err := apischema.ConvertJSON(input)
4647
assert.NoError(t, err)
4748

4849
var got, want map[string]any

0 commit comments

Comments
 (0)