|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + |
| 8 | + dba "github.com/app-sre/dba-operator/api/v1alpha1" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "sigs.k8s.io/controller-runtime/pkg/event" |
| 11 | +) |
| 12 | + |
| 13 | +var managedDatabaseUpdatesForPredicateTests = []struct { |
| 14 | + oldGeneration int |
| 15 | + oldCurrentVersion string |
| 16 | + newGeneration int |
| 17 | + newCurrentVersion string |
| 18 | + expected bool |
| 19 | +}{ |
| 20 | + {0, "v1", 0, "v2", true}, // Status subresource not enabled, currentVersion change |
| 21 | + {0, "v1", 0, "v1", true}, // Status subresource not enabled, some update other than currentVersion |
| 22 | + {1, "v1", 1, "v1", false}, // Same generation, same currentVersion (change to some other field) |
| 23 | + {1, "v1", 1, "v2", true}, // currentVersion change, same generation |
| 24 | + {1, "v1", 2, "v1", true}, // Generation change, same currentVersion |
| 25 | + {1, "v1", 2, "v2", true}, // Generation change & version change |
| 26 | +} |
| 27 | + |
| 28 | +func managedDatabase(generation int64, currentVersion string) *dba.ManagedDatabase { |
| 29 | + managedDatabase := &dba.ManagedDatabase{ |
| 30 | + ObjectMeta: metav1.ObjectMeta{ |
| 31 | + Generation: generation, |
| 32 | + }, |
| 33 | + Status: dba.ManagedDatabaseStatus{ |
| 34 | + CurrentVersion: currentVersion, |
| 35 | + }, |
| 36 | + } |
| 37 | + return managedDatabase |
| 38 | +} |
| 39 | + |
| 40 | +func TestManagedDatabaseVersionChangedPredicate(t *testing.T) { |
| 41 | + mdbPredicate := ManagedDatabaseVersionChangedPredicate{} |
| 42 | + |
| 43 | + for _, update := range managedDatabaseUpdatesForPredicateTests { |
| 44 | + oldManagedDatabase := managedDatabase(int64(update.oldGeneration), update.oldCurrentVersion) |
| 45 | + newManagedDatabase := managedDatabase(int64(update.newGeneration), update.newCurrentVersion) |
| 46 | + |
| 47 | + updateEvent := event.UpdateEvent{ |
| 48 | + MetaOld: &oldManagedDatabase.ObjectMeta, |
| 49 | + ObjectOld: oldManagedDatabase, |
| 50 | + MetaNew: &newManagedDatabase.ObjectMeta, |
| 51 | + ObjectNew: newManagedDatabase, |
| 52 | + } |
| 53 | + |
| 54 | + assert.Equal(t, mdbPredicate.Update(updateEvent), update.expected) |
| 55 | + } |
| 56 | +} |
0 commit comments