2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-16 10:05:25 +00:00

Add test for to prevent double create

This commit is contained in:
Alexander Neumann 2016-01-23 18:07:15 +01:00
parent 16b7cc7655
commit 4952f86682
5 changed files with 79 additions and 44 deletions

View File

@ -7,13 +7,14 @@ import (
"github.com/restic/restic/backend/test" "github.com/restic/restic/backend/test"
) )
func TestLocalBackendCreate(t *testing.T) { test.Create(t) } func TestLocalBackendCreate(t *testing.T) { test.Create(t) }
func TestLocalBackendOpen(t *testing.T) { test.Open(t) } func TestLocalBackendOpen(t *testing.T) { test.Open(t) }
func TestLocalBackendLocation(t *testing.T) { test.Location(t) } func TestLocalBackendCreateWithConfig(t *testing.T) { test.CreateWithConfig(t) }
func TestLocalBackendConfig(t *testing.T) { test.Config(t) } func TestLocalBackendLocation(t *testing.T) { test.Location(t) }
func TestLocalBackendGetReader(t *testing.T) { test.GetReader(t) } func TestLocalBackendConfig(t *testing.T) { test.Config(t) }
func TestLocalBackendLoad(t *testing.T) { test.Load(t) } func TestLocalBackendGetReader(t *testing.T) { test.GetReader(t) }
func TestLocalBackendWrite(t *testing.T) { test.Write(t) } func TestLocalBackendLoad(t *testing.T) { test.Load(t) }
func TestLocalBackendGeneric(t *testing.T) { test.Generic(t) } func TestLocalBackendWrite(t *testing.T) { test.Write(t) }
func TestLocalBackendDelete(t *testing.T) { test.Delete(t) } func TestLocalBackendGeneric(t *testing.T) { test.Generic(t) }
func TestLocalBackendCleanup(t *testing.T) { test.Cleanup(t) } func TestLocalBackendDelete(t *testing.T) { test.Delete(t) }
func TestLocalBackendCleanup(t *testing.T) { test.Cleanup(t) }

View File

@ -1,7 +1,6 @@
package local_test package local_test
import ( import (
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
@ -15,28 +14,35 @@ var tempBackendDir string
//go:generate go run ../test/generate_backend_tests.go //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() { func init() {
test.CreateFn = func() (backend.Backend, error) { test.CreateFn = func() (backend.Backend, error) {
if tempBackendDir != "" { err := createTempdir()
return nil, errors.New("temporary local backend dir already exists")
}
tempdir, err := ioutil.TempDir("", "restic-local-test-")
if err != nil { if err != nil {
return nil, err return nil, err
} }
return local.Create(tempBackendDir)
fmt.Printf("created new test backend at %v\n", tempdir)
tempBackendDir = tempdir
return local.Create(tempdir)
} }
test.OpenFn = func() (backend.Backend, error) { test.OpenFn = func() (backend.Backend, error) {
if tempBackendDir == "" { err := createTempdir()
return nil, errors.New("repository not initialized") if err != nil {
return nil, err
} }
return local.Open(tempBackendDir) return local.Open(tempBackendDir)
} }

View File

@ -7,13 +7,14 @@ import (
"github.com/restic/restic/backend/test" "github.com/restic/restic/backend/test"
) )
func TestMemBackendCreate(t *testing.T) { test.Create(t) } func TestMemBackendCreate(t *testing.T) { test.Create(t) }
func TestMemBackendOpen(t *testing.T) { test.Open(t) } func TestMemBackendOpen(t *testing.T) { test.Open(t) }
func TestMemBackendLocation(t *testing.T) { test.Location(t) } func TestMemBackendCreateWithConfig(t *testing.T) { test.CreateWithConfig(t) }
func TestMemBackendConfig(t *testing.T) { test.Config(t) } func TestMemBackendLocation(t *testing.T) { test.Location(t) }
func TestMemBackendGetReader(t *testing.T) { test.GetReader(t) } func TestMemBackendConfig(t *testing.T) { test.Config(t) }
func TestMemBackendLoad(t *testing.T) { test.Load(t) } func TestMemBackendGetReader(t *testing.T) { test.GetReader(t) }
func TestMemBackendWrite(t *testing.T) { test.Write(t) } func TestMemBackendLoad(t *testing.T) { test.Load(t) }
func TestMemBackendGeneric(t *testing.T) { test.Generic(t) } func TestMemBackendWrite(t *testing.T) { test.Write(t) }
func TestMemBackendDelete(t *testing.T) { test.Delete(t) } func TestMemBackendGeneric(t *testing.T) { test.Generic(t) }
func TestMemBackendCleanup(t *testing.T) { test.Cleanup(t) } func TestMemBackendDelete(t *testing.T) { test.Delete(t) }
func TestMemBackendCleanup(t *testing.T) { test.Cleanup(t) }

View File

@ -7,13 +7,14 @@ import (
"github.com/restic/restic/backend/test" "github.com/restic/restic/backend/test"
) )
func TestTestBackendCreate(t *testing.T) { test.Create(t) } func TestTestBackendCreate(t *testing.T) { test.Create(t) }
func TestTestBackendOpen(t *testing.T) { test.Open(t) } func TestTestBackendOpen(t *testing.T) { test.Open(t) }
func TestTestBackendLocation(t *testing.T) { test.Location(t) } func TestTestBackendCreateWithConfig(t *testing.T) { test.CreateWithConfig(t) }
func TestTestBackendConfig(t *testing.T) { test.Config(t) } func TestTestBackendLocation(t *testing.T) { test.Location(t) }
func TestTestBackendGetReader(t *testing.T) { test.GetReader(t) } func TestTestBackendConfig(t *testing.T) { test.Config(t) }
func TestTestBackendLoad(t *testing.T) { test.Load(t) } func TestTestBackendGetReader(t *testing.T) { test.GetReader(t) }
func TestTestBackendWrite(t *testing.T) { test.Write(t) } func TestTestBackendLoad(t *testing.T) { test.Load(t) }
func TestTestBackendGeneric(t *testing.T) { test.Generic(t) } func TestTestBackendWrite(t *testing.T) { test.Write(t) }
func TestTestBackendDelete(t *testing.T) { test.Delete(t) } func TestTestBackendGeneric(t *testing.T) { test.Generic(t) }
func TestTestBackendCleanup(t *testing.T) { test.Cleanup(t) } func TestTestBackendDelete(t *testing.T) { test.Delete(t) }
func TestTestBackendCleanup(t *testing.T) { test.Cleanup(t) }

View File

@ -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. // Location tests that a location string is returned.
func Location(t testing.TB) { func Location(t testing.TB) {
b := open(t) b := open(t)