@@ -2584,11 +2584,11 @@ func newCNPGStrategyTestClient(t *testing.T, objs ...client.Object) client.Clien
25842584// the stream (no recoveryTime) or the newest one ending by recoveryTime, on
25852585// any timeline, which is not necessarily the backup the RestoreJob names.
25862586func TestCNPGRestoreBackupID (t * testing.T ) {
2587- stopped := time .Date (2026 , 9 , 21 , 6 , 27 , 23 , 0 , time .UTC )
2587+ stopped := metav1 . NewTime ( time .Date (2026 , 9 , 21 , 6 , 27 , 23 , 0 , time .UTC ) )
25882588 cases := []struct {
25892589 name string
25902590 backupID string
2591- stoppedAt * time .Time
2591+ stoppedAt * metav1 .Time
25922592 recoveryTime string
25932593 want string
25942594 }{
@@ -2604,11 +2604,17 @@ func TestCNPGRestoreBackupID(t *testing.T) {
26042604 }
26052605 for _ , tc := range cases {
26062606 t .Run (tc .name , func (t * testing.T ) {
2607- if got := cnpgRestoreBackupID (tc .backupID , tc .stoppedAt , tc .recoveryTime ); got != tc .want {
2607+ source := & cnpgtypes.Backup {Status : cnpgtypes.BackupStatus {BackupID : tc .backupID , StoppedAt : tc .stoppedAt }}
2608+ if got := cnpgRestoreBackupID (source , tc .recoveryTime ); got != tc .want {
26082609 t .Errorf ("cnpgRestoreBackupID(%q, %v, %q) = %q, want %q" , tc .backupID , tc .stoppedAt , tc .recoveryTime , got , tc .want )
26092610 }
26102611 })
26112612 }
2613+ t .Run ("a cnpg.io/Backup that is gone pins nothing" , func (t * testing.T ) {
2614+ if got := cnpgRestoreBackupID (nil , "" ); got != "" {
2615+ t .Errorf ("cnpgRestoreBackupID(nil, \" \" ) = %q, want none" , got )
2616+ }
2617+ })
26122618}
26132619
26142620// TestBuildPostgresAppRestorePatch_ReplacesBackupID guards against a
@@ -2628,8 +2634,10 @@ func TestBuildPostgresAppRestorePatch_ReplacesBackupID(t *testing.T) {
26282634
26292635// TestReconcileCNPGRestore_PinsTheRequestedBackup drives a restore through the
26302636// purge step and reads back what the target Postgres app is patched with: the
2631- // barman backupId of the backup the RestoreJob names, taken from the artifact,
2632- // or from its cnpg.io/Backup for an artifact that predates recording it.
2637+ // barman backupId of the backup the RestoreJob names, read from its
2638+ // cnpg.io/Backup. Retention deletes that cnpg.io/Backup together with the base
2639+ // backup, and pinning an id the catalog has lost fails the recovery after the
2640+ // purge, so a cnpg.io/Backup that is gone leaves the choice to the plugin.
26332641func TestReconcileCNPGRestore_PinsTheRequestedBackup (t * testing.T ) {
26342642 const (
26352643 ns = "tenant"
@@ -2644,7 +2652,7 @@ func TestReconcileCNPGRestore_PinsTheRequestedBackup(t *testing.T) {
26442652 startedAt := metav1 .NewTime (time .Now ())
26452653 stoppedAt := metav1 .NewTime (time .Date (2026 , 9 , 21 , 6 , 27 , 23 , 0 , time .UTC ))
26462654
2647- mkBackupArtifact := func (t * testing.T , md map [ string ] string ) * backupsv1alpha1.Backup {
2655+ mkBackupArtifact := func (t * testing.T ) * backupsv1alpha1.Backup {
26482656 t .Helper ()
26492657 snap , err := marshalCNPGBackupSnapshot (newPostgresApp (appName , ns ), nil )
26502658 if err != nil {
@@ -2655,9 +2663,6 @@ func TestReconcileCNPGRestore_PinsTheRequestedBackup(t *testing.T) {
26552663 cnpgDestinationPathKey : "s3://bucket/" + appName + "/" ,
26562664 cnpgBackupNameKey : cnpgBkName ,
26572665 }
2658- for k , v := range md {
2659- driverMD [k ] = v
2660- }
26612666 return & backupsv1alpha1.Backup {
26622667 ObjectMeta : metav1.ObjectMeta {Namespace : ns , Name : "bk" },
26632668 Spec : backupsv1alpha1.BackupSpec {
@@ -2698,10 +2703,16 @@ func TestReconcileCNPGRestore_PinsTheRequestedBackup(t *testing.T) {
26982703 }
26992704 return rj
27002705 }
2701- restoredBootstrap := func (t * testing.T , backup * backupsv1alpha1. Backup , cnpgBackup * cnpgtypes.Backup , recoveryTime string ) postgresapp.Bootstrap {
2706+ restoredBootstrap := func (t * testing.T , cnpgBackup * cnpgtypes.Backup , recoveryTime string ) ( postgresapp.Bootstrap , * record. FakeRecorder ) {
27022707 t .Helper ()
2703- c := newCNPGStrategyTestClient (t , backup , mkRestoreJob (recoveryTime ), strategy , cnpgBackup , newPostgresApp (appName , ns ))
2704- r := & RestoreJobReconciler {Client : c , Interface : dynamicfake .NewSimpleDynamicClient (testCNPGScheme (t ))}
2708+ backup := mkBackupArtifact (t )
2709+ objs := []client.Object {backup , mkRestoreJob (recoveryTime ), strategy , newPostgresApp (appName , ns )}
2710+ if cnpgBackup != nil {
2711+ objs = append (objs , cnpgBackup )
2712+ }
2713+ c := newCNPGStrategyTestClient (t , objs ... )
2714+ recorder := record .NewFakeRecorder (10 )
2715+ r := & RestoreJobReconciler {Client : c , Interface : dynamicfake .NewSimpleDynamicClient (testCNPGScheme (t )), Recorder : recorder }
27052716 rj := & backupsv1alpha1.RestoreJob {}
27062717 if err := c .Get (ctx , client.ObjectKey {Namespace : ns , Name : "rj" }, rj ); err != nil {
27072718 t .Fatalf ("get seeded RestoreJob: %v" , err )
@@ -2716,73 +2727,42 @@ func TestReconcileCNPGRestore_PinsTheRequestedBackup(t *testing.T) {
27162727 if ! app .Spec .Bootstrap .Enabled {
27172728 t .Fatalf ("the reconcile did not reach the restore patch" )
27182729 }
2719- return app .Spec .Bootstrap
2730+ return app .Spec .Bootstrap , recorder
27202731 }
27212732
27222733 t .Run ("without recoveryTime the requested backup is pinned" , func (t * testing.T ) {
2723- md := map [string ]string {cnpgBackupIDKey : backupID , cnpgBackupStoppedAtKey : stoppedAt .UTC ().Format (time .RFC3339 )}
2724- if got := restoredBootstrap (t , mkBackupArtifact (t , md ), mkCNPGBackup (backupID ), "" ).BackupID ; got != backupID {
2725- t .Errorf ("bootstrap.backupID: got %q want %q" , got , backupID )
2734+ if b , _ := restoredBootstrap (t , mkCNPGBackup (backupID ), "" ); b .BackupID != backupID {
2735+ t .Errorf ("bootstrap.backupID: got %q want %q" , b .BackupID , backupID )
27262736 }
27272737 })
27282738
27292739 t .Run ("a recoveryTime after the backup ended pins it" , func (t * testing.T ) {
2730- md := map [string ]string {cnpgBackupIDKey : backupID , cnpgBackupStoppedAtKey : stoppedAt .UTC ().Format (time .RFC3339 )}
2731- b := restoredBootstrap (t , mkBackupArtifact (t , md ), mkCNPGBackup (backupID ), "2026-09-22T10:00:00Z" )
2740+ b , _ := restoredBootstrap (t , mkCNPGBackup (backupID ), "2026-09-22T10:00:00Z" )
27322741 if b .BackupID != backupID || b .RecoveryTime != "2026-09-22T10:00:00Z" {
27332742 t .Errorf ("bootstrap: got backupID %q recoveryTime %q, want %q and the requested instant" , b .BackupID , b .RecoveryTime , backupID )
27342743 }
27352744 })
27362745
27372746 t .Run ("a recoveryTime before the backup ended leaves the choice to the plugin" , func (t * testing.T ) {
2738- md := map [string ]string {cnpgBackupIDKey : backupID , cnpgBackupStoppedAtKey : stoppedAt .UTC ().Format (time .RFC3339 )}
2739- if got := restoredBootstrap (t , mkBackupArtifact (t , md ), mkCNPGBackup (backupID ), "2026-09-21T05:00:00Z" ).BackupID ; got != "" {
2740- t .Errorf ("bootstrap.backupID: got %q, want none: the backup cannot serve an instant before its end" , got )
2741- }
2742- })
2743-
2744- t .Run ("an artifact without the recorded id falls back to its cnpg.io/Backup" , func (t * testing.T ) {
2745- if got := restoredBootstrap (t , mkBackupArtifact (t , nil ), mkCNPGBackup (backupID ), "" ).BackupID ; got != backupID {
2746- t .Errorf ("bootstrap.backupID: got %q want %q" , got , backupID )
2747+ if b , _ := restoredBootstrap (t , mkCNPGBackup (backupID ), "2026-09-21T05:00:00Z" ); b .BackupID != "" {
2748+ t .Errorf ("bootstrap.backupID: got %q, want none: the backup cannot serve an instant before its end" , b .BackupID )
27472749 }
27482750 })
2749- }
2750-
2751- // TestCreateCNPGBackupArtifact_RecordsBackupIdentity: a restore pins the
2752- // backup it starts from by barman's backupId, and must be able to after the
2753- // cnpg.io/Backup is gone, so the artifact carries it and the backup's end.
2754- func TestCreateCNPGBackupArtifact_RecordsBackupIdentity (t * testing.T ) {
2755- apiGroup := backupsv1alpha1 .DefaultApplicationAPIGroup
2756- strategyGroup := strategyv1alpha1 .GroupVersion .Group
2757- c := newCNPGStrategyTestClient (t )
2758- r := & BackupJobReconciler {Client : c }
27592751
2760- j := & backupsv1alpha1.BackupJob {
2761- ObjectMeta : metav1.ObjectMeta {Namespace : "tenant" , Name : "bj" },
2762- Spec : backupsv1alpha1.BackupJobSpec {
2763- ApplicationRef : corev1.TypedLocalObjectReference {APIGroup : & apiGroup , Kind : postgresAppKind , Name : "pg" },
2764- },
2765- }
2766- resolved := & ResolvedBackupConfig {
2767- StrategyRef : corev1.TypedLocalObjectReference {APIGroup : & strategyGroup , Kind : strategyv1alpha1 .CNPGStrategyKind , Name : "strat" },
2768- }
2769- stopped := metav1 .NewTime (time .Date (2026 , 9 , 21 , 6 , 27 , 23 , 0 , time .UTC ))
2770- cnpgBk := & cnpgtypes.Backup {
2771- ObjectMeta : metav1.ObjectMeta {Name : "cnpg-bk" },
2772- Status : cnpgtypes.BackupStatus {BackupID : "20260921T062713" , StoppedAt : & stopped },
2773- }
2774- rendered := & strategyv1alpha1.CNPGTemplate {
2775- BarmanObjectStore : strategyv1alpha1.BarmanObjectStoreTemplate {DestinationPath : "s3://b/" },
2776- }
2777-
2778- got , err := r .createCNPGBackupArtifact (context .Background (), j , resolved , cnpgBk , "postgres-pg" , "postgres-pg" , rendered , newPostgresApp ("pg" , "tenant" ))
2779- if err != nil {
2780- t .Fatalf ("createCNPGBackupArtifact: %v" , err )
2781- }
2782- if id := got .Spec .DriverMetadata [cnpgBackupIDKey ]; id != "20260921T062713" {
2783- t .Errorf ("driverMetadata[%s]: got %q" , cnpgBackupIDKey , id )
2784- }
2785- if at := got .Spec .DriverMetadata [cnpgBackupStoppedAtKey ]; at != "2026-09-21T06:27:23Z" {
2786- t .Errorf ("driverMetadata[%s]: got %q" , cnpgBackupStoppedAtKey , at )
2752+ for _ , recoveryTime := range []string {"" , "2026-09-22T10:00:00Z" } {
2753+ t .Run ("a cnpg.io/Backup removed by retention leaves the choice to the plugin, recoveryTime=" + recoveryTime , func (t * testing.T ) {
2754+ b , recorder := restoredBootstrap (t , nil , recoveryTime )
2755+ if b .BackupID != "" {
2756+ t .Errorf ("bootstrap.backupID: got %q, want none: the catalog no longer holds that base backup" , b .BackupID )
2757+ }
2758+ select {
2759+ case ev := <- recorder .Events :
2760+ if ! strings .Contains (ev , "Warning BaseBackupGone" ) {
2761+ t .Errorf ("event: got %q, want a BaseBackupGone warning" , ev )
2762+ }
2763+ default :
2764+ t .Errorf ("no event: a restore that cannot start from the backup it names must say so" )
2765+ }
2766+ })
27872767 }
27882768}
0 commit comments