2017-07-08 13:34:23 +00:00
|
|
|
package gs_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-01-27 19:12:34 +00:00
|
|
|
"github.com/restic/restic/internal/backend"
|
2017-07-08 13:34:23 +00:00
|
|
|
"github.com/restic/restic/internal/backend/gs"
|
|
|
|
"github.com/restic/restic/internal/backend/test"
|
2017-08-05 15:15:59 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2017-07-08 13:34:23 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest "github.com/restic/restic/internal/test"
|
2017-07-08 13:34:23 +00:00
|
|
|
)
|
|
|
|
|
2023-04-21 19:06:56 +00:00
|
|
|
func newGSTestSuite(t testing.TB) *test.Suite[gs.Config] {
|
2018-01-27 19:12:34 +00:00
|
|
|
tr, err := backend.Transport(backend.TransportOptions{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("cannot create transport for tests: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-04-21 19:06:56 +00:00
|
|
|
return &test.Suite[gs.Config]{
|
2017-07-08 13:34:23 +00:00
|
|
|
// do not use excessive data
|
|
|
|
MinimalData: true,
|
|
|
|
|
|
|
|
// 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() (*gs.Config, error) {
|
2023-04-20 20:40:21 +00:00
|
|
|
cfg, err := gs.ParseConfig(os.Getenv("RESTIC_TEST_GS_REPOSITORY"))
|
2017-07-08 13:34:23 +00:00
|
|
|
if err != nil {
|
2023-04-21 19:35:34 +00:00
|
|
|
return nil, err
|
2017-07-08 13:34:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg.ProjectID = os.Getenv("RESTIC_TEST_GS_PROJECT_ID")
|
|
|
|
cfg.Prefix = fmt.Sprintf("test-%d", time.Now().UnixNano())
|
|
|
|
return cfg, nil
|
|
|
|
},
|
|
|
|
|
|
|
|
// CreateFn is a function that creates a temporary repository for the tests.
|
2023-04-21 19:06:56 +00:00
|
|
|
Create: func(cfg gs.Config) (restic.Backend, error) {
|
2023-04-08 09:59:44 +00:00
|
|
|
be, err := gs.Create(context.Background(), cfg, tr)
|
2017-07-08 13:34:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
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-07-08 13:34:23 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-03 10:28:10 +00:00
|
|
|
if err == nil {
|
2017-07-08 13:34:23 +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 gs.Config) (restic.Backend, error) {
|
2018-01-27 19:12:34 +00:00
|
|
|
return gs.Open(cfg, tr)
|
2017-07-08 13:34:23 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// CleanupFn removes data created during the tests.
|
2023-04-21 19:06:56 +00:00
|
|
|
Cleanup: func(cfg gs.Config) error {
|
2018-01-27 19:12:34 +00:00
|
|
|
be, err := gs.Open(cfg, tr)
|
2017-07-08 13:34:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-10-14 13:56:38 +00:00
|
|
|
return be.Delete(context.TODO())
|
2017-07-08 13:34:23 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackendGS(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
if t.Skipped() {
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.SkipDisallowed(t, "restic/backend/gs.TestBackendGS")
|
2017-07-08 13:34:23 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
vars := []string{
|
|
|
|
"RESTIC_TEST_GS_PROJECT_ID",
|
|
|
|
"RESTIC_TEST_GS_REPOSITORY",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range vars {
|
|
|
|
if os.Getenv(v) == "" {
|
|
|
|
t.Skipf("environment variable %v not set", v)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-07-21 17:24:30 +00:00
|
|
|
if os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")+os.Getenv("GOOGLE_ACCESS_TOKEN") == "" {
|
|
|
|
t.Skipf("environment variable GOOGLE_APPLICATION_CREDENTIALS not set, nor GOOGLE_ACCESS_TOKEN")
|
|
|
|
return
|
|
|
|
}
|
2017-07-08 13:34:23 +00:00
|
|
|
|
|
|
|
t.Logf("run tests")
|
|
|
|
newGSTestSuite(t).RunTests(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkBackendGS(t *testing.B) {
|
|
|
|
vars := []string{
|
|
|
|
"RESTIC_TEST_GS_PROJECT_ID",
|
|
|
|
"RESTIC_TEST_GS_REPOSITORY",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range vars {
|
|
|
|
if os.Getenv(v) == "" {
|
|
|
|
t.Skipf("environment variable %v not set", v)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-07-21 17:24:30 +00:00
|
|
|
if os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")+os.Getenv("GOOGLE_ACCESS_TOKEN") == "" {
|
|
|
|
t.Skipf("environment variable GOOGLE_APPLICATION_CREDENTIALS not set, nor GOOGLE_ACCESS_TOKEN")
|
|
|
|
return
|
|
|
|
}
|
2017-07-08 13:34:23 +00:00
|
|
|
|
|
|
|
t.Logf("run tests")
|
|
|
|
newGSTestSuite(t).RunBenchmarks(t)
|
|
|
|
}
|