github.com/mckael/restic@v0.8.3/internal/backend/azure/azure_test.go (about) 1 package azure_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/azure" 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 newAzureTestSuite(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 azcfg, err := azure.ParseConfig(os.Getenv("RESTIC_TEST_AZURE_REPOSITORY")) 31 if err != nil { 32 return nil, err 33 } 34 35 cfg := azcfg.(azure.Config) 36 cfg.AccountName = os.Getenv("RESTIC_TEST_AZURE_ACCOUNT_NAME") 37 cfg.AccountKey = os.Getenv("RESTIC_TEST_AZURE_ACCOUNT_KEY") 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.(azure.Config) 45 46 be, err := azure.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.(azure.Config) 66 67 return azure.Open(cfg, tr) 68 }, 69 70 // CleanupFn removes data created during the tests. 71 Cleanup: func(config interface{}) error { 72 cfg := config.(azure.Config) 73 74 be, err := azure.Open(cfg, tr) 75 if err != nil { 76 return err 77 } 78 79 return be.Delete(context.TODO()) 80 }, 81 } 82 } 83 84 func TestBackendAzure(t *testing.T) { 85 defer func() { 86 if t.Skipped() { 87 rtest.SkipDisallowed(t, "restic/backend/azure.TestBackendAzure") 88 } 89 }() 90 91 vars := []string{ 92 "RESTIC_TEST_AZURE_ACCOUNT_NAME", 93 "RESTIC_TEST_AZURE_ACCOUNT_KEY", 94 "RESTIC_TEST_AZURE_REPOSITORY", 95 } 96 97 for _, v := range vars { 98 if os.Getenv(v) == "" { 99 t.Skipf("environment variable %v not set", v) 100 return 101 } 102 } 103 104 t.Logf("run tests") 105 newAzureTestSuite(t).RunTests(t) 106 } 107 108 func BenchmarkBackendAzure(t *testing.B) { 109 vars := []string{ 110 "RESTIC_TEST_AZURE_ACCOUNT_NAME", 111 "RESTIC_TEST_AZURE_ACCOUNT_KEY", 112 "RESTIC_TEST_AZURE_REPOSITORY", 113 } 114 115 for _, v := range vars { 116 if os.Getenv(v) == "" { 117 t.Skipf("environment variable %v not set", v) 118 return 119 } 120 } 121 122 t.Logf("run tests") 123 newAzureTestSuite(t).RunBenchmarks(t) 124 }