44 "context"
55 "errors"
66 "fmt"
7+ "io"
78 "log"
89 "os"
910 "testing"
@@ -26,27 +27,10 @@ import (
2627 "go.mongodb.org/mongo-driver/bson/primitive"
2728)
2829
29- type TestStorageEnv struct {
30- Config * config.Config
31- Storage storage.Storage
32- }
33-
34- func (tse * TestStorageEnv ) IsProfile () bool {
35- return tse .Config .IsProfile
36- }
37-
38- func (tse * TestStorageEnv ) Name () string {
39- return tse .Config .Name
40- }
41-
42- func (tse * TestStorageEnv ) StgConf () config.StorageConf {
43- return tse .Config .Storage
44- }
45-
4630type TestEnvironment struct {
4731 Mongo * TestMongo
4832 StorageBase string
49- StorageEnv * TestStorageEnv
33+ StorageEnv Storage
5034 Brief topo.NodeBrief
5135}
5236
@@ -159,13 +143,10 @@ func (tenv *TestEnvironment) Reset(t *testing.T) {
159143 if err != nil {
160144 assert .FailNow (t , "failed to reset config: %v" , err )
161145 }
162- stg , err := tenv .storage (& cfg .Storage )
163- if err != nil {
164- assert .FailNow (t , "failed to create storage: %v" , err )
165- }
166- tenv .StorageEnv = & TestStorageEnv {
167- Config : cfg ,
168- Storage : stg ,
146+ tenv .StorageEnv = Storage {
147+ IsProfile : false ,
148+ Name : "" ,
149+ StorageConf : cfg .Storage ,
169150 }
170151}
171152
@@ -195,16 +176,19 @@ func (tenv *TestEnvironment) SetConfigWithStorage(cfg *config.Config) (*config.C
195176 return cfg , err
196177}
197178
198- func (tenv * TestEnvironment ) AddStorageProfile (t * testing.T , name string ) * TestStorageEnv {
179+ func (tenv * TestEnvironment ) AddStorageProfile (t * testing.T , name string ) Storage {
199180 cfg , err := tenv .SetConfigWithStorage (& config.Config {IsProfile : true , Name : name })
200181 if err != nil {
201182 assert .FailNow (t , "failed to add storage profile: %v" , err )
202183 }
203- stg , err := tenv .storage (& cfg .Storage )
204184 if err != nil {
205185 assert .FailNow (t , "failed to create storage: %v" , err )
206186 }
207- return & TestStorageEnv {Config : cfg , Storage : stg }
187+ return Storage {
188+ IsProfile : true ,
189+ Name : name ,
190+ StorageConf : cfg .Storage ,
191+ }
208192}
209193
210194func (tenv * TestEnvironment ) cleanupStorage () error {
@@ -280,6 +264,7 @@ func TestMain(m *testing.M) {
280264
281265type bcps = map [string ][]bcp
282266type bcp struct {
267+ Name string
283268 LwT time.Time
284269 Delete bool
285270 BcpType defs.BackupType
@@ -298,16 +283,87 @@ func NewRelativeTime(t time.Time, unit time.Duration) func(int) time.Time {
298283 }
299284}
300285
301- func Test_DeleteBackupUnsafe (t * testing.T ) {
286+ type TestStorage struct {
287+ stgType storage.Type
288+ deletes []string
289+ }
302290
291+ func (ts * TestStorage ) AssertDeleted (t * testing.T , name string ) {
292+ t .Helper ()
293+ assert .Contains (t , ts .deletes , name )
303294}
304295
305- func assertBackupDeleted ( t * testing.T , client connect. Client , storage storage. Storage , name string ) {
296+ func ( ts * TestStorage ) AssertDeleteCalls ( t * testing.T , count int ) {
306297 t .Helper ()
298+ assert .Len (t , ts .deletes , count , "expected %d delete calls, got %d" , count , len (ts .deletes ))
299+ }
307300
308- count , err := client .BcpCollection ().CountDocuments (t .Context (), bson.M {"name" : name })
309- assert .NoError (t , err )
310- assert .Equal (t , int64 (0 ), count , "backup metadata %s should be deleted" , name )
301+ func (ts * TestStorage ) Type () storage.Type {
302+ return ts .stgType
303+ }
304+
305+ func (ts * TestStorage ) Save (name string , _ io.Reader , _ ... storage.Option ) error {
306+ return nil
307+ }
308+
309+ func (ts * TestStorage ) SourceReader (name string ) (io.ReadCloser , error ) {
310+ return nil , errors .New ("not implemented" )
311+ }
312+
313+ func (ts * TestStorage ) FileStat (name string ) (storage.FileInfo , error ) {
314+ return storage.FileInfo {Name : name , Size : 0 }, nil
315+ }
316+
317+ func (ts * TestStorage ) List (prefix , suffix string ) ([]storage.FileInfo , error ) {
318+ return nil , nil
319+ }
320+
321+ func (ts * TestStorage ) Delete (name string ) error {
322+ ts .deletes = append (ts .deletes , name )
323+ return nil
324+ }
325+
326+ func (ts * TestStorage ) Copy (src , dst string ) error {
327+ return nil
328+ }
329+
330+ func (ts * TestStorage ) DownloadStat () storage.DownloadStat {
331+ return storage.DownloadStat {}
332+ }
333+
334+ func NewTestStorage (stgType storage.Type ) * TestStorage {
335+ return & TestStorage {stgType : stgType }
336+ }
337+
338+ func Test_DeleteBackupUnsafe (t * testing.T ) {
339+ tests := []struct {
340+ name string
341+ backup bcp
342+ stg * TestStorage
343+ }{
344+ {
345+ name : "metadata and FS storage" ,
346+ backup : bcp {Name : "my-test-bcp" , LwT : time .Now ()},
347+ stg : NewTestStorage (storage .Filesystem ),
348+ },
349+ {
350+ name : "metadata and object storage" ,
351+ backup : bcp {Name : "my-test-bcp" , LwT : time .Now ()},
352+ stg : NewTestStorage (storage .S3 ),
353+ },
354+ }
355+
356+ for _ , tt := range tests {
357+ t .Run (tt .name , func (t * testing.T ) {
358+ TestEnv .Reset (t )
359+
360+ b := insertTestBcpMeta (t , TestEnv , TestEnv .StorageEnv , tt .backup )
361+
362+ err := DeleteBackupUnsafe (t .Context (), TestEnv .Mongo .Client , tt .stg , b .Name )
363+ assert .NoError (t , err )
364+ assertBackupDeleted (t , TestEnv .Mongo .Client , tt .stg , b .Name )
365+ })
366+ }
311367}
312368
313369func Test_ListDeleteBackups (t * testing.T ) {
@@ -655,6 +711,22 @@ func extractChunkStarts(chunks []oplog.OplogChunk) []uint32 {
655711 return starts
656712}
657713
714+ func assertBackupDeleted (t * testing.T , client connect.Client , stg * TestStorage , name string ) {
715+ t .Helper ()
716+ count , err := client .BcpCollection ().CountDocuments (t .Context (), bson.M {"name" : name })
717+ assert .NoError (t , err )
718+ assert .Equal (t , int64 (0 ), count , "backup metadata %s should be deleted" , name )
719+
720+ if stg .Type () == storage .Filesystem {
721+ stg .AssertDeleteCalls (t , 2 )
722+ stg .AssertDeleted (t , name )
723+ stg .AssertDeleted (t , name + defs .MetadataFileSuffix )
724+ } else {
725+ // only metadata should be deleted as test storage doesn't list files
726+ stg .AssertDeleteCalls (t , 1 )
727+ }
728+ }
729+
658730func assertChunkList (t * testing.T , expectedChunks []oplog.OplogChunk , actualChunks []oplog.OplogChunk , profile string ) {
659731 t .Helper ()
660732
@@ -704,8 +776,8 @@ func assertBackupList(t *testing.T, expectedBackups []BackupMeta, actualBackups
704776 )
705777}
706778
707- func stgsFromTestBackups (t * testing.T , env * TestEnvironment , backups bcps ) map [string ]* TestStorageEnv {
708- storages := make (map [string ]* TestStorageEnv )
779+ func stgsFromTestBackups (t * testing.T , env * TestEnvironment , backups bcps ) map [string ]Storage {
780+ storages := make (map [string ]Storage )
709781 storages ["" ] = env .StorageEnv
710782
711783 // initialize storage profiles
@@ -721,16 +793,16 @@ func stgsFromTestBackups(t *testing.T, env *TestEnvironment, backups bcps) map[s
721793func insertTestBackups (
722794 t * testing.T ,
723795 env * TestEnvironment ,
724- storages map [string ]* TestStorageEnv ,
796+ storages map [string ]Storage ,
725797 profileBcps bcps ,
726798) map [string ][]BackupMeta {
727799 t .Helper ()
728800
729801 tbd := make (map [string ][]BackupMeta )
730802 for profile , bcps := range profileBcps {
731803 for i , bcp := range bcps {
732- name : = fmt .Sprintf ("backup-%s-%d" , profile , i )
733- inserted := insertTestBcpMeta (t , env , storages [profile ], name , bcp )
804+ bcp . Name = fmt .Sprintf ("backup-%s-%d" , profile , i )
805+ inserted := insertTestBcpMeta (t , env , storages [profile ], bcp )
734806
735807 if bcp .Delete {
736808 tbd [profile ] = append (tbd [profile ], inserted )
@@ -753,7 +825,7 @@ func insertTestChunks(t *testing.T, env *TestEnvironment, cs chunks) []oplog.Opl
753825 return tbd
754826}
755827
756- func insertTestBcpMeta (t * testing.T , env * TestEnvironment , stgEnv * TestStorageEnv , name string , b bcp ) BackupMeta {
828+ func insertTestBcpMeta (t * testing.T , env * TestEnvironment , stg Storage , b bcp ) BackupMeta {
757829 t .Helper ()
758830
759831 firstWrite := b .LwT .Add (- 10 * time .Minute )
@@ -762,16 +834,12 @@ func insertTestBcpMeta(t *testing.T, env *TestEnvironment, stgEnv *TestStorageEn
762834 }
763835
764836 meta := & BackupMeta {
765- Type : b .BcpType ,
766- OPID : ctrl .OPID (primitive .NilObjectID ).String (),
767- Name : name ,
768- Namespaces : make ([]string , 0 ),
769- Compression : compress .CompressionTypeS2 ,
770- Store : Storage {
771- Name : stgEnv .Config .Name ,
772- IsProfile : stgEnv .IsProfile (),
773- StorageConf : stgEnv .Config .Storage ,
774- },
837+ Type : b .BcpType ,
838+ OPID : ctrl .OPID (primitive .NilObjectID ).String (),
839+ Name : b .Name ,
840+ Namespaces : make ([]string , 0 ),
841+ Compression : compress .CompressionTypeS2 ,
842+ Store : stg ,
775843 StartTS : time .Now ().Unix (),
776844 Status : defs .StatusDone ,
777845 Replsets : []BackupReplset {},
0 commit comments