Skip to content

Commit 5c6fc10

Browse files
committed
test(postgres): pin three guards no test caught when changed
A job whose own Backup exists skips the wait for another job's plugin rollout, and the size probe keeps the strategy's Secret key names while it takes the Secret from the record, adding a credentials block only when the strategy has none. Each of these could be changed with the suite still green: dropping the own-Backup check, or forcing the missing-credentials branch either way. Cases for a job with a running Backup next to a recent foreign attach, a strategy without credentials and a strategy with custom key names now fail on each change. Assisted-by: LLM Signed-off-by: Alexey Artamonov <[email protected]>
1 parent 1d22013 commit 5c6fc10

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

‎internal/backupcontroller/cnpg_artifact_size_test.go‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,3 +514,65 @@ func TestCreateCNPGBackupArtifact_SizeProbeSkipsARecordWithoutCredentials(t *tes
514514
t.Errorf("Artifact = %#v, want no size", got.Status.Artifact)
515515
}
516516
}
517+
518+
// probeRecordedBackup runs createCNPGBackupArtifact for a backup recorded as
519+
// written with credentials Secret recordedSecret, against rendered, and
520+
// returns the size it measured and the connection it measured through.
521+
func probeRecordedBackup(t *testing.T, rendered *strategyv1alpha1.CNPGTemplate, recordedSecret *corev1.Secret) (int64, s3Connection) {
522+
t.Helper()
523+
apiGroup := backupsv1alpha1.DefaultApplicationAPIGroup
524+
j := &backupsv1alpha1.BackupJob{
525+
ObjectMeta: metav1.ObjectMeta{Namespace: "tenant", Name: "bj-probe"},
526+
Spec: backupsv1alpha1.BackupJobSpec{
527+
ApplicationRef: corev1.TypedLocalObjectReference{APIGroup: &apiGroup, Kind: postgresAppKind, Name: "pg"},
528+
},
529+
}
530+
cnpgBk := &cnpgtypes.Backup{ObjectMeta: metav1.ObjectMeta{Name: "cnpg-bk"}, Status: cnpgtypes.BackupStatus{BackupID: "20261021T100000"}}
531+
secretName := recordedSecret.Name
532+
written := cnpgDestination{DestinationPath: "s3://bkt/tenant/pg/", EndpointURL: "https://s3.example:8443", S3SecretRef: &secretName}
533+
l := &fakeS3Lister{objects: []s3ObjectInfo{{Key: "tenant/pg/srv/base/20261021T100000/data.tar.gz", Size: 42}}}
534+
r := reconcilerWithLister(t, l)
535+
r.Client = newCNPGStrategyTestClient(t, recordedSecret)
536+
537+
got, err := r.createCNPGBackupArtifact(context.Background(), j, &ResolvedBackupConfig{}, cnpgBk, "postgres-pg", "srv", "postgresql", written, rendered, newPostgresApp("pg", "tenant"))
538+
if err != nil {
539+
t.Fatalf("createCNPGBackupArtifact: %v", err)
540+
}
541+
if got.Status.Artifact == nil {
542+
t.Fatalf("no artifact")
543+
}
544+
return got.Status.Artifact.SizeBytes, l.conn
545+
}
546+
547+
// A strategy that has since dropped its credentials still measures a backup
548+
// recorded with a Secret, through that Secret and its default keys.
549+
func TestCreateCNPGBackupArtifact_SizeProbeOfARecordedSecretUnderAStrategyWithoutCredentials(t *testing.T) {
550+
store := newSizedBarmanObjectStore()
551+
store.S3Credentials = nil
552+
size, conn := probeRecordedBackup(t, &strategyv1alpha1.CNPGTemplate{BarmanObjectStore: *store}, s3CredsSecret("tenant", "creds-recorded"))
553+
if size != 42 {
554+
t.Errorf("SizeBytes = %d, want 42 measured through the recorded Secret", size)
555+
}
556+
if conn.AccessKey != "AKID" {
557+
t.Errorf("probed with access key %q, want the recorded Secret's AKID", conn.AccessKey)
558+
}
559+
}
560+
561+
// The record names the Secret, not its keys: the strategy's key names stay,
562+
// so a strategy with its own key names still reads the recorded Secret.
563+
func TestCreateCNPGBackupArtifact_SizeProbeKeepsTheStrategysSecretKeys(t *testing.T) {
564+
store := newSizedBarmanObjectStore()
565+
store.S3Credentials.AccessKeyIDKey = "id"
566+
store.S3Credentials.SecretAccessKeyKey = "key"
567+
secret := &corev1.Secret{
568+
ObjectMeta: metav1.ObjectMeta{Namespace: "tenant", Name: "creds-recorded"},
569+
Data: map[string][]byte{"id": []byte("CUSTOMID"), "key": []byte("custom-secret")},
570+
}
571+
size, conn := probeRecordedBackup(t, &strategyv1alpha1.CNPGTemplate{BarmanObjectStore: *store}, secret)
572+
if size != 42 {
573+
t.Errorf("SizeBytes = %d, want 42 measured with the strategy's key names", size)
574+
}
575+
if conn.AccessKey != "CUSTOMID" {
576+
t.Errorf("probed with access key %q, want CUSTOMID read under the strategy's key name", conn.AccessKey)
577+
}
578+
}

‎internal/backupcontroller/cnpgstrategy_controller_test.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3824,6 +3824,23 @@ func TestReconcileCNPG_JobWithoutLatchWaitsForAnotherJobsRollout(t *testing.T) {
38243824
}
38253825
}
38263826

3827+
// A job whose own Backup already exists is past every wait before it: it
3828+
// tracks that Backup even while another job's recent attach is still rolling
3829+
// the instances, since waiting would only hold the result of a Backup that
3830+
// runs regardless.
3831+
func TestReconcileCNPG_JobWithItsOwnBackupDoesNotWaitForAnotherJobsRollout(t *testing.T) {
3832+
cluster := newRolloutCluster(cnpgClusterHealthyPhase, buildBarmanPlugin(rolloutCluster, rolloutCluster))
3833+
cluster.Annotations = map[string]string{cnpgPluginAttachedAtAnnotation: time.Now().Add(-5 * time.Minute).UTC().Format(time.RFC3339)}
3834+
f := newCNPGRolloutFixture(t, newRolloutBackupJob(time.Now().Add(-time.Hour)), cluster,
3835+
newRolloutInstancePod(time.Now().Add(-10*time.Minute).Truncate(time.Second)))
3836+
f.seedCNPGBackup("bj-cnpg", "bj", rolloutCluster, "running")
3837+
3838+
j, _ := f.pass()
3839+
if ready := apimeta.FindStatusCondition(j.Status.Conditions, "Ready"); ready == nil || ready.Reason != "WaitingForCNPGBackup" {
3840+
t.Errorf("Ready = %+v, want WaitingForCNPGBackup tracking the job's own Backup", ready)
3841+
}
3842+
}
3843+
38273844
// A plugin attached by someone else after the latch, another backup job or
38283845
// the chart re-rendering a restored Cluster outside the platform flow,
38293846
// started the rollout before this job's write, which only finds it there:

0 commit comments

Comments
 (0)