From 92e26475050eb45e3ec143d660b0fbf38ee94692 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Wed, 11 Feb 2015 20:17:55 +0100 Subject: [PATCH] Add tests for sftp backend, split out id tests --- backend/id_test.go | 32 ++++++++++++++++++++++++++++++ backend/interface.go | 2 +- backend/local_test.go | 39 +++++++------------------------------ backend/sftp.go | 7 ++++++- backend/sftp_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 91 insertions(+), 34 deletions(-) create mode 100644 backend/id_test.go create mode 100644 backend/sftp_test.go diff --git a/backend/id_test.go b/backend/id_test.go new file mode 100644 index 000000000..ebccc927a --- /dev/null +++ b/backend/id_test.go @@ -0,0 +1,32 @@ +package backend_test + +import ( + "testing" + + "github.com/restic/restic/backend" +) + +func TestID(t *testing.T) { + for _, test := range TestStrings { + id, err := backend.ParseID(test.id) + ok(t, err) + + id2, err := backend.ParseID(test.id) + ok(t, err) + assert(t, id.Equal(id2), "ID.Equal() does not work as expected") + + ret, err := id.EqualString(test.id) + ok(t, err) + assert(t, ret, "ID.EqualString() returned wrong value") + + // test json marshalling + buf, err := id.MarshalJSON() + ok(t, err) + equals(t, "\""+test.id+"\"", string(buf)) + + var id3 backend.ID + err = id3.UnmarshalJSON(buf) + ok(t, err) + equals(t, id, id3) + } +} diff --git a/backend/interface.go b/backend/interface.go index 76f96dd42..8eeb7bfe2 100644 --- a/backend/interface.go +++ b/backend/interface.go @@ -34,7 +34,7 @@ type Getter interface { type Creater interface { Create(Type, []byte) (ID, error) - CreateFrom(Type, rd io.Reader) (ID, error) + CreateFrom(Type, io.Reader) (ID, error) } type Tester interface { diff --git a/backend/local_test.go b/backend/local_test.go index 2639d8ba3..7e085e62d 100644 --- a/backend/local_test.go +++ b/backend/local_test.go @@ -23,7 +23,7 @@ var TestStrings = []struct { {"4e54d2c721cbdb730f01b10b62dec622962b36966ec685880effa63d71c808f2", "foo/../../baz"}, } -func setupBackend(t *testing.T) *backend.Local { +func setupLocalBackend(t *testing.T) *backend.Local { tempdir, err := ioutil.TempDir("", "restic-test-") ok(t, err) @@ -35,7 +35,7 @@ func setupBackend(t *testing.T) *backend.Local { return b } -func teardownBackend(t *testing.T, b *backend.Local) { +func teardownLocalBackend(t *testing.T, b *backend.Local) { if !*testCleanup { t.Logf("leaving local backend at %s\n", b.Location()) return @@ -44,7 +44,7 @@ func teardownBackend(t *testing.T, b *backend.Local) { ok(t, b.Delete()) } -func testBackend(b *backend.Local, t *testing.T) { +func testBackend(b backend.Backend, t *testing.T) { for _, tpe := range []backend.Type{backend.Data, backend.Key, backend.Lock, backend.Snapshot, backend.Tree} { // detect non-existing files for _, test := range TestStrings { @@ -152,15 +152,15 @@ func TestBackend(t *testing.T) { assert(t, err != nil, "opening invalid repository at /invalid-restic-test should have failed, but err is nil") assert(t, b == nil, fmt.Sprintf("opening invalid repository at /invalid-restic-test should have failed, but b is not nil: %v", b)) - s := setupBackend(t) - defer teardownBackend(t, s) + s := setupLocalBackend(t) + defer teardownLocalBackend(t, s) testBackend(s, t) } func TestLocalBackendCreationFailures(t *testing.T) { - b := setupBackend(t) - defer teardownBackend(t, b) + b := setupLocalBackend(t) + defer teardownLocalBackend(t, b) // test failure to create a new repository at the same location b2, err := backend.CreateLocal(b.Location()) @@ -170,28 +170,3 @@ func TestLocalBackendCreationFailures(t *testing.T) { b2, err = backend.CreateLocal(b.Location()) assert(t, err != nil && b2 == nil, fmt.Sprintf("creating a repository at %s for the second time should have failed", b.Location())) } - -func TestID(t *testing.T) { - for _, test := range TestStrings { - id, err := backend.ParseID(test.id) - ok(t, err) - - id2, err := backend.ParseID(test.id) - ok(t, err) - assert(t, id.Equal(id2), "ID.Equal() does not work as expected") - - ret, err := id.EqualString(test.id) - ok(t, err) - assert(t, ret, "ID.EqualString() returned wrong value") - - // test json marshalling - buf, err := id.MarshalJSON() - ok(t, err) - equals(t, "\""+test.id+"\"", string(buf)) - - var id3 backend.ID - err = id3.UnmarshalJSON(buf) - ok(t, err) - equals(t, id, id3) - } -} diff --git a/backend/sftp.go b/backend/sftp.go index 6abb2a5fa..92e604808 100644 --- a/backend/sftp.go +++ b/backend/sftp.go @@ -394,7 +394,12 @@ func (r *SFTP) Get(t Type, id ID) ([]byte, error) { // try to open file file, err := r.c.Open(r.filename(t, id)) - defer file.Close() + defer func() { + // TODO: report bug against sftp client, ignore Close() for nil file + if file != nil { + file.Close() + } + }() if err != nil { return nil, err } diff --git a/backend/sftp_test.go b/backend/sftp_test.go new file mode 100644 index 000000000..031b18db3 --- /dev/null +++ b/backend/sftp_test.go @@ -0,0 +1,45 @@ +package backend_test + +import ( + "flag" + "io/ioutil" + "os" + "testing" + + "github.com/restic/restic/backend" +) + +var sftpPath = flag.String("test.sftppath", "", "sftp binary path (default: empty)") + +func setupSFTPBackend(t *testing.T) *backend.SFTP { + tempdir, err := ioutil.TempDir("", "restic-test-") + ok(t, err) + + b, err := backend.CreateSFTP(tempdir, *sftpPath) + ok(t, err) + + t.Logf("created sftp backend locally at %s", tempdir) + + return b +} + +func teardownSFTPBackend(t *testing.T, b *backend.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 *sftpPath == "" { + t.Skipf("sftppath not set, skipping TestSFTPBackend") + } + + s := setupSFTPBackend(t) + defer teardownSFTPBackend(t, s) + + testBackend(s, t) +}