From 4952f8668251e6ac3c74cbf02652b7f797e07313 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sat, 23 Jan 2016 18:07:15 +0100 Subject: [PATCH] Add test for to prevent double create --- backend/local/backend_test.go | 21 +++++++++++---------- backend/local/local_test.go | 34 ++++++++++++++++++++-------------- backend/mem_backend_test.go | 21 +++++++++++---------- backend/test/backend_test.go | 21 +++++++++++---------- backend/test/tests.go | 26 ++++++++++++++++++++++++++ 5 files changed, 79 insertions(+), 44 deletions(-) diff --git a/backend/local/backend_test.go b/backend/local/backend_test.go index 14e865df5..2745f6e2c 100644 --- a/backend/local/backend_test.go +++ b/backend/local/backend_test.go @@ -7,13 +7,14 @@ import ( "github.com/restic/restic/backend/test" ) -func TestLocalBackendCreate(t *testing.T) { test.Create(t) } -func TestLocalBackendOpen(t *testing.T) { test.Open(t) } -func TestLocalBackendLocation(t *testing.T) { test.Location(t) } -func TestLocalBackendConfig(t *testing.T) { test.Config(t) } -func TestLocalBackendGetReader(t *testing.T) { test.GetReader(t) } -func TestLocalBackendLoad(t *testing.T) { test.Load(t) } -func TestLocalBackendWrite(t *testing.T) { test.Write(t) } -func TestLocalBackendGeneric(t *testing.T) { test.Generic(t) } -func TestLocalBackendDelete(t *testing.T) { test.Delete(t) } -func TestLocalBackendCleanup(t *testing.T) { test.Cleanup(t) } +func TestLocalBackendCreate(t *testing.T) { test.Create(t) } +func TestLocalBackendOpen(t *testing.T) { test.Open(t) } +func TestLocalBackendCreateWithConfig(t *testing.T) { test.CreateWithConfig(t) } +func TestLocalBackendLocation(t *testing.T) { test.Location(t) } +func TestLocalBackendConfig(t *testing.T) { test.Config(t) } +func TestLocalBackendGetReader(t *testing.T) { test.GetReader(t) } +func TestLocalBackendLoad(t *testing.T) { test.Load(t) } +func TestLocalBackendWrite(t *testing.T) { test.Write(t) } +func TestLocalBackendGeneric(t *testing.T) { test.Generic(t) } +func TestLocalBackendDelete(t *testing.T) { test.Delete(t) } +func TestLocalBackendCleanup(t *testing.T) { test.Cleanup(t) } diff --git a/backend/local/local_test.go b/backend/local/local_test.go index d574b015c..3335cbfa8 100644 --- a/backend/local/local_test.go +++ b/backend/local/local_test.go @@ -1,7 +1,6 @@ package local_test import ( - "errors" "fmt" "io/ioutil" "os" @@ -15,28 +14,35 @@ var tempBackendDir string //go:generate go run ../test/generate_backend_tests.go +func createTempdir() error { + if tempBackendDir != "" { + return nil + } + + tempdir, err := ioutil.TempDir("", "restic-local-test-") + if err != nil { + return err + } + + fmt.Printf("created new test backend at %v\n", tempdir) + tempBackendDir = tempdir + return nil +} + func init() { test.CreateFn = func() (backend.Backend, error) { - if tempBackendDir != "" { - return nil, errors.New("temporary local backend dir already exists") - } - - tempdir, err := ioutil.TempDir("", "restic-local-test-") + err := createTempdir() if err != nil { return nil, err } - - fmt.Printf("created new test backend at %v\n", tempdir) - tempBackendDir = tempdir - - return local.Create(tempdir) + return local.Create(tempBackendDir) } test.OpenFn = func() (backend.Backend, error) { - if tempBackendDir == "" { - return nil, errors.New("repository not initialized") + err := createTempdir() + if err != nil { + return nil, err } - return local.Open(tempBackendDir) } diff --git a/backend/mem_backend_test.go b/backend/mem_backend_test.go index 0e04e97f2..73a012e74 100644 --- a/backend/mem_backend_test.go +++ b/backend/mem_backend_test.go @@ -7,13 +7,14 @@ import ( "github.com/restic/restic/backend/test" ) -func TestMemBackendCreate(t *testing.T) { test.Create(t) } -func TestMemBackendOpen(t *testing.T) { test.Open(t) } -func TestMemBackendLocation(t *testing.T) { test.Location(t) } -func TestMemBackendConfig(t *testing.T) { test.Config(t) } -func TestMemBackendGetReader(t *testing.T) { test.GetReader(t) } -func TestMemBackendLoad(t *testing.T) { test.Load(t) } -func TestMemBackendWrite(t *testing.T) { test.Write(t) } -func TestMemBackendGeneric(t *testing.T) { test.Generic(t) } -func TestMemBackendDelete(t *testing.T) { test.Delete(t) } -func TestMemBackendCleanup(t *testing.T) { test.Cleanup(t) } +func TestMemBackendCreate(t *testing.T) { test.Create(t) } +func TestMemBackendOpen(t *testing.T) { test.Open(t) } +func TestMemBackendCreateWithConfig(t *testing.T) { test.CreateWithConfig(t) } +func TestMemBackendLocation(t *testing.T) { test.Location(t) } +func TestMemBackendConfig(t *testing.T) { test.Config(t) } +func TestMemBackendGetReader(t *testing.T) { test.GetReader(t) } +func TestMemBackendLoad(t *testing.T) { test.Load(t) } +func TestMemBackendWrite(t *testing.T) { test.Write(t) } +func TestMemBackendGeneric(t *testing.T) { test.Generic(t) } +func TestMemBackendDelete(t *testing.T) { test.Delete(t) } +func TestMemBackendCleanup(t *testing.T) { test.Cleanup(t) } diff --git a/backend/test/backend_test.go b/backend/test/backend_test.go index 3af250808..db6de1b5f 100644 --- a/backend/test/backend_test.go +++ b/backend/test/backend_test.go @@ -7,13 +7,14 @@ import ( "github.com/restic/restic/backend/test" ) -func TestTestBackendCreate(t *testing.T) { test.Create(t) } -func TestTestBackendOpen(t *testing.T) { test.Open(t) } -func TestTestBackendLocation(t *testing.T) { test.Location(t) } -func TestTestBackendConfig(t *testing.T) { test.Config(t) } -func TestTestBackendGetReader(t *testing.T) { test.GetReader(t) } -func TestTestBackendLoad(t *testing.T) { test.Load(t) } -func TestTestBackendWrite(t *testing.T) { test.Write(t) } -func TestTestBackendGeneric(t *testing.T) { test.Generic(t) } -func TestTestBackendDelete(t *testing.T) { test.Delete(t) } -func TestTestBackendCleanup(t *testing.T) { test.Cleanup(t) } +func TestTestBackendCreate(t *testing.T) { test.Create(t) } +func TestTestBackendOpen(t *testing.T) { test.Open(t) } +func TestTestBackendCreateWithConfig(t *testing.T) { test.CreateWithConfig(t) } +func TestTestBackendLocation(t *testing.T) { test.Location(t) } +func TestTestBackendConfig(t *testing.T) { test.Config(t) } +func TestTestBackendGetReader(t *testing.T) { test.GetReader(t) } +func TestTestBackendLoad(t *testing.T) { test.Load(t) } +func TestTestBackendWrite(t *testing.T) { test.Write(t) } +func TestTestBackendGeneric(t *testing.T) { test.Generic(t) } +func TestTestBackendDelete(t *testing.T) { test.Delete(t) } +func TestTestBackendCleanup(t *testing.T) { test.Cleanup(t) } diff --git a/backend/test/tests.go b/backend/test/tests.go index 65a3e723a..f99b5a60c 100644 --- a/backend/test/tests.go +++ b/backend/test/tests.go @@ -107,6 +107,32 @@ func Open(t testing.TB) { } } +// CreateWithConfig tests that creating a backend in a location which already +// has a config file fails. +func CreateWithConfig(t testing.TB) { + if CreateFn == nil { + t.Fatalf("CreateFn not set") + } + + b := open(t) + defer close(t) + + // save a config + store(t, b, backend.Config, []byte("test config")) + + // now create the backend again, this must fail + _, err := CreateFn() + if err == nil { + t.Fatalf("expected error not found for creating a backend with an existing config file") + } + + // remove config + err = b.Remove(backend.Config, "") + if err != nil { + t.Fatalf("unexpected error removing config: %v", err) + } +} + // Location tests that a location string is returned. func Location(t testing.TB) { b := open(t)