@@ -11,6 +11,9 @@ import (
1111 "testing"
1212 "time"
1313
14+ druidv1alpha1 "github.com/gardener/etcd-druid/api/core/v1alpha1"
15+ "github.com/gardener/etcd-druid/test/utils"
16+
1417 batchv1 "k8s.io/api/batch/v1"
1518 coordinationv1 "k8s.io/api/coordination/v1"
1619 corev1 "k8s.io/api/core/v1"
@@ -166,50 +169,94 @@ func TestJobStatusChangedForUpdateEvents(t *testing.T) {
166169 tests := []struct {
167170 name string
168171 isObjectJob bool
172+ isObjectCompactionJob bool
169173 isStatusChanged bool
170174 shouldAllowUpdateEvent bool
171175 }{
172176 {
173177 name : "object is not a job" ,
174178 isObjectJob : false ,
179+ isObjectCompactionJob : false ,
180+ shouldAllowUpdateEvent : false ,
181+ },
182+ {
183+ name : "object is a non-compaction job, and status is not changed" ,
184+ isObjectJob : true ,
185+ isObjectCompactionJob : false ,
186+ isStatusChanged : false ,
187+ shouldAllowUpdateEvent : false ,
188+ },
189+ {
190+ name : "object is a non-compaction job, and status is changed" ,
191+ isObjectJob : true ,
192+ isObjectCompactionJob : false ,
193+ isStatusChanged : true ,
175194 shouldAllowUpdateEvent : false ,
176195 },
177196 {
178- name : "object is a job, but status is not changed" ,
197+ name : "object is a compaction job, but status is not changed" ,
179198 isObjectJob : true ,
199+ isObjectCompactionJob : true ,
180200 isStatusChanged : false ,
181201 shouldAllowUpdateEvent : false ,
182202 },
183203 {
184- name : "object is a job, and status is changed" ,
204+ name : "object is a compaction job, and status is changed" ,
185205 isObjectJob : true ,
206+ isObjectCompactionJob : true ,
186207 isStatusChanged : true ,
187208 shouldAllowUpdateEvent : true ,
188209 },
189210 }
190211
191212 g := NewWithT (t )
192213 t .Parallel ()
193- predicate := jobStatusChanged ()
214+ predicate := compactionJobStatusChanged ()
194215 for _ , test := range tests {
195216 t .Run (test .name , func (t * testing.T ) {
196217 t .Parallel ()
197- obj , oldObj := createObjectsForJobStatusChangedPredicate (g , "etcd-test-compaction-job" , test .isObjectJob , test .isStatusChanged )
218+ obj , oldObj := createObjectsForJobStatusChangedPredicate (g , druidv1alpha1 . GetCompactionJobName (metav1. ObjectMeta { Name : utils . TestEtcdName }) , test .isObjectJob , test . isObjectCompactionJob , test .isStatusChanged )
198219 g .Expect (predicate .Update (event.UpdateEvent {ObjectOld : oldObj , ObjectNew : obj })).To (Equal (test .shouldAllowUpdateEvent ))
199220 })
200221 }
201222}
202223
203- func createObjectsForJobStatusChangedPredicate (g * WithT , name string , isJobObj , isStatusChanged bool ) (obj client.Object , oldObj client.Object ) {
224+ func createObjectsForJobStatusChangedPredicate (g * WithT , name string , isJobObj , isCompactionJob , isStatusChanged bool ) (obj client.Object , oldObj client.Object ) {
204225 // if the object is not a job object, create a config map (random type chosen, could have been anything else as well).
205226 if ! isJobObj {
206227 obj = createConfigMap (g , name )
207228 oldObj = createConfigMap (g , name )
208229 return
209230 }
231+ // If the object is a job but not a compaction job, create a regular job
232+ if ! isCompactionJob {
233+ obj = createNonCompactionJob (g , name )
234+ oldObj = createNonCompactionJob (g , name )
235+ return
236+ }
237+
210238 now := time .Now ()
239+
240+ etcdName := utils .TestEtcdName
241+ etcdKind := druidv1alpha1 .SchemeGroupVersion .WithKind ("Etcd" ).Kind
242+
243+ // Create proper owner reference for compaction job
244+ ownerRef := metav1.OwnerReference {
245+ APIVersion : druidv1alpha1 .SchemeGroupVersion .String (),
246+ Kind : etcdKind ,
247+ Name : etcdName ,
248+ UID : "test-etcd-uid-12345" ,
249+ Controller : ptr .To (true ),
250+ BlockOwnerDeletion : ptr .To (true ),
251+ }
252+
211253 // create job objects
212254 oldObj = & batchv1.Job {
255+ ObjectMeta : metav1.ObjectMeta {
256+ Name : name ,
257+ Namespace : utils .TestNamespace ,
258+ OwnerReferences : []metav1.OwnerReference {ownerRef },
259+ },
213260 Status : batchv1.JobStatus {
214261 Active : 1 ,
215262 StartTime : & metav1.Time {
@@ -219,6 +266,11 @@ func createObjectsForJobStatusChangedPredicate(g *WithT, name string, isJobObj,
219266 }
220267 if isStatusChanged {
221268 obj = & batchv1.Job {
269+ ObjectMeta : metav1.ObjectMeta {
270+ Name : name ,
271+ Namespace : utils .TestNamespace ,
272+ OwnerReferences : []metav1.OwnerReference {ownerRef },
273+ },
222274 Status : batchv1.JobStatus {
223275 Succeeded : 1 ,
224276 StartTime : & metav1.Time {
@@ -290,6 +342,22 @@ func createConfigMap(g *WithT, name string) *corev1.ConfigMap {
290342 }
291343}
292344
345+ func createNonCompactionJob (g * WithT , name string ) * batchv1.Job {
346+ randInt := generateRandomInt (g )
347+ return & batchv1.Job {
348+ ObjectMeta : metav1.ObjectMeta {
349+ Name : name ,
350+ Namespace : utils .TestNamespace ,
351+ },
352+ Spec : batchv1.JobSpec {
353+ ActiveDeadlineSeconds : ptr .To (int64 (randInt )),
354+ },
355+ Status : batchv1.JobStatus {
356+ Active : 1 ,
357+ },
358+ }
359+ }
360+
293361func generateRandomInt (g * WithT ) int {
294362 randInt , err := rand .Int (rand .Reader , big .NewInt (1000 ))
295363 g .Expect (err ).NotTo (HaveOccurred ())
0 commit comments