@@ -594,3 +594,69 @@ func TestLegacyFindPriorBackups(t *testing.T) {
594594 })
595595 }
596596}
597+
598+ func TestCleanupMakeBackupDestinationStores (t * testing.T ) {
599+ defer leaktest .AfterTest (t )()
600+ // This test ensures that in the event that MakeBackupDestinationStores
601+ // encounters an error either during store creation or during cleanup that all
602+ // stores that were opened have Close called.
603+ ctx := context .Background ()
604+
605+ const maxStores = 10
606+ failAfterN := rand .Intn (maxStores + 1 )
607+
608+ stores := make ([]* fakeStore , 0 , maxStores )
609+ mkStore := func (
610+ _ context.Context ,
611+ _ string ,
612+ _ username.SQLUsername ,
613+ _ ... cloud.ExternalStorageOption ,
614+ ) (cloud.ExternalStorage , error ) {
615+ if len (stores ) == failAfterN {
616+ return nil , fmt .Errorf ("simulated failure after %d stores" , failAfterN )
617+ }
618+ s := & fakeStore {closeErrProbability : 0.1 }
619+ stores = append (stores , s )
620+ return s , nil
621+ }
622+
623+ destinations := make ([]string , maxStores )
624+ _ , cleanup , err := backupdest .MakeBackupDestinationStores (
625+ ctx , username .RootUserName (), mkStore , destinations ,
626+ )
627+
628+ countOpenStores := func () int {
629+ count := 0
630+ for _ , s := range stores {
631+ if ! s .calledClose {
632+ count ++
633+ }
634+ }
635+ return count
636+ }
637+
638+ if failAfterN < maxStores {
639+ require .Error (t , err )
640+ require .Equal (t , 0 , countOpenStores ())
641+ } else {
642+ require .NoError (t , err )
643+ require .Equal (t , maxStores , countOpenStores ())
644+ _ = cleanup ()
645+ require .Equal (t , 0 , countOpenStores ())
646+ }
647+ }
648+
649+ type fakeStore struct {
650+ cloud.ExternalStorage
651+ calledClose bool
652+ closeErrProbability float32
653+ }
654+
655+ // Close() simulates closing the store and probabilistically returns an error.
656+ func (s * fakeStore ) Close () error {
657+ s .calledClose = true
658+ if rand .Float32 () < s .closeErrProbability {
659+ return fmt .Errorf ("simulated close error" )
660+ }
661+ return nil
662+ }
0 commit comments