test(postgres): pin that an older attach record keeps the latch

The rollout check takes the later of the latch and the Cluster's attach
record. A record left by an earlier attach of a plugin since removed is
older than the latch when someone who keeps no record attaches the
plugin again, and taking it would accept an instance recreated before
the latch as one that rolled with the plugin.

The record is absent not only when no backup job attached the plugin
but also when the chart did, or a controller that kept no record, so
the comment on the field says that.

Assisted-by: LLM
Signed-off-by: Alexey Artamonov <aleksei.artamonov@aenix.io>
This commit is contained in:
Alexey Artamonov committed 2026-11-10 00:08:41 +03:00
1 parent 325d2b815c
commit 8d92aee796
2 files changed
+40 -1

No files matched your search

@@ -835,7 +835,8 @@ type cnpgPluginAttachment struct {
// missing reports a Cluster that carried no barman-cloud plugin.
missing bool
// attachedAt is the cnpgPluginAttachedAtAnnotation the Cluster carried,
// "" when no backup job attached the plugin.
// "" when it carries no record: the plugin was attached by the chart, or
// before the record existed.
attachedAt string
clusterStatus cnpgtypes.ClusterStatus
}
@@ -2985,6 +2985,44 @@ func TestReconcileCNPG_LostAttachRecordStillWaitsForTheRollout(t *testing.T) {
}
}
// A record older than the latch is left from an earlier attach of a plugin
// since removed; the plugin found now was attached by someone who keeps no
// record, after the latch, so an instance recreated between the record and
// the latch still has to roll.
func TestReconcileCNPG_AttachRecordOlderThanTheLatchKeepsTheLatch(t *testing.T) {
latchedAt := time.Now().Add(-10 * time.Minute).Truncate(time.Second)
oldPod := newRolloutInstancePod(latchedAt.Add(-2 * time.Hour))
f := newCNPGRolloutFixture(t, newRolloutBackupJob(latchedAt.Add(-2*time.Hour)),
newRolloutCluster(cnpgClusterHealthyPhase), oldPod)
j, _ := f.pass() // latch
latch := apimeta.FindStatusCondition(j.Status.Conditions, backupCondPluginRolledOut)
if latch == nil {
t.Fatalf("no %s latch: %+v", backupCondPluginRolledOut, j.Status.Conditions)
}
latch.LastTransitionTime = metav1.NewTime(latchedAt)
if err := f.c.Status().Update(context.Background(), j); err != nil {
t.Fatalf("age the latch: %v", err)
}
f.replacePod(oldPod, newRolloutInstancePod(latchedAt.Add(-time.Minute)))
live := newRolloutCluster(cnpgClusterHealthyPhase, buildBarmanPlugin(rolloutCluster, rolloutCluster))
live.Annotations = map[string]string{cnpgPluginAttachedAtAnnotation: latchedAt.Add(-time.Hour).UTC().Format(time.RFC3339)}
f.observeCNPG(live)
var backups []cnpgtypes.Backup
for range 3 {
if j, backups = f.pass(); len(backups) != 0 || j.Status.Phase == backupsv1alpha1.BackupJobPhaseFailed {
break
}
}
if len(backups) != 0 {
t.Fatalf("a cnpg.io/Backup was created against an instance recreated before the latch: %+v", backups)
}
if j.Status.Phase == backupsv1alpha1.BackupJobPhaseFailed {
t.Fatalf("the job failed instead of waiting for the rollout: %s", j.Status.Message)
}
}
// A later write to a Cluster that already carries the plugin re-sends the
// attach record as it is: moving it would make the instances that rolled
// with the plugin look older than the attach.