900f67e3d0
* Implement psclient interface * Add string method to pieceID type * try to fix linter errors * Whoops missed an error * More linter errors * Typo * Lol double typo * Get everything working, begin adding tests for psclient rpc * goimports * Forgot to change the piecestore cli when changed the piecestore code * Fix CLI * remove ID length, added validator to pieceID * Move grpc ranger to client Change client PUT api to take a reader rather than return a writer * GRPCRanger -> PieceRanger; Make PieceRanger a RangeCloser * Forgot to remove offset * Added message upon successful store * Do that thing dennis and kaloyan wanted * goimports * Make closeConn a part of the interface for psclient * Use interface * Removed uneccessary new lines * goimport * Whoops * Actually we don't want to use the interface in Piece Ranger * Renamed piecestore in examples to piecestore-client; moved piecestore-cli to examples * Make comments look nicer
142 lines
4.1 KiB
Go
142 lines
4.1 KiB
Go
// Copyright (C) 2018 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package client
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"testing"
|
|
|
|
"github.com/golang/mock/gomock"
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
pb "storj.io/storj/protos/piecestore"
|
|
)
|
|
|
|
func TestPieceRanger(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
|
|
for i, tt := range []struct {
|
|
data string
|
|
size, offset, length int64
|
|
substr string
|
|
errString string
|
|
}{
|
|
{"", 0, 0, 0, "", ""},
|
|
{"abcdef", 6, 0, 0, "", ""},
|
|
{"abcdef", 6, 3, 0, "", ""},
|
|
{"abcdef", 6, 0, 6, "abcdef", ""},
|
|
{"abcdef", 6, 0, 5, "abcde", ""},
|
|
{"abcdef", 6, 0, 4, "abcd", ""},
|
|
{"abcdef", 6, 1, 4, "bcde", ""},
|
|
{"abcdef", 6, 2, 4, "cdef", ""},
|
|
{"abcdefg", 7, 1, 4, "bcde", ""},
|
|
{"abcdef", 6, 0, 7, "abcdef", "pieceRanger error: range beyond end"},
|
|
{"abcdef", 6, -1, 7, "abcde", "pieceRanger error: negative offset"},
|
|
{"abcdef", 6, 0, -1, "abcde", "pieceRanger error: negative length"},
|
|
} {
|
|
errTag := fmt.Sprintf("Test case #%d", i)
|
|
|
|
route := pb.NewMockPieceStoreRoutesClient(ctrl)
|
|
calls := []*gomock.Call{
|
|
route.EXPECT().Piece(
|
|
gomock.Any(), gomock.Any(), gomock.Any(),
|
|
).Return(&pb.PieceSummary{Size: int64(len(tt.data))}, nil),
|
|
}
|
|
if tt.offset >= 0 && tt.length > 0 && tt.offset+tt.length <= tt.size {
|
|
stream := pb.NewMockPieceStoreRoutes_RetrieveClient(ctrl)
|
|
calls = append(calls,
|
|
route.EXPECT().Retrieve(
|
|
gomock.Any(), gomock.Any(), gomock.Any(),
|
|
).Return(stream, nil),
|
|
stream.EXPECT().Recv().Return(
|
|
&pb.PieceRetrievalStream{
|
|
Size: tt.length,
|
|
Content: []byte(tt.data)[tt.offset : tt.offset+tt.length],
|
|
}, nil),
|
|
stream.EXPECT().Recv().Return(&pb.PieceRetrievalStream{}, io.EOF),
|
|
)
|
|
}
|
|
gomock.InOrder(calls...)
|
|
|
|
ctx := context.Background()
|
|
c := NewCustomRoute(route)
|
|
rr, err := PieceRanger(ctx, c, "")
|
|
if assert.NoError(t, err, errTag) {
|
|
assert.Equal(t, tt.size, rr.Size(), errTag)
|
|
}
|
|
r, err := rr.Range(ctx, tt.offset, tt.length)
|
|
if tt.errString != "" {
|
|
assert.EqualError(t, err, tt.errString, errTag)
|
|
continue
|
|
}
|
|
assert.NoError(t, err, errTag)
|
|
data, err := ioutil.ReadAll(r)
|
|
if assert.NoError(t, err, errTag) {
|
|
assert.Equal(t, []byte(tt.substr), data, errTag)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPieceRangerSize(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
|
|
for i, tt := range []struct {
|
|
data string
|
|
size, offset, length int64
|
|
substr string
|
|
errString string
|
|
}{
|
|
{"", 0, 0, 0, "", ""},
|
|
{"abcdef", 6, 0, 0, "", ""},
|
|
{"abcdef", 6, 3, 0, "", ""},
|
|
{"abcdef", 6, 0, 6, "abcdef", ""},
|
|
{"abcdef", 6, 0, 5, "abcde", ""},
|
|
{"abcdef", 6, 0, 4, "abcd", ""},
|
|
{"abcdef", 6, 1, 4, "bcde", ""},
|
|
{"abcdef", 6, 2, 4, "cdef", ""},
|
|
{"abcdefg", 7, 1, 4, "bcde", ""},
|
|
{"abcdef", 6, 0, 7, "abcdef", "pieceRanger error: range beyond end"},
|
|
{"abcdef", 6, -1, 7, "abcde", "pieceRanger error: negative offset"},
|
|
{"abcdef", 6, 0, -1, "abcde", "pieceRanger error: negative length"},
|
|
} {
|
|
errTag := fmt.Sprintf("Test case #%d", i)
|
|
|
|
route := pb.NewMockPieceStoreRoutesClient(ctrl)
|
|
if tt.offset >= 0 && tt.length > 0 && tt.offset+tt.length <= tt.size {
|
|
stream := pb.NewMockPieceStoreRoutes_RetrieveClient(ctrl)
|
|
gomock.InOrder(
|
|
route.EXPECT().Retrieve(
|
|
gomock.Any(), gomock.Any(), gomock.Any(),
|
|
).Return(stream, nil),
|
|
stream.EXPECT().Recv().Return(
|
|
&pb.PieceRetrievalStream{
|
|
Size: tt.size,
|
|
Content: []byte(tt.data)[tt.offset : tt.offset+tt.length],
|
|
}, nil),
|
|
stream.EXPECT().Recv().Return(&pb.PieceRetrievalStream{}, io.EOF),
|
|
)
|
|
}
|
|
|
|
ctx := context.Background()
|
|
c := NewCustomRoute(route)
|
|
rr := PieceRangerSize(c, "", tt.size)
|
|
assert.Equal(t, tt.size, rr.Size(), errTag)
|
|
r, err := rr.Range(ctx, tt.offset, tt.length)
|
|
if tt.errString != "" {
|
|
assert.EqualError(t, err, tt.errString, errTag)
|
|
continue
|
|
}
|
|
assert.NoError(t, err, errTag)
|
|
data, err := ioutil.ReadAll(r)
|
|
if assert.NoError(t, err, errTag) {
|
|
assert.Equal(t, []byte(tt.substr), data, errTag)
|
|
}
|
|
}
|
|
}
|