From 15c8b85a4ba35395725524c3c0c1175df44e0eba Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sat, 23 Jan 2016 18:46:04 +0100 Subject: [PATCH] Add tests for s3 backend --- backend/s3/backend_test.go | 87 ++++++++++++++++++++++++++++++++++++++ backend/s3/s3_test.go | 71 +++++++++++++++++++++++++++++-- 2 files changed, 155 insertions(+), 3 deletions(-) create mode 100644 backend/s3/backend_test.go diff --git a/backend/s3/backend_test.go b/backend/s3/backend_test.go new file mode 100644 index 000000000..f8f824bd6 --- /dev/null +++ b/backend/s3/backend_test.go @@ -0,0 +1,87 @@ +// DO NOT EDIT, AUTOMATICALLY GENERATED +package s3_test + +import ( + "testing" + + "github.com/restic/restic/backend/test" +) + +var SkipMessage string + +func TestS3BackendCreate(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Create(t) +} + +func TestS3BackendOpen(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Open(t) +} + +func TestS3BackendCreateWithConfig(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.CreateWithConfig(t) +} + +func TestS3BackendLocation(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Location(t) +} + +func TestS3BackendConfig(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Config(t) +} + +func TestS3BackendGetReader(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.GetReader(t) +} + +func TestS3BackendLoad(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Load(t) +} + +func TestS3BackendWrite(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Write(t) +} + +func TestS3BackendGeneric(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Generic(t) +} + +func TestS3BackendDelete(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Delete(t) +} + +func TestS3BackendCleanup(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Cleanup(t) +} diff --git a/backend/s3/s3_test.go b/backend/s3/s3_test.go index 289748485..050e4300a 100644 --- a/backend/s3/s3_test.go +++ b/backend/s3/s3_test.go @@ -1,7 +1,72 @@ -package s3 +package s3_test -import "testing" +import ( + "errors" + "fmt" + "net/url" + "os" -func TestGetReader(t *testing.T) { + "github.com/restic/restic/backend" + "github.com/restic/restic/backend/s3" + "github.com/restic/restic/backend/test" + . "github.com/restic/restic/test" +) +//go:generate go run ../test/generate_backend_tests.go + +func init() { + if TestS3Server == "" { + SkipMessage = "s3 test server not available" + return + } + + url, err := url.Parse(TestS3Server) + if err != nil { + fmt.Fprintf(os.Stderr, "invalid url: %v\n", err) + return + } + + cfg := s3.Config{ + Endpoint: url.Host, + Bucket: "restictestbucket", + KeyID: os.Getenv("AWS_ACCESS_KEY_ID"), + Secret: os.Getenv("AWS_SECRET_ACCESS_KEY"), + } + + if url.Scheme == "http" { + cfg.UseHTTP = true + } + + test.CreateFn = func() (backend.Backend, error) { + be, err := s3.Open(cfg) + if err != nil { + return nil, err + } + + exists, err := be.Test(backend.Config, "") + if err != nil { + return nil, err + } + + if exists { + return nil, errors.New("config already exists") + } + + return be, nil + } + + test.OpenFn = func() (backend.Backend, error) { + return s3.Open(cfg) + } + + // test.CleanupFn = func() error { + // if tempBackendDir == "" { + // return nil + // } + + // fmt.Printf("removing test backend at %v\n", tempBackendDir) + // err := os.RemoveAll(tempBackendDir) + // tempBackendDir = "" + // return err + // } }