@@ -18,19 +18,22 @@ package apiutil_test
18
18
19
19
import (
20
20
"context"
21
+ "fmt"
21
22
"net/http"
22
23
"testing"
23
24
24
- "k8s.io/apimachinery/pkg/api/meta"
25
-
26
25
_ "github.com/onsi/ginkgo/v2"
27
26
gmg "github.com/onsi/gomega"
27
+ "github.com/onsi/gomega/format"
28
+ gomegatypes "github.com/onsi/gomega/types"
28
29
29
30
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
31
+ "k8s.io/apimachinery/pkg/api/meta"
30
32
"k8s.io/apimachinery/pkg/runtime/schema"
31
33
"k8s.io/apimachinery/pkg/types"
32
34
"k8s.io/client-go/kubernetes/scheme"
33
35
"k8s.io/client-go/rest"
36
+
34
37
"sigs.k8s.io/controller-runtime/pkg/client"
35
38
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
36
39
"sigs.k8s.io/controller-runtime/pkg/envtest"
@@ -303,6 +306,10 @@ func TestLazyRestMapperProvider(t *testing.T) {
303
306
lazyRestMapper , err := apiutil .NewDynamicRESTMapper (restCfg , httpClient )
304
307
g .Expect (err ).NotTo (gmg .HaveOccurred ())
305
308
309
+ // A version is specified but the group doesn't exist.
310
+ // For each group, we expect 1 call to the version-specific discovery endpoint:
311
+ // #1: GET https://host/apis/<group>/<version>
312
+
306
313
_ , err = lazyRestMapper .RESTMapping (schema.GroupKind {Group : "INVALID1" }, "v1" )
307
314
g .Expect (err ).To (gmg .HaveOccurred ())
308
315
g .Expect (meta .IsNoMatchError (err )).To (gmg .BeTrue ())
@@ -332,6 +339,35 @@ func TestLazyRestMapperProvider(t *testing.T) {
332
339
g .Expect (err ).To (gmg .HaveOccurred ())
333
340
g .Expect (meta .IsNoMatchError (err )).To (gmg .BeTrue ())
334
341
g .Expect (crt .GetRequestCount ()).To (gmg .Equal (6 ))
342
+
343
+ // No version is specified but the group doesn't exist.
344
+ // For each group, we expect 2 calls to discover all group versions:
345
+ // #1: GET https://host/api
346
+ // #2: GET https://host/apis
347
+
348
+ _ , err = lazyRestMapper .RESTMapping (schema.GroupKind {Group : "INVALID7" })
349
+ g .Expect (err ).To (beNoMatchError ())
350
+ g .Expect (crt .GetRequestCount ()).To (gmg .Equal (8 ))
351
+
352
+ _ , err = lazyRestMapper .RESTMappings (schema.GroupKind {Group : "INVALID8" })
353
+ g .Expect (err ).To (beNoMatchError ())
354
+ g .Expect (crt .GetRequestCount ()).To (gmg .Equal (10 ))
355
+
356
+ _ , err = lazyRestMapper .KindFor (schema.GroupVersionResource {Group : "INVALID9" })
357
+ g .Expect (err ).To (beNoMatchError ())
358
+ g .Expect (crt .GetRequestCount ()).To (gmg .Equal (12 ))
359
+
360
+ _ , err = lazyRestMapper .KindsFor (schema.GroupVersionResource {Group : "INVALID10" })
361
+ g .Expect (err ).To (beNoMatchError ())
362
+ g .Expect (crt .GetRequestCount ()).To (gmg .Equal (14 ))
363
+
364
+ _ , err = lazyRestMapper .ResourceFor (schema.GroupVersionResource {Group : "INVALID11" })
365
+ g .Expect (err ).To (beNoMatchError ())
366
+ g .Expect (crt .GetRequestCount ()).To (gmg .Equal (16 ))
367
+
368
+ _ , err = lazyRestMapper .ResourcesFor (schema.GroupVersionResource {Group : "INVALID12" })
369
+ g .Expect (err ).To (beNoMatchError ())
370
+ g .Expect (crt .GetRequestCount ()).To (gmg .Equal (18 ))
335
371
})
336
372
337
373
t .Run ("LazyRESTMapper should return an error if a resource doesn't exist" , func (t * testing.T ) {
@@ -529,3 +565,35 @@ func TestLazyRestMapperProvider(t *testing.T) {
529
565
g .Expect (mapping .GroupVersionKind .Kind ).To (gmg .Equal ("rider" ))
530
566
})
531
567
}
568
+
569
+ func beNoMatchError () gomegatypes.GomegaMatcher {
570
+ return & errorMatcher {
571
+ checkFunc : meta .IsNoMatchError ,
572
+ message : "NoMatch" ,
573
+ }
574
+ }
575
+
576
+ type errorMatcher struct {
577
+ checkFunc func (error ) bool
578
+ message string
579
+ }
580
+
581
+ func (e * errorMatcher ) Match (actual interface {}) (success bool , err error ) {
582
+ if actual == nil {
583
+ return false , nil
584
+ }
585
+
586
+ actualErr , actualOk := actual .(error )
587
+ if ! actualOk {
588
+ return false , fmt .Errorf ("expected an error-type. got:\n %s" , format .Object (actual , 1 ))
589
+ }
590
+
591
+ return e .checkFunc (actualErr ), nil
592
+ }
593
+
594
+ func (e * errorMatcher ) FailureMessage (actual interface {}) (message string ) {
595
+ return format .Message (actual , fmt .Sprintf ("to be %s error" , e .message ))
596
+ }
597
+ func (e * errorMatcher ) NegatedFailureMessage (actual interface {}) (message string ) {
598
+ return format .Message (actual , fmt .Sprintf ("not to be %s error" , e .message ))
599
+ }
0 commit comments