From 480054bc3ad4c35aa7c440f7f8980f6be0a7042e Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 22 Nov 2015 16:38:58 +0100 Subject: [PATCH] MemoryBackend: handle config correctly, add tests for that --- backend/backend_test.go | 27 +++++++++++++++++++++++++++ backend/mem_backend.go | 17 ++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/backend/backend_test.go b/backend/backend_test.go index ef928cf8e..f38fbced5 100644 --- a/backend/backend_test.go +++ b/backend/backend_test.go @@ -12,7 +12,34 @@ import ( . "github.com/restic/restic/test" ) +func testBackendConfig(b backend.Backend, t *testing.T) { + // create config and read it back + _, err := b.Get(backend.Config, "") + Assert(t, err != nil, "did not get expected error for non-existing config") + + blob, err := b.Create() + OK(t, err) + + _, err = blob.Write([]byte("Config")) + OK(t, err) + OK(t, blob.Finalize(backend.Config, "")) + + // try accessing the config with different names, should all return the + // same config + for _, name := range []string{"", "foo", "bar", "0000000000000000000000000000000000000000000000000000000000000000"} { + rd, err := b.Get(backend.Config, name) + Assert(t, err == nil, "unable to read config") + + buf, err := ioutil.ReadAll(rd) + OK(t, err) + OK(t, rd.Close()) + Assert(t, string(buf) == "Config", "wrong data returned for config") + } +} + func testBackend(b backend.Backend, t *testing.T) { + testBackendConfig(b, t) + for _, tpe := range []backend.Type{ backend.Data, backend.Key, backend.Lock, backend.Snapshot, backend.Index, diff --git a/backend/mem_backend.go b/backend/mem_backend.go index e6b51cf8c..61f23e4ca 100644 --- a/backend/mem_backend.go +++ b/backend/mem_backend.go @@ -103,11 +103,18 @@ func (e *tempMemEntry) Size() uint { } func (e *tempMemEntry) Finalize(t Type, name string) error { + if t == Config { + name = "" + } + + debug.Log("MemoryBackend", "save blob %p as %v %v", e, t, name) return e.be.insert(t, name, e.data.Bytes()) } func memCreate(be *MemoryBackend) (Blob, error) { - return &tempMemEntry{be: be}, nil + blob := &tempMemEntry{be: be} + debug.Log("MemoryBackend.Create", "create new blob %p", blob) + return blob, nil } // readCloser wraps a reader and adds a noop Close method. @@ -123,6 +130,10 @@ func memGet(be *MemoryBackend, t Type, name string) (io.ReadCloser, error) { be.m.Lock() defer be.m.Unlock() + if t == Config { + name = "" + } + debug.Log("MemoryBackend.Get", "get %v %v", t, name) if _, ok := be.data[entry{t, name}]; !ok { @@ -136,6 +147,10 @@ func memGetReader(be *MemoryBackend, t Type, name string, offset, length uint) ( be.m.Lock() defer be.m.Unlock() + if t == Config { + name = "" + } + debug.Log("MemoryBackend.GetReader", "get %v %v", t, name) if _, ok := be.data[entry{t, name}]; !ok {