restic/internal/backend/b2/b2_test.go

99 lines
2.2 KiB
Go
Raw Normal View History

package b2_test
import (
2017-06-03 15:39:57 +00:00
"context"
"fmt"
"os"
"testing"
"time"
"github.com/restic/restic/internal/backend"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/backend/b2"
"github.com/restic/restic/internal/backend/test"
2017-07-24 15:42:25 +00:00
"github.com/restic/restic/internal/restic"
2017-10-02 13:06:39 +00:00
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,
// wait for at most 10 seconds for removed files to disappear
WaitForDelayedRemoval: 10 * time.Second,
// NewConfig returns a config for a new temporary backend that will be used in tests.
NewConfig: func() (*b2.Config, error) {
cfg, err := b2.ParseConfig(os.Getenv("RESTIC_TEST_B2_REPOSITORY"))
if err != nil {
return nil, err
}
2023-04-21 20:02:43 +00:00
err = cfg.ApplyEnvironment("RESTIC_TEST_")
if err != nil {
return nil, err
}
cfg.Prefix = fmt.Sprintf("test-%d", time.Now().UnixNano())
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
}
2017-10-14 13:56:38 +00:00
return be.Delete(context.TODO())
},
}
}
func testVars(t testing.TB) {
vars := []string{
"RESTIC_TEST_B2_ACCOUNT_ID",
"RESTIC_TEST_B2_ACCOUNT_KEY",
"RESTIC_TEST_B2_REPOSITORY",
}
for _, v := range vars {
if os.Getenv(v) == "" {
t.Skipf("environment variable %v not set", v)
return
}
}
}
func TestBackendB2(t *testing.T) {
defer func() {
if t.Skipped() {
2017-10-02 13:06:39 +00:00
rtest.SkipDisallowed(t, "restic/backend/b2.TestBackendB2")
}
}()
testVars(t)
newB2TestSuite(t).RunTests(t)
}
func BenchmarkBackendb2(t *testing.B) {
testVars(t)
newB2TestSuite(t).RunBenchmarks(t)
}