Skip to content

Commit e06131a

Browse files
authored
Merge pull request #34 from fanzhangio/travis
Add runtime test
2 parents 88b9161 + 036f37f commit e06131a

File tree

8 files changed

+554
-12
lines changed

8 files changed

+554
-12
lines changed

pkg/cache/informertest/fake_cache.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ func (c *FakeInformers) GetInformerForKind(gvk schema.GroupVersionKind) (toolsca
4343
if c.Scheme == nil {
4444
c.Scheme = scheme.Scheme
4545
}
46-
obj, _ := c.Scheme.New(gvk)
46+
obj, err := c.Scheme.New(gvk)
47+
if err != nil {
48+
return nil, err
49+
}
4750
return c.informerFor(gvk, obj)
4851
}
4952

@@ -52,7 +55,10 @@ func (c *FakeInformers) FakeInformerForKind(gvk schema.GroupVersionKind) (*contr
5255
if c.Scheme == nil {
5356
c.Scheme = scheme.Scheme
5457
}
55-
obj, _ := c.Scheme.New(gvk)
58+
obj, err := c.Scheme.New(gvk)
59+
if err != nil {
60+
return nil, err
61+
}
5662
i, err := c.informerFor(gvk, obj)
5763
if err != nil {
5864
return nil, err
@@ -65,7 +71,10 @@ func (c *FakeInformers) GetInformer(obj runtime.Object) (toolscache.SharedIndexI
6571
if c.Scheme == nil {
6672
c.Scheme = scheme.Scheme
6773
}
68-
gvks, _, _ := c.Scheme.ObjectKinds(obj)
74+
gvks, _, err := c.Scheme.ObjectKinds(obj)
75+
if err != nil {
76+
return nil, err
77+
}
6978
gvk := gvks[0]
7079
return c.informerFor(gvk, obj)
7180
}
@@ -83,7 +92,10 @@ func (c *FakeInformers) FakeInformerFor(obj runtime.Object) (*controllertest.Fak
8392
if c.Scheme == nil {
8493
c.Scheme = scheme.Scheme
8594
}
86-
gvks, _, _ := c.Scheme.ObjectKinds(obj)
95+
gvks, _, err := c.Scheme.ObjectKinds(obj)
96+
if err != nil {
97+
return nil, err
98+
}
8799
gvk := gvks[0]
88100
i, err := c.informerFor(gvk, obj)
89101
if err != nil {

pkg/manager/testutil.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ limitations under the License.
1616

1717
package manager
1818

19-
//
20-
//func SetCacheForTest(options *Options, c func(config *rest.Config, opts cache.Options) (cache.Cache, error)) {
21-
// options.newCache = c
22-
//}
23-
//
24-
//func SetClientForTest(options *Options, c func(config *rest.Config, options client.Options) (client.Client, error)) {
25-
// options.newClient = c
26-
//}
19+
// func SetCacheForTest(options *Options, c func(config *rest.Config, opts cache.Options) (cache.Cache, error)) {
20+
// options.newCache = c
21+
// }
22+
23+
// func SetClientForTest(options *Options, c func(config *rest.Config, options client.Options) (client.Client, error)) {
24+
// options.newClient = c
25+
// }
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Copyright 2018 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 inject
18+
19+
import (
20+
"testing"
21+
22+
. "github.com/onsi/ginkgo"
23+
. "github.com/onsi/gomega"
24+
"sigs.k8s.io/controller-runtime/pkg/test"
25+
)
26+
27+
func TestSource(t *testing.T) {
28+
RegisterFailHandler(Fail)
29+
RunSpecsWithDefaultAndCustomReporters(t, "Runtime Injection Suite", []Reporter{test.NewlineReporter{}})
30+
}

pkg/runtime/inject/inject_test.go

Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
1+
/*
2+
Copyright 2018 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 inject
18+
19+
import (
20+
"fmt"
21+
"reflect"
22+
23+
. "github.com/onsi/ginkgo"
24+
. "github.com/onsi/gomega"
25+
"k8s.io/apimachinery/pkg/runtime"
26+
"k8s.io/client-go/rest"
27+
"sigs.k8s.io/controller-runtime/pkg/cache"
28+
"sigs.k8s.io/controller-runtime/pkg/cache/informertest"
29+
"sigs.k8s.io/controller-runtime/pkg/client"
30+
)
31+
32+
var instance *testSource
33+
var uninjectable *failSource
34+
var errInjectFail = fmt.Errorf("injection fails")
35+
var expectedFalse = false
36+
37+
var _ = Describe("runtime inject", func() {
38+
39+
BeforeEach(func() {
40+
instance = &testSource{}
41+
uninjectable = &failSource{}
42+
})
43+
44+
It("should set informers", func() {
45+
injectedCache := &informertest.FakeInformers{}
46+
47+
By("Validating injecting the informer")
48+
res, err := CacheInto(injectedCache, instance)
49+
Expect(err).NotTo(HaveOccurred())
50+
Expect(res).To(Equal(true))
51+
Expect(injectedCache).To(Equal(instance.GetCache()))
52+
53+
By("Returing false if the type does not implement inject.Cache")
54+
res, err = CacheInto(injectedCache, uninjectable)
55+
Expect(err).NotTo(HaveOccurred())
56+
Expect(res).To(Equal(expectedFalse))
57+
Expect(uninjectable.GetCache()).To(BeNil())
58+
59+
By("Returning an error if informer injection fails")
60+
res, err = CacheInto(nil, instance)
61+
Expect(err).To(Equal(errInjectFail))
62+
Expect(res).To(Equal(true))
63+
64+
})
65+
66+
It("should set config", func() {
67+
68+
cfg := &rest.Config{}
69+
70+
By("Validating injecting config")
71+
res, err := ConfigInto(cfg, instance)
72+
Expect(err).NotTo(HaveOccurred())
73+
Expect(res).To(Equal(true))
74+
Expect(cfg).To(Equal(instance.GetConfig()))
75+
76+
By("Returning false if the type does not implement inject.Config")
77+
res, err = ConfigInto(cfg, uninjectable)
78+
Expect(err).NotTo(HaveOccurred())
79+
Expect(res).To(Equal(false))
80+
Expect(uninjectable.GetConfig()).To(BeNil())
81+
82+
By("Returning an error if config injection fails")
83+
res, err = ConfigInto(nil, instance)
84+
Expect(err).To(Equal(errInjectFail))
85+
Expect(res).To(Equal(true))
86+
})
87+
88+
It("should set client", func() {
89+
client := client.DelegatingClient{}
90+
91+
By("Validating injecting client")
92+
res, err := ClientInto(client, instance)
93+
Expect(err).NotTo(HaveOccurred())
94+
Expect(res).To(Equal(true))
95+
Expect(client).To(Equal(instance.GetClient()))
96+
97+
By("Returning false if the type does not implement inject.Client")
98+
res, err = ClientInto(client, uninjectable)
99+
Expect(err).NotTo(HaveOccurred())
100+
Expect(res).To(Equal(false))
101+
Expect(uninjectable.GetClient()).To(BeNil())
102+
103+
By("Returning an error if client injection fails")
104+
res, err = ClientInto(nil, instance)
105+
Expect(err).To(Equal(errInjectFail))
106+
Expect(res).To(Equal(true))
107+
})
108+
109+
It("should set scheme", func() {
110+
111+
scheme := runtime.NewScheme()
112+
113+
By("Validating injecting scheme")
114+
res, err := SchemeInto(scheme, instance)
115+
Expect(err).NotTo(HaveOccurred())
116+
Expect(res).To(Equal(true))
117+
Expect(scheme).To(Equal(instance.GetScheme()))
118+
119+
By("Returning false if the type does not implement inject.Scheme")
120+
res, err = SchemeInto(scheme, uninjectable)
121+
Expect(err).NotTo(HaveOccurred())
122+
Expect(res).To(Equal(false))
123+
Expect(uninjectable.GetScheme()).To(BeNil())
124+
125+
By("Returning an error if scheme injection fails")
126+
res, err = SchemeInto(nil, instance)
127+
Expect(err).To(Equal(errInjectFail))
128+
Expect(res).To(Equal(true))
129+
})
130+
131+
It("should set stop channel", func() {
132+
133+
stop := make(<-chan struct{})
134+
135+
By("Validating injecting stop channel")
136+
res, err := StopChannelInto(stop, instance)
137+
Expect(err).NotTo(HaveOccurred())
138+
Expect(res).To(Equal(true))
139+
Expect(stop).To(Equal(instance.GetStop()))
140+
141+
By("Returning false if the type does not implement inject.Stoppable")
142+
res, err = StopChannelInto(stop, uninjectable)
143+
Expect(err).NotTo(HaveOccurred())
144+
Expect(res).To(Equal(false))
145+
Expect(uninjectable.GetStop()).To(BeNil())
146+
147+
By("Returning an error if stop channel injection fails")
148+
res, err = StopChannelInto(nil, instance)
149+
Expect(err).To(Equal(errInjectFail))
150+
Expect(res).To(Equal(true))
151+
})
152+
153+
It("should set dependencies", func() {
154+
155+
f := func(interface{}) error { return nil }
156+
157+
By("Validating injecting dependencies")
158+
res, err := InjectorInto(f, instance)
159+
Expect(err).NotTo(HaveOccurred())
160+
Expect(res).To(Equal(true))
161+
Expect(reflect.ValueOf(f).Pointer()).To(Equal(reflect.ValueOf(instance.GetFunc()).Pointer()))
162+
163+
By("Returning false if the type does not implement inject.Injector")
164+
res, err = InjectorInto(f, uninjectable)
165+
Expect(err).NotTo(HaveOccurred())
166+
Expect(res).To(Equal(false))
167+
Expect(uninjectable.GetFunc()).To(BeNil())
168+
169+
By("Returning an error if dependencies injection fails")
170+
res, err = InjectorInto(nil, instance)
171+
Expect(err).To(Equal(errInjectFail))
172+
Expect(res).To(Equal(true))
173+
})
174+
175+
})
176+
177+
type testSource struct {
178+
scheme *runtime.Scheme
179+
cache cache.Cache
180+
config *rest.Config
181+
client client.Client
182+
f Func
183+
stop <-chan struct{}
184+
}
185+
186+
func (s *testSource) InjectCache(c cache.Cache) error {
187+
if c != nil {
188+
s.cache = c
189+
return nil
190+
}
191+
return fmt.Errorf("injection fails")
192+
}
193+
194+
func (s *testSource) InjectConfig(config *rest.Config) error {
195+
if config != nil {
196+
s.config = config
197+
return nil
198+
}
199+
return fmt.Errorf("injection fails")
200+
}
201+
202+
func (s *testSource) InjectClient(client client.Client) error {
203+
if client != nil {
204+
s.client = client
205+
return nil
206+
}
207+
return fmt.Errorf("injection fails")
208+
}
209+
210+
func (s *testSource) InjectScheme(scheme *runtime.Scheme) error {
211+
if scheme != nil {
212+
s.scheme = scheme
213+
return nil
214+
}
215+
return fmt.Errorf("injection fails")
216+
}
217+
218+
func (s *testSource) InjectStopChannel(stop <-chan struct{}) error {
219+
if stop != nil {
220+
s.stop = stop
221+
return nil
222+
}
223+
return fmt.Errorf("injection fails")
224+
}
225+
226+
func (s *testSource) InjectFunc(f Func) error {
227+
if f != nil {
228+
s.f = f
229+
return nil
230+
}
231+
return fmt.Errorf("injection fails")
232+
}
233+
234+
func (s *testSource) GetCache() cache.Cache {
235+
return s.cache
236+
}
237+
238+
func (s *testSource) GetConfig() *rest.Config {
239+
return s.config
240+
}
241+
242+
func (s *testSource) GetScheme() *runtime.Scheme {
243+
return s.scheme
244+
}
245+
246+
func (s *testSource) GetClient() client.Client {
247+
return s.client
248+
}
249+
250+
func (s *testSource) GetFunc() Func {
251+
return s.f
252+
}
253+
254+
func (s *testSource) GetStop() <-chan struct{} {
255+
return s.stop
256+
}
257+
258+
type failSource struct {
259+
scheme *runtime.Scheme
260+
cache cache.Cache
261+
config *rest.Config
262+
client client.Client
263+
f Func
264+
stop <-chan struct{}
265+
}
266+
267+
func (s *failSource) GetCache() cache.Cache {
268+
return s.cache
269+
}
270+
271+
func (s *failSource) GetConfig() *rest.Config {
272+
return s.config
273+
}
274+
275+
func (s *failSource) GetScheme() *runtime.Scheme {
276+
return s.scheme
277+
}
278+
279+
func (s *failSource) GetClient() client.Client {
280+
return s.client
281+
}
282+
283+
func (s *failSource) GetFunc() Func {
284+
return s.f
285+
}
286+
287+
func (s *failSource) GetStop() <-chan struct{} {
288+
return s.stop
289+
}

0 commit comments

Comments
 (0)