Skip to content

Commit 65fb07a

Browse files
committed
add recorder test
1 parent e169648 commit 65fb07a

File tree

3 files changed

+127
-6
lines changed

3 files changed

+127
-6
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 recorder_test
18+
19+
import (
20+
"testing"
21+
22+
logf "github.com/kubernetes-sigs/controller-runtime/pkg/runtime/log"
23+
"github.com/kubernetes-sigs/controller-runtime/pkg/test"
24+
. "github.com/onsi/ginkgo"
25+
. "github.com/onsi/gomega"
26+
"k8s.io/client-go/rest"
27+
)
28+
29+
func TestRecorder(t *testing.T) {
30+
RegisterFailHandler(Fail)
31+
RunSpecsWithDefaultAndCustomReporters(t, "Recorder Integration Suite", []Reporter{test.NewlineReporter{}})
32+
}
33+
34+
var testenv *test.Environment
35+
var cfg *rest.Config
36+
37+
var _ = BeforeSuite(func(done Done) {
38+
logf.SetLogger(logf.ZapLogger(false))
39+
40+
testenv = &test.Environment{}
41+
42+
var err error
43+
cfg, err = testenv.Start()
44+
Expect(err).NotTo(HaveOccurred())
45+
46+
close(done)
47+
}, 60)
48+
49+
var _ = AfterSuite(func() {
50+
testenv.Stop()
51+
})
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 recorder_test
18+
19+
import (
20+
"github.com/kubernetes-sigs/controller-runtime/pkg/internal/recorder"
21+
. "github.com/onsi/ginkgo"
22+
. "github.com/onsi/gomega"
23+
"k8s.io/client-go/kubernetes/scheme"
24+
)
25+
26+
var _ = Describe("recorder", func() {
27+
Describe("NewProvider", func() {
28+
It("should return a provider instance and a nil error.", func() {
29+
provider, err := recorder.NewProvider(cfg, scheme.Scheme)
30+
Expect(provider).NotTo(BeNil())
31+
Expect(err).NotTo(HaveOccurred())
32+
})
33+
34+
It("should return an error if failed to init clientSet.", func() {
35+
// Invalid the config
36+
cfg1 := *cfg
37+
cfg1.ContentType = "invalid-type"
38+
_, err := recorder.NewProvider(&cfg1, scheme.Scheme)
39+
Expect(err.Error()).To(ContainSubstring("failed to init clientSet"))
40+
})
41+
})
42+
Describe("GetEventRecorder", func() {
43+
It("should return a recorder instance.", func() {
44+
provider, err := recorder.NewProvider(cfg, scheme.Scheme)
45+
Expect(err).NotTo(HaveOccurred())
46+
47+
recorder := provider.GetEventRecorderFor("test")
48+
Expect(recorder).NotTo(BeNil())
49+
})
50+
})
51+
})

pkg/manager/manager_test.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/kubernetes-sigs/controller-runtime/pkg/cache/informertest"
2424
"github.com/kubernetes-sigs/controller-runtime/pkg/client"
2525
"github.com/kubernetes-sigs/controller-runtime/pkg/reconcile"
26+
"github.com/kubernetes-sigs/controller-runtime/pkg/recorder"
2627
"github.com/kubernetes-sigs/controller-runtime/pkg/runtime/inject"
2728
. "github.com/onsi/ginkgo"
2829
. "github.com/onsi/gomega"
@@ -81,6 +82,17 @@ var _ = Describe("manger.Manager", func() {
8182
Expect(err).To(HaveOccurred())
8283
Expect(err.Error()).To(ContainSubstring("expected error"))
8384

85+
close(done)
86+
})
87+
It("should return an error it can't create a recorder.Provider", func(done Done) {
88+
m, err := New(cfg, Options{
89+
newRecorderProvider: func(config *rest.Config, scheme *runtime.Scheme) (recorder.Provider, error) {
90+
return nil, fmt.Errorf("expected error")
91+
}})
92+
Expect(m).To(BeNil())
93+
Expect(err).To(HaveOccurred())
94+
Expect(err.Error()).To(ContainSubstring("expected error"))
95+
8496
close(done)
8597
})
8698
})
@@ -362,6 +374,12 @@ var _ = Describe("manger.Manager", func() {
362374
Expect(ok).To(BeTrue())
363375
Expect(m.GetFieldIndexer()).To(Equal(mrg.fieldIndexes))
364376
})
377+
378+
It("should provide a function to get the EventRecorder", func() {
379+
m, err := New(cfg, Options{})
380+
Expect(err).NotTo(HaveOccurred())
381+
Expect(m.GetRecorder("test")).NotTo(BeNil())
382+
})
365383
})
366384

367385
var _ reconcile.Reconcile = &failRec{}
@@ -389,12 +407,12 @@ var _ inject.Config = &injectable{}
389407
var _ inject.Stoppable = &injectable{}
390408

391409
type injectable struct {
392-
scheme func(scheme *runtime.Scheme) error
393-
client func(client.Client) error
394-
config func(config *rest.Config) error
395-
cache func(cache.Cache) error
396-
f func(inject.Func) error
397-
stop func(<-chan struct{}) error
410+
scheme func(scheme *runtime.Scheme) error
411+
client func(client.Client) error
412+
config func(config *rest.Config) error
413+
cache func(cache.Cache) error
414+
f func(inject.Func) error
415+
stop func(<-chan struct{}) error
398416
}
399417

400418
func (i *injectable) InjectCache(c cache.Cache) error {
@@ -442,3 +460,4 @@ func (i *injectable) InjectStopChannel(stop <-chan struct{}) error {
442460
func (i *injectable) Start(<-chan struct{}) error {
443461
return nil
444462
}
463+

0 commit comments

Comments
 (0)