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

Add tests for sftp backend, split out id tests

This commit is contained in:
Alexander Neumann 2015-02-11 20:17:55 +01:00
parent 6c68150e45
commit 92e2647505
5 changed files with 91 additions and 34 deletions

32
backend/id_test.go Normal file
View File

@ -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)
}
}

View File

@ -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 {

View File

@ -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)
}
}

View File

@ -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
}

45
backend/sftp_test.go Normal file
View File

@ -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)
}