2018-01-23 21:13:46 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 The Perkeep Authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package overlay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"perkeep.org/pkg/blob"
|
|
|
|
"perkeep.org/pkg/blobserver"
|
|
|
|
"perkeep.org/pkg/blobserver/storagetest"
|
|
|
|
"perkeep.org/pkg/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ctxbg = context.Background()
|
|
|
|
|
2018-02-23 07:32:44 +00:00
|
|
|
func newOverlay(t *testing.T, withDel bool) blobserver.Storage {
|
|
|
|
overlay, _ := newOverlayWithLower(t, withDel)
|
|
|
|
return overlay
|
2018-01-23 21:13:46 +00:00
|
|
|
}
|
|
|
|
|
2018-02-23 07:32:44 +00:00
|
|
|
func newOverlayWithLower(t *testing.T, withDel bool) (sto, lower blobserver.Storage) {
|
2018-01-23 21:13:46 +00:00
|
|
|
ld := test.NewLoader()
|
2018-02-23 07:32:44 +00:00
|
|
|
lower, _ = ld.GetStorage("/good-lower/")
|
|
|
|
ld.GetStorage("/good-upper/")
|
|
|
|
conf := map[string]interface{}{
|
|
|
|
"lower": "/good-lower/",
|
|
|
|
"upper": "/good-upper/",
|
|
|
|
}
|
|
|
|
if withDel {
|
|
|
|
conf["deleted"] = map[string]interface{}{
|
2018-01-23 21:13:46 +00:00
|
|
|
"type": "memory",
|
2018-02-23 07:32:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sto, err := newFromConfig(ld, conf)
|
2018-01-23 21:13:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Invalid config: %v", err)
|
|
|
|
}
|
2018-02-23 07:32:44 +00:00
|
|
|
return sto, lower
|
2018-01-23 21:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStorageTest(t *testing.T) {
|
|
|
|
storagetest.Test(t, func(t *testing.T) (_ blobserver.Storage, cleanup func()) {
|
2018-02-23 07:32:44 +00:00
|
|
|
return newOverlay(t, true), func() {}
|
|
|
|
})
|
|
|
|
storagetest.Test(t, func(t *testing.T) (_ blobserver.Storage, cleanup func()) {
|
|
|
|
return newOverlay(t, false), func() {}
|
2018-01-23 21:13:46 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDelete(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2018-02-23 07:32:44 +00:00
|
|
|
sto, lower := newOverlayWithLower(t, true)
|
2018-01-23 21:13:46 +00:00
|
|
|
|
|
|
|
var (
|
2018-02-23 07:32:44 +00:00
|
|
|
// blobs that go into lower
|
|
|
|
S0 = &test.Blob{Contents: "lower blob 0"}
|
|
|
|
S1 = &test.Blob{Contents: "lower blob 1"}
|
2018-01-23 21:13:46 +00:00
|
|
|
|
2018-02-23 07:32:44 +00:00
|
|
|
// blobs that go into sto
|
2018-01-23 21:13:46 +00:00
|
|
|
A = &test.Blob{Contents: "some small blob"}
|
|
|
|
B = &test.Blob{Contents: strings.Repeat("some middle blob", 100)}
|
|
|
|
C = &test.Blob{Contents: strings.Repeat("A 8192 bytes length largish blob", 8192/32)}
|
|
|
|
)
|
|
|
|
|
2018-02-23 07:32:44 +00:00
|
|
|
// add S0 and S1 to lower
|
2018-01-23 21:13:46 +00:00
|
|
|
for _, tb := range []*test.Blob{S0, S1} {
|
2018-02-23 07:32:44 +00:00
|
|
|
sb, err := lower.ReceiveBlob(ctxbg, tb.BlobRef(), tb.Reader())
|
2018-01-23 21:13:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ReceiveBlob of %s: %v", sb, err)
|
|
|
|
}
|
|
|
|
if sb != tb.SizedRef() {
|
|
|
|
t.Fatalf("Received %v; want %v", sb, tb.SizedRef())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 07:32:44 +00:00
|
|
|
lowerRefs := []blob.SizedRef{
|
2018-01-23 21:13:46 +00:00
|
|
|
S0.SizedRef(),
|
|
|
|
S1.SizedRef(),
|
|
|
|
}
|
|
|
|
|
|
|
|
type step func() error
|
|
|
|
|
2018-02-23 07:32:44 +00:00
|
|
|
stepAdd := func(tb *test.Blob) step { // add the blob to sto
|
2018-01-23 21:13:46 +00:00
|
|
|
return func() error {
|
2018-02-23 07:32:44 +00:00
|
|
|
sb, err := sto.ReceiveBlob(ctxbg, tb.BlobRef(), tb.Reader())
|
2018-01-23 21:13:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("ReceiveBlob of %s: %v", sb, err)
|
|
|
|
}
|
|
|
|
if sb != tb.SizedRef() {
|
|
|
|
return fmt.Errorf("Received %v; want %v", sb, tb.SizedRef())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stepCheck := func(want ...*test.Blob) step { // check the blob
|
|
|
|
wantRefs := make([]blob.SizedRef, len(want))
|
|
|
|
for i, tb := range want {
|
|
|
|
wantRefs[i] = tb.SizedRef()
|
|
|
|
}
|
|
|
|
return func() error {
|
2018-02-23 07:32:44 +00:00
|
|
|
// ensure lower was not modified
|
|
|
|
if err := storagetest.CheckEnumerate(lower, lowerRefs); err != nil {
|
2018-01-23 21:13:46 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-02-23 07:32:44 +00:00
|
|
|
return storagetest.CheckEnumerate(sto, wantRefs)
|
2018-01-23 21:13:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 07:32:44 +00:00
|
|
|
stepDelete := func(tb *test.Blob) step { // delete the blob in sto
|
2018-01-23 21:13:46 +00:00
|
|
|
return func() error {
|
2018-02-23 07:32:44 +00:00
|
|
|
if err := sto.RemoveBlobs(ctx, []blob.Ref{tb.BlobRef()}); err != nil {
|
2018-01-23 21:13:46 +00:00
|
|
|
return fmt.Errorf("RemoveBlob(%s): %v", tb.BlobRef(), err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var deleteTests = [][]step{
|
|
|
|
{
|
|
|
|
stepAdd(A),
|
|
|
|
stepDelete(A),
|
|
|
|
stepCheck(S0, S1),
|
|
|
|
stepAdd(B),
|
|
|
|
stepCheck(S0, S1, B),
|
|
|
|
stepDelete(B),
|
|
|
|
stepCheck(S0, S1),
|
|
|
|
stepAdd(C),
|
|
|
|
stepCheck(S0, S1, C),
|
|
|
|
stepAdd(A),
|
|
|
|
stepCheck(S0, S1, A, C),
|
|
|
|
stepDelete(A),
|
|
|
|
stepDelete(C),
|
|
|
|
stepCheck(S0, S1),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
stepAdd(A),
|
|
|
|
stepAdd(B),
|
|
|
|
stepAdd(C),
|
|
|
|
stepCheck(S0, S1, A, B, C),
|
|
|
|
stepDelete(C),
|
|
|
|
stepCheck(S0, S1, A, B),
|
|
|
|
stepDelete(S0),
|
|
|
|
stepCheck(S1, A, B),
|
|
|
|
stepDelete(A),
|
|
|
|
stepDelete(B),
|
|
|
|
stepCheck(S1),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
stepAdd(S0),
|
|
|
|
stepCheck(S0, S1),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
stepDelete(S0),
|
|
|
|
stepDelete(S1),
|
|
|
|
stepCheck(),
|
|
|
|
stepAdd(A),
|
|
|
|
stepAdd(B),
|
|
|
|
stepCheck(A, B),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, steps := range deleteTests {
|
|
|
|
for j, s := range steps {
|
|
|
|
if err := s(); err != nil {
|
|
|
|
t.Errorf("error at test %d, step %d: %v", i+1, j+1, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|