Skip to content

Commit f4b02d7

Browse files
author
fanzhangio
committed
Add runtime test
1 parent 8cdff9c commit f4b02d7

File tree

4 files changed

+303
-0
lines changed

4 files changed

+303
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
"time"
22+
23+
logf "github.com/kubernetes-sigs/controller-runtime/pkg/runtime/log"
24+
"github.com/kubernetes-sigs/controller-runtime/pkg/test"
25+
. "github.com/onsi/ginkgo"
26+
. "github.com/onsi/gomega"
27+
"k8s.io/client-go/kubernetes"
28+
"k8s.io/client-go/rest"
29+
)
30+
31+
func TestSource(t *testing.T) {
32+
RegisterFailHandler(Fail)
33+
RunSpecsWithDefaultAndCustomReporters(t, "Runtime Injection Suite", []Reporter{test.NewlineReporter{}})
34+
}
35+
36+
var testenv *test.Environment
37+
var cfg *rest.Config
38+
var clientset *kubernetes.Clientset
39+
40+
var _ = BeforeSuite(func(done Done) {
41+
logf.SetLogger(logf.ZapLogger(false))
42+
43+
testenv = &test.Environment{}
44+
45+
var err error
46+
cfg, err = testenv.Start()
47+
Expect(err).NotTo(HaveOccurred())
48+
49+
time.Sleep(1 * time.Second)
50+
51+
clientset, err = kubernetes.NewForConfig(cfg)
52+
Expect(err).NotTo(HaveOccurred())
53+
54+
close(done)
55+
}, 60)
56+
57+
var _ = AfterSuite(func() {
58+
testenv.Stop()
59+
})

pkg/runtime/inject/inject_test.go

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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+
res, err := ConfigInto(cfg, instance)
62+
Expect(err).NotTo(HaveOccurred())
63+
Expect(res).To(Equal(true))
64+
res, err = ConfigInto(cfg, neg)
65+
Expect(err).NotTo(HaveOccurred())
66+
Expect(res).To(Equal(false))
67+
})
68+
69+
It("should set client", func() {
70+
instance := testSource{
71+
client: func(c client.Client) error {
72+
if c != nil {
73+
return nil
74+
}
75+
return fmt.Errorf("")
76+
},
77+
}
78+
client, err := client.New(cfg, client.Options{})
79+
res, err := ClientInto(client, instance)
80+
Expect(err).NotTo(HaveOccurred())
81+
Expect(res).To(Equal(true))
82+
res, err = ClientInto(client, neg)
83+
Expect(err).NotTo(HaveOccurred())
84+
Expect(res).To(Equal(false))
85+
})
86+
87+
It("should set scheme", func() {
88+
instance := testSource{
89+
scheme: func(c *runtime.Scheme) error {
90+
if c != nil {
91+
return nil
92+
}
93+
return fmt.Errorf("")
94+
},
95+
}
96+
scheme := runtime.NewScheme()
97+
res, err := SchemeInto(scheme, instance)
98+
Expect(err).NotTo(HaveOccurred())
99+
Expect(res).To(Equal(true))
100+
res, err = SchemeInto(scheme, neg)
101+
Expect(err).NotTo(HaveOccurred())
102+
Expect(res).To(Equal(false))
103+
})
104+
105+
It("should set stop channel", func() {
106+
instance := testSource{
107+
stop: func(c <-chan struct{}) error {
108+
if c != nil {
109+
return nil
110+
}
111+
return fmt.Errorf("")
112+
},
113+
}
114+
stop := make(chan struct{})
115+
res, err := StopChannelInto(stop, instance)
116+
Expect(err).NotTo(HaveOccurred())
117+
Expect(res).To(Equal(true))
118+
res, err = StopChannelInto(stop, neg)
119+
Expect(err).NotTo(HaveOccurred())
120+
Expect(res).To(Equal(false))
121+
})
122+
123+
It("should set stop channel", func() {
124+
instance := testSource{
125+
f: func(c Func) error {
126+
if c != nil {
127+
return nil
128+
}
129+
return fmt.Errorf("")
130+
},
131+
}
132+
f := func(interface{}) error { return nil }
133+
res, err := InjectorInto(f, instance)
134+
Expect(err).NotTo(HaveOccurred())
135+
Expect(res).To(Equal(true))
136+
res, err = InjectorInto(f, neg)
137+
Expect(err).NotTo(HaveOccurred())
138+
Expect(res).To(Equal(false))
139+
})
140+
141+
})
142+
143+
type testSource struct {
144+
scheme func(scheme *runtime.Scheme) error
145+
cache func(cache.Cache) error
146+
config func(config *rest.Config) error
147+
client func(client.Client) error
148+
f func(Func) error
149+
stop func(<-chan struct{}) error
150+
}
151+
152+
func (s testSource) InjectCache(c cache.Cache) error {
153+
if s.cache != nil {
154+
return s.cache(c)
155+
}
156+
return nil
157+
}
158+
159+
func (s testSource) InjectConfig(config *rest.Config) error {
160+
if s.config != nil {
161+
return s.config(config)
162+
}
163+
return nil
164+
}
165+
166+
func (s testSource) InjectClient(client client.Client) error {
167+
if s.client != nil {
168+
return s.client(client)
169+
}
170+
return nil
171+
}
172+
173+
func (s testSource) InjectScheme(scheme *runtime.Scheme) error {
174+
if s.scheme != nil {
175+
return s.scheme(scheme)
176+
}
177+
return nil
178+
}
179+
180+
func (s testSource) InjectStopChannel(stop <-chan struct{}) error {
181+
if s.stop != nil {
182+
return s.stop(stop)
183+
}
184+
return nil
185+
}
186+
187+
func (s testSource) InjectFunc(f Func) error {
188+
if s.f != nil {
189+
return s.f(f)
190+
}
191+
return nil
192+
}

pkg/runtime/log/log_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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

pkg/runtime/signals/signal_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 signals
18+
19+
import (
20+
"syscall"
21+
22+
. "github.com/onsi/ginkgo"
23+
. "github.com/onsi/gomega"
24+
)
25+
26+
var _ = Describe("runtime signal", func() {
27+
28+
It("should setup signal handler", func() {
29+
stop := SetupSignalHandler()
30+
syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
31+
_, ok := <-stop
32+
Expect(ok).To(Equal(false))
33+
})
34+
35+
})

0 commit comments

Comments
 (0)