@@ -18,13 +18,14 @@ import (
1818 "k8s.io/apimachinery/pkg/api/resource"
1919 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2020 "k8s.io/client-go/discovery"
21- "k8s.io/client-go/tools/record"
2221 "sigs.k8s.io/controller-runtime/pkg/client"
2322
23+ "github.com/crunchydata/postgres-operator/internal/controller/runtime"
2424 "github.com/crunchydata/postgres-operator/internal/feature"
2525 "github.com/crunchydata/postgres-operator/internal/initialize"
2626 "github.com/crunchydata/postgres-operator/internal/naming"
2727 "github.com/crunchydata/postgres-operator/internal/testing/cmp"
28+ "github.com/crunchydata/postgres-operator/internal/testing/events"
2829 "github.com/crunchydata/postgres-operator/internal/testing/require"
2930 "github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
3031
@@ -38,7 +39,7 @@ func TestReconcileVolumeSnapshots(t *testing.T) {
3839 discoveryClient , err := discovery .NewDiscoveryClientForConfig (cfg )
3940 assert .NilError (t , err )
4041
41- recorder := record . NewFakeRecorder ( 100 )
42+ recorder := events . NewRecorder ( t , runtime . Scheme )
4243 r := & Reconciler {
4344 Client : cc ,
4445 Owner : client .FieldOwner (t .Name ()),
@@ -47,8 +48,15 @@ func TestReconcileVolumeSnapshots(t *testing.T) {
4748 }
4849 ns := setupNamespace (t , cc )
4950
51+ // Enable snapshots feature gate
52+ gate := feature .NewGate ()
53+ assert .NilError (t , gate .SetFromMap (map [string ]bool {
54+ feature .VolumeSnapshots : true ,
55+ }))
56+ ctx = feature .NewContext (ctx , gate )
57+
5058 t .Run ("SnapshotsDisabledDeleteSnapshots" , func (t * testing.T ) {
51- // Create cluster
59+ // Create cluster (without snapshots spec)
5260 cluster := testCluster ()
5361 cluster .Namespace = ns .Name
5462 cluster .ObjectMeta .UID = "the-uid-123"
@@ -96,11 +104,11 @@ func TestReconcileVolumeSnapshots(t *testing.T) {
96104 })
97105
98106 t .Run ("SnapshotsEnabledTablespacesEnabled" , func (t * testing.T ) {
99- // Enable snapshots feature gate
107+ // Enable both tablespaces and snapshots feature gates
100108 gate := feature .NewGate ()
101109 assert .NilError (t , gate .SetFromMap (map [string ]bool {
102- feature .VolumeSnapshots : true ,
103110 feature .TablespaceVolumes : true ,
111+ feature .VolumeSnapshots : true ,
104112 }))
105113 ctx := feature .NewContext (ctx , gate )
106114
@@ -126,20 +134,17 @@ func TestReconcileVolumeSnapshots(t *testing.T) {
126134 err = r .reconcileVolumeSnapshots (ctx , cluster , pvc )
127135 assert .NilError (t , err )
128136
129- // Assert warning event was created
130- event , ok := <- recorder .Events
131- assert .Equal (t , ok , true )
132- assert .Assert (t , cmp .Contains (event , "IncompatibleFeatures" ))
137+ // Assert warning event was created and has expected attributes
138+ if assert .Check (t , len (recorder .Events ) > 0 ) {
139+ assert .Equal (t , recorder .Events [0 ].Type , "Warning" )
140+ assert .Equal (t , recorder .Events [0 ].Regarding .Kind , "PostgresCluster" )
141+ assert .Equal (t , recorder .Events [0 ].Regarding .Name , "hippo" )
142+ assert .Equal (t , recorder .Events [0 ].Reason , "IncompatibleFeatures" )
143+ assert .Assert (t , cmp .Contains (recorder .Events [0 ].Note , "VolumeSnapshots not currently compatible with TablespaceVolumes" ))
144+ }
133145 })
134146
135147 t .Run ("SnapshotsEnabledNoPvcAnnotation" , func (t * testing.T ) {
136- // Enable snapshots feature gate
137- gate := feature .NewGate ()
138- assert .NilError (t , gate .SetFromMap (map [string ]bool {
139- feature .VolumeSnapshots : true ,
140- }))
141- ctx := feature .NewContext (ctx , gate )
142-
143148 // Create a volume snapshot class
144149 volumeSnapshotClassName := "my-snapshotclass"
145150 volumeSnapshotClass := & volumesnapshotv1.VolumeSnapshotClass {
@@ -183,13 +188,6 @@ func TestReconcileVolumeSnapshots(t *testing.T) {
183188 })
184189
185190 t .Run ("SnapshotsEnabledReadySnapshotsExist" , func (t * testing.T ) {
186- // Enable snapshots feature gate
187- gate := feature .NewGate ()
188- assert .NilError (t , gate .SetFromMap (map [string ]bool {
189- feature .VolumeSnapshots : true ,
190- }))
191- ctx := feature .NewContext (ctx , gate )
192-
193191 // volumeSnapshotClass still exists
194192 // Create a cluster with snapshots enabled
195193 cluster := testCluster ()
@@ -303,13 +301,6 @@ func TestReconcileVolumeSnapshots(t *testing.T) {
303301 })
304302
305303 t .Run ("SnapshotsEnabledCreateSnapshot" , func (t * testing.T ) {
306- // Enable snapshots feature gate
307- gate := feature .NewGate ()
308- assert .NilError (t , gate .SetFromMap (map [string ]bool {
309- feature .VolumeSnapshots : true ,
310- }))
311- ctx := feature .NewContext (ctx , gate )
312-
313304 // volumeSnapshotClass still exists
314305 // Create a cluster with snapshots enabled
315306 cluster := testCluster ()
@@ -357,16 +348,23 @@ func TestReconcileDedicatedSnapshotVolume(t *testing.T) {
357348 discoveryClient , err := discovery .NewDiscoveryClientForConfig (cfg )
358349 assert .NilError (t , err )
359350
360- recorder := record . NewFakeRecorder ( 100 )
351+ recorder := events . NewRecorder ( t , runtime . Scheme )
361352 r := & Reconciler {
362353 Client : cc ,
363354 Owner : client .FieldOwner (t .Name ()),
364355 DiscoveryClient : discoveryClient ,
365356 Recorder : recorder ,
366357 }
367358
359+ // Enable snapshots feature gate
360+ gate := feature .NewGate ()
361+ assert .NilError (t , gate .SetFromMap (map [string ]bool {
362+ feature .VolumeSnapshots : true ,
363+ }))
364+ ctx = feature .NewContext (ctx , gate )
365+
368366 t .Run ("SnapshotsDisabledDeletePvc" , func (t * testing.T ) {
369- // Create cluster
367+ // Create cluster without snapshots spec
370368 ns := setupNamespace (t , cc )
371369 cluster := testCluster ()
372370 cluster .Namespace = ns .Name
@@ -426,13 +424,6 @@ func TestReconcileDedicatedSnapshotVolume(t *testing.T) {
426424 })
427425
428426 t .Run ("SnapshotsEnabledCreatePvcNoBackupNoRestore" , func (t * testing.T ) {
429- // Enable snapshots feature gate
430- gate := feature .NewGate ()
431- assert .NilError (t , gate .SetFromMap (map [string ]bool {
432- feature .VolumeSnapshots : true ,
433- }))
434- ctx := feature .NewContext (ctx , gate )
435-
436427 // Create cluster with snapshots enabled
437428 ns := setupNamespace (t , cc )
438429 cluster := testCluster ()
@@ -466,13 +457,6 @@ func TestReconcileDedicatedSnapshotVolume(t *testing.T) {
466457 })
467458
468459 t .Run ("SnapshotsEnabledBackupExistsCreateRestore" , func (t * testing.T ) {
469- // Enable snapshots feature gate
470- gate := feature .NewGate ()
471- assert .NilError (t , gate .SetFromMap (map [string ]bool {
472- feature .VolumeSnapshots : true ,
473- }))
474- ctx := feature .NewContext (ctx , gate )
475-
476460 // Create cluster with snapshots enabled
477461 ns := setupNamespace (t , cc )
478462 cluster := testCluster ()
@@ -524,13 +508,6 @@ func TestReconcileDedicatedSnapshotVolume(t *testing.T) {
524508 })
525509
526510 t .Run ("SnapshotsEnabledSuccessfulRestoreExists" , func (t * testing.T ) {
527- // Enable snapshots feature gate
528- gate := feature .NewGate ()
529- assert .NilError (t , gate .SetFromMap (map [string ]bool {
530- feature .VolumeSnapshots : true ,
531- }))
532- ctx := feature .NewContext (ctx , gate )
533-
534511 // Create cluster with snapshots enabled
535512 ns := setupNamespace (t , cc )
536513 cluster := testCluster ()
@@ -604,13 +581,6 @@ func TestReconcileDedicatedSnapshotVolume(t *testing.T) {
604581 })
605582
606583 t .Run ("SnapshotsEnabledFailedRestoreExists" , func (t * testing.T ) {
607- // Enable snapshots feature gate
608- gate := feature .NewGate ()
609- assert .NilError (t , gate .SetFromMap (map [string ]bool {
610- feature .VolumeSnapshots : true ,
611- }))
612- ctx := feature .NewContext (ctx , gate )
613-
614584 // Create cluster with snapshots enabled
615585 ns := setupNamespace (t , cc )
616586 cluster := testCluster ()
@@ -668,10 +638,14 @@ func TestReconcileDedicatedSnapshotVolume(t *testing.T) {
668638 assert .NilError (t , err )
669639 assert .Assert (t , pvc != nil )
670640
671- // Assert warning event was created
672- event , ok := <- recorder .Events
673- assert .Equal (t , ok , true )
674- assert .Assert (t , cmp .Contains (event , "DedicatedSnapshotVolumeRestoreJobError" ))
641+ // Assert warning event was created and has expected attributes
642+ if assert .Check (t , len (recorder .Events ) > 0 ) {
643+ assert .Equal (t , recorder .Events [0 ].Type , "Warning" )
644+ assert .Equal (t , recorder .Events [0 ].Regarding .Kind , "PostgresCluster" )
645+ assert .Equal (t , recorder .Events [0 ].Regarding .Name , "hippo" )
646+ assert .Equal (t , recorder .Events [0 ].Reason , "DedicatedSnapshotVolumeRestoreJobError" )
647+ assert .Assert (t , cmp .Contains (recorder .Events [0 ].Note , "restore job failed, check the logs" ))
648+ }
675649 })
676650}
677651
0 commit comments