From c6db567e3fde05a46d70b35199472ffe4fea6309 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sat, 23 Jan 2016 18:30:02 +0100 Subject: [PATCH] Add sftp tests --- backend/local/backend_test.go | 89 ++++++++++++++++++++++---- backend/mem_backend_test.go | 89 ++++++++++++++++++++++---- backend/sftp/backend_test.go | 87 +++++++++++++++++++++++++ backend/sftp/sftp.go | 4 ++ backend/sftp/sftp_backend_test.go | 81 +++++++++++++++++++++++ backend/sftp_test.go | 65 ------------------- backend/test/backend_test.go | 89 ++++++++++++++++++++++---- backend/test/generate_backend_tests.go | 14 +++- 8 files changed, 418 insertions(+), 100 deletions(-) create mode 100644 backend/sftp/backend_test.go create mode 100644 backend/sftp/sftp_backend_test.go delete mode 100644 backend/sftp_test.go diff --git a/backend/local/backend_test.go b/backend/local/backend_test.go index 2745f6e2c..e1ac22dd8 100644 --- a/backend/local/backend_test.go +++ b/backend/local/backend_test.go @@ -7,14 +7,81 @@ import ( "github.com/restic/restic/backend/test" ) -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) } +var SkipMessage string + +func TestLocalBackendCreate(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Create(t) +} + +func TestLocalBackendOpen(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Open(t) +} + +func TestLocalBackendCreateWithConfig(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.CreateWithConfig(t) +} + +func TestLocalBackendLocation(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Location(t) +} + +func TestLocalBackendConfig(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Config(t) +} + +func TestLocalBackendGetReader(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.GetReader(t) +} + +func TestLocalBackendLoad(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Load(t) +} + +func TestLocalBackendWrite(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Write(t) +} + +func TestLocalBackendGeneric(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Generic(t) +} + +func TestLocalBackendDelete(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Delete(t) +} + +func TestLocalBackendCleanup(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Cleanup(t) +} diff --git a/backend/mem_backend_test.go b/backend/mem_backend_test.go index 73a012e74..41ce60041 100644 --- a/backend/mem_backend_test.go +++ b/backend/mem_backend_test.go @@ -7,14 +7,81 @@ import ( "github.com/restic/restic/backend/test" ) -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) } +var SkipMessage string + +func TestMemBackendCreate(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Create(t) +} + +func TestMemBackendOpen(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Open(t) +} + +func TestMemBackendCreateWithConfig(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.CreateWithConfig(t) +} + +func TestMemBackendLocation(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Location(t) +} + +func TestMemBackendConfig(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Config(t) +} + +func TestMemBackendGetReader(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.GetReader(t) +} + +func TestMemBackendLoad(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Load(t) +} + +func TestMemBackendWrite(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Write(t) +} + +func TestMemBackendGeneric(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Generic(t) +} + +func TestMemBackendDelete(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Delete(t) +} + +func TestMemBackendCleanup(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Cleanup(t) +} diff --git a/backend/sftp/backend_test.go b/backend/sftp/backend_test.go new file mode 100644 index 000000000..973c7e4aa --- /dev/null +++ b/backend/sftp/backend_test.go @@ -0,0 +1,87 @@ +// DO NOT EDIT, AUTOMATICALLY GENERATED +package sftp_test + +import ( + "testing" + + "github.com/restic/restic/backend/test" +) + +var SkipMessage string + +func TestSftpBackendCreate(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Create(t) +} + +func TestSftpBackendOpen(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Open(t) +} + +func TestSftpBackendCreateWithConfig(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.CreateWithConfig(t) +} + +func TestSftpBackendLocation(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Location(t) +} + +func TestSftpBackendConfig(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Config(t) +} + +func TestSftpBackendGetReader(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.GetReader(t) +} + +func TestSftpBackendLoad(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Load(t) +} + +func TestSftpBackendWrite(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Write(t) +} + +func TestSftpBackendGeneric(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Generic(t) +} + +func TestSftpBackendDelete(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Delete(t) +} + +func TestSftpBackendCleanup(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Cleanup(t) +} diff --git a/backend/sftp/sftp.go b/backend/sftp/sftp.go index 007e2088a..a3d32e3d6 100644 --- a/backend/sftp/sftp.go +++ b/backend/sftp/sftp.go @@ -369,6 +369,10 @@ func (r *SFTP) GetReader(t backend.Type, name string, offset, length uint) (io.R // Load returns the data stored in the backend for h at the given offset // and saves it in p. Load has the same semantics as io.ReaderAt. func (r *SFTP) Load(h backend.Handle, p []byte, off int64) (n int, err error) { + if err := h.Valid(); err != nil { + return 0, err + } + f, err := r.c.Open(r.filename(h.Type, h.Name)) if err != nil { return 0, err diff --git a/backend/sftp/sftp_backend_test.go b/backend/sftp/sftp_backend_test.go new file mode 100644 index 000000000..431abff2a --- /dev/null +++ b/backend/sftp/sftp_backend_test.go @@ -0,0 +1,81 @@ +package sftp_test + +import ( + "fmt" + "io/ioutil" + "os" + "path/filepath" + "strings" + + "github.com/restic/restic/backend" + "github.com/restic/restic/backend/sftp" + "github.com/restic/restic/backend/test" + + . "github.com/restic/restic/test" +) + +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() { + sftpserver := "" + + for _, dir := range strings.Split(TestSFTPPath, ":") { + testpath := filepath.Join(dir, "sftp-server") + fd, err := os.Open(testpath) + fd.Close() + if !os.IsNotExist(err) { + sftpserver = testpath + break + } + } + + if sftpserver == "" { + SkipMessage = "sftp server binary not found, skipping tests" + return + } + + test.CreateFn = func() (backend.Backend, error) { + err := createTempdir() + if err != nil { + return nil, err + } + + return sftp.Create(tempBackendDir, sftpserver) + } + + test.OpenFn = func() (backend.Backend, error) { + err := createTempdir() + if err != nil { + return nil, err + } + return sftp.Open(tempBackendDir, sftpserver) + } + + test.CleanupFn = func() error { + if tempBackendDir == "" { + return nil + } + + fmt.Printf("removing test backend at %v\n", tempBackendDir) + err := os.RemoveAll(tempBackendDir) + tempBackendDir = "" + return err + } +} diff --git a/backend/sftp_test.go b/backend/sftp_test.go deleted file mode 100644 index b678e8ea9..000000000 --- a/backend/sftp_test.go +++ /dev/null @@ -1,65 +0,0 @@ -package backend_test - -import ( - "io/ioutil" - "os" - "path/filepath" - "strings" - "testing" - - "github.com/restic/restic/backend/sftp" - . "github.com/restic/restic/test" -) - -func setupSFTPBackend(t *testing.T) *sftp.SFTP { - sftpserver := "" - - for _, dir := range strings.Split(TestSFTPPath, ":") { - testpath := filepath.Join(dir, "sftp-server") - fd, err := os.Open(testpath) - fd.Close() - if !os.IsNotExist(err) { - sftpserver = testpath - break - } - } - - if sftpserver == "" { - return nil - } - - tempdir, err := ioutil.TempDir("", "restic-test-") - OK(t, err) - - b, err := sftp.Create(tempdir, sftpserver) - OK(t, err) - - t.Logf("created sftp backend locally at %s", tempdir) - - return b -} - -func teardownSFTPBackend(t *testing.T, b *sftp.SFTP) { - if !TestCleanup { - t.Logf("leaving backend at %s\n", b.Location()) - return - } - - err := os.RemoveAll(b.Location()) - OK(t, err) -} - -func TestSFTPBackend(t *testing.T) { - if !RunIntegrationTest { - t.Skip("integration tests disabled") - } - - s := setupSFTPBackend(t) - if s == nil { - t.Skip("unable to find sftp-server binary") - return - } - defer teardownSFTPBackend(t, s) - - testBackend(s, t) -} diff --git a/backend/test/backend_test.go b/backend/test/backend_test.go index db6de1b5f..f53fb1e47 100644 --- a/backend/test/backend_test.go +++ b/backend/test/backend_test.go @@ -7,14 +7,81 @@ import ( "github.com/restic/restic/backend/test" ) -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) } +var SkipMessage string + +func TestTestBackendCreate(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Create(t) +} + +func TestTestBackendOpen(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Open(t) +} + +func TestTestBackendCreateWithConfig(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.CreateWithConfig(t) +} + +func TestTestBackendLocation(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Location(t) +} + +func TestTestBackendConfig(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Config(t) +} + +func TestTestBackendGetReader(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.GetReader(t) +} + +func TestTestBackendLoad(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Load(t) +} + +func TestTestBackendWrite(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Write(t) +} + +func TestTestBackendGeneric(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Generic(t) +} + +func TestTestBackendDelete(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Delete(t) +} + +func TestTestBackendCleanup(t *testing.T) { + if SkipMessage != "" { + t.Skip(SkipMessage) + } + test.Cleanup(t) +} diff --git a/backend/test/generate_backend_tests.go b/backend/test/generate_backend_tests.go index a08250129..9a0aa2968 100644 --- a/backend/test/generate_backend_tests.go +++ b/backend/test/generate_backend_tests.go @@ -33,8 +33,15 @@ import ( "github.com/restic/restic/backend/test" ) +var SkipMessage string + {{ $prefix := .PackagePrefix }} -{{ range $f := .Funcs }}func Test{{ $prefix }}{{ $f }}(t *testing.T){ test.{{ $f }}(t) } +{{ range $f := .Funcs }} +func Test{{ $prefix }}{{ $f }}(t *testing.T){ + if SkipMessage != "" { t.Skip(SkipMessage) } + test.{{ $f }}(t) +} + {{ end }} ` @@ -42,6 +49,7 @@ var testFile = flag.String("testfile", "../test/tests.go", "file to search test var outputFile = flag.String("output", "backend_test.go", "output file to write generated code to") var packageName = flag.String("package", "", "the package name to use") var prefix = flag.String("prefix", "", "test function prefix") +var quiet = flag.Bool("quiet", false, "be quiet") func errx(err error) { if err == nil { @@ -126,5 +134,7 @@ func main() { errx(f.Close()) - fmt.Printf("wrote backend tests for package %v to %v\n", data.Package, *outputFile) + if !*quiet { + fmt.Printf("wrote backend tests for package %v to %v\n", data.Package, *outputFile) + } }