@@ -549,43 +549,122 @@ func TestBuildPostgresAppRestorePatch_NoSecretRefIsSkipped(t *testing.T) {
549549// freshly-recovered Cluster on a status-update failure. The controller used
550550// to rely solely on a Status condition: if the post-purge Status().Update
551551// raced or failed, the next reconcile would re-purge the just-restored
552- // Cluster, destroying recovery progress. Cross-checking the live Cluster's
553- // bootstrap.recovery makes the second purge a no-op.
552+ // Cluster, destroying recovery progress. Cross-checking that the live
553+ // Cluster is a *freshly-recovered* one (bootstrap.recovery + created after
554+ // the job started) makes that second purge a no-op - while still purging a
555+ // stale recovery Cluster left over from an earlier completed restore, so a
556+ // repeat in-place restore is not a silent no-op.
554557func TestCNPGPurgeNeeded (t * testing.T ) {
555558 cases := []struct {
556- name string
557- purgedCondition bool
558- hasRecovery bool
559- want bool
559+ name string
560+ purgedCondition bool
561+ liveClusterFreshlyRecovered bool
562+ want bool
560563 }{
561564 {
562- name : "fresh restore, old cluster still in place: purge" ,
563- purgedCondition : false ,
564- hasRecovery : false ,
565- want : true ,
565+ name : "fresh restore, old cluster still in place: purge" ,
566+ purgedCondition : false ,
567+ liveClusterFreshlyRecovered : false ,
568+ want : true ,
566569 },
567570 {
568- name : "purge already recorded: skip" ,
569- purgedCondition : true ,
570- hasRecovery : false ,
571- want : false ,
571+ name : "purge already recorded: skip" ,
572+ purgedCondition : true ,
573+ liveClusterFreshlyRecovered : false ,
574+ want : false ,
572575 },
573576 {
574- name : "live cluster already recovered (status write raced): skip - the bug fix " ,
575- purgedCondition : false ,
576- hasRecovery : true ,
577- want : false ,
577+ name : "live cluster freshly recovered by this restore (status write raced): skip" ,
578+ purgedCondition : false ,
579+ liveClusterFreshlyRecovered : true ,
580+ want : false ,
578581 },
579582 {
580- name : "both true: skip (post-purge steady state)" ,
581- purgedCondition : true ,
582- hasRecovery : true ,
583- want : false ,
583+ name : "stale recovery cluster from a previous completed restore: purge (repeat-restore fix)" ,
584+ purgedCondition : false ,
585+ liveClusterFreshlyRecovered : false ,
586+ want : true ,
587+ },
588+ {
589+ name : "both true: skip (post-purge steady state)" ,
590+ purgedCondition : true ,
591+ liveClusterFreshlyRecovered : true ,
592+ want : false ,
593+ },
594+ }
595+ for _ , tc := range cases {
596+ t .Run (tc .name , func (t * testing.T ) {
597+ got := cnpgPurgeNeeded (tc .purgedCondition , tc .liveClusterFreshlyRecovered )
598+ if got != tc .want {
599+ t .Errorf ("got %v want %v" , got , tc .want )
600+ }
601+ })
602+ }
603+ }
604+
605+ // TestCNPGClusterFreshlyRecovered locks in the signal that separates a
606+ // recovery Cluster THIS restore just produced from one left over by an
607+ // earlier completed restore. The repeat-in-place-restore bug was exactly a
608+ // stale recovery Cluster (created before StartedAt) being mistaken for a
609+ // freshly-recovered one and skipping the purge.
610+ func TestCNPGClusterFreshlyRecovered (t * testing.T ) {
611+ started := metav1 .NewTime (time .Now ())
612+ after := metav1 .NewTime (started .Add (time .Minute ))
613+ before := metav1 .NewTime (started .Add (- time .Hour ))
614+
615+ cases := []struct {
616+ name string
617+ hasRecovery bool
618+ createdAt * metav1.Time
619+ startedAt * metav1.Time
620+ want bool
621+ }{
622+ {
623+ name : "no recovery bootstrap: not fresh" ,
624+ hasRecovery : false ,
625+ createdAt : & after ,
626+ startedAt : & started ,
627+ want : false ,
628+ },
629+ {
630+ name : "recovery cluster created after start (our own purge re-render): fresh" ,
631+ hasRecovery : true ,
632+ createdAt : & after ,
633+ startedAt : & started ,
634+ want : true ,
635+ },
636+ {
637+ name : "recovery cluster created before start (leftover from previous restore): not fresh" ,
638+ hasRecovery : true ,
639+ createdAt : & before ,
640+ startedAt : & started ,
641+ want : false ,
642+ },
643+ {
644+ name : "recovery cluster created exactly at start: fresh (boundary)" ,
645+ hasRecovery : true ,
646+ createdAt : & started ,
647+ startedAt : & started ,
648+ want : true ,
649+ },
650+ {
651+ name : "missing cluster creation timestamp: not fresh" ,
652+ hasRecovery : true ,
653+ createdAt : nil ,
654+ startedAt : & started ,
655+ want : false ,
656+ },
657+ {
658+ name : "missing job start timestamp: not fresh" ,
659+ hasRecovery : true ,
660+ createdAt : & after ,
661+ startedAt : nil ,
662+ want : false ,
584663 },
585664 }
586665 for _ , tc := range cases {
587666 t .Run (tc .name , func (t * testing.T ) {
588- got := cnpgPurgeNeeded (tc .purgedCondition , tc .hasRecovery )
667+ got := cnpgClusterFreshlyRecovered (tc .hasRecovery , tc .createdAt , tc . startedAt )
589668 if got != tc .want {
590669 t .Errorf ("got %v want %v" , got , tc .want )
591670 }
@@ -898,7 +977,7 @@ func TestApplyClusterBarmanObjectStore_NotFoundOnMissingCluster(t *testing.T) {
898977 }
899978}
900979
901- // TestClusterHasRecoveryBootstrap_TerminatingCluster locks in the
980+ // TestRecoveryBootstrapClusterState_TerminatingCluster locks in the
902981// DeletionTimestamp guard. Without it, a Cluster CR mid-deletion (after
903982// purgeExistingCluster fired r.Delete but cnpg.io's finalizers haven't
904983// drained yet) would get treated as "still has bootstrap" by the
@@ -907,7 +986,7 @@ func TestApplyClusterBarmanObjectStore_NotFoundOnMissingCluster(t *testing.T) {
907986// might SSA-merge the chart's bootstrap.recovery onto the terminating
908987// CR and cnpg-operator's bootstrap-immutability check would drop the
909988// change, leaving the cluster on the original initdb spec.
910- func TestClusterHasRecoveryBootstrap_TerminatingCluster (t * testing.T ) {
989+ func TestRecoveryBootstrapClusterState_TerminatingCluster (t * testing.T ) {
911990 now := metav1 .Now ()
912991 t .Run ("terminating cluster reports not-yet-recovered" , func (t * testing.T ) {
913992 cluster := & cnpgtypes.Cluster {
@@ -925,18 +1004,22 @@ func TestClusterHasRecoveryBootstrap_TerminatingCluster(t *testing.T) {
9251004 }
9261005 c := newCNPGStrategyTestClient (t , cluster )
9271006 r := & RestoreJobReconciler {Client : c }
928- got , err := r .clusterHasRecoveryBootstrap (context .Background (), "tenant" , "postgres-app" )
1007+ got , createdAt , err := r .recoveryBootstrapClusterState (context .Background (), "tenant" , "postgres-app" )
9291008 if err != nil {
9301009 t .Fatalf ("unexpected error: %v" , err )
9311010 }
9321011 if got {
9331012 t .Fatalf ("expected false for terminating cluster, got true" )
9341013 }
1014+ if createdAt != nil {
1015+ t .Fatalf ("expected nil createdAt for terminating cluster, got %v" , createdAt )
1016+ }
9351017 })
9361018
937- t .Run ("live recovery cluster reports recovered" , func (t * testing.T ) {
1019+ t .Run ("live recovery cluster reports recovered with creation timestamp" , func (t * testing.T ) {
1020+ created := metav1 .NewTime (time .Now ().Add (- time .Hour ))
9381021 cluster := & cnpgtypes.Cluster {
939- ObjectMeta : metav1.ObjectMeta {Namespace : "tenant" , Name : "postgres-app" },
1022+ ObjectMeta : metav1.ObjectMeta {Namespace : "tenant" , Name : "postgres-app" , CreationTimestamp : created },
9401023 Spec : cnpgtypes.ClusterSpec {
9411024 Bootstrap : & cnpgtypes.BootstrapConfiguration {
9421025 Recovery : & cnpgtypes.RecoverySource {Source : "pg-src" },
@@ -945,25 +1028,31 @@ func TestClusterHasRecoveryBootstrap_TerminatingCluster(t *testing.T) {
9451028 }
9461029 c := newCNPGStrategyTestClient (t , cluster )
9471030 r := & RestoreJobReconciler {Client : c }
948- got , err := r .clusterHasRecoveryBootstrap (context .Background (), "tenant" , "postgres-app" )
1031+ got , createdAt , err := r .recoveryBootstrapClusterState (context .Background (), "tenant" , "postgres-app" )
9491032 if err != nil {
9501033 t .Fatalf ("unexpected error: %v" , err )
9511034 }
9521035 if ! got {
9531036 t .Fatalf ("expected true for live recovery cluster, got false" )
9541037 }
1038+ if createdAt == nil {
1039+ t .Fatalf ("expected non-nil createdAt for live recovery cluster" )
1040+ }
9551041 })
9561042
9571043 t .Run ("missing cluster reports not-yet" , func (t * testing.T ) {
9581044 c := newCNPGStrategyTestClient (t )
9591045 r := & RestoreJobReconciler {Client : c }
960- got , err := r .clusterHasRecoveryBootstrap (context .Background (), "tenant" , "postgres-app" )
1046+ got , createdAt , err := r .recoveryBootstrapClusterState (context .Background (), "tenant" , "postgres-app" )
9611047 if err != nil {
9621048 t .Fatalf ("unexpected error: %v" , err )
9631049 }
9641050 if got {
9651051 t .Fatalf ("expected false for missing cluster, got true" )
9661052 }
1053+ if createdAt != nil {
1054+ t .Fatalf ("expected nil createdAt for missing cluster, got %v" , createdAt )
1055+ }
9671056 })
9681057}
9691058
0 commit comments