2016-01-23 18:46:04 +01:00
|
|
|
package s3_test
|
2016-01-02 14:38:45 +01:00
|
|
|
|
2016-01-23 18:46:04 +01:00
|
|
|
import (
|
2017-05-11 21:54:04 +02:00
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"net"
|
2016-01-23 18:46:04 +01:00
|
|
|
"os"
|
2017-05-11 21:54:04 +02:00
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2016-08-31 22:51:35 +02:00
|
|
|
"restic"
|
2017-05-11 21:54:04 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
2016-08-21 17:46:23 +02:00
|
|
|
|
2016-02-14 15:29:28 +01:00
|
|
|
"restic/backend/s3"
|
|
|
|
"restic/backend/test"
|
|
|
|
. "restic/test"
|
2016-01-23 18:46:04 +01:00
|
|
|
)
|
2016-01-02 14:38:45 +01:00
|
|
|
|
2017-05-11 21:54:04 +02:00
|
|
|
func mkdir(t testing.TB, dir string) {
|
|
|
|
err := os.MkdirAll(dir, 0700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2016-01-23 18:46:04 +01:00
|
|
|
}
|
2017-05-11 21:54:04 +02:00
|
|
|
}
|
2016-01-23 18:46:04 +01:00
|
|
|
|
2017-05-11 21:54:04 +02:00
|
|
|
func runMinio(ctx context.Context, t testing.TB, dir, key, secret string) func() {
|
|
|
|
mkdir(t, filepath.Join(dir, "config"))
|
|
|
|
mkdir(t, filepath.Join(dir, "root"))
|
|
|
|
|
|
|
|
cmd := exec.CommandContext(ctx, "minio",
|
|
|
|
"server",
|
|
|
|
"--address", "127.0.0.1:9000",
|
|
|
|
"--config-dir", filepath.Join(dir, "config"),
|
|
|
|
filepath.Join(dir, "root"))
|
|
|
|
cmd.Env = append(os.Environ(),
|
|
|
|
"MINIO_ACCESS_KEY="+key,
|
|
|
|
"MINIO_SECRET_KEY="+secret,
|
|
|
|
)
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
err := cmd.Start()
|
2016-01-23 18:46:04 +01:00
|
|
|
if err != nil {
|
2017-05-11 21:54:04 +02:00
|
|
|
t.Fatal(err)
|
2016-01-23 18:46:04 +01:00
|
|
|
}
|
|
|
|
|
2017-05-11 21:54:04 +02:00
|
|
|
// wait until the TCP port is reachable
|
|
|
|
var success bool
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
c, err := net.Dial("tcp", "localhost:9000")
|
|
|
|
if err != nil {
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
success = true
|
|
|
|
if err := c.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-01-23 18:46:04 +01:00
|
|
|
}
|
|
|
|
|
2017-05-11 21:54:04 +02:00
|
|
|
if !success {
|
|
|
|
t.Fatal("unable to connect to minio server")
|
|
|
|
return nil
|
2016-01-23 18:46:04 +01:00
|
|
|
}
|
|
|
|
|
2017-05-11 21:54:04 +02:00
|
|
|
return func() {
|
|
|
|
err = cmd.Process.Kill()
|
2016-01-23 18:46:04 +01:00
|
|
|
if err != nil {
|
2017-05-11 21:54:04 +02:00
|
|
|
t.Fatal(err)
|
2016-01-23 18:46:04 +01:00
|
|
|
}
|
2017-05-11 21:54:04 +02:00
|
|
|
}
|
|
|
|
}
|
2016-01-23 18:46:04 +01:00
|
|
|
|
2017-05-11 21:54:04 +02:00
|
|
|
func newCredentials(t testing.TB) (key, secret string) {
|
|
|
|
buf := make([]byte, 10)
|
|
|
|
_, err := io.ReadFull(rand.Reader, buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
key = hex.EncodeToString(buf)
|
2016-01-23 18:46:04 +01:00
|
|
|
|
2017-05-11 21:54:04 +02:00
|
|
|
_, err = io.ReadFull(rand.Reader, buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
secret = hex.EncodeToString(buf)
|
2016-01-23 18:46:04 +01:00
|
|
|
|
2017-05-11 21:54:04 +02:00
|
|
|
return key, secret
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackendMinio(t *testing.T) {
|
|
|
|
// try to find a minio binary
|
|
|
|
_, err := exec.LookPath("minio")
|
|
|
|
if err != nil {
|
|
|
|
t.Skip(err)
|
|
|
|
return
|
2016-01-23 18:46:04 +01:00
|
|
|
}
|
|
|
|
|
2017-05-11 21:54:04 +02:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
s3.Config
|
|
|
|
|
|
|
|
tempdir string
|
|
|
|
removeTempdir func()
|
|
|
|
stopServer func()
|
2016-01-23 18:46:04 +01:00
|
|
|
}
|
|
|
|
|
2017-05-11 21:54:04 +02:00
|
|
|
suite := test.Suite{
|
|
|
|
// NewConfig returns a config for a new temporary backend that will be used in tests.
|
|
|
|
NewConfig: func() (interface{}, error) {
|
|
|
|
cfg := Config{}
|
|
|
|
|
|
|
|
cfg.tempdir, cfg.removeTempdir = TempDir(t)
|
|
|
|
key, secret := newCredentials(t)
|
|
|
|
cfg.stopServer = runMinio(ctx, t, cfg.tempdir, key, secret)
|
|
|
|
|
|
|
|
cfg.Config = s3.Config{
|
|
|
|
Endpoint: "localhost:9000",
|
|
|
|
Bucket: "restictestbucket",
|
|
|
|
UseHTTP: true,
|
|
|
|
KeyID: key,
|
|
|
|
Secret: secret,
|
|
|
|
}
|
|
|
|
return cfg, nil
|
|
|
|
},
|
|
|
|
|
|
|
|
// CreateFn is a function that creates a temporary repository for the tests.
|
|
|
|
Create: func(config interface{}) (restic.Backend, error) {
|
|
|
|
cfg := config.(Config)
|
|
|
|
|
|
|
|
be, err := s3.Open(cfg.Config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
exists, err := be.Test(restic.Handle{Type: restic.ConfigFile})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
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.(Config)
|
|
|
|
return s3.Open(cfg.Config)
|
|
|
|
},
|
|
|
|
|
|
|
|
// CleanupFn removes data created during the tests.
|
|
|
|
Cleanup: func(config interface{}) error {
|
|
|
|
cfg := config.(Config)
|
|
|
|
if cfg.stopServer != nil {
|
|
|
|
cfg.stopServer()
|
|
|
|
}
|
|
|
|
if cfg.removeTempdir != nil {
|
|
|
|
t.Logf("removeTempdir %v", config)
|
|
|
|
cfg.removeTempdir()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2016-01-23 18:46:04 +01:00
|
|
|
|
2017-05-11 21:54:04 +02:00
|
|
|
suite.RunTests(t)
|
2016-01-02 14:38:45 +01:00
|
|
|
}
|