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

@ -12,18 +12,12 @@ import (
"github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/backend/azure"
"github.com/restic/restic/internal/backend/test"
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/options"
"github.com/restic/restic/internal/restic"
rtest "github.com/restic/restic/internal/test"
)
func newAzureTestSuite(t testing.TB) *test.Suite[azure.Config] {
tr, err := backend.Transport(backend.TransportOptions{})
if err != nil {
t.Fatalf("cannot create transport for tests: %v", err)
}
return &test.Suite[azure.Config]{
// do not use excessive data
MinimalData: true,
@ -40,42 +34,7 @@ func newAzureTestSuite(t testing.TB) *test.Suite[azure.Config] {
return cfg, nil
},
// CreateFn is a function that creates a temporary repository for the tests.
Create: func(cfg azure.Config) (restic.Backend, error) {
ctx := context.TODO()
be, err := azure.Create(ctx, cfg, tr)
if err != nil {
return nil, err
}
_, err = be.Stat(context.TODO(), restic.Handle{Type: restic.ConfigFile})
if err != nil && !be.IsNotExist(err) {
return nil, err
}
if err == nil {
return nil, errors.New("config already exists")
}
return be, nil
},
// OpenFn is a function that opens a previously created temporary repository.
Open: func(cfg azure.Config) (restic.Backend, error) {
ctx := context.TODO()
return azure.Open(ctx, cfg, tr)
},
// CleanupFn removes data created during the tests.
Cleanup: func(cfg azure.Config) error {
ctx := context.TODO()
be, err := azure.Open(ctx, cfg, tr)
if err != nil {
return err
}
return be.Delete(context.TODO())
},
Factory: azure.NewFactory(),
}
}