|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +package backupcontroller |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "testing" |
| 7 | + |
| 8 | + corev1 "k8s.io/api/core/v1" |
| 9 | + "k8s.io/apimachinery/pkg/api/meta" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/apimachinery/pkg/runtime" |
| 12 | + "k8s.io/apimachinery/pkg/types" |
| 13 | + "k8s.io/client-go/kubernetes/scheme" |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 15 | + clientfake "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 17 | + |
| 18 | + strategyv1alpha1 "github.com/cozystack/cozystack/api/backups/strategy/v1alpha1" |
| 19 | + backupsv1alpha1 "github.com/cozystack/cozystack/api/backups/v1alpha1" |
| 20 | +) |
| 21 | + |
| 22 | +const lockNS = "tenant-acme" |
| 23 | + |
| 24 | +func lockTestClient(t *testing.T, objs ...client.Object) client.Client { |
| 25 | + t.Helper() |
| 26 | + s := runtime.NewScheme() |
| 27 | + _ = scheme.AddToScheme(s) |
| 28 | + _ = backupsv1alpha1.AddToScheme(s) |
| 29 | + return clientfake.NewClientBuilder(). |
| 30 | + WithScheme(s). |
| 31 | + WithObjects(objs...). |
| 32 | + WithStatusSubresource(&backupsv1alpha1.BackupJob{}, &backupsv1alpha1.RestoreJob{}). |
| 33 | + Build() |
| 34 | +} |
| 35 | + |
| 36 | +func pgRef(name string) corev1.TypedLocalObjectReference { |
| 37 | + return corev1.TypedLocalObjectReference{APIGroup: new("apps.cozystack.io"), Kind: "Postgres", Name: name} |
| 38 | +} |
| 39 | + |
| 40 | +func lockBackup(name string, app corev1.TypedLocalObjectReference) *backupsv1alpha1.Backup { |
| 41 | + return &backupsv1alpha1.Backup{ |
| 42 | + ObjectMeta: metav1.ObjectMeta{Namespace: lockNS, Name: name}, |
| 43 | + Spec: backupsv1alpha1.BackupSpec{ApplicationRef: app}, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func lockRestore(name, backup string, phase backupsv1alpha1.RestoreJobPhase, target *corev1.TypedLocalObjectReference, options string) *backupsv1alpha1.RestoreJob { |
| 48 | + rj := &backupsv1alpha1.RestoreJob{ |
| 49 | + ObjectMeta: metav1.ObjectMeta{Namespace: lockNS, Name: name}, |
| 50 | + Spec: backupsv1alpha1.RestoreJobSpec{ |
| 51 | + BackupRef: corev1.LocalObjectReference{Name: backup}, |
| 52 | + TargetApplicationRef: target, |
| 53 | + }, |
| 54 | + Status: backupsv1alpha1.RestoreJobStatus{Phase: phase}, |
| 55 | + } |
| 56 | + if options != "" { |
| 57 | + rj.Spec.Options = &runtime.RawExtension{Raw: []byte(options)} |
| 58 | + } |
| 59 | + return rj |
| 60 | +} |
| 61 | + |
| 62 | +func TestActiveRestoreTargeting(t *testing.T) { |
| 63 | + other := pgRef("other") |
| 64 | + anotherKind := corev1.TypedLocalObjectReference{APIGroup: new("apps.cozystack.io"), Kind: "MariaDB", Name: "pg"} |
| 65 | + for _, tc := range []struct { |
| 66 | + name string |
| 67 | + objects []client.Object |
| 68 | + want string |
| 69 | + }{ |
| 70 | + {"no restore", nil, ""}, |
| 71 | + {"in-place restore of the same application", []client.Object{ |
| 72 | + lockBackup("pg-1", pgRef("pg")), lockRestore("r1", "pg-1", backupsv1alpha1.RestoreJobPhaseRunning, nil, ""), |
| 73 | + }, "r1"}, |
| 74 | + {"restore not picked up yet", []client.Object{ |
| 75 | + lockBackup("pg-1", pgRef("pg")), lockRestore("r1", "pg-1", "", nil, ""), |
| 76 | + }, "r1"}, |
| 77 | + {"to-copy restore into this application", []client.Object{ |
| 78 | + lockBackup("other-1", other), lockRestore("r1", "other-1", backupsv1alpha1.RestoreJobPhaseRunning, &corev1.TypedLocalObjectReference{Kind: "Postgres", Name: "pg"}, ""), |
| 79 | + }, "r1"}, |
| 80 | + {"to-copy restore out of this application", []client.Object{ |
| 81 | + lockBackup("pg-1", pgRef("pg")), lockRestore("r1", "pg-1", backupsv1alpha1.RestoreJobPhaseRunning, &other, ""), |
| 82 | + }, ""}, |
| 83 | + {"finished restores", []client.Object{ |
| 84 | + lockBackup("pg-1", pgRef("pg")), |
| 85 | + lockRestore("r1", "pg-1", backupsv1alpha1.RestoreJobPhaseSucceeded, nil, ""), |
| 86 | + lockRestore("r2", "pg-1", backupsv1alpha1.RestoreJobPhaseFailed, nil, ""), |
| 87 | + }, ""}, |
| 88 | + {"same name, another kind", []client.Object{ |
| 89 | + lockBackup("m-1", anotherKind), lockRestore("r1", "m-1", backupsv1alpha1.RestoreJobPhaseRunning, nil, ""), |
| 90 | + }, ""}, |
| 91 | + {"restore sent to another namespace", []client.Object{ |
| 92 | + lockBackup("pg-1", pgRef("pg")), lockRestore("r1", "pg-1", backupsv1alpha1.RestoreJobPhaseRunning, nil, `{"targetNamespace":"tenant-other"}`), |
| 93 | + }, ""}, |
| 94 | + {"backup gone", []client.Object{ |
| 95 | + lockRestore("r1", "pg-1", backupsv1alpha1.RestoreJobPhaseRunning, nil, ""), |
| 96 | + }, ""}, |
| 97 | + } { |
| 98 | + t.Run(tc.name, func(t *testing.T) { |
| 99 | + c := lockTestClient(t, tc.objects...) |
| 100 | + got, err := activeRestoreTargeting(context.Background(), c, lockNS, pgRef("pg")) |
| 101 | + if err != nil { |
| 102 | + t.Fatalf("activeRestoreTargeting: %v", err) |
| 103 | + } |
| 104 | + if got != tc.want { |
| 105 | + t.Errorf("got %q, want %q", got, tc.want) |
| 106 | + } |
| 107 | + }) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func lockBackupClass() *backupsv1alpha1.BackupClass { |
| 112 | + return &backupsv1alpha1.BackupClass{ |
| 113 | + ObjectMeta: metav1.ObjectMeta{Name: "cozy-default"}, |
| 114 | + Spec: backupsv1alpha1.BackupClassSpec{ |
| 115 | + Strategies: []backupsv1alpha1.BackupClassStrategy{{ |
| 116 | + Application: backupsv1alpha1.ApplicationSelector{APIGroup: new("apps.cozystack.io"), Kind: "Postgres"}, |
| 117 | + StrategyRef: corev1.TypedLocalObjectReference{ |
| 118 | + APIGroup: new(strategyv1alpha1.GroupVersion.Group), Kind: strategyv1alpha1.CNPGStrategyKind, Name: "cozy-default-cnpg", |
| 119 | + }, |
| 120 | + }}, |
| 121 | + }, |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func lockBackupJob(started bool) *backupsv1alpha1.BackupJob { |
| 126 | + j := &backupsv1alpha1.BackupJob{ |
| 127 | + ObjectMeta: metav1.ObjectMeta{Namespace: lockNS, Name: "nightly-29842560"}, |
| 128 | + Spec: backupsv1alpha1.BackupJobSpec{BackupClassName: "cozy-default", ApplicationRef: pgRef("pg")}, |
| 129 | + } |
| 130 | + if started { |
| 131 | + now := metav1.Now() |
| 132 | + j.Status.StartedAt = &now |
| 133 | + j.Status.Phase = backupsv1alpha1.BackupJobPhaseRunning |
| 134 | + } |
| 135 | + return j |
| 136 | +} |
| 137 | + |
| 138 | +// TestReconcile_HoldsABackupJobWhileItsApplicationIsRestored is the |
| 139 | +// regression: a Plan-created BackupJob for a Postgres application under an |
| 140 | +// in-place restore reached the CNPG driver, which re-applied the Cluster's |
| 141 | +// barman plugin and put the restored cluster back on the source's WAL |
| 142 | +// prefix. The job must wait, non-terminal, without reaching projection or |
| 143 | +// dispatch; and once the restore is over, it must go ahead. |
| 144 | +func TestReconcile_HoldsABackupJobWhileItsApplicationIsRestored(t *testing.T) { |
| 145 | + restore := lockRestore("restore-pg", "pg-1", backupsv1alpha1.RestoreJobPhaseRunning, nil, "") |
| 146 | + c := lockTestClient(t, lockBackupJob(false), lockBackupClass(), lockBackup("pg-1", pgRef("pg")), restore, flatSourceSecret()) |
| 147 | + r := &BackupJobReconciler{Client: c, CredentialsConfig: defaultCfg()} |
| 148 | + key := types.NamespacedName{Namespace: lockNS, Name: "nightly-29842560"} |
| 149 | + |
| 150 | + res, err := r.Reconcile(context.Background(), reconcile.Request{NamespacedName: key}) |
| 151 | + if err != nil { |
| 152 | + t.Fatalf("Reconcile: %v", err) |
| 153 | + } |
| 154 | + if res.RequeueAfter != RestoreInProgressRequeue { |
| 155 | + t.Errorf("RequeueAfter = %v, want %v", res.RequeueAfter, RestoreInProgressRequeue) |
| 156 | + } |
| 157 | + got := &backupsv1alpha1.BackupJob{} |
| 158 | + if err := c.Get(context.Background(), key, got); err != nil { |
| 159 | + t.Fatal(err) |
| 160 | + } |
| 161 | + if got.Status.Phase != backupsv1alpha1.BackupJobPhasePending { |
| 162 | + t.Errorf("Phase = %q, want Pending", got.Status.Phase) |
| 163 | + } |
| 164 | + if got.Status.StartedAt != nil { |
| 165 | + t.Error("a held BackupJob must not be marked started") |
| 166 | + } |
| 167 | + cond := meta.FindStatusCondition(got.Status.Conditions, "Ready") |
| 168 | + if cond == nil || cond.Reason != ConditionReasonRestoreInProgress { |
| 169 | + t.Fatalf("Ready condition = %+v, want reason %s", cond, ConditionReasonRestoreInProgress) |
| 170 | + } |
| 171 | + secret := &corev1.Secret{} |
| 172 | + if err := c.Get(context.Background(), types.NamespacedName{Namespace: lockNS, Name: "cozy-backups-creds"}, secret); err == nil { |
| 173 | + t.Error("a held BackupJob went on to project credentials") |
| 174 | + } |
| 175 | + |
| 176 | + // The restore ends: the job is no longer held. |
| 177 | + fresh := &backupsv1alpha1.RestoreJob{} |
| 178 | + if err := c.Get(context.Background(), types.NamespacedName{Namespace: lockNS, Name: "restore-pg"}, fresh); err != nil { |
| 179 | + t.Fatal(err) |
| 180 | + } |
| 181 | + fresh.Status.Phase = backupsv1alpha1.RestoreJobPhaseSucceeded |
| 182 | + if err := c.Status().Update(context.Background(), fresh); err != nil { |
| 183 | + t.Fatal(err) |
| 184 | + } |
| 185 | + if held, err := activeRestoreTargeting(context.Background(), c, lockNS, pgRef("pg")); err != nil || held != "" { |
| 186 | + t.Errorf("after the restore ended: held by %q (%v), want none", held, err) |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +// TestReconcile_DoesNotHoldARunningBackupJob: the gate is on starting. A run |
| 191 | +// already moving data is left to its driver, whose own state it would |
| 192 | +// otherwise strand. |
| 193 | +func TestReconcile_DoesNotHoldARunningBackupJob(t *testing.T) { |
| 194 | + restore := lockRestore("restore-pg", "pg-1", backupsv1alpha1.RestoreJobPhaseRunning, nil, "") |
| 195 | + // No source Secret: projection fails transiently, which stops the |
| 196 | + // reconcile before dispatch and shows the gate was passed. |
| 197 | + c := lockTestClient(t, lockBackupJob(true), lockBackupClass(), lockBackup("pg-1", pgRef("pg")), restore) |
| 198 | + r := &BackupJobReconciler{Client: c, CredentialsConfig: defaultCfg()} |
| 199 | + key := types.NamespacedName{Namespace: lockNS, Name: "nightly-29842560"} |
| 200 | + |
| 201 | + if _, err := r.Reconcile(context.Background(), reconcile.Request{NamespacedName: key}); err != nil { |
| 202 | + t.Fatalf("Reconcile: %v", err) |
| 203 | + } |
| 204 | + got := &backupsv1alpha1.BackupJob{} |
| 205 | + if err := c.Get(context.Background(), key, got); err != nil { |
| 206 | + t.Fatal(err) |
| 207 | + } |
| 208 | + if cond := meta.FindStatusCondition(got.Status.Conditions, "Ready"); cond != nil && cond.Reason == ConditionReasonRestoreInProgress { |
| 209 | + t.Error("a BackupJob already started was held") |
| 210 | + } |
| 211 | +} |
0 commit comments