Skip to content

Commit

Permalink
storagetesting: new test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
jkowalski committed Oct 27, 2018
1 parent 7237ded commit 0c67743
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 19 deletions.
42 changes: 23 additions & 19 deletions internal/storagetesting/asserts.go
Expand Up @@ -3,25 +3,25 @@ package storagetesting
import (
"bytes"
"context"
"fmt"
"path/filepath"
"reflect"
"runtime"
"sort"
"testing"

"github.com/kopia/repo/storage"
)

// AssertGetBlock asserts that the specified storage block has correct content.
func AssertGetBlock(ctx context.Context, t *testing.T, s storage.Storage, block string, expected []byte) {
t.Helper()

b, err := s.GetBlock(ctx, block, 0, -1)
if err != nil {
t.Errorf(errorPrefix()+"GetBlock(%v) returned error %v, expected data: %v", block, err, expected)
t.Errorf("GetBlock(%v) returned error %v, expected data: %v", block, err, expected)
return
}

if !bytes.Equal(b, expected) {
t.Errorf(errorPrefix()+"GetBlock(%v) returned %x, but expected %x", block, b, expected)
t.Errorf("GetBlock(%v) returned %x, but expected %x", block, b, expected)
}

half := int64(len(expected) / 2)
Expand All @@ -31,36 +31,39 @@ func AssertGetBlock(ctx context.Context, t *testing.T, s storage.Storage, block

b, err = s.GetBlock(ctx, block, 0, half)
if err != nil {
t.Errorf(errorPrefix()+"GetBlock(%v) returned error %v, expected data: %v", block, err, expected)
t.Errorf("GetBlock(%v) returned error %v, expected data: %v", block, err, expected)
return
}

if !bytes.Equal(b, expected[0:half]) {
t.Errorf(errorPrefix()+"GetBlock(%v) returned %x, but expected %x", block, b, expected[0:half])
t.Errorf("GetBlock(%v) returned %x, but expected %x", block, b, expected[0:half])
}

b, err = s.GetBlock(ctx, block, half, int64(len(expected))-half)
if err != nil {
t.Errorf(errorPrefix()+"GetBlock(%v) returned error %v, expected data: %v", block, err, expected)
t.Errorf("GetBlock(%v) returned error %v, expected data: %v", block, err, expected)
return
}

if !bytes.Equal(b, expected[len(expected)-int(half):]) {
t.Errorf(errorPrefix()+"GetBlock(%v) returned %x, but expected %x", block, b, expected[len(expected)-int(half):])
t.Errorf("GetBlock(%v) returned %x, but expected %x", block, b, expected[len(expected)-int(half):])
}

}

// AssertGetBlockNotFound asserts that GetBlock() for specified storage block returns ErrBlockNotFound.
func AssertGetBlockNotFound(ctx context.Context, t *testing.T, s storage.Storage, block string) {
t.Helper()

b, err := s.GetBlock(ctx, block, 0, -1)
if err != storage.ErrBlockNotFound || b != nil {
t.Errorf(errorPrefix()+"GetBlock(%v) returned %v, %v but expected ErrBlockNotFound", block, b, err)
t.Errorf("GetBlock(%v) returned %v, %v but expected ErrBlockNotFound", block, b, err)
}
}

// AssertListResults asserts that the list results with given prefix return the specified list of names in order.
func AssertListResults(ctx context.Context, t *testing.T, s storage.Storage, prefix string, expected ...string) {
func AssertListResults(ctx context.Context, t *testing.T, s storage.Storage, prefix string, want ...string) {
t.Helper()
var names []string

if err := s.ListBlocks(ctx, prefix, func(e storage.BlockMetadata) error {
Expand All @@ -70,15 +73,16 @@ func AssertListResults(ctx context.Context, t *testing.T, s storage.Storage, pre
t.Fatalf("err: %v", err)
}

if !reflect.DeepEqual(names, expected) {
t.Errorf(errorPrefix()+"ListBlocks(%v) returned %v, but expected %v", prefix, names, expected)
}
}
names = sorted(names)
want = sorted(want)

func errorPrefix() string {
if _, fn, line, ok := runtime.Caller(2); ok {
return fmt.Sprintf("called from %v:%v: ", filepath.Base(fn), line)
if !reflect.DeepEqual(names, want) {
t.Errorf("ListBlocks(%v) returned %v, but wanted %v", prefix, names, want)
}
}

return ""
func sorted(s []string) []string {
x := append([]string(nil), s...)
sort.Strings(x)
return x
}
19 changes: 19 additions & 0 deletions internal/storagetesting/verify.go
Expand Up @@ -19,6 +19,7 @@ func VerifyStorage(ctx context.Context, t *testing.T, r storage.Storage) {
{blk: string("zxce0e35630770c54668a8cfb4e414c6bf8f"), contents: []byte{1}},
{blk: string("abff4585856ebf0748fd989e1dd623a8963d"), contents: bytes.Repeat([]byte{1}, 1000)},
{blk: string("abgc3dca496d510f492c858a2df1eb824e62"), contents: bytes.Repeat([]byte{1}, 10000)},
{blk: string("kopia.repository"), contents: bytes.Repeat([]byte{2}, 100)},
}

// First verify that blocks don't exist.
Expand All @@ -35,7 +36,25 @@ func VerifyStorage(ctx context.Context, t *testing.T, r storage.Storage) {
AssertGetBlock(ctx, t, r, b.blk, b.contents)
}

AssertListResults(ctx, t, r, "", blocks[0].blk, blocks[1].blk, blocks[2].blk, blocks[3].blk, blocks[4].blk)
AssertListResults(ctx, t, r, "ab", blocks[0].blk, blocks[2].blk, blocks[3].blk)

// Overwrite blocks.
for _, b := range blocks {
if err := r.PutBlock(ctx, b.blk, b.contents); err != nil {
t.Errorf("can't put block: %v", err)
}

AssertGetBlock(ctx, t, r, b.blk, b.contents)
}
if err := r.DeleteBlock(ctx, blocks[0].blk); err != nil {
t.Errorf("unable to delete block: %v", err)
}
if err := r.DeleteBlock(ctx, blocks[0].blk); err != nil {
t.Errorf("invalid error when deleting deleted block: %v", err)
}
AssertListResults(ctx, t, r, "ab", blocks[2].blk, blocks[3].blk)
AssertListResults(ctx, t, r, "", blocks[1].blk, blocks[2].blk, blocks[3].blk, blocks[4].blk)
}

// AssertConnectionInfoRoundTrips verifies that the ConnectionInfo returned by a given storage can be used to create
Expand Down

0 comments on commit 0c67743

Please sign in to comment.