2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-02 19:49:44 +00:00
restic/internal/backend/swift/swift_test.go

113 lines
2.5 KiB
Go
Raw Normal View History

package swift_test
import (
2017-06-03 15:39:57 +00:00
"context"
"fmt"
2017-05-01 08:13:03 +00:00
"os"
"testing"
"time"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/errors"
. "github.com/restic/restic/internal/test"
"github.com/restic/restic/internal/backend/swift"
"github.com/restic/restic/internal/backend/test"
)
2017-05-01 08:13:03 +00:00
func newSwiftTestSuite(t testing.TB) *test.Suite {
return &test.Suite{
// do not use excessive data
MinimalData: true,
// wait for removals for at least 60s
WaitForDelayedRemoval: 60 * time.Second,
2017-05-01 08:13:03 +00:00
// NewConfig returns a config for a new temporary backend that will be used in tests.
NewConfig: func() (interface{}, error) {
swiftcfg, err := swift.ParseConfig(os.Getenv("RESTIC_TEST_SWIFT"))
if err != nil {
return nil, err
}
2017-05-01 08:13:03 +00:00
cfg := swiftcfg.(swift.Config)
if err = swift.ApplyEnvironment("RESTIC_TEST_", &cfg); err != nil {
return nil, err
}
cfg.Prefix += fmt.Sprintf("/test-%d", time.Now().UnixNano())
t.Logf("using prefix %v", cfg.Prefix)
return cfg, nil
},
2017-05-01 08:13:03 +00:00
// CreateFn is a function that creates a temporary repository for the tests.
Create: func(config interface{}) (restic.Backend, error) {
cfg := config.(swift.Config)
2017-05-01 08:13:03 +00:00
be, err := swift.Open(cfg)
if err != nil {
return nil, err
}
2017-06-03 15:39:57 +00:00
exists, err := be.Test(context.TODO(), restic.Handle{Type: restic.ConfigFile})
2017-05-01 08:13:03 +00:00
if err != nil {
return nil, err
}
2017-05-01 08:13:03 +00:00
if exists {
return nil, errors.New("config already exists")
}
return be, nil
},
// OpenFn is a function that opens a previously created temporary repository.
Open: func(config interface{}) (restic.Backend, error) {
cfg := config.(swift.Config)
return swift.Open(cfg)
},
// CleanupFn removes data created during the tests.
Cleanup: func(config interface{}) error {
cfg := config.(swift.Config)
2017-05-01 08:13:03 +00:00
be, err := swift.Open(cfg)
if err != nil {
return err
}
2017-06-03 15:39:57 +00:00
if err := be.(restic.Deleter).Delete(context.TODO()); err != nil {
2017-05-01 08:13:03 +00:00
return err
}
return nil
},
}
2017-05-01 08:13:03 +00:00
}
2017-05-01 08:13:03 +00:00
func TestBackendSwift(t *testing.T) {
defer func() {
if t.Skipped() {
SkipDisallowed(t, "restic/backend/swift.TestBackendSwift")
}
2017-05-01 08:13:03 +00:00
}()
if os.Getenv("RESTIC_TEST_SWIFT") == "" {
t.Skip("RESTIC_TEST_SWIFT unset, skipping test")
return
}
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)
}