@@ -225,7 +225,7 @@ func TestRenderCNPGTemplate_RoundTripsParametersThroughSnapshot(t *testing.T) {
225225 }
226226
227227 // Round-trip parameters through Backup.status.underlyingResources.
228- raw , err := marshalCNPGBackupSnapshot (app , parameters )
228+ raw , err := marshalCNPGBackupSnapshot (app , parameters , "" )
229229 if err != nil {
230230 t .Fatalf ("marshal snapshot: %v" , err )
231231 }
@@ -880,7 +880,7 @@ func TestMarshalUnmarshalCNPGBackupSnapshot(t *testing.T) {
880880 "region" : "eu-west-1" ,
881881 }
882882
883- raw , err := marshalCNPGBackupSnapshot (src , parameters )
883+ raw , err := marshalCNPGBackupSnapshot (src , parameters , "" )
884884 if err != nil {
885885 t .Fatalf ("marshal: %v" , err )
886886 }
@@ -1033,7 +1033,7 @@ func TestCreateCNPGBackupArtifact_AlreadyExistsReturnsExisting(t *testing.T) {
10331033 }
10341034
10351035 sourceApp := newPostgresApp ("pg" , "tenant" )
1036- got , err := r .createCNPGBackupArtifact (context .Background (), j , resolved , cnpgBk , "postgres-pg" , "postgres-pg" , rendered , sourceApp )
1036+ got , err := r .createCNPGBackupArtifact (context .Background (), j , resolved , cnpgBk , "postgres-pg" , "postgres-pg" , "postgresql" , rendered , sourceApp )
10371037 if err != nil {
10381038 t .Fatalf ("expected AlreadyExists to be swallowed, got error %v" , err )
10391039 }
@@ -1059,7 +1059,7 @@ func TestApplyClusterPluginBackup_NotFoundOnMissingCluster(t *testing.T) {
10591059 },
10601060 }
10611061
1062- _ , err := r .applyClusterPluginBackup (context .Background (), "tenant" , "postgres-missing" , tmpl , "postgres-missing" )
1062+ _ , _ , err := r .applyClusterPluginBackup (context .Background (), "tenant" , "postgres-missing" , tmpl , "postgres-missing" )
10631063 if err == nil {
10641064 t .Fatalf ("expected NotFound error, got nil" )
10651065 }
@@ -1697,7 +1697,7 @@ func TestApplyClusterPluginBackup_ReadsTheLiveClusterNotTheCache(t *testing.T) {
16971697 BarmanObjectStore : strategyv1alpha1.BarmanObjectStoreTemplate {DestinationPath : "s3://bucket/x/" },
16981698 }
16991699
1700- got , err := r .applyClusterPluginBackup (context .Background (), "tenant" , "postgres-app" , tmpl , "strategy-name" )
1700+ got , _ , err := r .applyClusterPluginBackup (context .Background (), "tenant" , "postgres-app" , tmpl , "strategy-name" )
17011701 if err != nil {
17021702 t .Fatalf ("unexpected error: %v" , err )
17031703 }
@@ -1718,6 +1718,108 @@ func TestApplyClusterPluginBackup_ReadsTheLiveClusterNotTheCache(t *testing.T) {
17181718 }
17191719}
17201720
1721+ // TestReconcileCNPG_RecordsTheLiveClusterFlavor: the chart refuses a flavor
1722+ // change on a live Cluster, but the Postgres app keeps the refused value. The
1723+ // Backup must record the flavor the Cluster actually runs, or a restore would
1724+ // accept a target of the wrong image family and refuse the right one.
1725+ func TestReconcileCNPG_RecordsTheLiveClusterFlavor (t * testing.T ) {
1726+ apiGroup := backupsv1alpha1 .DefaultApplicationAPIGroup
1727+ strategyGroup := strategyv1alpha1 .GroupVersion .Group
1728+ const ns , appName = "tenant" , "pg"
1729+ clusterName := cnpgClusterNameForApp (appName )
1730+
1731+ cases := []struct {
1732+ name string
1733+ liveImage string
1734+ specFlavor string
1735+ want string
1736+ }{
1737+ {name : "app switched to postgis on a postgresql cluster" , liveImage : "ghcr.io/cloudnative-pg/postgresql:17.5" , specFlavor : "postgis" , want : "postgresql" },
1738+ {name : "app switched to postgresql on a postgis cluster" , liveImage : "ghcr.io/cloudnative-pg/postgis:17-3.5" , specFlavor : "" , want : "postgis" },
1739+ {name : "cluster on the CNPG default image" , liveImage : "" , specFlavor : "postgis" , want : "postgresql" },
1740+ }
1741+ for _ , tc := range cases {
1742+ t .Run (tc .name , func (t * testing.T ) {
1743+ startedAt := metav1 .Now ()
1744+ job := & backupsv1alpha1.BackupJob {
1745+ ObjectMeta : metav1.ObjectMeta {Namespace : ns , Name : "bj" },
1746+ Spec : backupsv1alpha1.BackupJobSpec {
1747+ ApplicationRef : corev1.TypedLocalObjectReference {APIGroup : & apiGroup , Kind : postgresAppKind , Name : appName },
1748+ },
1749+ Status : backupsv1alpha1.BackupJobStatus {StartedAt : & startedAt },
1750+ }
1751+ strategy := & strategyv1alpha1.CNPG {
1752+ ObjectMeta : metav1.ObjectMeta {Name : "cnpg-strategy" },
1753+ Spec : strategyv1alpha1.CNPGSpec {
1754+ Template : strategyv1alpha1.CNPGTemplate {
1755+ BarmanObjectStore : strategyv1alpha1.BarmanObjectStoreTemplate {DestinationPath : "s3://bucket/" },
1756+ },
1757+ },
1758+ }
1759+ app := newPostgresApp (appName , ns )
1760+ app .Spec .Flavor = tc .specFlavor
1761+ cluster := & cnpgtypes.Cluster {
1762+ ObjectMeta : metav1.ObjectMeta {Namespace : ns , Name : clusterName , UID : "cluster-uid" },
1763+ Spec : cnpgtypes.ClusterSpec {ImageName : tc .liveImage },
1764+ }
1765+ cnpgBackup := & cnpgtypes.Backup {
1766+ ObjectMeta : metav1.ObjectMeta {Namespace : ns , Name : "bj-cnpg" , Labels : map [string ]string {
1767+ backupsv1alpha1 .OwningJobNameLabel : job .Name ,
1768+ backupsv1alpha1 .OwningJobNamespaceLabel : job .Namespace ,
1769+ }},
1770+ Spec : cnpgtypes.BackupSpec {Cluster : cnpgtypes.ClusterReference {Name : clusterName }},
1771+ Status : cnpgtypes.BackupStatus {Phase : cnpgBackupPhaseComplete },
1772+ }
1773+ c := newCNPGStrategyTestClient (t , job , strategy , app , cluster , cnpgBackup )
1774+ r := & BackupJobReconciler {Client : c , Interface : cnpgDynamicFor (t , cluster )}
1775+ resolved := & ResolvedBackupConfig {
1776+ StrategyRef : corev1.TypedLocalObjectReference {APIGroup : & strategyGroup , Kind : strategyv1alpha1 .CNPGStrategyKind , Name : strategy .Name },
1777+ }
1778+
1779+ j := & backupsv1alpha1.BackupJob {}
1780+ if err := c .Get (context .Background (), client .ObjectKeyFromObject (job ), j ); err != nil {
1781+ t .Fatalf ("get BackupJob: %v" , err )
1782+ }
1783+ if _ , err := r .reconcileCNPG (context .Background (), j , resolved ); err != nil {
1784+ t .Fatalf ("reconcileCNPG: %v" , err )
1785+ }
1786+ if j .Status .Phase != backupsv1alpha1 .BackupJobPhaseSucceeded {
1787+ t .Fatalf ("BackupJob phase = %q (%s), want Succeeded" , j .Status .Phase , j .Status .Message )
1788+ }
1789+
1790+ backup := & backupsv1alpha1.Backup {}
1791+ if err := c .Get (context .Background (), client.ObjectKey {Namespace : ns , Name : job .Name }, backup ); err != nil {
1792+ t .Fatalf ("get Backup artifact: %v" , err )
1793+ }
1794+ snap , err := unmarshalCNPGBackupSnapshot (backup )
1795+ if err != nil {
1796+ t .Fatalf ("unmarshal snapshot: %v" , err )
1797+ }
1798+ if got := cnpgFlavor (snap .Flavor ); got != tc .want {
1799+ t .Fatalf ("Backup records flavor %q, want the live Cluster's %q" , got , tc .want )
1800+ }
1801+ })
1802+ }
1803+ }
1804+
1805+ // TestCNPGImageFlavor pins the image-to-flavor mapping to the chart's
1806+ // postgres.flavorGuard: only the image name counts, never the host, tag or
1807+ // digest.
1808+ func TestCNPGImageFlavor (t * testing.T ) {
1809+ cases := map [string ]string {
1810+ "" : "postgresql" ,
1811+ "ghcr.io/cloudnative-pg/postgresql:18.1" : "postgresql" ,
1812+ "ghcr.io/cloudnative-pg/postgis:18-3.6" : "postgis" ,
1813+ "mirror.local:5000/cloudnative-pg/postgis:18-3.6@sha256:0123" : "postgis" ,
1814+ "postgis@sha256:0123" : "postgis" ,
1815+ }
1816+ for image , want := range cases {
1817+ if got := cnpgImageFlavor (image ); got != want {
1818+ t .Errorf ("cnpgImageFlavor(%q) = %q, want %q" , image , got , want )
1819+ }
1820+ }
1821+ }
1822+
17211823// cnpgDynamicFor builds the dynamic client applyClusterPluginBackup reads
17221824// Clusters through, seeded independently of the typed client.
17231825func cnpgDynamicFor (t * testing.T , clusters ... * cnpgtypes.Cluster ) dynamic.Interface {
@@ -1805,7 +1907,7 @@ func TestApplyClusterPluginBackup_PreservesLiveServerName(t *testing.T) {
18051907 BarmanObjectStore : strategyv1alpha1.BarmanObjectStoreTemplate {DestinationPath : "s3://bucket/x/" },
18061908 }
18071909
1808- got , err := r .applyClusterPluginBackup (context .Background (), "tenant" , "postgres-app" , tmpl , "strategy-name" )
1910+ got , _ , err := r .applyClusterPluginBackup (context .Background (), "tenant" , "postgres-app" , tmpl , "strategy-name" )
18091911 if err != nil {
18101912 t .Fatalf ("unexpected error: %v" , err )
18111913 }
@@ -1844,7 +1946,7 @@ func TestApplyClusterPluginBackup_PatchesExistingCluster(t *testing.T) {
18441946 },
18451947 }
18461948
1847- if _ , err := r .applyClusterPluginBackup (context .Background (), "tenant" , "postgres-app" , tmpl , "tenant-app" ); err != nil {
1949+ if _ , _ , err := r .applyClusterPluginBackup (context .Background (), "tenant" , "postgres-app" , tmpl , "tenant-app" ); err != nil {
18481950 t .Fatalf ("unexpected error: %v" , err )
18491951 }
18501952
@@ -2177,7 +2279,7 @@ func TestReconcileCNPGRestore_RepeatInPlacePurgesStaleRecoveryCluster(t *testing
21772279
21782280 mkBackupArtifact := func (t * testing.T ) * backupsv1alpha1.Backup {
21792281 t .Helper ()
2180- snap , err := marshalCNPGBackupSnapshot (newPostgresApp (appName , ns ), nil )
2282+ snap , err := marshalCNPGBackupSnapshot (newPostgresApp (appName , ns ), nil , "" )
21812283 if err != nil {
21822284 t .Fatalf ("marshal snapshot: %v" , err )
21832285 }
@@ -2338,9 +2440,7 @@ func TestReconcileCNPGRestore_RefusesCrossFlavorRestore(t *testing.T) {
23382440 }
23392441 for _ , tc := range cases {
23402442 t .Run (tc .name , func (t * testing.T ) {
2341- source := newPostgresApp (appName , ns )
2342- source .Spec .Flavor = tc .sourceFlavor
2343- snap , err := marshalCNPGBackupSnapshot (source , nil )
2443+ snap , err := marshalCNPGBackupSnapshot (newPostgresApp (appName , ns ), nil , tc .sourceFlavor )
23442444 if err != nil {
23452445 t .Fatalf ("marshal snapshot: %v" , err )
23462446 }
@@ -2440,7 +2540,7 @@ func TestReconcileCNPGRestore_HealthyClusterSucceeds(t *testing.T) {
24402540 ctx := context .Background ()
24412541 startedAt := metav1 .NewTime (time .Now ())
24422542
2443- snap , err := marshalCNPGBackupSnapshot (newPostgresApp (appName , ns ), nil )
2543+ snap , err := marshalCNPGBackupSnapshot (newPostgresApp (appName , ns ), nil , "" )
24442544 if err != nil {
24452545 t .Fatalf ("marshal snapshot: %v" , err )
24462546 }
@@ -2559,7 +2659,7 @@ func TestReconcileCNPGRestore_HealthyDisablesBootstrap(t *testing.T) {
25592659 ctx := context .Background ()
25602660 startedAt := metav1 .NewTime (time .Now ().Add (- time .Hour ))
25612661
2562- snap , err := marshalCNPGBackupSnapshot (newPostgresApp (appName , ns ), nil )
2662+ snap , err := marshalCNPGBackupSnapshot (newPostgresApp (appName , ns ), nil , "" )
25632663 if err != nil {
25642664 t .Fatalf ("marshal snapshot: %v" , err )
25652665 }
@@ -2648,7 +2748,7 @@ func TestReconcileCNPGRestore_BootstrapDisableFailsPastDeadline(t *testing.T) {
26482748 // Well past the 30m default deadline.
26492749 startedAt := metav1 .NewTime (time .Now ().Add (- 72 * time .Hour ))
26502750
2651- snap , err := marshalCNPGBackupSnapshot (newPostgresApp (appName , ns ), nil )
2751+ snap , err := marshalCNPGBackupSnapshot (newPostgresApp (appName , ns ), nil , "" )
26522752 if err != nil {
26532753 t .Fatalf ("marshal snapshot: %v" , err )
26542754 }
@@ -2761,7 +2861,7 @@ func TestReconcileCNPGRestore_BootstrapDisableTransientErrorRequeues(t *testing.
27612861 startedAt := metav1 .NewTime (time .Now ().Add (- 72 * time .Hour ))
27622862 convergedAt := metav1 .Now ()
27632863
2764- snap , err := marshalCNPGBackupSnapshot (newPostgresApp (appName , ns ), nil )
2864+ snap , err := marshalCNPGBackupSnapshot (newPostgresApp (appName , ns ), nil , "" )
27652865 if err != nil {
27662866 t .Fatalf ("marshal snapshot: %v" , err )
27672867 }
@@ -2888,7 +2988,7 @@ func TestReconcileCNPGRestore_BootstrapDisableShortTimeoutHonorsGraceFloor(t *te
28882988 // within the 5m post-convergence floor.
28892989 convergedAt := metav1 .NewTime (time .Now ().Add (- 90 * time .Second ))
28902990
2891- snap , err := marshalCNPGBackupSnapshot (newPostgresApp (appName , ns ), nil )
2991+ snap , err := marshalCNPGBackupSnapshot (newPostgresApp (appName , ns ), nil , "" )
28922992 if err != nil {
28932993 t .Fatalf ("marshal snapshot: %v" , err )
28942994 }
@@ -3000,7 +3100,7 @@ func TestReconcileCNPGRestore_HealthyPastDeadlineSucceeds(t *testing.T) {
30003100 // Far past the 30m default deadline: this is the reconcile-gap case.
30013101 startedAt := metav1 .NewTime (time .Now ().Add (- 72 * time .Hour ))
30023102
3003- snap , err := marshalCNPGBackupSnapshot (newPostgresApp (appName , ns ), nil )
3103+ snap , err := marshalCNPGBackupSnapshot (newPostgresApp (appName , ns ), nil , "" )
30043104 if err != nil {
30053105 t .Fatalf ("marshal snapshot: %v" , err )
30063106 }
@@ -3096,7 +3196,7 @@ func TestReconcileCNPGRestore_DeadlineClassifiesRecoveryTargetUnreachable(t *tes
30963196 // enters the deadline-classification branch.
30973197 startedAt := metav1 .NewTime (time .Now ().Add (- 72 * time .Hour ))
30983198
3099- snap , err := marshalCNPGBackupSnapshot (newPostgresApp (appName , ns ), nil )
3199+ snap , err := marshalCNPGBackupSnapshot (newPostgresApp (appName , ns ), nil , "" )
31003200 if err != nil {
31013201 t .Fatalf ("marshal snapshot: %v" , err )
31023202 }
@@ -3435,7 +3535,7 @@ func TestReconcileCNPGRestore_PinsTheRequestedBackup(t *testing.T) {
34353535
34363536 mkBackupArtifact := func (t * testing.T ) * backupsv1alpha1.Backup {
34373537 t .Helper ()
3438- snap , err := marshalCNPGBackupSnapshot (newPostgresApp (appName , ns ), nil )
3538+ snap , err := marshalCNPGBackupSnapshot (newPostgresApp (appName , ns ), nil , "" )
34393539 if err != nil {
34403540 t .Fatalf ("marshal snapshot: %v" , err )
34413541 }
0 commit comments