Files
cozystack/internal/backupcontroller/cnpg_source_image_test.go
T
Andrey Kolkov 21d66c4cce fix(postgres): keep bullseye clusters on their image until a clean locale check
Clusters created before the move to standard-trixie run the bare tag, the
system variant on Debian bullseye. Moving them changes glibc and ICU under
the data directory, and the collation repair pass cannot see every case
(C.UTF-8 carries no collation version), so text indexes can return wrong
results and unique indexes can accept duplicates.

Such a cluster now stays on its image until a pre-upgrade hook, run from
that image, finds nothing stored that depends on a collation other than C,
POSIX or builtin. The hook records clean, affected or unknown on the
Cluster; anything but clean holds the cluster, so a check that cannot
finish keeps it where it is rather than letting it move unchecked. Helm
renders before hooks run, so the hook fails an attempt only when its
answer contradicts what that attempt rendered, and the retry follows the
answer. allowCollationChange moves a held cluster on the owner's terms, and
a held cluster refuses a change of version, since new majors are built only
on the new images.

A physical restore replaces the Cluster that carries the answer. The CNPG
backup driver records the source Cluster's image in the backup snapshot and
passes it on as bootstrap.sourceImageName without interpreting it; the
chart restores onto it only when it is one of the bullseye tags it shipped,
and the next upgrade checks the restored data.

Assisted-By: LLM
Signed-off-by: Andrey Kolkov <androndo@gmail.com>
2026-11-27 18:21:43 +07:00

104 lines
4.3 KiB
Go

package backupcontroller
import (
"context"
"testing"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
clientfake "sigs.k8s.io/controller-runtime/pkg/client/fake"
strategyv1alpha1 "github.com/cozystack/cozystack/api/backups/strategy/v1alpha1"
backupsv1alpha1 "github.com/cozystack/cozystack/api/backups/v1alpha1"
cnpgtypes "github.com/cozystack/cozystack/internal/backupcontroller/cnpgtypes"
"github.com/cozystack/cozystack/internal/backupcontroller/postgresapp"
)
func TestApplyClusterPluginBackup_RecordsImageName(t *testing.T) {
for _, image := range []string{"ghcr.io/cloudnative-pg/postgresql:17.7", "ghcr.io/cloudnative-pg/postgresql:17.12-standard-trixie"} {
t.Run(image, func(t *testing.T) {
cluster := &cnpgtypes.Cluster{
ObjectMeta: metav1.ObjectMeta{Namespace: "tenant", Name: "postgres-app", UID: "cluster-uid-123"},
}
cluster.Spec.ImageName = image
c := newCNPGStrategyTestClient(t, cluster)
r := &BackupJobReconciler{Client: c, Interface: cnpgDynamicFor(t, cluster)}
tmpl := &strategyv1alpha1.CNPGTemplate{
BarmanObjectStore: strategyv1alpha1.BarmanObjectStoreTemplate{DestinationPath: "s3://bucket/x/"},
}
got, err := r.applyClusterPluginBackup(context.Background(), "tenant", "postgres-app", tmpl, "strategy-name", true, true, "bj")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got.imageName != image {
t.Fatalf("imageName = %q, want %q", got.imageName, image)
}
})
}
}
func TestCNPGBackupSnapshot_CarriesImageName(t *testing.T) {
raw, err := marshalCNPGBackupSnapshot(newPostgresApp("pg", "tenant"), nil, "postgresql", "ghcr.io/cloudnative-pg/postgresql:17.7")
if err != nil {
t.Fatalf("marshal snapshot: %v", err)
}
snap, err := unmarshalCNPGBackupSnapshot(&backupsv1alpha1.Backup{Status: backupsv1alpha1.BackupStatus{UnderlyingResources: raw}})
if err != nil {
t.Fatalf("unmarshal snapshot: %v", err)
}
if snap.ImageName != "ghcr.io/cloudnative-pg/postgresql:17.7" {
t.Fatalf("ImageName = %q", snap.ImageName)
}
}
func TestBuildPostgresAppRestorePatch_SetsAndClearsSourceImage(t *testing.T) {
app := newPostgresApp("pg", "tenant")
app.Spec.Bootstrap.SourceImageName = "ghcr.io/cloudnative-pg/postgresql:16.11"
if got := buildPostgresAppRestorePatch(app, "src", "pg-new", "s3://b/", "", "", "", nil, nil, nil, nil, "ghcr.io/cloudnative-pg/postgresql:17.7").Spec.Bootstrap.SourceImageName; got != "ghcr.io/cloudnative-pg/postgresql:17.7" {
t.Errorf("bootstrap.sourceImageName: got %q", got)
}
if got := buildPostgresAppRestorePatch(app, "src", "pg-new", "s3://b/", "", "", "", nil, nil, nil, nil, "").Spec.Bootstrap.SourceImageName; got != "" {
t.Errorf("bootstrap.sourceImageName from a previous restore must be cleared; got %q", got)
}
}
func TestDisablePostgresAppBootstrap_ClearsSourceImage(t *testing.T) {
app := newPostgresApp("pg", "tenant")
app.Spec.Bootstrap.Enabled = true
app.Spec.Bootstrap.SourceImageName = "ghcr.io/cloudnative-pg/postgresql:17.7"
s := runtime.NewScheme()
_ = scheme.AddToScheme(s)
_ = postgresapp.AddToScheme(s)
r := &RestoreJobReconciler{Client: clientfake.NewClientBuilder().WithScheme(s).WithObjects(app).Build()}
if err := r.disablePostgresAppBootstrap(context.Background(), "tenant", "pg"); err != nil {
t.Fatalf("disable bootstrap: %v", err)
}
got, err := r.getPostgresApp(context.Background(), "tenant", "pg")
if err != nil {
t.Fatalf("get app: %v", err)
}
if got.Spec.Bootstrap.Enabled || got.Spec.Bootstrap.SourceImageName != "" {
t.Fatalf("bootstrap after disable: enabled=%v sourceImageName=%q", got.Spec.Bootstrap.Enabled, got.Spec.Bootstrap.SourceImageName)
}
}
func TestCNPGImageMajor(t *testing.T) {
for image, want := range map[string]int{
"ghcr.io/cloudnative-pg/postgresql:17.7": 17,
"ghcr.io/cloudnative-pg/postgresql:13.22": 13,
"ghcr.io/cloudnative-pg/postgresql:17.12-standard-trixie": 17,
"ghcr.io/cloudnative-pg/postgis:17-3.6.1-standard-trixie": 17,
"ghcr.io/cloudnative-pg/postgresql:18.1@sha256:0123": 18,
"ghcr.io/cloudnative-pg/postgresql": 0,
"": 0,
} {
if got := cnpgImageMajor(image); got != want {
t.Errorf("cnpgImageMajor(%q) = %d, want %d", image, got, want)
}
}
}