@@ -402,8 +402,9 @@ var _ = Describe("BackupCronJobReconciler", func() {
402402 dw := createDevWorkspace ("dw-old" , "ns-b" , false , metav1 .NewTime (time .Now ().Add (- 60 * time .Minute )))
403403 dw .Status .Phase = dwv2 .DevWorkspaceStatusStopped
404404 dw .Status .DevWorkspaceId = "id-old"
405- // Set successful annotation so the time-based logic is checked
405+ // Set successful annotation and backup time so the time-based logic is checked
406406 dw .Annotations = map [string ]string {
407+ constants .DevWorkspaceLastBackupFinishedAtAnnotation : lastBackupTime .Format (time .RFC3339Nano ),
407408 constants .DevWorkspaceLastBackupSuccessfulAnnotation : "true" ,
408409 }
409410 Expect (fakeClient .Create (ctx , dw )).To (Succeed ())
@@ -842,11 +843,12 @@ var _ = Describe("BackupCronJobReconciler", func() {
842843 lastBackupTime := metav1 .NewTime (time .Now ().Add (- 5 * time .Minute ))
843844 workspaceStoppedTime := metav1 .NewTime (time .Now ().Add (- 10 * time .Minute ))
844845 dw := createDevWorkspace ("dw-test" , "ns-test" , false , workspaceStoppedTime )
845- // Set successful annotation so the time-based logic is checked
846+ // Set successful annotation and backup time so the time-based logic is checked
846847 dw .Annotations = map [string ]string {
848+ constants .DevWorkspaceLastBackupFinishedAtAnnotation : lastBackupTime .Format (time .RFC3339Nano ),
847849 constants .DevWorkspaceLastBackupSuccessfulAnnotation : "true" ,
848850 }
849- result := reconciler .wasStoppedSinceLastBackup (dw , & lastBackupTime , log )
851+ result := reconciler .wasStoppedSinceLastBackup (dw , nil , log )
850852 Expect (result ).To (BeFalse ())
851853 })
852854 It ("returns true if there is no last backup time" , func () {
@@ -917,6 +919,33 @@ var _ = Describe("BackupCronJobReconciler", func() {
917919 result := reconciler .wasStoppedSinceLastBackup (dw , & globalLastBackupTime , log )
918920 Expect (result ).To (BeTrue ())
919921 })
922+
923+ It ("falls back to global last backup time when annotations are nil" , func () {
924+ globalLastBackupTime := metav1 .NewTime (time .Now ().Add (- 20 * time .Minute ))
925+ workspaceStoppedTime := metav1 .NewTime (time .Now ().Add (- 10 * time .Minute ))
926+
927+ dw := createDevWorkspace ("dw-test-nil-annotations" , "ns-test-nil-annotations" , false , workspaceStoppedTime )
928+ // Explicitly ensure annotations is nil
929+ dw .Annotations = nil
930+
931+ // With global time (-20min), workspace stopped at -10min, should return true
932+ // lastBackupSuccessful should be treated as true when falling back
933+ result := reconciler .wasStoppedSinceLastBackup (dw , & globalLastBackupTime , log )
934+ Expect (result ).To (BeTrue ())
935+ })
936+
937+ It ("returns false when annotations are nil and workspace stopped before global backup time" , func () {
938+ globalLastBackupTime := metav1 .NewTime (time .Now ().Add (- 5 * time .Minute ))
939+ workspaceStoppedTime := metav1 .NewTime (time .Now ().Add (- 10 * time .Minute ))
940+
941+ dw := createDevWorkspace ("dw-test-nil-old-stop" , "ns-test-nil-old-stop" , false , workspaceStoppedTime )
942+ // Explicitly ensure annotations is nil
943+ dw .Annotations = nil
944+
945+ // With global time (-5min), workspace stopped at -10min, should return false
946+ result := reconciler .wasStoppedSinceLastBackup (dw , & globalLastBackupTime , log )
947+ Expect (result ).To (BeFalse ())
948+ })
920949 })
921950
922951})
0 commit comments