Skip to content

Commit 2d0510c

Browse files
committed
add recorder test
1 parent 207d326 commit 2d0510c

File tree

4 files changed

+250
-0
lines changed

4 files changed

+250
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
appsv1 "k8s.io/api/apps/v1"
21+
corev1 "k8s.io/api/core/v1"
22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
"k8s.io/apimachinery/pkg/watch"
24+
"k8s.io/client-go/kubernetes/scheme"
25+
ref "k8s.io/client-go/tools/reference"
26+
"sigs.k8s.io/controller-runtime/pkg/controller"
27+
"sigs.k8s.io/controller-runtime/pkg/handler"
28+
"sigs.k8s.io/controller-runtime/pkg/manager"
29+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
30+
"sigs.k8s.io/controller-runtime/pkg/source"
31+
32+
. "github.com/onsi/ginkgo"
33+
. "github.com/onsi/gomega"
34+
)
35+
36+
var _ = Describe("recorder", func() {
37+
var stop chan struct{}
38+
39+
BeforeEach(func() {
40+
stop = make(chan struct{})
41+
Expect(cfg).NotTo(BeNil())
42+
})
43+
44+
AfterEach(func() {
45+
close(stop)
46+
})
47+
48+
Describe("recorder", func() {
49+
It("should publish events", func(done Done) {
50+
By("Creating the Manager")
51+
cm, err := manager.New(cfg, manager.Options{})
52+
Expect(err).NotTo(HaveOccurred())
53+
54+
By("Creating the Controller")
55+
recorder := cm.GetRecorder("test-recorder")
56+
instance, err := controller.New("foo-controller", cm, controller.Options{
57+
Reconcile: reconcile.Func(
58+
func(request reconcile.Request) (reconcile.Result, error) {
59+
dp, err := clientset.AppsV1().Deployments(request.Namespace).Get(request.Name, metav1.GetOptions{})
60+
Expect(err).NotTo(HaveOccurred())
61+
recorder.Event(dp, corev1.EventTypeNormal, "test-reason", "test-msg")
62+
return reconcile.Result{}, nil
63+
}),
64+
})
65+
Expect(err).NotTo(HaveOccurred())
66+
67+
By("Watching Resources")
68+
err = instance.Watch(&source.Kind{Type: &appsv1.Deployment{}}, &handler.Enqueue{})
69+
Expect(err).NotTo(HaveOccurred())
70+
71+
By("Starting the Manager")
72+
go func() {
73+
defer GinkgoRecover()
74+
Expect(cm.Start(stop)).NotTo(HaveOccurred())
75+
}()
76+
77+
deployment := &appsv1.Deployment{
78+
ObjectMeta: metav1.ObjectMeta{Name: "deployment-name"},
79+
Spec: appsv1.DeploymentSpec{
80+
Selector: &metav1.LabelSelector{
81+
MatchLabels: map[string]string{"foo": "bar"},
82+
},
83+
Template: corev1.PodTemplateSpec{
84+
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"foo": "bar"}},
85+
Spec: corev1.PodSpec{
86+
Containers: []corev1.Container{
87+
{
88+
Name: "nginx",
89+
Image: "nginx",
90+
},
91+
},
92+
},
93+
},
94+
},
95+
}
96+
97+
By("Invoking Reconciling")
98+
deployment, err = clientset.AppsV1().Deployments("default").Create(deployment)
99+
Expect(err).NotTo(HaveOccurred())
100+
101+
By("Validate event is published as expected")
102+
evtWatcher, err := clientset.CoreV1().Events("default").Watch(metav1.ListOptions{})
103+
Expect(err).NotTo(HaveOccurred())
104+
105+
resultEvent := <-evtWatcher.ResultChan()
106+
107+
Expect(resultEvent.Type).To(Equal(watch.Added))
108+
evt, isEvent := resultEvent.Object.(*corev1.Event)
109+
Expect(isEvent).To(BeTrue())
110+
111+
dpRef, err := ref.GetReference(scheme.Scheme, deployment)
112+
Expect(err).NotTo(HaveOccurred())
113+
114+
Expect(evt.InvolvedObject).To(Equal(*dpRef))
115+
Expect(evt.Type).To(Equal(corev1.EventTypeNormal))
116+
Expect(evt.Reason).To(Equal("test-reason"))
117+
Expect(evt.Message).To(Equal("test-msg"))
118+
119+
close(done)
120+
})
121+
})
122+
})
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 recorder_test
18+
19+
import (
20+
"testing"
21+
"time"
22+
23+
. "github.com/onsi/ginkgo"
24+
. "github.com/onsi/gomega"
25+
"k8s.io/client-go/kubernetes"
26+
"k8s.io/client-go/rest"
27+
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
28+
"sigs.k8s.io/controller-runtime/pkg/test"
29+
)
30+
31+
func TestRecorder(t *testing.T) {
32+
RegisterFailHandler(Fail)
33+
RunSpecsWithDefaultAndCustomReporters(t, "Recorder Integration 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+
})
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/onsi/ginkgo"
21+
. "github.com/onsi/gomega"
22+
"k8s.io/client-go/kubernetes/scheme"
23+
"sigs.k8s.io/controller-runtime/pkg/internal/recorder"
24+
)
25+
26+
var _ = Describe("recorder.Provider", 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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"sigs.k8s.io/controller-runtime/pkg/cache/informertest"
2929
"sigs.k8s.io/controller-runtime/pkg/client"
3030
"sigs.k8s.io/controller-runtime/pkg/reconcile"
31+
"sigs.k8s.io/controller-runtime/pkg/recorder"
3132
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
3233
)
3334

@@ -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{}

0 commit comments

Comments
 (0)