2015-03-14 17:15:47 +00:00
|
|
|
package restic_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/restic/restic"
|
|
|
|
"github.com/restic/restic/backend"
|
2015-04-09 19:15:48 +00:00
|
|
|
. "github.com/restic/restic/test"
|
2015-03-14 17:15:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCache(t *testing.T) {
|
2015-04-26 12:46:15 +00:00
|
|
|
server := SetupBackend(t)
|
|
|
|
defer TeardownBackend(t, server)
|
|
|
|
key := SetupKey(t, server, "geheim")
|
2015-03-28 14:07:08 +00:00
|
|
|
server.SetKey(key)
|
2015-03-14 17:15:47 +00:00
|
|
|
|
|
|
|
cache, err := restic.NewCache(server)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-03-14 17:15:47 +00:00
|
|
|
|
|
|
|
arch, err := restic.NewArchiver(server)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-03-14 17:15:47 +00:00
|
|
|
|
|
|
|
// archive some files, this should automatically cache all blobs from the snapshot
|
|
|
|
_, id, err := arch.Snapshot(nil, []string{*benchArchiveDirectory}, nil)
|
|
|
|
|
|
|
|
// try to load map from cache
|
|
|
|
rd, err := cache.Load(backend.Snapshot, "blobs", id)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-03-14 17:15:47 +00:00
|
|
|
|
|
|
|
dec := json.NewDecoder(rd)
|
|
|
|
|
|
|
|
m := &restic.Map{}
|
|
|
|
err = dec.Decode(m)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-03-14 17:15:47 +00:00
|
|
|
|
|
|
|
// remove cached blob list
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, cache.Purge(backend.Snapshot, "blobs", id))
|
2015-03-14 17:15:47 +00:00
|
|
|
|
2015-03-22 13:41:51 +00:00
|
|
|
// load map from cache again, this should fail
|
|
|
|
rd, err = cache.Load(backend.Snapshot, "blobs", id)
|
2015-04-09 19:15:48 +00:00
|
|
|
Assert(t, err != nil, "Expected failure did not occur")
|
2015-03-22 13:41:51 +00:00
|
|
|
|
2015-03-14 17:15:47 +00:00
|
|
|
// recreate cached blob list
|
2015-03-22 13:41:51 +00:00
|
|
|
err = cache.RefreshSnapshots(server, nil)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-03-22 13:41:51 +00:00
|
|
|
|
|
|
|
// load map from cache again
|
|
|
|
rd, err = cache.Load(backend.Snapshot, "blobs", id)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-03-22 13:41:51 +00:00
|
|
|
|
|
|
|
dec = json.NewDecoder(rd)
|
|
|
|
|
|
|
|
m2 := &restic.Map{}
|
|
|
|
err = dec.Decode(m2)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-03-14 17:15:47 +00:00
|
|
|
|
|
|
|
// compare maps
|
2015-04-09 19:15:48 +00:00
|
|
|
Assert(t, m.Equals(m2), "Maps are not equal")
|
2015-03-14 17:15:47 +00:00
|
|
|
}
|