2017-07-08 13:38:48 +00:00
|
|
|
package azure_test
|
|
|
|
|
|
|
|
import (
|
2018-05-31 19:26:28 +00:00
|
|
|
"bytes"
|
2017-07-08 13:38:48 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2018-05-31 19:26:28 +00:00
|
|
|
"io"
|
2017-07-08 13:38:48 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-09-24 18:04:23 +00:00
|
|
|
"github.com/restic/restic/internal/backend"
|
2017-07-08 13:38:48 +00:00
|
|
|
"github.com/restic/restic/internal/backend/azure"
|
|
|
|
"github.com/restic/restic/internal/backend/test"
|
2017-08-05 19:46:15 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2021-08-04 20:56:18 +00:00
|
|
|
"github.com/restic/restic/internal/options"
|
2017-07-08 13:38:48 +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:38:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func newAzureTestSuite(t testing.TB) *test.Suite {
|
2018-01-27 12:57:43 +00:00
|
|
|
tr, err := backend.Transport(backend.TransportOptions{})
|
2017-09-24 18:04:23 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("cannot create transport for tests: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-07-08 13:38:48 +00:00
|
|
|
return &test.Suite{
|
|
|
|
// do not use excessive data
|
|
|
|
MinimalData: true,
|
|
|
|
|
|
|
|
// NewConfig returns a config for a new temporary backend that will be used in tests.
|
|
|
|
NewConfig: func() (interface{}, error) {
|
|
|
|
azcfg, err := azure.ParseConfig(os.Getenv("RESTIC_TEST_AZURE_REPOSITORY"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := azcfg.(azure.Config)
|
|
|
|
cfg.AccountName = os.Getenv("RESTIC_TEST_AZURE_ACCOUNT_NAME")
|
2021-08-04 20:56:18 +00:00
|
|
|
cfg.AccountKey = options.NewSecretString(os.Getenv("RESTIC_TEST_AZURE_ACCOUNT_KEY"))
|
2017-07-08 13:38:48 +00:00
|
|
|
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(config interface{}) (restic.Backend, error) {
|
|
|
|
cfg := config.(azure.Config)
|
|
|
|
|
2022-11-18 22:04:31 +00:00
|
|
|
ctx := context.TODO()
|
|
|
|
be, err := azure.Create(ctx, cfg, tr)
|
2017-07-08 13:38:48 +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:38:48 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-03 10:28:10 +00:00
|
|
|
if err == nil {
|
2017-07-08 13:38:48 +00:00
|
|
|
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.(azure.Config)
|
2022-11-18 22:04:31 +00:00
|
|
|
ctx := context.TODO()
|
|
|
|
return azure.Open(ctx, cfg, tr)
|
2017-07-08 13:38:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// CleanupFn removes data created during the tests.
|
|
|
|
Cleanup: func(config interface{}) error {
|
|
|
|
cfg := config.(azure.Config)
|
2022-11-18 22:04:31 +00:00
|
|
|
ctx := context.TODO()
|
|
|
|
be, err := azure.Open(ctx, cfg, tr)
|
2017-07-08 13:38:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-10-14 13:56:38 +00:00
|
|
|
return be.Delete(context.TODO())
|
2017-07-08 13:38:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackendAzure(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
if t.Skipped() {
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.SkipDisallowed(t, "restic/backend/azure.TestBackendAzure")
|
2017-07-08 13:38:48 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
vars := []string{
|
|
|
|
"RESTIC_TEST_AZURE_ACCOUNT_NAME",
|
|
|
|
"RESTIC_TEST_AZURE_ACCOUNT_KEY",
|
|
|
|
"RESTIC_TEST_AZURE_REPOSITORY",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range vars {
|
|
|
|
if os.Getenv(v) == "" {
|
|
|
|
t.Skipf("environment variable %v not set", v)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("run tests")
|
|
|
|
newAzureTestSuite(t).RunTests(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkBackendAzure(t *testing.B) {
|
|
|
|
vars := []string{
|
|
|
|
"RESTIC_TEST_AZURE_ACCOUNT_NAME",
|
|
|
|
"RESTIC_TEST_AZURE_ACCOUNT_KEY",
|
|
|
|
"RESTIC_TEST_AZURE_REPOSITORY",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range vars {
|
|
|
|
if os.Getenv(v) == "" {
|
|
|
|
t.Skipf("environment variable %v not set", v)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("run tests")
|
|
|
|
newAzureTestSuite(t).RunBenchmarks(t)
|
|
|
|
}
|
2018-05-31 19:26:28 +00:00
|
|
|
|
|
|
|
func TestUploadLargeFile(t *testing.T) {
|
|
|
|
if os.Getenv("RESTIC_AZURE_TEST_LARGE_UPLOAD") == "" {
|
|
|
|
t.Skip("set RESTIC_AZURE_TEST_LARGE_UPLOAD=1 to test large uploads")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.TODO())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if os.Getenv("RESTIC_TEST_AZURE_REPOSITORY") == "" {
|
|
|
|
t.Skipf("environment variables not available")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
azcfg, err := azure.ParseConfig(os.Getenv("RESTIC_TEST_AZURE_REPOSITORY"))
|
|
|
|
if err != nil {
|
2020-12-11 08:41:59 +00:00
|
|
|
t.Fatal(err)
|
2018-05-31 19:26:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg := azcfg.(azure.Config)
|
|
|
|
cfg.AccountName = os.Getenv("RESTIC_TEST_AZURE_ACCOUNT_NAME")
|
2021-08-04 20:56:18 +00:00
|
|
|
cfg.AccountKey = options.NewSecretString(os.Getenv("RESTIC_TEST_AZURE_ACCOUNT_KEY"))
|
2018-05-31 19:26:28 +00:00
|
|
|
cfg.Prefix = fmt.Sprintf("test-upload-large-%d", time.Now().UnixNano())
|
|
|
|
|
|
|
|
tr, err := backend.Transport(backend.TransportOptions{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-11-18 22:04:31 +00:00
|
|
|
be, err := azure.Create(ctx, cfg, tr)
|
2018-05-31 19:26:28 +00:00
|
|
|
if err != nil {
|
2020-12-11 08:41:59 +00:00
|
|
|
t.Fatal(err)
|
2018-05-31 19:26:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err := be.Delete(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
data := rtest.Random(23, 300*1024*1024)
|
|
|
|
id := restic.Hash(data)
|
2020-08-16 09:16:38 +00:00
|
|
|
h := restic.Handle{Name: id.String(), Type: restic.PackFile}
|
2018-05-31 19:26:28 +00:00
|
|
|
|
|
|
|
t.Logf("hash of %d bytes: %v", len(data), id)
|
|
|
|
|
2020-12-19 11:39:48 +00:00
|
|
|
err = be.Save(ctx, h, restic.NewByteReader(data, be.Hasher()))
|
2018-05-31 19:26:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err := be.Remove(ctx, h)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var tests = []struct {
|
|
|
|
offset, length int
|
|
|
|
}{
|
|
|
|
{0, len(data)},
|
|
|
|
{23, 1024},
|
|
|
|
{23 + 100*1024, 500},
|
|
|
|
{888 + 200*1024, 89999},
|
|
|
|
{888 + 100*1024*1024, 120 * 1024 * 1024},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
want := data[test.offset : test.offset+test.length]
|
|
|
|
|
|
|
|
buf := make([]byte, test.length)
|
|
|
|
err = be.Load(ctx, h, test.length, int64(test.offset), func(rd io.Reader) error {
|
|
|
|
_, err = io.ReadFull(rd, buf)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(buf, want) {
|
|
|
|
t.Fatalf("wrong bytes returned")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|