@@ -867,6 +867,187 @@ func TestReconcileMariaDBRestore_KindCheckedBeforeSrcBackupGet(t *testing.T) {
867867 }
868868}
869869
870+ // The operator reports a failed backing Job as Complete=True with reason
871+ // JobFailed (SetCompleteWithJob in mariadb-operator 25.10.2), so the
872+ // Complete status alone does not tell success from failure.
873+ var mariadbCompleteOutcomeCases = []struct {
874+ name string
875+ status metav1.ConditionStatus
876+ reason string
877+ wantFailed bool
878+ wantDone bool
879+ }{
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 },
886+ }
887+
888+ func mariadbCompleteCondition (status metav1.ConditionStatus , reason string ) []metav1.Condition {
889+ return []metav1.Condition {{
890+ Type : mariadbtypes .ConditionTypeComplete ,
891+ Status : status ,
892+ Reason : reason ,
893+ Message : "Failed" ,
894+ LastTransitionTime : metav1 .Now (),
895+ }}
896+ }
897+
898+ func TestReconcileMariaDBBackup_CompleteConditionOutcome (t * testing.T ) {
899+ strategyAPIGroup := strategyv1alpha1 .GroupVersion .Group
900+ resolved := & ResolvedBackupConfig {
901+ StrategyRef : corev1.TypedLocalObjectReference {
902+ APIGroup : & strategyAPIGroup ,
903+ Kind : strategyv1alpha1 .MariaDBStrategyKind ,
904+ Name : "mdb-strategy" ,
905+ },
906+ }
907+
908+ for _ , tc := range mariadbCompleteOutcomeCases {
909+ t .Run (tc .name , func (t * testing.T ) {
910+ now := metav1 .Now ()
911+ seed := newMariaDBBackupJob ("bj" , "tenant" )
912+ seed .Status .StartedAt = & now
913+ strategy := & strategyv1alpha1.MariaDB {
914+ ObjectMeta : metav1.ObjectMeta {Name : "mdb-strategy" },
915+ Spec : strategyv1alpha1.MariaDBSpec {Template : * newRenderedMariaDBTemplate ()},
916+ }
917+ operatorMariaDB := & mariadbtypes.MariaDB {
918+ ObjectMeta : metav1.ObjectMeta {Namespace : "tenant" , Name : mariadbNameForApp (seed .Spec .ApplicationRef .Name )},
919+ }
920+ operatorBackup := & mariadbtypes.Backup {
921+ ObjectMeta : metav1.ObjectMeta {
922+ Namespace : "tenant" ,
923+ Name : "bj-op" ,
924+ Labels : map [string ]string {
925+ backupsv1alpha1 .OwningJobNameLabel : seed .Name ,
926+ backupsv1alpha1 .OwningJobNamespaceLabel : seed .Namespace ,
927+ },
928+ },
929+ Status : mariadbtypes.BackupStatus {Conditions : mariadbCompleteCondition (tc .status , tc .reason )},
930+ }
931+ c := newMariaDBStrategyTestClient (t , seed , newMariaDBApp (seed .Spec .ApplicationRef .Name , "tenant" ),
932+ strategy , operatorMariaDB , operatorBackup )
933+ r := & BackupJobReconciler {Client : c , Scheme : c .Scheme (), Recorder : record .NewFakeRecorder (10 )}
934+
935+ bj := & backupsv1alpha1.BackupJob {}
936+ if err := c .Get (context .Background (), client .ObjectKeyFromObject (seed ), bj ); err != nil {
937+ t .Fatalf ("seed Get: %v" , err )
938+ }
939+ res , err := r .reconcileMariaDB (context .Background (), bj , resolved )
940+ if err != nil {
941+ t .Fatalf ("reconcileMariaDB: %v" , err )
942+ }
943+
944+ artifacts := & backupsv1alpha1.BackupList {}
945+ if err := c .List (context .Background (), artifacts , client .InNamespace ("tenant" )); err != nil {
946+ t .Fatalf ("list Backups: %v" , err )
947+ }
948+ switch {
949+ case tc .wantFailed :
950+ if bj .Status .Phase != backupsv1alpha1 .BackupJobPhaseFailed {
951+ t .Errorf ("expected Phase=Failed, got %q" , bj .Status .Phase )
952+ }
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 ))
958+ }
959+ 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 )
962+ }
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 ))
965+ }
966+ default :
967+ if bj .Status .Phase != backupsv1alpha1 .BackupJobPhaseRunning {
968+ t .Errorf ("expected Phase=Running, got %q" , bj .Status .Phase )
969+ }
970+ if res .RequeueAfter == 0 {
971+ t .Errorf ("expected a requeue while the run is in progress, got %+v" , res )
972+ }
973+ if len (artifacts .Items ) != 0 {
974+ t .Errorf ("an unfinished run must not produce a Backup, got %d" , len (artifacts .Items ))
975+ }
976+ }
977+ })
978+ }
979+ }
980+
981+ func TestReconcileMariaDBRestore_CompleteConditionOutcome (t * testing.T ) {
982+ apps := mariadbapp .GroupName
983+
984+ for _ , tc := range mariadbCompleteOutcomeCases {
985+ t .Run (tc .name , func (t * testing.T ) {
986+ now := metav1 .Now ()
987+ cozyBackup := & backupsv1alpha1.Backup {
988+ ObjectMeta : metav1.ObjectMeta {Namespace : "tenant" , Name : "src-cozy-bk" },
989+ Spec : backupsv1alpha1.BackupSpec {
990+ ApplicationRef : corev1.TypedLocalObjectReference {Kind : "MariaDB" , Name : "src" , APIGroup : & apps },
991+ DriverMetadata : map [string ]string {mariadbBackupNameKey : "src-op-bk" },
992+ },
993+ }
994+ seed := & backupsv1alpha1.RestoreJob {
995+ ObjectMeta : metav1.ObjectMeta {Namespace : "tenant" , Name : "rj" },
996+ Spec : backupsv1alpha1.RestoreJobSpec {
997+ BackupRef : corev1.LocalObjectReference {Name : cozyBackup .Name },
998+ TargetApplicationRef : & corev1.TypedLocalObjectReference {Kind : "MariaDB" , Name : "target" , APIGroup : & apps },
999+ },
1000+ Status : backupsv1alpha1.RestoreJobStatus {StartedAt : & now , Phase : backupsv1alpha1 .RestoreJobPhaseRunning },
1001+ }
1002+ operatorRestore := & mariadbtypes.Restore {
1003+ ObjectMeta : metav1.ObjectMeta {
1004+ Namespace : "tenant" ,
1005+ Name : "rj-op" ,
1006+ Labels : map [string ]string {
1007+ backupsv1alpha1 .OwningJobNameLabel : seed .Name ,
1008+ backupsv1alpha1 .OwningJobNamespaceLabel : seed .Namespace ,
1009+ },
1010+ },
1011+ Status : mariadbtypes.RestoreStatus {Conditions : mariadbCompleteCondition (tc .status , tc .reason )},
1012+ }
1013+ c := newMariaDBStrategyTestClient (t , cozyBackup , seed , operatorRestore ,
1014+ & mariadbtypes.Backup {ObjectMeta : metav1.ObjectMeta {Namespace : "tenant" , Name : "src-op-bk" }},
1015+ & mariadbtypes.MariaDB {ObjectMeta : metav1.ObjectMeta {Namespace : "tenant" , Name : mariadbNameForApp ("target" )}})
1016+ r := & RestoreJobReconciler {Client : c , Recorder : record .NewFakeRecorder (10 )}
1017+
1018+ rj := & backupsv1alpha1.RestoreJob {}
1019+ if err := c .Get (context .Background (), client .ObjectKeyFromObject (seed ), rj ); err != nil {
1020+ t .Fatalf ("seed Get: %v" , err )
1021+ }
1022+ res , err := r .reconcileMariaDBRestore (context .Background (), rj , cozyBackup )
1023+ if err != nil {
1024+ t .Fatalf ("reconcileMariaDBRestore: %v" , err )
1025+ }
1026+
1027+ switch {
1028+ 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 )
1034+ }
1035+ 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 )
1038+ }
1039+ default :
1040+ if rj .Status .Phase != backupsv1alpha1 .RestoreJobPhaseRunning {
1041+ t .Errorf ("expected Phase=Running, got %q" , rj .Status .Phase )
1042+ }
1043+ if res .RequeueAfter == 0 {
1044+ t .Errorf ("expected a requeue while the run is in progress, got %+v" , res )
1045+ }
1046+ }
1047+ })
1048+ }
1049+ }
1050+
8701051// ---------------------------------------------------------------------------
8711052// Test helpers
8721053// ---------------------------------------------------------------------------
0 commit comments