2017-03-29 21:58:25 +00:00
|
|
|
package swift_test
|
|
|
|
|
|
|
|
import (
|
2017-06-03 15:39:57 +00:00
|
|
|
"context"
|
2017-03-29 21:58:25 +00:00
|
|
|
"fmt"
|
2017-05-01 08:13:03 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
2017-03-29 21:58:25 +00:00
|
|
|
"time"
|
|
|
|
|
2017-09-24 18:04:23 +00:00
|
|
|
"github.com/restic/restic/internal/backend"
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/backend/swift"
|
|
|
|
"github.com/restic/restic/internal/backend/test"
|
2017-10-02 13:06:39 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"github.com/restic/restic/internal/restic"
|
|
|
|
rtest "github.com/restic/restic/internal/test"
|
2017-03-29 21:58:25 +00:00
|
|
|
)
|
|
|
|
|
2023-04-21 19:06:56 +00:00
|
|
|
func newSwiftTestSuite(t testing.TB) *test.Suite[swift.Config] {
|
2018-01-27 12:57:43 +00:00
|
|
|
tr, err := backend.Transport(backend.TransportOptions{})
|
2017-09-24 18:04:23 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("cannot create transport for tests: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-04-21 19:06:56 +00:00
|
|
|
return &test.Suite[swift.Config]{
|
2017-05-01 08:13:03 +00:00
|
|
|
// do not use excessive data
|
|
|
|
MinimalData: true,
|
2017-03-29 21:58:25 +00:00
|
|
|
|
2017-09-16 11:59:55 +00:00
|
|
|
// wait for removals for at least 5m
|
|
|
|
WaitForDelayedRemoval: 5 * time.Minute,
|
|
|
|
|
|
|
|
ErrorHandler: func(t testing.TB, be restic.Backend, err error) error {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if be.IsNotExist(err) {
|
|
|
|
t.Logf("swift: ignoring error %v", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
},
|
2017-06-18 15:36:57 +00:00
|
|
|
|
2017-05-01 08:13:03 +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() (*swift.Config, error) {
|
2023-04-20 20:40:21 +00:00
|
|
|
cfg, err := swift.ParseConfig(os.Getenv("RESTIC_TEST_SWIFT"))
|
2017-05-01 08:13:03 +00:00
|
|
|
if err != nil {
|
2023-04-21 19:35:34 +00:00
|
|
|
return nil, err
|
2017-05-01 08:13:03 +00:00
|
|
|
}
|
2017-03-29 21:58:25 +00:00
|
|
|
|
2023-06-08 13:28:07 +00:00
|
|
|
cfg.ApplyEnvironment("RESTIC_TEST_")
|
2017-05-01 08:13:03 +00:00
|
|
|
cfg.Prefix += fmt.Sprintf("/test-%d", time.Now().UnixNano())
|
|
|
|
t.Logf("using prefix %v", cfg.Prefix)
|
|
|
|
return cfg, nil
|
|
|
|
},
|
2017-03-29 21:58:25 +00:00
|
|
|
|
2017-05-01 08:13:03 +00:00
|
|
|
// CreateFn is a function that creates a temporary repository for the tests.
|
2023-04-21 19:06:56 +00:00
|
|
|
Create: func(cfg swift.Config) (restic.Backend, error) {
|
2021-04-09 09:24:28 +00:00
|
|
|
be, err := swift.Open(context.TODO(), cfg, tr)
|
2017-05-01 08:13:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-29 21:58:25 +00:00
|
|
|
|
2022-12-03 10:28:10 +00:00
|
|
|
_, err = be.Stat(context.TODO(), restic.Handle{Type: restic.ConfigFile})
|
|
|
|
if err != nil && !be.IsNotExist(err) {
|
2017-05-01 08:13:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-29 21:58:25 +00:00
|
|
|
|
2022-12-03 10:28:10 +00:00
|
|
|
if err == nil {
|
2017-05-01 08:13:03 +00:00
|
|
|
return nil, errors.New("config already exists")
|
|
|
|
}
|
|
|
|
|
|
|
|
return be, nil
|
|
|
|
},
|
|
|
|
|
|
|
|
// OpenFn is a function that opens a previously created temporary repository.
|
2023-04-21 19:06:56 +00:00
|
|
|
Open: func(cfg swift.Config) (restic.Backend, error) {
|
2021-04-09 09:24:28 +00:00
|
|
|
return swift.Open(context.TODO(), cfg, tr)
|
2017-05-01 08:13:03 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// CleanupFn removes data created during the tests.
|
2023-04-21 19:06:56 +00:00
|
|
|
Cleanup: func(cfg swift.Config) error {
|
2021-04-09 09:24:28 +00:00
|
|
|
be, err := swift.Open(context.TODO(), cfg, tr)
|
2017-05-01 08:13:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-10-14 13:56:38 +00:00
|
|
|
return be.Delete(context.TODO())
|
2017-05-01 08:13:03 +00:00
|
|
|
},
|
2017-03-29 21:58:25 +00:00
|
|
|
}
|
2017-05-01 08:13:03 +00:00
|
|
|
}
|
2017-03-29 21:58:25 +00:00
|
|
|
|
2017-05-01 08:13:03 +00:00
|
|
|
func TestBackendSwift(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
if t.Skipped() {
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.SkipDisallowed(t, "restic/backend/swift.TestBackendSwift")
|
2017-03-29 21:58:25 +00:00
|
|
|
}
|
2017-05-01 08:13:03 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
if os.Getenv("RESTIC_TEST_SWIFT") == "" {
|
|
|
|
t.Skip("RESTIC_TEST_SWIFT unset, skipping test")
|
|
|
|
return
|
2017-03-29 21:58:25 +00:00
|
|
|
}
|
2017-05-01 08:13:03 +00:00
|
|
|
|
|
|
|
t.Logf("run tests")
|
|
|
|
newSwiftTestSuite(t).RunTests(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkBackendSwift(t *testing.B) {
|
|
|
|
if os.Getenv("RESTIC_TEST_SWIFT") == "" {
|
|
|
|
t.Skip("RESTIC_TEST_SWIFT unset, skipping test")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("run tests")
|
|
|
|
newSwiftTestSuite(t).RunBenchmarks(t)
|
2017-03-29 21:58:25 +00:00
|
|
|
}
|