@@ -131,15 +131,15 @@ func (tenv *TestEnvironment) Cleanup(ctx context.Context) error {
131131 return errors .Join (mongoErr , stgErr )
132132}
133133
134- func (tenv * TestEnvironment ) Reset (t * testing.T ) {
134+ func (tenv * TestEnvironment ) ResetWithConfig (t * testing.T , baseCfg * config. Config ) {
135135 if err := tenv .resetStorage (); err != nil {
136136 assert .FailNow (t , "failed to reset storage: %v" , err )
137137 }
138138 if err := tenv .resetMongo (t .Context ()); err != nil {
139139 assert .FailNow (t , "failed to reset mongo: %v" , err )
140140 }
141141
142- cfg , err := tenv .SetConfigWithStorage (nil )
142+ cfg , err := tenv .SetConfigWithStorage (baseCfg )
143143 if err != nil {
144144 assert .FailNow (t , "failed to reset config: %v" , err )
145145 }
@@ -150,6 +150,10 @@ func (tenv *TestEnvironment) Reset(t *testing.T) {
150150 }
151151}
152152
153+ func (tenv * TestEnvironment ) Reset (t * testing.T ) {
154+ tenv .ResetWithConfig (t , nil )
155+ }
156+
153157func (tenv * TestEnvironment ) SetConfigWithStorage (cfg * config.Config ) (* config.Config , error ) {
154158 if cfg == nil {
155159 cfg = & config.Config {}
@@ -335,6 +339,87 @@ func NewTestStorage(stgType storage.Type) *TestStorage {
335339 return & TestStorage {stgType : stgType }
336340}
337341
342+ func Test_isRequiredForOplogSlicing (t * testing.T ) {
343+ tm := NewRelativeTime (time .Now (), time .Minute )
344+
345+ tests := []struct {
346+ name string
347+ lwt time.Time
348+ baseLWT time.Time
349+ backups []bcp
350+ chunks chunks
351+ pitrEnabled [2 ]bool
352+ expected bool
353+ }{
354+ {
355+ name : "more recent snapshot exists" ,
356+ pitrEnabled : [2 ]bool {true , false },
357+ lwt : tm (20 ),
358+ backups : []bcp {{LwT : tm (25 )}, {LwT : tm (20 )}, {LwT : tm (15 )}},
359+ chunks : chunks {
360+ {From : tm (14 ), To : tm (5 )},
361+ },
362+ expected : false ,
363+ },
364+ {
365+ name : "gap in pitr timeline" ,
366+ pitrEnabled : [2 ]bool {true , false },
367+ lwt : tm (20 ),
368+ backups : []bcp {{LwT : tm (30 )}, {LwT : tm (20 )}},
369+ chunks : chunks {
370+ {From : tm (29 ), To : tm (25 )},
371+ {From : tm (22 ), To : tm (20 )},
372+ },
373+ expected : false ,
374+ },
375+ {
376+ name : "continuous pitr timeline (single chunk)" ,
377+ pitrEnabled : [2 ]bool {true , false },
378+ lwt : tm (20 ),
379+ backups : []bcp {{LwT : tm (30 )}, {LwT : tm (20 )}},
380+ chunks : chunks {
381+ {From : tm (29 ), To : tm (20 )},
382+ },
383+ expected : true ,
384+ },
385+ {
386+ name : "continuous pitr timeline (multiple chunks)" ,
387+ pitrEnabled : [2 ]bool {true , false },
388+ lwt : tm (20 ),
389+ backups : []bcp {{LwT : tm (30 )}, {LwT : tm (20 )}},
390+ chunks : chunks {
391+ {From : tm (29 ), To : tm (25 )},
392+ {From : tm (25 ), To : tm (20 )},
393+ },
394+ expected : true ,
395+ },
396+ }
397+
398+ for _ , tt := range tests {
399+ t .Run (tt .name , func (t * testing.T ) {
400+ cfg := & config.Config {
401+ PITR : & config.PITRConf {
402+ Enabled : tt .pitrEnabled [0 ],
403+ OplogOnly : tt .pitrEnabled [1 ],
404+ },
405+ }
406+ TestEnv .ResetWithConfig (t , cfg )
407+ insertTestBackupsStorage (t , TestEnv , TestEnv .StorageEnv , tt .backups )
408+ insertTestChunks (t , TestEnv , tt .chunks )
409+
410+ lwtBson := primitive.Timestamp {T : uint32 (tt .lwt .Unix ())}
411+ baseLWTBson := primitive.Timestamp {T : uint32 (tt .baseLWT .Unix ())}
412+ if baseLWTBson .IsZero () {
413+ baseLWTBson = primitive.Timestamp {}
414+ }
415+
416+ actual , err := isRequiredForOplogSlicing (t .Context (), TestEnv .Mongo .Client , lwtBson , baseLWTBson )
417+ assert .NoError (t , err )
418+ assert .Equal (t , tt .expected , actual )
419+ })
420+ }
421+ }
422+
338423func Test_DeleteBackupUnsafe (t * testing.T ) {
339424 tests := []struct {
340425 name string
@@ -790,6 +875,17 @@ func stgsFromTestBackups(t *testing.T, env *TestEnvironment, backups bcps) map[s
790875 return storages
791876}
792877
878+ func insertTestBackupsStorage (t * testing.T , env * TestEnvironment , stg Storage , bcps []bcp ) []BackupMeta {
879+ inserted := make ([]BackupMeta , len (bcps ))
880+ for i , b := range bcps {
881+ if b .Name == "" {
882+ b .Name = fmt .Sprintf ("backup-%s-%d" , stg .Name , i )
883+ }
884+ inserted [i ] = insertTestBcpMeta (t , env , stg , b )
885+ }
886+ return inserted
887+ }
888+
793889func insertTestBackups (
794890 t * testing.T ,
795891 env * TestEnvironment ,
@@ -800,12 +896,10 @@ func insertTestBackups(
800896
801897 tbd := make (map [string ][]BackupMeta )
802898 for profile , bcps := range profileBcps {
899+ inserted := insertTestBackupsStorage (t , env , storages [profile ], bcps )
803900 for i , bcp := range bcps {
804- bcp .Name = fmt .Sprintf ("backup-%s-%d" , profile , i )
805- inserted := insertTestBcpMeta (t , env , storages [profile ], bcp )
806-
807901 if bcp .Delete {
808- tbd [profile ] = append (tbd [profile ], inserted )
902+ tbd [profile ] = append (tbd [profile ], inserted [ i ] )
809903 }
810904 }
811905 }
0 commit comments