2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-29 13:22:43 +00:00
restic/backend/local_test.go

59 lines
1.7 KiB
Go
Raw Normal View History

2014-09-23 20:39:12 +00:00
package backend_test
import (
"flag"
"fmt"
"io/ioutil"
"testing"
2015-03-28 10:50:23 +00:00
"github.com/restic/restic/backend/local"
2014-09-23 20:39:12 +00:00
)
var testCleanup = flag.Bool("test.cleanup", true, "clean up after running tests (remove local backend directory with all content)")
2015-03-28 10:50:23 +00:00
func setupLocalBackend(t *testing.T) *local.Local {
2014-12-05 20:45:49 +00:00
tempdir, err := ioutil.TempDir("", "restic-test-")
2014-09-23 20:39:12 +00:00
ok(t, err)
2015-03-28 10:50:23 +00:00
b, err := local.Create(tempdir)
2014-09-23 20:39:12 +00:00
ok(t, err)
t.Logf("created local backend at %s", tempdir)
return b
}
2015-03-28 10:50:23 +00:00
func teardownLocalBackend(t *testing.T, b *local.Local) {
2014-09-23 20:39:12 +00:00
if !*testCleanup {
t.Logf("leaving local backend at %s\n", b.Location())
return
}
2014-12-21 14:57:41 +00:00
ok(t, b.Delete())
2014-09-23 20:39:12 +00:00
}
2015-03-28 10:50:23 +00:00
func TestLocalBackend(t *testing.T) {
2014-09-23 20:39:12 +00:00
// test for non-existing backend
2015-03-28 10:50:23 +00:00
b, err := local.Open("/invalid-restic-test")
2014-12-05 20:45:49 +00:00
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))
2014-09-23 20:39:12 +00:00
s := setupLocalBackend(t)
defer teardownLocalBackend(t, s)
2014-09-23 20:39:12 +00:00
2014-12-21 14:57:41 +00:00
testBackend(s, t)
2014-09-23 20:39:12 +00:00
}
func TestLocalBackendCreationFailures(t *testing.T) {
b := setupLocalBackend(t)
defer teardownLocalBackend(t, b)
2014-09-23 20:39:12 +00:00
// test failure to create a new repository at the same location
2015-03-28 10:50:23 +00:00
b2, err := local.Create(b.Location())
2014-09-23 20:39:12 +00:00
assert(t, err != nil && b2 == nil, fmt.Sprintf("creating a repository at %s for the second time should have failed", b.Location()))
// test failure to create a new repository at the same location without a config file
2015-03-28 10:50:23 +00:00
b2, err = local.Create(b.Location())
2014-09-23 20:39:12 +00:00
assert(t, err != nil && b2 == nil, fmt.Sprintf("creating a repository at %s for the second time should have failed", b.Location()))
}