1
1
package apischema
2
2
3
3
import (
4
- "encoding/json"
5
4
"errors"
6
5
"testing"
7
6
8
7
"github.com/openmfp/kubernetes-graphql-gateway/common"
8
+ apischemaMocks "github.com/openmfp/kubernetes-graphql-gateway/listener/apischema/mocks"
9
9
"github.com/stretchr/testify/assert"
10
10
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
11
11
"k8s.io/apimachinery/pkg/api/meta"
@@ -15,20 +15,6 @@ import (
15
15
"k8s.io/kube-openapi/pkg/validation/spec"
16
16
)
17
17
18
- type fakeClient struct {
19
- paths map [string ]openapi.GroupVersion
20
- }
21
-
22
- type fakeErrClient struct {}
23
-
24
- func (f * fakeClient ) Paths () (map [string ]openapi.GroupVersion , error ) {
25
- return f .paths , nil
26
- }
27
-
28
- func (f * fakeErrClient ) Paths () (map [string ]openapi.GroupVersion , error ) {
29
- return nil , errors .New ("fail Paths" )
30
- }
31
-
32
18
// TestGetOpenAPISchemaKey tests the getOpenAPISchemaKey function. It checks if the
33
19
// function correctly formats the GroupVersionKind into the expected schema key format.
34
20
func TestGetOpenAPISchemaKey (t * testing.T ) {
@@ -103,33 +89,52 @@ func TestGetCRDGroupVersionKind(t *testing.T) {
103
89
// and the expected schema key.
104
90
func TestNewSchemaBuilder (t * testing.T ) {
105
91
tests := []struct {
106
- name string
107
- client openapi.Client
108
- wantLen int
109
- wantKey string
110
- wantError bool
92
+ name string
93
+ client openapi.Client
94
+ wantErr error
95
+ wantLen int
96
+ wantKey string
111
97
}{
112
98
{
113
99
name : "populates_schemas" ,
114
- client : & fakeClient {paths : map [string ]openapi.GroupVersion {"/X/v1" : fakeGV {data : func () []byte {
115
- d , _ := json .Marshal (& schemaResponse {
116
- Components : schemasComponentsWrapper {Schemas : map [string ]* spec.Schema {"X.v1.K" : {}}}})
117
- return d
118
- }(), err : nil }}},
100
+ client : func () openapi.Client {
101
+ mock := apischemaMocks .NewMockClient (t )
102
+ mockGV := apischemaMocks .NewMockGroupVersion (t )
103
+ paths := map [string ]openapi.GroupVersion {
104
+ "/v1" : mockGV ,
105
+ }
106
+ mock .EXPECT ().Paths ().Return (paths , nil )
107
+ mockGV .EXPECT ().Schema ("application/json" ).Return ([]byte (`{
108
+ "components": {
109
+ "schemas": {
110
+ "v1.Pod": {
111
+ "type": "object",
112
+ "x-kubernetes-group-version-kind": [{"group": "", "kind": "Pod", "version": "v1"}]
113
+ }
114
+ }
115
+ }
116
+ }` ), nil )
117
+ return mock
118
+ }(),
119
+ wantErr : nil ,
119
120
wantLen : 1 ,
120
- wantKey : "X. v1.K " ,
121
+ wantKey : "v1.Pod " ,
121
122
},
122
123
{
123
- name : "error_on_Paths" ,
124
- client : & fakeErrClient {},
125
- wantLen : 0 ,
126
- wantError : true ,
124
+ name : "error_on_Paths" ,
125
+ client : func () openapi.Client {
126
+ mock := apischemaMocks .NewMockClient (t )
127
+ mock .EXPECT ().Paths ().Return (nil , errors .New ("paths error" ))
128
+ return mock
129
+ }(),
130
+ wantErr : ErrGetOpenAPIPaths ,
127
131
},
128
132
}
133
+
129
134
for _ , tc := range tests {
130
135
t .Run (tc .name , func (t * testing.T ) {
131
- b := NewSchemaBuilder (tc .client , []string {"X/ v1" })
132
- if tc .wantError {
136
+ b := NewSchemaBuilder (tc .client , []string {"v1" })
137
+ if tc .wantErr != nil {
133
138
assert .NotNil (t , b .err , "expected error, got nil" )
134
139
assert .Equal (t , 0 , len (b .schemas ), "expected 0 schemas on error" )
135
140
return
@@ -144,7 +149,8 @@ func TestNewSchemaBuilder(t *testing.T) {
144
149
}
145
150
146
151
// TestWithCRDCategories tests the WithCRDCategories method
147
- // for the SchemaBuilder struct. It checks if the categories are correctly added to the schema's extensions.
152
+ // for the SchemaBuilder struct. It checks if the categories are correctly added
153
+ // to the schema's extensions.
148
154
func TestWithCRDCategories (t * testing.T ) {
149
155
tests := []struct {
150
156
name string
@@ -190,14 +196,10 @@ func TestWithCRDCategories(t *testing.T) {
190
196
b .WithCRDCategories (tc .crd )
191
197
ext , found := b .schemas [tc .key ].VendorExtensible .Extensions [common .CategoriesExtensionKey ]
192
198
if tc .wantCats == nil {
193
- if found {
194
- t .Errorf ("expected no categories, but found: %#v" , ext )
195
- }
199
+ assert .False (t , found , "expected no categories" )
196
200
return
197
201
}
198
- if ! found {
199
- t .Fatal ("expected CategoriesExtensionKey to be set" )
200
- }
202
+ assert .True (t , found , "expected CategoriesExtensionKey to be set" )
201
203
cats , ok := ext .([]string )
202
204
assert .True (t , ok , "categories should be []string" )
203
205
assert .Equal (t , tc .wantCats , cats , "categories mismatch" )
@@ -206,7 +208,8 @@ func TestWithCRDCategories(t *testing.T) {
206
208
}
207
209
208
210
// TestWithApiResourceCategories tests the WithApiResourceCategories method
209
- // for the SchemaBuilder struct. It checks if the categories are correctly added to the schema's extensions.
211
+ // for the SchemaBuilder struct. It checks if the categories are correctly added
212
+ // to the schema's extensions.
210
213
func TestWithApiResourceCategories (t * testing.T ) {
211
214
tests := []struct {
212
215
name string
@@ -241,14 +244,10 @@ func TestWithApiResourceCategories(t *testing.T) {
241
244
b .WithApiResourceCategories (tc .list )
242
245
ext , found := b .schemas [tc .key ].VendorExtensible .Extensions [common .CategoriesExtensionKey ]
243
246
if tc .wantCats == nil {
244
- if found {
245
- t .Errorf ("expected no categories, but found: %#v" , ext )
246
- }
247
+ assert .False (t , found , "expected no categories" )
247
248
return
248
249
}
249
- if ! found {
250
- t .Fatal ("expected CategoriesExtensionKey to be set by WithApiResourceCategories" )
251
- }
250
+ assert .True (t , found , "expected CategoriesExtensionKey to be set" )
252
251
cats , ok := ext .([]string )
253
252
assert .True (t , ok , "categories should be []string" )
254
253
assert .Equal (t , tc .wantCats , cats , "categories mismatch" )
0 commit comments