Skip to content

Commit 5224cd1

Browse files
committed
DRY up reconciler object creation in recon test
Adds a new `reconcilerMocks` utility function to `pkg/runtime/reconciler_test.go` that DRYs up a chunk of repetitive code in the unit tests that creates a resource reconciler and a mocked kubernetes client.
1 parent 5e799c5 commit 5224cd1

File tree

1 file changed

+26
-55
lines changed

1 file changed

+26
-55
lines changed

pkg/runtime/reconciler_test.go

Lines changed: 26 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
ackmetrics "github.com/aws-controllers-k8s/runtime/pkg/metrics"
3232
ackrt "github.com/aws-controllers-k8s/runtime/pkg/runtime"
3333
ackrtcache "github.com/aws-controllers-k8s/runtime/pkg/runtime/cache"
34+
acktypes "github.com/aws-controllers-k8s/runtime/pkg/types"
3435

3536
k8srtmocks "github.com/aws-controllers-k8s/runtime/mocks/apimachinery/pkg/runtime"
3637
k8srtschemamocks "github.com/aws-controllers-k8s/runtime/mocks/apimachinery/pkg/runtime/schema"
@@ -69,6 +70,28 @@ func resourceMocks() (
6970
return res, rtObj, metaObj
7071
}
7172

73+
func reconcilerMocks(
74+
rmf acktypes.AWSResourceManagerFactory,
75+
) (
76+
acktypes.AWSResourceReconciler,
77+
*ctrlrtclientmock.Client,
78+
) {
79+
zapOptions := ctrlrtzap.Options{
80+
Development: true,
81+
Level: zapcore.InfoLevel,
82+
}
83+
fakeLogger := ctrlrtzap.New(ctrlrtzap.UseFlagOptions(&zapOptions))
84+
cfg := ackcfg.Config{}
85+
metrics := ackmetrics.NewMetrics("bookstore")
86+
87+
sc := &ackmocks.ServiceController{}
88+
kc := &ctrlrtclientmock.Client{}
89+
90+
return ackrt.NewReconcilerWithClient(
91+
sc, kc, rmf, fakeLogger, cfg, metrics, ackrtcache.Caches{},
92+
), kc
93+
}
94+
7295
func TestReconcilerUpdate(t *testing.T) {
7396
require := require.New(t)
7497

@@ -117,32 +140,18 @@ func TestReconcilerUpdate(t *testing.T) {
117140
reg := ackrt.NewRegistry()
118141
reg.RegisterResourceManagerFactory(rmf)
119142

120-
zapOptions := ctrlrtzap.Options{
121-
Development: true,
122-
Level: zapcore.InfoLevel,
123-
}
124-
fakeLogger := ctrlrtzap.New(ctrlrtzap.UseFlagOptions(&zapOptions))
125-
cfg := ackcfg.Config{}
126-
metrics := ackmetrics.NewMetrics("bookstore")
143+
r, kc := reconcilerMocks(rmf)
127144

128-
sc := &ackmocks.ServiceController{}
129-
kc := &ctrlrtclientmock.Client{}
130145
statusWriter := &ctrlrtclientmock.StatusWriter{}
131-
132146
kc.On("Status").Return(statusWriter)
133147
statusWriter.On("Patch", ctx, latestRTObj, client.MergeFrom(desiredRTObj)).Return(nil)
134148
kc.On("Patch", ctx, latestRTObj, client.MergeFrom(desiredRTObj)).Return(nil)
135149

136-
// TODO(jaypipes): Place the above setup into helper functions that can be
137-
// re-used by future unit tests of the reconciler code paths.
138-
139150
// With the above mocks and below assertions, we check that if we got a
140151
// non-error return from `AWSResourceManager.ReadOne()` and the
141152
// `AWSResourceDescriptor.Delta()` returned a non-empty Delta, that we end
142153
// up calling the AWSResourceManager.Update() call in the Reconciler.Sync()
143154
// method,
144-
r := ackrt.NewReconcilerWithClient(sc, kc, rmf, fakeLogger, cfg, metrics, ackrtcache.Caches{})
145-
146155
err := r.Sync(ctx, rm, desired)
147156
require.Nil(err)
148157
rm.AssertCalled(t, "ReadOne", ctx, desired)
@@ -203,32 +212,13 @@ func TestReconcilerUpdate_PatchMetadataAndSpec_DiffInMetadata(t *testing.T) {
203212
reg := ackrt.NewRegistry()
204213
reg.RegisterResourceManagerFactory(rmf)
205214

206-
zapOptions := ctrlrtzap.Options{
207-
Development: true,
208-
Level: zapcore.InfoLevel,
209-
}
210-
fakeLogger := ctrlrtzap.New(ctrlrtzap.UseFlagOptions(&zapOptions))
211-
cfg := ackcfg.Config{}
212-
metrics := ackmetrics.NewMetrics("bookstore")
215+
r, kc := reconcilerMocks(rmf)
213216

214-
sc := &ackmocks.ServiceController{}
215-
kc := &ctrlrtclientmock.Client{}
216217
statusWriter := &ctrlrtclientmock.StatusWriter{}
217-
218218
kc.On("Status").Return(statusWriter)
219219
statusWriter.On("Patch", ctx, latestRTObj, client.MergeFrom(desiredRTObj)).Return(nil)
220220
kc.On("Patch", ctx, latestRTObj, client.MergeFrom(desiredRTObj)).Return(nil)
221221

222-
// TODO(jaypipes): Place the above setup into helper functions that can be
223-
// re-used by future unit tests of the reconciler code paths.
224-
225-
// With the above mocks and below assertions, we check that if we got a
226-
// non-error return from `AWSResourceManager.ReadOne()` and the
227-
// `AWSResourceDescriptor.Delta()` returned a non-empty Delta, that we end
228-
// up calling the AWSResourceManager.Update() call in the Reconciler.Sync()
229-
// method,
230-
r := ackrt.NewReconcilerWithClient(sc, kc, rmf, fakeLogger, cfg, metrics, ackrtcache.Caches{})
231-
232222
err := r.Sync(ctx, rm, desired)
233223
require.Nil(err)
234224
rm.AssertCalled(t, "ReadOne", ctx, desired)
@@ -286,32 +276,13 @@ func TestReconcilerUpdate_PatchMetadataAndSpec_DiffInSpec(t *testing.T) {
286276
reg := ackrt.NewRegistry()
287277
reg.RegisterResourceManagerFactory(rmf)
288278

289-
zapOptions := ctrlrtzap.Options{
290-
Development: true,
291-
Level: zapcore.InfoLevel,
292-
}
293-
fakeLogger := ctrlrtzap.New(ctrlrtzap.UseFlagOptions(&zapOptions))
294-
cfg := ackcfg.Config{}
295-
metrics := ackmetrics.NewMetrics("bookstore")
279+
r, kc := reconcilerMocks(rmf)
296280

297-
sc := &ackmocks.ServiceController{}
298-
kc := &ctrlrtclientmock.Client{}
299281
statusWriter := &ctrlrtclientmock.StatusWriter{}
300-
301282
kc.On("Status").Return(statusWriter)
302283
statusWriter.On("Patch", ctx, latestRTObj, client.MergeFrom(desiredRTObj)).Return(nil)
303284
kc.On("Patch", ctx, latestRTObj, client.MergeFrom(desiredRTObj)).Return(nil)
304285

305-
// TODO(jaypipes): Place the above setup into helper functions that can be
306-
// re-used by future unit tests of the reconciler code paths.
307-
308-
// With the above mocks and below assertions, we check that if we got a
309-
// non-error return from `AWSResourceManager.ReadOne()` and the
310-
// `AWSResourceDescriptor.Delta()` returned a non-empty Delta, that we end
311-
// up calling the AWSResourceManager.Update() call in the Reconciler.Sync()
312-
// method,
313-
r := ackrt.NewReconcilerWithClient(sc, kc, rmf, fakeLogger, cfg, metrics, ackrtcache.Caches{})
314-
315286
err := r.Sync(ctx, rm, desired)
316287
require.Nil(err)
317288
rm.AssertCalled(t, "ReadOne", ctx, desired)

0 commit comments

Comments
 (0)