@@ -23,11 +23,23 @@ import (
23
23
24
24
. "github.com/onsi/ginkgo/v2"
25
25
. "github.com/onsi/gomega"
26
+ corev1 "k8s.io/api/core/v1"
26
27
apierrors "k8s.io/apimachinery/pkg/api/errors"
28
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27
29
"k8s.io/apimachinery/pkg/types"
30
+ "sigs.k8s.io/controller-runtime/pkg/client"
31
+ "sigs.k8s.io/controller-runtime/pkg/envtest"
28
32
"sigs.k8s.io/controller-runtime/pkg/reconcile"
29
33
)
30
34
35
+ type mockObjectReconciler struct {
36
+ reconcileFunc func (context.Context , * corev1.ConfigMap ) (reconcile.Result , error )
37
+ }
38
+
39
+ func (r * mockObjectReconciler ) Reconcile (ctx context.Context , cm * corev1.ConfigMap ) (reconcile.Result , error ) {
40
+ return r .reconcileFunc (ctx , cm )
41
+ }
42
+
31
43
var _ = Describe ("reconcile" , func () {
32
44
Describe ("Result" , func () {
33
45
It ("IsZero should return true if empty" , func () {
@@ -102,4 +114,75 @@ var _ = Describe("reconcile", func() {
102
114
Expect (err .Error ()).To (Equal ("nil terminal error" ))
103
115
})
104
116
})
117
+
118
+ Describe ("AsReconciler" , func () {
119
+ var testenv * envtest.Environment
120
+ var testClient client.Client
121
+
122
+ BeforeEach (func () {
123
+ testenv = & envtest.Environment {}
124
+
125
+ cfg , err := testenv .Start ()
126
+ Expect (err ).NotTo (HaveOccurred ())
127
+
128
+ testClient , err = client .New (cfg , client.Options {})
129
+ Expect (err ).NotTo (HaveOccurred ())
130
+ })
131
+
132
+ AfterEach (func () {
133
+ Expect (testenv .Stop ()).NotTo (HaveOccurred ())
134
+ })
135
+
136
+ Context ("with an existing object" , func () {
137
+ var key client.ObjectKey
138
+
139
+ BeforeEach (func () {
140
+ cm := & corev1.ConfigMap {
141
+ ObjectMeta : metav1.ObjectMeta {
142
+ Namespace : "default" ,
143
+ Name : "test" ,
144
+ },
145
+ }
146
+ key = client .ObjectKeyFromObject (cm )
147
+
148
+ err := testClient .Create (context .Background (), cm )
149
+ Expect (err ).NotTo (HaveOccurred ())
150
+ })
151
+
152
+ It ("should Get the object and call the ObjectReconciler" , func () {
153
+ var actual * corev1.ConfigMap
154
+ reconciler := reconcile .AsReconciler (testClient , & mockObjectReconciler {
155
+ reconcileFunc : func (ctx context.Context , cm * corev1.ConfigMap ) (reconcile.Result , error ) {
156
+ actual = cm
157
+ return reconcile.Result {}, nil
158
+ },
159
+ })
160
+
161
+ res , err := reconciler .Reconcile (context .Background (), reconcile.Request {NamespacedName : key })
162
+ Expect (err ).NotTo (HaveOccurred ())
163
+ Expect (res ).To (BeZero ())
164
+ Expect (actual ).NotTo (BeNil ())
165
+ Expect (actual .ObjectMeta .Name ).To (Equal (key .Name ))
166
+ Expect (actual .ObjectMeta .Namespace ).To (Equal (key .Namespace ))
167
+ })
168
+ })
169
+
170
+ Context ("with an object that doesn't exist" , func () {
171
+ It ("should not call the ObjectReconciler" , func () {
172
+ called := false
173
+ reconciler := reconcile .AsReconciler (testClient , & mockObjectReconciler {
174
+ reconcileFunc : func (ctx context.Context , cm * corev1.ConfigMap ) (reconcile.Result , error ) {
175
+ called = true
176
+ return reconcile.Result {}, nil
177
+ },
178
+ })
179
+
180
+ key := types.NamespacedName {Namespace : "default" , Name : "fake-obj" }
181
+ res , err := reconciler .Reconcile (context .Background (), reconcile.Request {NamespacedName : key })
182
+ Expect (err ).NotTo (HaveOccurred ())
183
+ Expect (res ).To (BeZero ())
184
+ Expect (called ).To (BeFalse ())
185
+ })
186
+ })
187
+ })
105
188
})
0 commit comments