Skip to content

Commit 11647f6

Browse files
authored
Add unit tests for pkg/shard/controller (#486)
* Prefactor: extract `matchers.BeFunc` * Prefactor: simplify instantiating `shardcontroller.Reconciler` * Add unit tests for `pkg/shard/controller`
1 parent 3fbf214 commit 11647f6

File tree

8 files changed

+517
-33
lines changed

8 files changed

+517
-33
lines changed

pkg/shard/controller/builder.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ import (
2323
"sigs.k8s.io/controller-runtime/pkg/client"
2424
"sigs.k8s.io/controller-runtime/pkg/manager"
2525
"sigs.k8s.io/controller-runtime/pkg/reconcile"
26-
27-
shardingv1alpha1 "github.com/timebertt/kubernetes-controller-sharding/pkg/apis/sharding/v1alpha1"
2826
)
2927

3028
// Builder can build a sharded reconciler.
@@ -42,9 +40,7 @@ type Builder struct {
4240
// reconciler that takes care of the sharding-related logic and calls the delegate reconciler whenever the shard is
4341
// responsible for reconciling an object.
4442
func NewShardedReconciler(mgr manager.Manager) *Builder {
45-
return &Builder{
46-
client: mgr.GetClient(),
47-
}
43+
return (&Builder{}).WithClient(mgr.GetClient())
4844
}
4945

5046
// For sets the object kind being reconciled by the reconciler.
@@ -105,11 +101,10 @@ func (b *Builder) Build(r reconcile.Reconciler) (reconcile.Reconciler, error) {
105101
}
106102

107103
return &Reconciler{
108-
Object: b.object,
109-
Client: b.client,
110-
ShardName: b.shardName,
111-
LabelShard: shardingv1alpha1.LabelShard(b.controllerRingName),
112-
LabelDrain: shardingv1alpha1.LabelDrain(b.controllerRingName),
113-
Do: r,
104+
Object: b.object,
105+
Client: b.client,
106+
ControllerRingName: b.controllerRingName,
107+
ShardName: b.shardName,
108+
Do: r,
114109
}, nil
115110
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
Copyright 2025 Tim Ebert.
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 controller_test
18+
19+
import (
20+
"context"
21+
22+
. "github.com/onsi/ginkgo/v2"
23+
. "github.com/onsi/gomega"
24+
corev1 "k8s.io/api/core/v1"
25+
"sigs.k8s.io/controller-runtime/pkg/client"
26+
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
27+
"sigs.k8s.io/controller-runtime/pkg/manager"
28+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
29+
30+
. "github.com/timebertt/kubernetes-controller-sharding/pkg/shard/controller"
31+
. "github.com/timebertt/kubernetes-controller-sharding/pkg/utils/test/matchers"
32+
)
33+
34+
var _ = Describe("#Builder", func() {
35+
var (
36+
mgr fakeManager
37+
b *Builder
38+
39+
obj *corev1.Pod
40+
controllerRingName string
41+
shardName string
42+
client client.Client
43+
r reconcile.Reconciler
44+
)
45+
46+
BeforeEach(func() {
47+
obj = &corev1.Pod{}
48+
controllerRingName = "operator"
49+
shardName = "operator-0"
50+
client = fakeclient.NewFakeClient()
51+
r = reconcile.Func(func(context.Context, reconcile.Request) (reconcile.Result, error) {
52+
return reconcile.Result{}, nil
53+
})
54+
})
55+
56+
JustBeforeEach(func() {
57+
mgr = fakeManager{Client: client}
58+
b = NewShardedReconciler(mgr).
59+
For(obj).
60+
InControllerRing(controllerRingName).
61+
WithShardName(shardName)
62+
})
63+
64+
Describe("#For", func() {
65+
It("should complain about calling For twice", func() {
66+
Expect(b.For(obj).Build(r)).Error().To(MatchError("must not call For() more than once"))
67+
})
68+
69+
It("should complain about not calling For", func() {
70+
b = NewShardedReconciler(mgr).
71+
InControllerRing(controllerRingName).
72+
WithShardName(shardName)
73+
Expect(b.Build(r)).Error().To(MatchError("missing object kind, must call to For()"))
74+
})
75+
})
76+
77+
Describe("#WithClient", func() {
78+
It("should use the custom client instead of the manager's client", func() {
79+
customClient := fakeclient.NewFakeClient()
80+
Expect(b.WithClient(customClient).Build(r)).To(
81+
HaveField("Client", BeIdenticalTo(customClient)),
82+
)
83+
})
84+
85+
It("should complain about missing client", func() {
86+
Expect(b.WithClient(nil).Build(r)).Error().To(MatchError("missing client"))
87+
})
88+
})
89+
90+
Describe("#InControllerRing", func() {
91+
It("should complain about missing ControllerRing name", func() {
92+
Expect(b.InControllerRing("").Build(r)).Error().To(MatchError("missing ControllerRing name"))
93+
})
94+
})
95+
96+
Describe("#WithShardName", func() {
97+
It("should complain about missing shard name", func() {
98+
Expect(b.WithShardName("").Build(r)).Error().To(MatchError("missing shard name"))
99+
})
100+
})
101+
102+
Describe("#Build", func() {
103+
It("should complain about nil reconciler", func() {
104+
Expect(b.Build(nil)).Error().To(MatchError("must provide a non-nil Reconciler"))
105+
})
106+
107+
It("should correctly set up the Reconciler", func() {
108+
shardReconciler, err := b.Build(r)
109+
Expect(err).NotTo(HaveOccurred())
110+
111+
reconciler, ok := shardReconciler.(*Reconciler)
112+
Expect(ok).To(BeTrue())
113+
114+
Expect(reconciler.Object).To(BeAssignableToTypeOf(obj))
115+
Expect(reconciler.Client).To(BeIdenticalTo(client))
116+
Expect(reconciler.ControllerRingName).To(Equal(controllerRingName))
117+
Expect(reconciler.ShardName).To(Equal(shardName))
118+
Expect(reconciler.Do).To(BeFunc(r))
119+
})
120+
})
121+
122+
Describe("#MustBuild", func() {
123+
It("should panic for nil reconciler", func() {
124+
Expect(func() {
125+
b.MustBuild(nil)
126+
}).To(Panic())
127+
})
128+
129+
It("should correctly set up the Reconciler", func() {
130+
var shardReconciler reconcile.Reconciler
131+
Expect(func() {
132+
shardReconciler = b.MustBuild(r)
133+
}).NotTo(Panic())
134+
135+
reconciler, ok := shardReconciler.(*Reconciler)
136+
Expect(ok).To(BeTrue())
137+
138+
Expect(reconciler.Object).To(BeAssignableToTypeOf(obj))
139+
Expect(reconciler.Client).To(BeIdenticalTo(client))
140+
Expect(reconciler.ControllerRingName).To(Equal(controllerRingName))
141+
Expect(reconciler.ShardName).To(Equal(shardName))
142+
Expect(reconciler.Do).To(BeFunc(r))
143+
})
144+
})
145+
})
146+
147+
type fakeManager struct {
148+
manager.Manager
149+
Client client.Client
150+
}
151+
152+
func (f fakeManager) GetClient() client.Client {
153+
return f.Client
154+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright 2025 Tim Ebert.
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 controller_test
18+
19+
import (
20+
"testing"
21+
22+
. "github.com/onsi/ginkgo/v2"
23+
. "github.com/onsi/gomega"
24+
)
25+
26+
func TestController(t *testing.T) {
27+
RegisterFailHandler(Fail)
28+
RunSpecs(t, "Shard Controller Suite")
29+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
Copyright 2025 Tim Ebert.
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 controller_test
18+
19+
import (
20+
. "github.com/onsi/ginkgo/v2"
21+
. "github.com/onsi/gomega"
22+
corev1 "k8s.io/api/core/v1"
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
"sigs.k8s.io/controller-runtime/pkg/client"
25+
"sigs.k8s.io/controller-runtime/pkg/event"
26+
"sigs.k8s.io/controller-runtime/pkg/predicate"
27+
28+
. "github.com/timebertt/kubernetes-controller-sharding/pkg/shard/controller"
29+
)
30+
31+
var _ = Describe("#Predicate", func() {
32+
var (
33+
controllerRingName string
34+
shardName string
35+
36+
mainPredicate, p predicate.Predicate
37+
38+
obj, objOld *corev1.Pod
39+
)
40+
41+
BeforeEach(func() {
42+
controllerRingName = "operator"
43+
shardName = "operator-0"
44+
45+
obj = &corev1.Pod{}
46+
})
47+
48+
JustBeforeEach(func() {
49+
p = Predicate(controllerRingName, shardName, mainPredicate)
50+
})
51+
52+
When("main predicate returns false", func() {
53+
BeforeEach(func() {
54+
mainPredicate = predicate.NewPredicateFuncs(func(client.Object) bool {
55+
return false
56+
})
57+
})
58+
59+
It("should handle drained objects", func() {
60+
metav1.SetMetaDataLabel(&obj.ObjectMeta, "drain.alpha.sharding.timebertt.dev/"+controllerRingName, "true")
61+
objOld = obj.DeepCopy()
62+
63+
Expect(p.Create(event.CreateEvent{Object: obj})).To(BeTrue())
64+
Expect(p.Update(event.UpdateEvent{ObjectOld: objOld, ObjectNew: obj})).To(BeTrue())
65+
Expect(p.Delete(event.DeleteEvent{Object: obj})).To(BeTrue())
66+
Expect(p.Generic(event.GenericEvent{Object: obj})).To(BeTrue())
67+
})
68+
69+
It("should handle assigned and drained objects", func() {
70+
metav1.SetMetaDataLabel(&obj.ObjectMeta, "shard.alpha.sharding.timebertt.dev/"+controllerRingName, shardName)
71+
metav1.SetMetaDataLabel(&obj.ObjectMeta, "drain.alpha.sharding.timebertt.dev/"+controllerRingName, "true")
72+
objOld = obj.DeepCopy()
73+
74+
Expect(p.Create(event.CreateEvent{Object: obj})).To(BeTrue())
75+
Expect(p.Update(event.UpdateEvent{ObjectOld: objOld, ObjectNew: obj})).To(BeTrue())
76+
Expect(p.Delete(event.DeleteEvent{Object: obj})).To(BeTrue())
77+
Expect(p.Generic(event.GenericEvent{Object: obj})).To(BeTrue())
78+
})
79+
80+
It("should not handle assigned objects", func() {
81+
metav1.SetMetaDataLabel(&obj.ObjectMeta, "shard.alpha.sharding.timebertt.dev/"+controllerRingName, shardName)
82+
objOld = obj.DeepCopy()
83+
84+
Expect(p.Create(event.CreateEvent{Object: obj})).To(BeFalse())
85+
Expect(p.Update(event.UpdateEvent{ObjectOld: objOld, ObjectNew: obj})).To(BeFalse())
86+
Expect(p.Delete(event.DeleteEvent{Object: obj})).To(BeFalse())
87+
Expect(p.Generic(event.GenericEvent{Object: obj})).To(BeFalse())
88+
})
89+
})
90+
91+
When("main predicate returns true", func() {
92+
BeforeEach(func() {
93+
mainPredicate = predicate.NewPredicateFuncs(func(client.Object) bool {
94+
return true
95+
})
96+
})
97+
98+
It("should handle drained objects", func() {
99+
metav1.SetMetaDataLabel(&obj.ObjectMeta, "drain.alpha.sharding.timebertt.dev/"+controllerRingName, "true")
100+
objOld = obj.DeepCopy()
101+
102+
Expect(p.Create(event.CreateEvent{Object: obj})).To(BeTrue())
103+
Expect(p.Update(event.UpdateEvent{ObjectOld: objOld, ObjectNew: obj})).To(BeTrue())
104+
Expect(p.Delete(event.DeleteEvent{Object: obj})).To(BeTrue())
105+
Expect(p.Generic(event.GenericEvent{Object: obj})).To(BeTrue())
106+
})
107+
108+
It("should handle assigned objects", func() {
109+
metav1.SetMetaDataLabel(&obj.ObjectMeta, "shard.alpha.sharding.timebertt.dev/"+controllerRingName, shardName)
110+
objOld = obj.DeepCopy()
111+
112+
Expect(p.Create(event.CreateEvent{Object: obj})).To(BeTrue())
113+
Expect(p.Update(event.UpdateEvent{ObjectOld: objOld, ObjectNew: obj})).To(BeTrue())
114+
Expect(p.Delete(event.DeleteEvent{Object: obj})).To(BeTrue())
115+
Expect(p.Generic(event.GenericEvent{Object: obj})).To(BeTrue())
116+
})
117+
118+
It("should handle assigned and drained objects", func() {
119+
metav1.SetMetaDataLabel(&obj.ObjectMeta, "shard.alpha.sharding.timebertt.dev/"+controllerRingName, shardName)
120+
metav1.SetMetaDataLabel(&obj.ObjectMeta, "drain.alpha.sharding.timebertt.dev/"+controllerRingName, "true")
121+
objOld = obj.DeepCopy()
122+
123+
Expect(p.Create(event.CreateEvent{Object: obj})).To(BeTrue())
124+
Expect(p.Update(event.UpdateEvent{ObjectOld: objOld, ObjectNew: obj})).To(BeTrue())
125+
Expect(p.Delete(event.DeleteEvent{Object: obj})).To(BeTrue())
126+
Expect(p.Generic(event.GenericEvent{Object: obj})).To(BeTrue())
127+
})
128+
})
129+
})

0 commit comments

Comments
 (0)