@@ -6,10 +6,12 @@ import (
6
6
"os"
7
7
"path"
8
8
"path/filepath"
9
+ "reflect"
9
10
"strings"
10
11
"testing"
11
12
"time"
12
13
14
+ "github.com/percona/percona-backup-mongodb/pbm/defs"
13
15
"github.com/percona/percona-backup-mongodb/pbm/log"
14
16
)
15
17
@@ -221,6 +223,136 @@ func TestWaitMgoFreePort(t *testing.T) {
221
223
})
222
224
}
223
225
226
+ func TestResolveCleanupStrategy (t * testing.T ) {
227
+ cmpStrategy := func (fn any ) uintptr {
228
+ return reflect .ValueOf (fn ).Pointer ()
229
+ }
230
+ type strategyDesc string
231
+ const (
232
+ fallbackStrategy strategyDesc = "fallback"
233
+ fullCleanupStrategy strategyDesc = "fullCleanup"
234
+ )
235
+
236
+ testCases := []struct {
237
+ desc string
238
+ fallback bool
239
+ allowPartlyDone bool
240
+ clusterStatus defs.Status
241
+ wantStrategy strategyDesc
242
+ }{
243
+ // fallback enabled
244
+ {
245
+ desc : "fallback: enabled, allow-partly-done: enabled, status: error" ,
246
+ fallback : true ,
247
+ allowPartlyDone : true ,
248
+ clusterStatus : defs .StatusError ,
249
+ wantStrategy : fallbackStrategy ,
250
+ },
251
+ {
252
+ desc : "fallback: enabled, allow-partly-done: enabled, status: partly-done" ,
253
+ fallback : true ,
254
+ allowPartlyDone : true ,
255
+ clusterStatus : defs .StatusPartlyDone ,
256
+ wantStrategy : fullCleanupStrategy ,
257
+ },
258
+ {
259
+ desc : "fallback: enabled, allow-partly-done: enabled, status: done" ,
260
+ fallback : true ,
261
+ allowPartlyDone : true ,
262
+ clusterStatus : defs .StatusDone ,
263
+ wantStrategy : fullCleanupStrategy ,
264
+ },
265
+
266
+ {
267
+ desc : "fallback: enabled, allow-partly-done: disabled, status: error" ,
268
+ fallback : true ,
269
+ allowPartlyDone : false ,
270
+ clusterStatus : defs .StatusError ,
271
+ wantStrategy : fallbackStrategy ,
272
+ },
273
+ {
274
+ desc : "fallback: enabled, allow-partly-done: disabled, status: partly-done" ,
275
+ fallback : true ,
276
+ allowPartlyDone : false ,
277
+ clusterStatus : defs .StatusPartlyDone ,
278
+ wantStrategy : fallbackStrategy ,
279
+ },
280
+ {
281
+ desc : "fallback: enabled, allow-partly-done: disabled, status: done" ,
282
+ fallback : true ,
283
+ allowPartlyDone : false ,
284
+ clusterStatus : defs .StatusDone ,
285
+ wantStrategy : fullCleanupStrategy ,
286
+ },
287
+
288
+ // fallback disabled
289
+ {
290
+ desc : "fallback: disabled, allow-partly-done: enabled, status: error" ,
291
+ fallback : false ,
292
+ allowPartlyDone : true ,
293
+ clusterStatus : defs .StatusError ,
294
+ wantStrategy : fullCleanupStrategy ,
295
+ },
296
+ {
297
+ desc : "fallback: disabled, allow-partly-done: enabled, status: partly-done" ,
298
+ fallback : false ,
299
+ allowPartlyDone : true ,
300
+ clusterStatus : defs .StatusPartlyDone ,
301
+ wantStrategy : fullCleanupStrategy ,
302
+ },
303
+ {
304
+ desc : "fallback: disabled, allow-partly-done: enabled, status: done" ,
305
+ fallback : false ,
306
+ allowPartlyDone : true ,
307
+ clusterStatus : defs .StatusDone ,
308
+ wantStrategy : fullCleanupStrategy ,
309
+ },
310
+
311
+ {
312
+ desc : "fallback: disabled, allow-partly-done: disabled, status: error" ,
313
+ fallback : false ,
314
+ allowPartlyDone : false ,
315
+ clusterStatus : defs .StatusError ,
316
+ wantStrategy : fullCleanupStrategy ,
317
+ },
318
+ {
319
+ desc : "fallback: disabled, allow-partly-done: disabled, status: partly-done" ,
320
+ fallback : false ,
321
+ allowPartlyDone : false ,
322
+ clusterStatus : defs .StatusPartlyDone ,
323
+ wantStrategy : fullCleanupStrategy ,
324
+ },
325
+ {
326
+ desc : "fallback: disabled, allow-partly-done: disabled, status: done" ,
327
+ fallback : false ,
328
+ allowPartlyDone : false ,
329
+ clusterStatus : defs .StatusDone ,
330
+ wantStrategy : fullCleanupStrategy ,
331
+ },
332
+ }
333
+
334
+ for _ , tC := range testCases {
335
+ t .Run (tC .desc , func (t * testing.T ) {
336
+ r := & PhysRestore {
337
+ fallback : tC .fallback ,
338
+ allowPartlyDone : tC .allowPartlyDone ,
339
+ log : log .DiscardLogger .NewDefaultEvent (),
340
+ }
341
+ strategy := r .resolveCleanupStrategy (tC .clusterStatus )
342
+ if tC .wantStrategy == fullCleanupStrategy {
343
+ if cmpStrategy (strategy ) != cmpStrategy (r .doFullCleanup ) {
344
+ t .Fatalf ("want=%s" , fullCleanupStrategy )
345
+
346
+ }
347
+ } else if tC .wantStrategy == fallbackStrategy {
348
+ if cmpStrategy (strategy ) != cmpStrategy (r .doFallbackCleanup ) {
349
+ t .Fatalf ("want=%s" , fallbackStrategy )
350
+ }
351
+ }
352
+ })
353
+ }
354
+ }
355
+
224
356
func TestRemoveAll (t * testing.T ) {
225
357
t .Run ("removes all files in dir" , func (t * testing.T ) {
226
358
tmpDir := setupTestFiles (t )
0 commit comments