2017-05-13 19:37:07 +00:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
2023-06-08 14:53:55 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2017-05-14 10:56:10 +00:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
2017-05-13 19:37:07 +00:00
|
|
|
"testing"
|
2017-06-18 15:36:57 +00:00
|
|
|
"time"
|
2017-07-23 12:21:03 +00:00
|
|
|
|
2023-06-08 14:53:55 +00:00
|
|
|
"github.com/restic/restic/internal/backend"
|
|
|
|
"github.com/restic/restic/internal/backend/location"
|
|
|
|
"github.com/restic/restic/internal/errors"
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/test"
|
2017-05-13 19:37:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Suite implements a test suite for restic backends.
|
2023-04-21 19:06:56 +00:00
|
|
|
type Suite[C any] struct {
|
2017-06-18 15:36:57 +00:00
|
|
|
// Config should be used to configure the backend.
|
2023-04-21 19:35:34 +00:00
|
|
|
Config *C
|
2017-05-13 19:37:07 +00:00
|
|
|
|
|
|
|
// NewConfig returns a config for a new temporary backend that will be used in tests.
|
2023-04-21 19:35:34 +00:00
|
|
|
NewConfig func() (*C, error)
|
2017-05-13 19:37:07 +00:00
|
|
|
|
2023-06-08 14:53:55 +00:00
|
|
|
// Factory contains a factory that can be used to create or open a repository for the tests.
|
|
|
|
Factory location.Factory
|
2017-05-13 19:37:07 +00:00
|
|
|
|
|
|
|
// MinimalData instructs the tests to not use excessive data.
|
|
|
|
MinimalData bool
|
2017-06-18 15:36:57 +00:00
|
|
|
|
|
|
|
// WaitForDelayedRemoval is set to a non-zero value to instruct the test
|
|
|
|
// suite to wait for this amount of time until a file that was removed
|
|
|
|
// really disappeared.
|
|
|
|
WaitForDelayedRemoval time.Duration
|
2017-09-16 11:59:55 +00:00
|
|
|
|
|
|
|
// ErrorHandler allows ignoring certain errors.
|
2023-10-01 09:40:12 +00:00
|
|
|
ErrorHandler func(testing.TB, backend.Backend, error) error
|
2017-05-13 19:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RunTests executes all defined tests as subtests of t.
|
2023-04-21 19:06:56 +00:00
|
|
|
func (s *Suite[C]) RunTests(t *testing.T) {
|
2017-05-13 19:37:07 +00:00
|
|
|
var err error
|
|
|
|
s.Config, err = s.NewConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// test create/open functions first
|
|
|
|
be := s.create(t)
|
|
|
|
s.close(t, be)
|
|
|
|
|
2017-05-14 10:56:10 +00:00
|
|
|
for _, test := range s.testFuncs(t) {
|
|
|
|
t.Run(test.Name, test.Fn)
|
2017-05-13 19:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !test.TestCleanupTempDirs {
|
|
|
|
t.Logf("not cleaning up backend")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-08 14:53:55 +00:00
|
|
|
s.cleanup(t)
|
2017-05-13 19:37:07 +00:00
|
|
|
}
|
|
|
|
|
2017-05-14 10:56:10 +00:00
|
|
|
type testFunction struct {
|
|
|
|
Name string
|
|
|
|
Fn func(*testing.T)
|
|
|
|
}
|
|
|
|
|
2023-04-21 19:06:56 +00:00
|
|
|
func (s *Suite[C]) testFuncs(t testing.TB) (funcs []testFunction) {
|
2017-05-14 10:56:10 +00:00
|
|
|
tpe := reflect.TypeOf(s)
|
|
|
|
v := reflect.ValueOf(s)
|
|
|
|
|
|
|
|
for i := 0; i < tpe.NumMethod(); i++ {
|
|
|
|
methodType := tpe.Method(i)
|
|
|
|
name := methodType.Name
|
|
|
|
|
|
|
|
// discard functions which do not have the right name
|
|
|
|
if !strings.HasPrefix(name, "Test") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
iface := v.Method(i).Interface()
|
|
|
|
f, ok := iface.(func(*testing.T))
|
|
|
|
if !ok {
|
|
|
|
t.Logf("warning: function %v of *Suite has the wrong signature for a test function\nwant: func(*testing.T),\nhave: %T",
|
|
|
|
name, iface)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
funcs = append(funcs, testFunction{
|
|
|
|
Name: name,
|
|
|
|
Fn: f,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return funcs
|
|
|
|
}
|
|
|
|
|
|
|
|
type benchmarkFunction struct {
|
|
|
|
Name string
|
|
|
|
Fn func(*testing.B)
|
|
|
|
}
|
|
|
|
|
2023-04-21 19:06:56 +00:00
|
|
|
func (s *Suite[C]) benchmarkFuncs(t testing.TB) (funcs []benchmarkFunction) {
|
2017-05-14 10:56:10 +00:00
|
|
|
tpe := reflect.TypeOf(s)
|
|
|
|
v := reflect.ValueOf(s)
|
|
|
|
|
|
|
|
for i := 0; i < tpe.NumMethod(); i++ {
|
|
|
|
methodType := tpe.Method(i)
|
|
|
|
name := methodType.Name
|
|
|
|
|
|
|
|
// discard functions which do not have the right name
|
|
|
|
if !strings.HasPrefix(name, "Benchmark") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
iface := v.Method(i).Interface()
|
|
|
|
f, ok := iface.(func(*testing.B))
|
|
|
|
if !ok {
|
|
|
|
t.Logf("warning: function %v of *Suite has the wrong signature for a test function\nwant: func(*testing.T),\nhave: %T",
|
|
|
|
name, iface)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
funcs = append(funcs, benchmarkFunction{
|
|
|
|
Name: name,
|
|
|
|
Fn: f,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return funcs
|
|
|
|
}
|
|
|
|
|
2017-05-13 19:37:07 +00:00
|
|
|
// RunBenchmarks executes all defined benchmarks as subtests of b.
|
2023-04-21 19:06:56 +00:00
|
|
|
func (s *Suite[C]) RunBenchmarks(b *testing.B) {
|
2017-05-13 19:37:07 +00:00
|
|
|
var err error
|
|
|
|
s.Config, err = s.NewConfig()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// test create/open functions first
|
|
|
|
be := s.create(b)
|
|
|
|
s.close(b, be)
|
|
|
|
|
2017-05-14 10:56:10 +00:00
|
|
|
for _, test := range s.benchmarkFuncs(b) {
|
|
|
|
b.Run(test.Name, test.Fn)
|
2017-05-13 19:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !test.TestCleanupTempDirs {
|
|
|
|
b.Logf("not cleaning up backend")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-08 14:53:55 +00:00
|
|
|
s.cleanup(b)
|
|
|
|
}
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
func (s *Suite[C]) createOrError() (backend.Backend, error) {
|
2023-06-08 14:53:55 +00:00
|
|
|
tr, err := backend.Transport(backend.TransportOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot create transport for tests: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
be, err := s.Factory.Create(context.TODO(), s.Config, tr, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
_, err = be.Stat(context.TODO(), backend.Handle{Type: backend.ConfigFile})
|
2023-06-08 14:53:55 +00:00
|
|
|
if err != nil && !be.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return nil, errors.New("config already exists")
|
2017-05-13 19:37:07 +00:00
|
|
|
}
|
2023-06-08 14:53:55 +00:00
|
|
|
|
|
|
|
return be, nil
|
2017-05-13 19:37:07 +00:00
|
|
|
}
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
func (s *Suite[C]) create(t testing.TB) backend.Backend {
|
2023-06-08 14:53:55 +00:00
|
|
|
be, err := s.createOrError()
|
2017-05-13 19:37:07 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return be
|
|
|
|
}
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
func (s *Suite[C]) open(t testing.TB) backend.Backend {
|
2023-06-08 14:53:55 +00:00
|
|
|
tr, err := backend.Transport(backend.TransportOptions{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("cannot create transport for tests: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
be, err := s.Factory.Open(context.TODO(), s.Config, tr, nil)
|
2017-05-13 19:37:07 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return be
|
|
|
|
}
|
|
|
|
|
2023-06-08 14:53:55 +00:00
|
|
|
func (s *Suite[C]) cleanup(t testing.TB) {
|
|
|
|
be := s.open(t)
|
|
|
|
if err := be.Delete(context.TODO()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s.close(t, be)
|
|
|
|
}
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
func (s *Suite[C]) close(t testing.TB, be backend.Backend) {
|
2017-05-13 19:37:07 +00:00
|
|
|
err := be.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|