restic/key_test.go

56 lines
1.3 KiB
Go
Raw Normal View History

2014-12-05 15:45:49 -05:00
package restic_test
2014-09-23 16:39:12 -04:00
import (
"flag"
"io/ioutil"
"os"
2015-03-14 07:10:08 -04:00
"path/filepath"
2014-09-23 16:39:12 -04:00
"testing"
2014-12-05 15:45:49 -05:00
"github.com/restic/restic"
2015-03-28 06:50:23 -04:00
"github.com/restic/restic/backend/local"
2015-04-09 15:15:48 -04:00
. "github.com/restic/restic/test"
2014-09-23 16:39:12 -04:00
)
var testPassword = "foobar"
2014-09-23 16:39:12 -04:00
var testCleanup = flag.Bool("test.cleanup", true, "clean up after running tests (remove local backend directory with all content)")
2015-02-21 10:53:14 -05:00
var testTempDir = flag.String("test.tempdir", "", "use this directory for temporary storage (default: system temp dir)")
2014-09-23 16:39:12 -04:00
2014-12-21 11:02:49 -05:00
func setupBackend(t testing.TB) restic.Server {
2015-02-21 10:53:14 -05:00
tempdir, err := ioutil.TempDir(*testTempDir, "restic-test-")
2015-04-09 15:15:48 -04:00
OK(t, err)
2014-09-23 16:39:12 -04:00
2015-03-14 07:10:08 -04:00
// create repository below temp dir
2015-03-28 06:50:23 -04:00
b, err := local.Create(filepath.Join(tempdir, "repo"))
2015-04-09 15:15:48 -04:00
OK(t, err)
2015-03-14 07:10:08 -04:00
// set cache dir
err = os.Setenv("RESTIC_CACHE", filepath.Join(tempdir, "cache"))
2015-04-09 15:15:48 -04:00
OK(t, err)
2014-09-23 16:39:12 -04:00
2014-12-21 11:02:49 -05:00
return restic.NewServer(b)
2014-09-23 16:39:12 -04:00
}
2014-12-21 11:02:49 -05:00
func teardownBackend(t testing.TB, s restic.Server) {
2014-09-23 16:39:12 -04:00
if !*testCleanup {
2015-03-28 06:50:23 -04:00
l := s.Backend().(*local.Local)
2014-12-21 11:02:49 -05:00
t.Logf("leaving local backend at %s\n", l.Location())
2014-09-23 16:39:12 -04:00
return
}
2015-04-09 15:15:48 -04:00
OK(t, s.Delete())
2014-09-23 16:39:12 -04:00
}
2014-12-21 11:02:49 -05:00
func setupKey(t testing.TB, s restic.Server, password string) *restic.Key {
k, err := restic.CreateKey(s, password)
2015-04-09 15:15:48 -04:00
OK(t, err)
2014-09-23 16:39:12 -04:00
return k
}
2014-09-23 16:39:12 -04:00
func TestRepo(t *testing.T) {
2014-12-21 11:02:49 -05:00
s := setupBackend(t)
defer teardownBackend(t, s)
_ = setupKey(t, s, testPassword)
2014-09-23 16:39:12 -04:00
}