backend: Adjust tests to use the Factory to instantiate the backend

This drastically reduces the amount of duplicated test code.
This commit is contained in:
Michael Eischer
2023-06-08 16:53:55 +02:00
parent 3d3bb88745
commit 13a8b5822f
13 changed files with 119 additions and 423 deletions

View File

@@ -1,26 +1,18 @@
package b2_test
import (
"context"
"fmt"
"os"
"testing"
"time"
"github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/backend/b2"
"github.com/restic/restic/internal/backend/test"
"github.com/restic/restic/internal/restic"
rtest "github.com/restic/restic/internal/test"
)
func newB2TestSuite(t testing.TB) *test.Suite[b2.Config] {
tr, err := backend.Transport(backend.TransportOptions{})
if err != nil {
t.Fatalf("cannot create transport for tests: %v", err)
}
return &test.Suite[b2.Config]{
// do not use excessive data
MinimalData: true,
@@ -40,25 +32,7 @@ func newB2TestSuite(t testing.TB) *test.Suite[b2.Config] {
return cfg, nil
},
// CreateFn is a function that creates a temporary repository for the tests.
Create: func(cfg b2.Config) (restic.Backend, error) {
return b2.Create(context.Background(), cfg, tr)
},
// OpenFn is a function that opens a previously created temporary repository.
Open: func(cfg b2.Config) (restic.Backend, error) {
return b2.Open(context.Background(), cfg, tr)
},
// CleanupFn removes data created during the tests.
Cleanup: func(cfg b2.Config) error {
be, err := b2.Open(context.Background(), cfg, tr)
if err != nil {
return err
}
return be.Delete(context.TODO())
},
Factory: b2.NewFactory(),
}
}