Skip to content

Commit 7e1fc71

Browse files
committed
test(mariadb): cover the absent Complete condition and expired deadlines
Every first reconcile after the operator Backup or Restore is created sees no Complete condition, and the guards that keep that case from dereferencing nil could be dropped with the outcome tables staying green. The tables now also cover the deadline failure for a run that never reports a known reason, and read the job status back from the API, so a lost status write fails them instead of passing on the in-memory object. Assisted-by: LLM Signed-off-by: Alexey Artamonov <[email protected]>
1 parent ccac0e1 commit 7e1fc71

1 file changed

Lines changed: 79 additions & 43 deletions

File tree

‎internal/backupcontroller/mariadbstrategy_controller_test.go‎

Lines changed: 79 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -871,30 +871,64 @@ func TestReconcileMariaDBRestore_KindCheckedBeforeSrcBackupGet(t *testing.T) {
871871
// JobFailed (SetCompleteWithJob in mariadb-operator 25.10.2), so the
872872
// Complete status alone does not tell success from failure.
873873
var mariadbCompleteOutcomeCases = []struct {
874-
name string
875-
status metav1.ConditionStatus
876-
reason string
877-
wantFailed bool
878-
wantDone bool
874+
name string
875+
noCondition bool
876+
status metav1.ConditionStatus
877+
reason string
878+
message string
879+
expired bool
880+
wantFailed bool
881+
wantDone bool
882+
wantMessage []string
879883
}{
880-
{name: "Complete=True/JobComplete succeeds", status: metav1.ConditionTrue, reason: mariadbtypes.ConditionReasonJobComplete, wantDone: true},
881-
{name: "Complete=True/JobFailed fails", status: metav1.ConditionTrue, reason: mariadbtypes.ConditionReasonJobFailed, wantFailed: true},
882-
{name: "Complete=False/JobFailed fails", status: metav1.ConditionFalse, reason: mariadbtypes.ConditionReasonJobFailed, wantFailed: true},
883-
{name: "Complete=True with an unknown reason keeps waiting", status: metav1.ConditionTrue, reason: "SomethingElse"},
884-
{name: "Complete=False/JobComplete keeps waiting", status: metav1.ConditionFalse, reason: mariadbtypes.ConditionReasonJobComplete},
885-
{name: "Complete=False/JobRunning keeps waiting", status: metav1.ConditionFalse, reason: mariadbtypes.ConditionReasonJobRunning},
884+
{name: "Complete=True/JobComplete succeeds", status: metav1.ConditionTrue, reason: mariadbtypes.ConditionReasonJobComplete, message: "Success", wantDone: true},
885+
{name: "Complete=True/JobFailed fails", status: metav1.ConditionTrue, reason: mariadbtypes.ConditionReasonJobFailed, message: "Failed", wantFailed: true,
886+
wantMessage: []string{"tenant/op"}},
887+
{name: "Complete=False/JobFailed fails", status: metav1.ConditionFalse, reason: mariadbtypes.ConditionReasonJobFailed, message: "Failed", wantFailed: true,
888+
wantMessage: []string{"tenant/op"}},
889+
{name: "Complete=True with an unknown reason keeps waiting", status: metav1.ConditionTrue, reason: "SomethingElse", message: "Something"},
890+
{name: "Complete=False/JobComplete keeps waiting", status: metav1.ConditionFalse, reason: mariadbtypes.ConditionReasonJobComplete, message: "Success"},
891+
{name: "Complete=False/JobRunning keeps waiting", status: metav1.ConditionFalse, reason: mariadbtypes.ConditionReasonJobRunning, message: "Running"},
892+
{name: "no Complete condition keeps waiting", noCondition: true},
893+
{name: "no Complete condition past the deadline fails", noCondition: true, expired: true, wantFailed: true,
894+
wantMessage: []string{"no Complete condition observed"}},
895+
{name: "Complete=True with an unknown reason past the deadline fails", status: metav1.ConditionTrue, reason: "SomethingElse", message: "Something", expired: true, wantFailed: true,
896+
wantMessage: []string{"Complete=True/SomethingElse"}},
886897
}
887898

888-
func mariadbCompleteCondition(status metav1.ConditionStatus, reason string) []metav1.Condition {
899+
func mariadbCompleteConditions(noCondition bool, status metav1.ConditionStatus, reason, message string) []metav1.Condition {
900+
if noCondition {
901+
return nil
902+
}
889903
return []metav1.Condition{{
890904
Type: mariadbtypes.ConditionTypeComplete,
891905
Status: status,
892906
Reason: reason,
893-
Message: "Failed",
907+
Message: message,
894908
LastTransitionTime: metav1.Now(),
895909
}}
896910
}
897911

912+
// mariadbOutcomeStartedAt returns a StartedAt that is fresh, or older than
913+
// both the backup and the default restore deadline.
914+
func mariadbOutcomeStartedAt(expired bool) *metav1.Time {
915+
if !expired {
916+
now := metav1.Now()
917+
return &now
918+
}
919+
past := metav1.NewTime(time.Now().Add(-2 * max(mariadbDefaultBackupDeadline, mariadbDefaultRestoreDeadline)))
920+
return &past
921+
}
922+
923+
func assertMariaDBFailureMessage(t *testing.T, message string, want []string) {
924+
t.Helper()
925+
for _, w := range want {
926+
if !strings.Contains(message, w) {
927+
t.Errorf("failure message must contain %q, got %q", w, message)
928+
}
929+
}
930+
}
931+
898932
func TestReconcileMariaDBBackup_CompleteConditionOutcome(t *testing.T) {
899933
strategyAPIGroup := strategyv1alpha1.GroupVersion.Group
900934
resolved := &ResolvedBackupConfig{
@@ -907,9 +941,8 @@ func TestReconcileMariaDBBackup_CompleteConditionOutcome(t *testing.T) {
907941

908942
for _, tc := range mariadbCompleteOutcomeCases {
909943
t.Run(tc.name, func(t *testing.T) {
910-
now := metav1.Now()
911944
seed := newMariaDBBackupJob("bj", "tenant")
912-
seed.Status.StartedAt = &now
945+
seed.Status.StartedAt = mariadbOutcomeStartedAt(tc.expired)
913946
strategy := &strategyv1alpha1.MariaDB{
914947
ObjectMeta: metav1.ObjectMeta{Name: "mdb-strategy"},
915948
Spec: strategyv1alpha1.MariaDBSpec{Template: *newRenderedMariaDBTemplate()},
@@ -920,13 +953,13 @@ func TestReconcileMariaDBBackup_CompleteConditionOutcome(t *testing.T) {
920953
operatorBackup := &mariadbtypes.Backup{
921954
ObjectMeta: metav1.ObjectMeta{
922955
Namespace: "tenant",
923-
Name: "bj-op",
956+
Name: "op",
924957
Labels: map[string]string{
925958
backupsv1alpha1.OwningJobNameLabel: seed.Name,
926959
backupsv1alpha1.OwningJobNamespaceLabel: seed.Namespace,
927960
},
928961
},
929-
Status: mariadbtypes.BackupStatus{Conditions: mariadbCompleteCondition(tc.status, tc.reason)},
962+
Status: mariadbtypes.BackupStatus{Conditions: mariadbCompleteConditions(tc.noCondition, tc.status, tc.reason, tc.message)},
930963
}
931964
c := newMariaDBStrategyTestClient(t, seed, newMariaDBApp(seed.Spec.ApplicationRef.Name, "tenant"),
932965
strategy, operatorMariaDB, operatorBackup)
@@ -941,31 +974,33 @@ func TestReconcileMariaDBBackup_CompleteConditionOutcome(t *testing.T) {
941974
t.Fatalf("reconcileMariaDB: %v", err)
942975
}
943976

977+
stored := &backupsv1alpha1.BackupJob{}
978+
if err := c.Get(context.Background(), client.ObjectKeyFromObject(seed), stored); err != nil {
979+
t.Fatalf("persisted Get: %v", err)
980+
}
944981
artifacts := &backupsv1alpha1.BackupList{}
945982
if err := c.List(context.Background(), artifacts, client.InNamespace("tenant")); err != nil {
946983
t.Fatalf("list Backups: %v", err)
947984
}
948985
switch {
949986
case tc.wantFailed:
950-
if bj.Status.Phase != backupsv1alpha1.BackupJobPhaseFailed {
951-
t.Errorf("expected Phase=Failed, got %q", bj.Status.Phase)
987+
if stored.Status.Phase != backupsv1alpha1.BackupJobPhaseFailed {
988+
t.Errorf("expected Phase=Failed, got %q", stored.Status.Phase)
952989
}
953-
if !strings.Contains(bj.Status.Message, "bj-op") {
954-
t.Errorf("failure message must name the operator Backup, got %q", bj.Status.Message)
955-
}
956-
if bj.Status.BackupRef != nil || len(artifacts.Items) != 0 {
957-
t.Errorf("a failed run must not produce a Backup: ref=%v artifacts=%d", bj.Status.BackupRef, len(artifacts.Items))
990+
assertMariaDBFailureMessage(t, stored.Status.Message, tc.wantMessage)
991+
if stored.Status.BackupRef != nil || len(artifacts.Items) != 0 {
992+
t.Errorf("a failed run must not produce a Backup: ref=%v artifacts=%d", stored.Status.BackupRef, len(artifacts.Items))
958993
}
959994
case tc.wantDone:
960-
if bj.Status.Phase != backupsv1alpha1.BackupJobPhaseSucceeded {
961-
t.Errorf("expected Phase=Succeeded, got %q (message %q)", bj.Status.Phase, bj.Status.Message)
995+
if stored.Status.Phase != backupsv1alpha1.BackupJobPhaseSucceeded {
996+
t.Errorf("expected Phase=Succeeded, got %q (message %q)", stored.Status.Phase, stored.Status.Message)
962997
}
963-
if bj.Status.BackupRef == nil || len(artifacts.Items) != 1 {
964-
t.Errorf("a successful run must produce exactly one Backup: ref=%v artifacts=%d", bj.Status.BackupRef, len(artifacts.Items))
998+
if stored.Status.BackupRef == nil || len(artifacts.Items) != 1 {
999+
t.Errorf("a successful run must produce exactly one Backup: ref=%v artifacts=%d", stored.Status.BackupRef, len(artifacts.Items))
9651000
}
9661001
default:
967-
if bj.Status.Phase != backupsv1alpha1.BackupJobPhaseRunning {
968-
t.Errorf("expected Phase=Running, got %q", bj.Status.Phase)
1002+
if stored.Status.Phase != backupsv1alpha1.BackupJobPhaseRunning {
1003+
t.Errorf("expected Phase=Running, got %q", stored.Status.Phase)
9691004
}
9701005
if res.RequeueAfter == 0 {
9711006
t.Errorf("expected a requeue while the run is in progress, got %+v", res)
@@ -983,7 +1018,6 @@ func TestReconcileMariaDBRestore_CompleteConditionOutcome(t *testing.T) {
9831018

9841019
for _, tc := range mariadbCompleteOutcomeCases {
9851020
t.Run(tc.name, func(t *testing.T) {
986-
now := metav1.Now()
9871021
cozyBackup := &backupsv1alpha1.Backup{
9881022
ObjectMeta: metav1.ObjectMeta{Namespace: "tenant", Name: "src-cozy-bk"},
9891023
Spec: backupsv1alpha1.BackupSpec{
@@ -997,18 +1031,18 @@ func TestReconcileMariaDBRestore_CompleteConditionOutcome(t *testing.T) {
9971031
BackupRef: corev1.LocalObjectReference{Name: cozyBackup.Name},
9981032
TargetApplicationRef: &corev1.TypedLocalObjectReference{Kind: "MariaDB", Name: "target", APIGroup: &apps},
9991033
},
1000-
Status: backupsv1alpha1.RestoreJobStatus{StartedAt: &now, Phase: backupsv1alpha1.RestoreJobPhaseRunning},
1034+
Status: backupsv1alpha1.RestoreJobStatus{StartedAt: mariadbOutcomeStartedAt(tc.expired), Phase: backupsv1alpha1.RestoreJobPhaseRunning},
10011035
}
10021036
operatorRestore := &mariadbtypes.Restore{
10031037
ObjectMeta: metav1.ObjectMeta{
10041038
Namespace: "tenant",
1005-
Name: "rj-op",
1039+
Name: "op",
10061040
Labels: map[string]string{
10071041
backupsv1alpha1.OwningJobNameLabel: seed.Name,
10081042
backupsv1alpha1.OwningJobNamespaceLabel: seed.Namespace,
10091043
},
10101044
},
1011-
Status: mariadbtypes.RestoreStatus{Conditions: mariadbCompleteCondition(tc.status, tc.reason)},
1045+
Status: mariadbtypes.RestoreStatus{Conditions: mariadbCompleteConditions(tc.noCondition, tc.status, tc.reason, tc.message)},
10121046
}
10131047
c := newMariaDBStrategyTestClient(t, cozyBackup, seed, operatorRestore,
10141048
&mariadbtypes.Backup{ObjectMeta: metav1.ObjectMeta{Namespace: "tenant", Name: "src-op-bk"}},
@@ -1024,21 +1058,23 @@ func TestReconcileMariaDBRestore_CompleteConditionOutcome(t *testing.T) {
10241058
t.Fatalf("reconcileMariaDBRestore: %v", err)
10251059
}
10261060

1061+
stored := &backupsv1alpha1.RestoreJob{}
1062+
if err := c.Get(context.Background(), client.ObjectKeyFromObject(seed), stored); err != nil {
1063+
t.Fatalf("persisted Get: %v", err)
1064+
}
10271065
switch {
10281066
case tc.wantFailed:
1029-
if rj.Status.Phase != backupsv1alpha1.RestoreJobPhaseFailed {
1030-
t.Errorf("expected Phase=Failed, got %q", rj.Status.Phase)
1031-
}
1032-
if !strings.Contains(rj.Status.Message, "rj-op") {
1033-
t.Errorf("failure message must name the operator Restore, got %q", rj.Status.Message)
1067+
if stored.Status.Phase != backupsv1alpha1.RestoreJobPhaseFailed {
1068+
t.Errorf("expected Phase=Failed, got %q", stored.Status.Phase)
10341069
}
1070+
assertMariaDBFailureMessage(t, stored.Status.Message, tc.wantMessage)
10351071
case tc.wantDone:
1036-
if rj.Status.Phase != backupsv1alpha1.RestoreJobPhaseSucceeded {
1037-
t.Errorf("expected Phase=Succeeded, got %q (message %q)", rj.Status.Phase, rj.Status.Message)
1072+
if stored.Status.Phase != backupsv1alpha1.RestoreJobPhaseSucceeded {
1073+
t.Errorf("expected Phase=Succeeded, got %q (message %q)", stored.Status.Phase, stored.Status.Message)
10381074
}
10391075
default:
1040-
if rj.Status.Phase != backupsv1alpha1.RestoreJobPhaseRunning {
1041-
t.Errorf("expected Phase=Running, got %q", rj.Status.Phase)
1076+
if stored.Status.Phase != backupsv1alpha1.RestoreJobPhaseRunning {
1077+
t.Errorf("expected Phase=Running, got %q", stored.Status.Phase)
10421078
}
10431079
if res.RequeueAfter == 0 {
10441080
t.Errorf("expected a requeue while the run is in progress, got %+v", res)

0 commit comments

Comments
 (0)