1- package apiutil_test
1+ /*
2+ Copyright 2021 The Kubernetes Authors.
3+
4+ Licensed under the Apache License, Version 2.0 (the "License");
5+ you may not use this file except in compliance with the License.
6+ You may obtain a copy of the License at
7+
8+ http://www.apache.org/licenses/LICENSE-2.0
9+
10+ Unless required by applicable law or agreed to in writing, software
11+ distributed under the License is distributed on an "AS IS" BASIS,
12+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ See the License for the specific language governing permissions and
14+ limitations under the License.
15+ */
16+
17+ package apiutil
218
319import (
20+ "fmt"
421 "time"
522
623 . "github.com/onsi/ginkgo"
@@ -10,8 +27,6 @@ import (
1027 "golang.org/x/time/rate"
1128 "k8s.io/apimachinery/pkg/api/meta"
1229 "k8s.io/apimachinery/pkg/runtime/schema"
13-
14- "sigs.k8s.io/controller-runtime/pkg/client/apiutil"
1530)
1631
1732var (
@@ -36,7 +51,7 @@ var _ = Describe("Dynamic REST Mapper", func() {
3651 }
3752
3853 lim = rate .NewLimiter (rate .Limit (5 ), 5 )
39- mapper , err = apiutil . NewDynamicRESTMapper (cfg , apiutil . WithLimiter (lim ), apiutil . WithCustomMapper (func () (meta.RESTMapper , error ) {
54+ mapper , err = NewDynamicRESTMapper (cfg , WithLimiter (lim ), WithCustomMapper (func () (meta.RESTMapper , error ) {
4055 baseMapper := meta .NewDefaultRESTMapper (nil )
4156 addToMapper (baseMapper )
4257
@@ -130,11 +145,28 @@ var _ = Describe("Dynamic REST Mapper", func() {
130145 By ("ensuring that it was only refreshed once" )
131146 Expect (count ).To (Equal (1 ))
132147 })
133- }
134148
135- PIt ("should lazily initialize if the lazy option is used" , func () {
136-
137- })
149+ It ("should lazily initialize if the lazy option is used" , func () {
150+ var err error
151+ var failedOnce bool
152+ mockErr := fmt .Errorf ("mock failed once" )
153+ mapper , err = NewDynamicRESTMapper (cfg , WithLazyDiscovery , WithCustomMapper (func () (meta.RESTMapper , error ) {
154+ // Make newMapper fail once
155+ if ! failedOnce {
156+ failedOnce = true
157+ return nil , mockErr
158+ }
159+ baseMapper := meta .NewDefaultRESTMapper (nil )
160+ addToMapper (baseMapper )
161+ return baseMapper , nil
162+ }))
163+ Expect (err ).NotTo (HaveOccurred ())
164+ Expect (mapper .(* dynamicRESTMapper ).staticMapper ).To (BeNil ())
165+
166+ Expect (callWithTarget ()).To (MatchError (mockErr ))
167+ Expect (callWithTarget ()).To (Succeed ())
168+ })
169+ }
138170
139171 Describe ("KindFor" , func () {
140172 mapperTest (func () error {
0 commit comments