Skip to content

Commit ff6ff6d

Browse files
author
fanzhangio
committed
Add runtime test for inject, log and signal
1 parent bdb5f7c commit ff6ff6d

File tree

7 files changed

+410
-4
lines changed

7 files changed

+410
-4
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 {
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/kubernetes-sigs/controller-runtime/pkg/test"
23+
. "github.com/onsi/ginkgo"
24+
. "github.com/onsi/gomega"
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: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
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+
22+
"github.com/kubernetes-sigs/controller-runtime/pkg/cache"
23+
"github.com/kubernetes-sigs/controller-runtime/pkg/cache/informertest"
24+
"github.com/kubernetes-sigs/controller-runtime/pkg/client"
25+
. "github.com/onsi/ginkgo"
26+
. "github.com/onsi/gomega"
27+
"k8s.io/apimachinery/pkg/runtime"
28+
"k8s.io/client-go/rest"
29+
)
30+
31+
var instance testSource
32+
var neg struct{}
33+
var _ = Describe("runtime inject", func() {
34+
35+
It("should set informers", func() {
36+
instance := testSource{
37+
cache: func(c cache.Cache) error {
38+
if c != nil {
39+
return nil
40+
}
41+
return fmt.Errorf("")
42+
},
43+
}
44+
res, err := CacheInto(&informertest.FakeInformers{}, instance)
45+
Expect(err).NotTo(HaveOccurred())
46+
Expect(res).To(Equal(true))
47+
res, err = CacheInto(&informertest.FakeInformers{}, neg)
48+
Expect(err).NotTo(HaveOccurred())
49+
Expect(res).To(Equal(false))
50+
})
51+
52+
It("should set config", func() {
53+
instance := testSource{
54+
config: func(c *rest.Config) error {
55+
if c != nil {
56+
return nil
57+
}
58+
return fmt.Errorf("")
59+
},
60+
}
61+
cfg := &rest.Config{}
62+
res, err := ConfigInto(cfg, instance)
63+
Expect(err).NotTo(HaveOccurred())
64+
Expect(res).To(Equal(true))
65+
res, err = ConfigInto(cfg, neg)
66+
Expect(err).NotTo(HaveOccurred())
67+
Expect(res).To(Equal(false))
68+
})
69+
70+
It("should set client", func() {
71+
instance := testSource{
72+
client: func(c client.Client) error {
73+
if c != nil {
74+
return nil
75+
}
76+
return fmt.Errorf("")
77+
},
78+
}
79+
client := client.DelegatingClient{}
80+
res, err := ClientInto(client, instance)
81+
Expect(err).NotTo(HaveOccurred())
82+
Expect(res).To(Equal(true))
83+
res, err = ClientInto(client, neg)
84+
Expect(err).NotTo(HaveOccurred())
85+
Expect(res).To(Equal(false))
86+
})
87+
88+
It("should set scheme", func() {
89+
instance := testSource{
90+
scheme: func(c *runtime.Scheme) error {
91+
if c != nil {
92+
return nil
93+
}
94+
return fmt.Errorf("")
95+
},
96+
}
97+
scheme := runtime.NewScheme()
98+
res, err := SchemeInto(scheme, instance)
99+
Expect(err).NotTo(HaveOccurred())
100+
Expect(res).To(Equal(true))
101+
res, err = SchemeInto(scheme, neg)
102+
Expect(err).NotTo(HaveOccurred())
103+
Expect(res).To(Equal(false))
104+
})
105+
106+
It("should set stop channel", func() {
107+
instance := testSource{
108+
stop: func(c <-chan struct{}) error {
109+
if c != nil {
110+
return nil
111+
}
112+
return fmt.Errorf("")
113+
},
114+
}
115+
stop := make(chan struct{})
116+
res, err := StopChannelInto(stop, instance)
117+
Expect(err).NotTo(HaveOccurred())
118+
Expect(res).To(Equal(true))
119+
res, err = StopChannelInto(stop, neg)
120+
Expect(err).NotTo(HaveOccurred())
121+
Expect(res).To(Equal(false))
122+
})
123+
124+
It("should set stop channel", func() {
125+
instance := testSource{
126+
f: func(c Func) error {
127+
if c != nil {
128+
return nil
129+
}
130+
return fmt.Errorf("")
131+
},
132+
}
133+
f := func(interface{}) error { return nil }
134+
res, err := InjectorInto(f, instance)
135+
Expect(err).NotTo(HaveOccurred())
136+
Expect(res).To(Equal(true))
137+
res, err = InjectorInto(f, neg)
138+
Expect(err).NotTo(HaveOccurred())
139+
Expect(res).To(Equal(false))
140+
})
141+
142+
})
143+
144+
type testSource struct {
145+
scheme func(scheme *runtime.Scheme) error
146+
cache func(cache.Cache) error
147+
config func(config *rest.Config) error
148+
client func(client.Client) error
149+
f func(Func) error
150+
stop func(<-chan struct{}) error
151+
}
152+
153+
func (s testSource) InjectCache(c cache.Cache) error {
154+
if s.cache != nil {
155+
return s.cache(c)
156+
}
157+
return nil
158+
}
159+
160+
func (s testSource) InjectConfig(config *rest.Config) error {
161+
if s.config != nil {
162+
return s.config(config)
163+
}
164+
return nil
165+
}
166+
167+
func (s testSource) InjectClient(client client.Client) error {
168+
if s.client != nil {
169+
return s.client(client)
170+
}
171+
return nil
172+
}
173+
174+
func (s testSource) InjectScheme(scheme *runtime.Scheme) error {
175+
if s.scheme != nil {
176+
return s.scheme(scheme)
177+
}
178+
return nil
179+
}
180+
181+
func (s testSource) InjectStopChannel(stop <-chan struct{}) error {
182+
if s.stop != nil {
183+
return s.stop(stop)
184+
}
185+
return nil
186+
}
187+
188+
func (s testSource) InjectFunc(f Func) error {
189+
if s.f != nil {
190+
return s.f(f)
191+
}
192+
return nil
193+
}

pkg/runtime/log/log_suite_test.go

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 log
18+
19+
import (
20+
"testing"
21+
22+
"github.com/kubernetes-sigs/controller-runtime/pkg/test"
23+
. "github.com/onsi/ginkgo"
24+
. "github.com/onsi/gomega"
25+
)
26+
27+
func TestSource(t *testing.T) {
28+
RegisterFailHandler(Fail)
29+
RunSpecsWithDefaultAndCustomReporters(t, "Runtime Log Suite", []Reporter{test.NewlineReporter{}})
30+
}

pkg/runtime/log/log_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 log
18+
19+
import (
20+
. "github.com/onsi/ginkgo"
21+
. "github.com/onsi/gomega"
22+
tlogr "github.com/thockin/logr/testing"
23+
)
24+
25+
var _ = Describe("runtime log", func() {
26+
27+
Context("Test Logger", func() {
28+
It("shoud set and fulfill with logger", func() {
29+
logger := ZapLogger(false)
30+
Expect(logger).NotTo(BeNil())
31+
Log.WithName("runtimeLog").WithTags("newtag", "newvalue")
32+
SetLogger(logger)
33+
logger.WithName("runtimeLog").WithTags("newtag", "newvalue")
34+
Expect(Log.promise).To(BeNil())
35+
Expect(Log.Logger).To(Equal(logger))
36+
devLogger := ZapLogger(true)
37+
Expect(devLogger).NotTo(BeNil())
38+
})
39+
40+
It("should delegate with name", func() {
41+
var name = "NoPromise"
42+
test := tlogr.NullLogger{}
43+
test.WithName(name)
44+
Log = &DelegatingLogger{
45+
Logger: tlogr.NullLogger{},
46+
}
47+
Log.WithName(name)
48+
Expect(Log.Logger).To(Equal(test))
49+
})
50+
51+
It("should delegate with tags", func() {
52+
tags := []interface{}{"new", "tags"}
53+
test := tlogr.NullLogger{}
54+
test.WithTags(tags)
55+
Log = &DelegatingLogger{
56+
Logger: tlogr.NullLogger{},
57+
}
58+
Log.WithTags(tags)
59+
Expect(Log.Logger).To(Equal(test))
60+
})
61+
62+
})
63+
64+
})

0 commit comments

Comments
 (0)