github.com/mckael/restic@v0.8.3/internal/backend/gs/gs_test.go (about) 1 package gs_test 2 3 import ( 4 "context" 5 "fmt" 6 "os" 7 "testing" 8 "time" 9 10 "github.com/restic/restic/internal/backend" 11 "github.com/restic/restic/internal/backend/gs" 12 "github.com/restic/restic/internal/backend/test" 13 "github.com/restic/restic/internal/errors" 14 "github.com/restic/restic/internal/restic" 15 rtest "github.com/restic/restic/internal/test" 16 ) 17 18 func newGSTestSuite(t testing.TB) *test.Suite { 19 tr, err := backend.Transport(backend.TransportOptions{}) 20 if err != nil { 21 t.Fatalf("cannot create transport for tests: %v", err) 22 } 23 24 return &test.Suite{ 25 // do not use excessive data 26 MinimalData: true, 27 28 // NewConfig returns a config for a new temporary backend that will be used in tests. 29 NewConfig: func() (interface{}, error) { 30 gscfg, err := gs.ParseConfig(os.Getenv("RESTIC_TEST_GS_REPOSITORY")) 31 if err != nil { 32 return nil, err 33 } 34 35 cfg := gscfg.(gs.Config) 36 cfg.ProjectID = os.Getenv("RESTIC_TEST_GS_PROJECT_ID") 37 cfg.JSONKeyPath = os.Getenv("RESTIC_TEST_GS_APPLICATION_CREDENTIALS") 38 cfg.Prefix = fmt.Sprintf("test-%d", time.Now().UnixNano()) 39 return cfg, nil 40 }, 41 42 // CreateFn is a function that creates a temporary repository for the tests. 43 Create: func(config interface{}) (restic.Backend, error) { 44 cfg := config.(gs.Config) 45 46 be, err := gs.Create(cfg, tr) 47 if err != nil { 48 return nil, err 49 } 50 51 exists, err := be.Test(context.TODO(), restic.Handle{Type: restic.ConfigFile}) 52 if err != nil { 53 return nil, err 54 } 55 56 if exists { 57 return nil, errors.New("config already exists") 58 } 59 60 return be, nil 61 }, 62 63 // OpenFn is a function that opens a previously created temporary repository. 64 Open: func(config interface{}) (restic.Backend, error) { 65 cfg := config.(gs.Config) 66 return gs.Open(cfg, tr) 67 }, 68 69 // CleanupFn removes data created during the tests. 70 Cleanup: func(config interface{}) error { 71 cfg := config.(gs.Config) 72 73 be, err := gs.Open(cfg, tr) 74 if err != nil { 75 return err 76 } 77 78 return be.Delete(context.TODO()) 79 }, 80 } 81 } 82 83 func TestBackendGS(t *testing.T) { 84 defer func() { 85 if t.Skipped() { 86 rtest.SkipDisallowed(t, "restic/backend/gs.TestBackendGS") 87 } 88 }() 89 90 vars := []string{ 91 "RESTIC_TEST_GS_PROJECT_ID", 92 "RESTIC_TEST_GS_APPLICATION_CREDENTIALS", 93 "RESTIC_TEST_GS_REPOSITORY", 94 } 95 96 for _, v := range vars { 97 if os.Getenv(v) == "" { 98 t.Skipf("environment variable %v not set", v) 99 return 100 } 101 } 102 103 t.Logf("run tests") 104 newGSTestSuite(t).RunTests(t) 105 } 106 107 func BenchmarkBackendGS(t *testing.B) { 108 vars := []string{ 109 "RESTIC_TEST_GS_PROJECT_ID", 110 "RESTIC_TEST_GS_APPLICATION_CREDENTIALS", 111 "RESTIC_TEST_GS_REPOSITORY", 112 } 113 114 for _, v := range vars { 115 if os.Getenv(v) == "" { 116 t.Skipf("environment variable %v not set", v) 117 return 118 } 119 } 120 121 t.Logf("run tests") 122 newGSTestSuite(t).RunBenchmarks(t) 123 }