7
7
8
8
"github.com/openmfp/kubernetes-graphql-gateway/common"
9
9
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
10
+ "k8s.io/apimachinery/pkg/api/meta"
10
11
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12
+ "k8s.io/apimachinery/pkg/runtime/schema"
11
13
"k8s.io/client-go/openapi"
12
14
"k8s.io/kube-openapi/pkg/validation/spec"
13
15
)
@@ -16,16 +18,32 @@ type fakeClient struct {
16
18
paths map [string ]openapi.GroupVersion
17
19
}
18
20
21
+ type fakeErrClient struct {}
22
+
23
+ type fakeRESTMapper struct {
24
+ isNamespaced bool
25
+ err error
26
+ }
27
+
19
28
func (f * fakeClient ) Paths () (map [string ]openapi.GroupVersion , error ) {
20
29
return f .paths , nil
21
30
}
22
31
23
- type fakeErrClient struct {}
24
-
25
32
func (f * fakeErrClient ) Paths () (map [string ]openapi.GroupVersion , error ) {
26
33
return nil , errors .New ("fail Paths" )
27
34
}
28
35
36
+ func (f * fakeRESTMapper ) RESTMapping (gk metav1.GroupKind , versions ... string ) (* meta.RESTMapping , error ) {
37
+ if f .err != nil {
38
+ return nil , f .err
39
+ }
40
+ scope := meta .RESTScopeRoot
41
+ if f .isNamespaced {
42
+ scope = meta .RESTScopeNamespace
43
+ }
44
+ return & meta.RESTMapping {Scope : scope }, nil
45
+ }
46
+
29
47
// TestGetOpenAPISchemaKey tests the getOpenAPISchemaKey function. It checks if the
30
48
// function correctly formats the GroupVersionKind into the expected schema key format.
31
49
func TestGetOpenAPISchemaKey (t * testing.T ) {
@@ -269,3 +287,40 @@ func TestWithApiResourceCategories(t *testing.T) {
269
287
})
270
288
}
271
289
}
290
+
291
+ // TestWithScope tests the WithScope method for the SchemaBuilder struct.
292
+ func TestWithScope (t * testing.T ) {
293
+ gvk := schema.GroupVersionKind {Group : "g" , Version : "v1" , Kind : "K" }
294
+
295
+ // Create schema with GVK extension
296
+ s := & spec.Schema {
297
+ VendorExtensible : spec.VendorExtensible {
298
+ Extensions : map [string ]interface {}{
299
+ common .GVKExtensionKey : []map [string ]string {
300
+ {"group" : gvk .Group , "version" : gvk .Version , "kind" : gvk .Kind },
301
+ },
302
+ },
303
+ },
304
+ }
305
+
306
+ b := & SchemaBuilder {
307
+ schemas : map [string ]* spec.Schema {
308
+ "g.v1.K" : s ,
309
+ },
310
+ }
311
+
312
+ // Create RESTMapper and mark GVK as namespaced
313
+ mapper := meta .NewDefaultRESTMapper ([]schema.GroupVersion {gvk .GroupVersion ()})
314
+ mapper .Add (gvk , meta .RESTScopeNamespace )
315
+
316
+ b .WithScope (mapper )
317
+
318
+ // Validate
319
+ scope , ok := b .schemas ["g.v1.K" ].VendorExtensible .Extensions [common .ScopeExtensionKey ]
320
+ if ! ok {
321
+ t .Fatal ("expected ScopeExtensionKey to be set" )
322
+ }
323
+ if scope != apiextensionsv1 .NamespaceScoped {
324
+ t .Errorf ("expected NamespaceScoped, got %v" , scope )
325
+ }
326
+ }
0 commit comments