forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadd_test.go
More file actions
208 lines (171 loc) · 7.51 KB
/
add_test.go
File metadata and controls
208 lines (171 loc) · 7.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package managedseed_test
import (
"context"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"go.uber.org/mock/gomock"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
seedmanagementv1alpha1 "github.com/gardener/gardener/pkg/apis/seedmanagement/v1alpha1"
"github.com/gardener/gardener/pkg/client/kubernetes"
gardenletconfigv1alpha1 "github.com/gardener/gardener/pkg/gardenlet/apis/config/v1alpha1"
. "github.com/gardener/gardener/pkg/gardenlet/controller/managedseed"
"github.com/gardener/gardener/pkg/utils/test"
mockworkqueue "github.com/gardener/gardener/third_party/mock/client-go/util/workqueue"
)
var _ = Describe("Add", func() {
var (
ctx = context.TODO()
fakeClient client.Client
reconciler *Reconciler
p predicate.Predicate
)
BeforeEach(func() {
fakeClient = fakeclient.NewClientBuilder().WithScheme(kubernetes.GardenScheme).Build()
reconciler = &Reconciler{
GardenClient: fakeClient,
GardenNamespaceGarden: v1beta1constants.GardenNamespace,
}
})
Describe("#SeedPredicate", func() {
var (
seed *gardencorev1beta1.Seed
)
BeforeEach(func() {
seed = &gardencorev1beta1.Seed{}
p = reconciler.SeedPredicate()
})
It("should return true", func() {
seed.Labels = map[string]string{
"name.seed.gardener.cloud/foo": "true",
"name.seed.gardener.cloud/bar": "true",
}
Expect(p.Create(event.TypedCreateEvent[client.Object]{Object: seed})).To(BeTrue())
Expect(p.Update(event.TypedUpdateEvent[client.Object]{ObjectNew: seed})).To(BeTrue())
Expect(p.Delete(event.TypedDeleteEvent[client.Object]{Object: seed})).To(BeTrue())
Expect(p.Generic(event.TypedGenericEvent[client.Object]{Object: seed})).To(BeTrue())
})
It("should return false", func() {
Expect(p.Create(event.TypedCreateEvent[client.Object]{Object: seed})).To(BeFalse())
Expect(p.Update(event.TypedUpdateEvent[client.Object]{ObjectNew: seed})).To(BeFalse())
Expect(p.Delete(event.TypedDeleteEvent[client.Object]{Object: seed})).To(BeFalse())
Expect(p.Generic(event.TypedGenericEvent[client.Object]{Object: seed})).To(BeFalse())
})
})
Describe("#EnqueueWithJitterDelay", func() {
var (
hdlr handler.EventHandler
queue *mockworkqueue.MockTypedRateLimitingInterface[reconcile.Request]
obj *seedmanagementv1alpha1.ManagedSeed
req reconcile.Request
cfg gardenletconfigv1alpha1.GardenletConfiguration
randomDuration = 10 * time.Millisecond
)
BeforeEach(func() {
cfg = gardenletconfigv1alpha1.GardenletConfiguration{
Controllers: &gardenletconfigv1alpha1.GardenletControllerConfiguration{
ManagedSeed: &gardenletconfigv1alpha1.ManagedSeedControllerConfiguration{
SyncJitterPeriod: &metav1.Duration{Duration: 50 * time.Millisecond},
},
},
}
hdlr = (&Reconciler{Config: cfg}).EnqueueWithJitterDelay()
queue = mockworkqueue.NewMockTypedRateLimitingInterface[reconcile.Request](gomock.NewController(GinkgoT()))
obj = &seedmanagementv1alpha1.ManagedSeed{ObjectMeta: metav1.ObjectMeta{Name: "managedseed", Namespace: "namespace"}}
req = reconcile.Request{NamespacedName: types.NamespacedName{Name: obj.Name, Namespace: obj.Namespace}}
DeferCleanup(func() {
test.WithVar(&RandomDurationWithMetaDuration, func(_ *metav1.Duration) time.Duration { return randomDuration })
})
})
It("should enqueue the object without delay for Create events when deletion timestamp is set", func() {
queue.EXPECT().Add(req)
now := metav1.Now()
obj.SetDeletionTimestamp(&now)
hdlr.Create(ctx, event.CreateEvent{Object: obj}, queue)
})
It("should enqueue the object without delay for Create events when generation is set to 1", func() {
queue.EXPECT().Add(req)
obj.Generation = 1
hdlr.Create(ctx, event.CreateEvent{Object: obj}, queue)
})
It("should enqueue the object without delay for Create events when generation changed and jitterudpates is set to false", func() {
queue.EXPECT().Add(req)
cfg.Controllers.ManagedSeed.JitterUpdates = ptr.To(false)
obj.Generation = 2
obj.Status.ObservedGeneration = 1
hdlr = (&Reconciler{Config: cfg}).EnqueueWithJitterDelay()
hdlr.Create(ctx, event.CreateEvent{Object: obj}, queue)
})
It("should enqueue the object with random delay for Create events when generation changed and jitterUpdates is set to true", func() {
queue.EXPECT().AddAfter(req, randomDuration)
cfg.Controllers.ManagedSeed.JitterUpdates = ptr.To(true)
obj.Generation = 2
obj.Status.ObservedGeneration = 1
hdlr = (&Reconciler{Config: cfg}).EnqueueWithJitterDelay()
hdlr.Create(ctx, event.CreateEvent{Object: obj}, queue)
})
It("should enqueue the object with random delay for Create events when there is no change in generation", func() {
queue.EXPECT().AddAfter(req, randomDuration)
cfg.Controllers.ManagedSeed.JitterUpdates = ptr.To(false)
obj.Generation = 2
obj.Status.ObservedGeneration = 2
hdlr = (&Reconciler{Config: cfg}).EnqueueWithJitterDelay()
hdlr.Create(ctx, event.CreateEvent{Object: obj}, queue)
})
It("should not enqueue the object for Update events when generation and observedGeneration are equal", func() {
obj.Generation = 1
obj.Status.ObservedGeneration = 1
hdlr.Update(ctx, event.UpdateEvent{ObjectNew: obj, ObjectOld: obj}, queue)
})
It("should enqueue the object for Update events when deletion timestamp is set", func() {
queue.EXPECT().Add(req)
obj.Generation = 2
obj.Status.ObservedGeneration = 1
now := metav1.Now()
obj.SetDeletionTimestamp(&now)
hdlr.Update(ctx, event.UpdateEvent{ObjectNew: obj, ObjectOld: obj}, queue)
})
It("should enqueue the object for Update events when generation is 1", func() {
queue.EXPECT().Add(req)
obj.Generation = 1
obj.Status.ObservedGeneration = 0
hdlr.Update(ctx, event.UpdateEvent{ObjectNew: obj, ObjectOld: obj}, queue)
})
It("should enqueue the object for Update events when jitterUpdates is set to false", func() {
queue.EXPECT().Add(req)
cfg.Controllers.ManagedSeed.JitterUpdates = ptr.To(false)
obj.Generation = 2
obj.Status.ObservedGeneration = 1
hdlr = (&Reconciler{Config: cfg}).EnqueueWithJitterDelay()
hdlr.Update(ctx, event.UpdateEvent{ObjectNew: obj, ObjectOld: obj}, queue)
})
It("should enqueue the object with random delay for Update events when jitterUpdates is set to true", func() {
queue.EXPECT().AddAfter(req, randomDuration)
cfg.Controllers.ManagedSeed.JitterUpdates = ptr.To(true)
obj.Generation = 2
obj.Status.ObservedGeneration = 1
hdlr = (&Reconciler{Config: cfg}).EnqueueWithJitterDelay()
hdlr.Update(ctx, event.UpdateEvent{ObjectNew: obj, ObjectOld: obj}, queue)
})
It("should enqueue the object for Delete events", func() {
queue.EXPECT().Add(req)
hdlr.Delete(ctx, event.DeleteEvent{Object: obj}, queue)
})
It("should not enqueue the object for Generic events", func() {
hdlr.Generic(ctx, event.GenericEvent{Object: obj}, queue)
})
})
})