@@ -6,14 +6,16 @@ import (
66 "github.com/stretchr/testify/assert"
77
88 dba "github.com/app-sre/dba-operator/api/v1alpha1"
9+ batchv1 "k8s.io/api/batch/v1"
910 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1011 "sigs.k8s.io/controller-runtime/pkg/event"
1112)
1213
13- func managedDatabase (generation int64 , currentVersion string ) * dba.ManagedDatabase {
14+ func managedDatabase (generation int64 , resourceVersion , currentVersion string ) * dba.ManagedDatabase {
1415 managedDatabase := & dba.ManagedDatabase {
1516 ObjectMeta : metav1.ObjectMeta {
16- Generation : generation ,
17+ ResourceVersion : resourceVersion ,
18+ Generation : generation ,
1719 },
1820 Status : dba.ManagedDatabaseStatus {
1921 CurrentVersion : currentVersion ,
@@ -22,27 +24,44 @@ func managedDatabase(generation int64, currentVersion string) *dba.ManagedDataba
2224 return managedDatabase
2325}
2426
27+ func job (generation int64 , resourceVersion string , active int32 ) * batchv1.Job {
28+ job := & batchv1.Job {
29+ ObjectMeta : metav1.ObjectMeta {
30+ ResourceVersion : resourceVersion ,
31+ Generation : generation ,
32+ },
33+ Status : batchv1.JobStatus {
34+ Active : active ,
35+ },
36+ }
37+ return job
38+ }
39+
2540func TestManagedDatabaseVersionChangedPredicate (t * testing.T ) {
41+ mdbPredicate := ManagedDatabaseVersionChangedPredicate {}
42+
2643 var managedDatabaseUpdatesForPredicateTests = []struct {
27- oldGeneration int
28- oldCurrentVersion string
29- newGeneration int
30- newCurrentVersion string
31- expected bool
44+ oldGeneration int
45+ oldResourceVersion string
46+ oldCurrentVersion string
47+ newGeneration int
48+ newResourceVersion string
49+ newCurrentVersion string
50+ expected bool
3251 }{
33- {0 , "v1" , 0 , "v2" , true }, // Status subresource not enabled, currentVersion change
34- {0 , "v1" , 0 , "v1" , true }, // Status subresource not enabled, some update other than currentVersion
35- {1 , "v1" , 1 , "v1" , false }, // Same generation, same currentVersion (change to some other field)
36- {1 , "v1" , 1 , "v2" , true }, // currentVersion change, same generation
37- {1 , "v1" , 2 , "v1" , true }, // Generation change, same currentVersion
38- {1 , "v1" , 2 , "v2" , true }, // Generation change & version change
52+ {0 , "1" , "v1" , 0 , "1" , "v1" , false }, // No-change update
53+ {1 , "1" , "v1" , 1 , "1" , "v1" , false }, // No-change update w/ generation field set
54+ {0 , "1" , "v1" , 0 , "2" , "v2" , true }, // Generation not set, currentVersion change (resourceVersion change)
55+ {0 , "1" , "v1" , 0 , "2" , "v1" , true }, // Generation not set, some update other than currentVersion
56+ {1 , "1" , "v1" , 1 , "2" , "v1" , false }, // Same generation, same currentVersion (change to some other field)
57+ {1 , "1" , "v1" , 1 , "2" , "v2" , true }, // currentVersion change, same generation
58+ {1 , "1" , "v1" , 2 , "2" , "v1" , true }, // Generation change, same currentVersion
59+ {1 , "1" , "v1" , 2 , "2" , "v2" , true }, // Generation change & currentVersion change
3960 }
4061
41- mdbPredicate := ManagedDatabaseVersionChangedPredicate {}
42-
4362 for _ , update := range managedDatabaseUpdatesForPredicateTests {
44- oldManagedDatabase := managedDatabase (int64 (update .oldGeneration ), update .oldCurrentVersion )
45- newManagedDatabase := managedDatabase (int64 (update .newGeneration ), update .newCurrentVersion )
63+ oldManagedDatabase := managedDatabase (int64 (update .oldGeneration ), update .oldResourceVersion , update . oldCurrentVersion )
64+ newManagedDatabase := managedDatabase (int64 (update .newGeneration ), update .newResourceVersion , update . newCurrentVersion )
4665
4766 updateEvent := event.UpdateEvent {
4867 MetaOld : & oldManagedDatabase .ObjectMeta ,
@@ -53,4 +72,37 @@ func TestManagedDatabaseVersionChangedPredicate(t *testing.T) {
5372
5473 assert .Equal (t , mdbPredicate .Update (updateEvent ), update .expected )
5574 }
75+
76+ var jobUpdatesForPredicateTests = []struct {
77+ oldGeneration int
78+ oldResourceVersion string
79+ oldActive int
80+ newGeneration int
81+ newResourceVersion string
82+ newActive int
83+ expected bool
84+ }{
85+ {0 , "1" , 0 , 0 , "1" , 0 , false }, // No-change update
86+ {1 , "1" , 0 , 1 , "1" , 0 , false }, // No-change update w/ generation field set
87+ {0 , "1" , 0 , 0 , "2" , 1 , true }, // Status change, no generation, version update
88+ {1 , "1" , 0 , 1 , "2" , 1 , true }, // Status change, generation, version update
89+ }
90+
91+ // Any other objects other than ManagedDatabase should reconcile unless there is no changes.
92+ //
93+ // NOTE: It doesn't look like generation in incremented (or used) for all types by the API server.
94+ // For example, as of 1.15, changing the Job types does not make use of the generation field.
95+ for _ , update := range jobUpdatesForPredicateTests {
96+ oldJob := job (int64 (update .oldGeneration ), update .oldResourceVersion , int32 (update .oldActive ))
97+ newJob := job (int64 (update .newGeneration ), update .newResourceVersion , int32 (update .newActive ))
98+
99+ updateEvent := event.UpdateEvent {
100+ MetaOld : & oldJob .ObjectMeta ,
101+ ObjectOld : oldJob ,
102+ MetaNew : & newJob .ObjectMeta ,
103+ ObjectNew : newJob ,
104+ }
105+
106+ assert .Equal (t , mdbPredicate .Update (updateEvent ), update .expected )
107+ }
56108}
0 commit comments