Skip to content

Commit ffacfa1

Browse files
committed
Predicate tests
1 parent c421edd commit ffacfa1

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

controllers/event_filters_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/prometheus/client_golang v0.9.0
1515
github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e
1616
github.com/sirupsen/logrus v1.4.2 // indirect
17+
github.com/stretchr/testify v1.3.0
1718
gopkg.in/alecthomas/kingpin.v2 v2.2.6 // indirect
1819
gopkg.in/yaml.v2 v2.2.2
1920
k8s.io/api v0.0.0-20190409021203-6e4e0e4f393b

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ github.com/pborman/uuid v0.0.0-20170612153648-e790cca94e6c h1:MUyE44mTvnI5A0xrxI
7474
github.com/pborman/uuid v0.0.0-20170612153648-e790cca94e6c/go.mod h1:VyrYX9gd7irzKovcSS6BIIEwPRkP2Wm2m9ufcdFSJ34=
7575
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
7676
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
77+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
7778
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7879
github.com/prometheus/client_golang v0.9.0 h1:tXuTFVHC03mW0D+Ua1Q2d1EAVqLTuggX50V0VLICCzY=
7980
github.com/prometheus/client_golang v0.9.0/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
@@ -93,6 +94,7 @@ github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn
9394
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
9495
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
9596
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
97+
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
9698
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
9799
go.uber.org/atomic v1.3.2 h1:2Oa65PReHzfn29GpvgsYwloV9AVFHPDk8tYxt2c2tr4=
98100
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=

0 commit comments

Comments
 (0)