2018-07-03 09:35:01 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package ecclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-08-27 19:35:27 +01:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
2018-07-03 09:35:01 +01:00
|
|
|
"crypto/rand"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2018-10-16 16:53:25 +01:00
|
|
|
"io/ioutil"
|
2018-07-03 09:35:01 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/vivint/infectious"
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
"storj.io/storj/internal/teststorj"
|
2018-07-03 09:35:01 +01:00
|
|
|
"storj.io/storj/pkg/eestream"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-11-06 17:49:17 +00:00
|
|
|
"storj.io/storj/pkg/piecestore/psclient"
|
2018-08-27 19:35:27 +01:00
|
|
|
"storj.io/storj/pkg/provider"
|
2018-07-03 09:35:01 +01:00
|
|
|
"storj.io/storj/pkg/ranger"
|
2018-11-06 17:49:17 +00:00
|
|
|
"storj.io/storj/pkg/transport"
|
2018-07-03 09:35:01 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
dialFailed = "dial failed"
|
|
|
|
opFailed = "op failed"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrDialFailed = errors.New(dialFailed)
|
|
|
|
ErrOpFailed = errors.New(opFailed)
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-11-29 18:39:27 +00:00
|
|
|
node0 = teststorj.MockNode("node-0")
|
|
|
|
node1 = teststorj.MockNode("node-1")
|
|
|
|
node2 = teststorj.MockNode("node-2")
|
|
|
|
node3 = teststorj.MockNode("node-3")
|
2018-07-03 09:35:01 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewECClient(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mbm := 1234
|
|
|
|
|
2018-08-27 19:35:27 +01:00
|
|
|
privKey, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
identity := &provider.FullIdentity{Key: privKey}
|
2018-11-06 17:49:17 +00:00
|
|
|
ec := NewClient(identity, mbm)
|
2018-07-03 09:35:01 +01:00
|
|
|
assert.NotNil(t, ec)
|
|
|
|
|
|
|
|
ecc, ok := ec.(*ecClient)
|
|
|
|
assert.True(t, ok)
|
2018-11-06 17:49:17 +00:00
|
|
|
assert.NotNil(t, ecc.transport)
|
|
|
|
assert.Equal(t, mbm, ecc.memoryLimit)
|
2018-07-03 09:35:01 +01:00
|
|
|
|
2018-11-06 17:49:17 +00:00
|
|
|
assert.NotNil(t, ecc.transport.Identity())
|
|
|
|
assert.Equal(t, ecc.transport.Identity(), identity)
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPut(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
2018-08-06 15:24:30 +01:00
|
|
|
size := 32 * 1024
|
|
|
|
k := 2
|
|
|
|
n := 4
|
|
|
|
fc, err := infectious.NewFEC(k, n)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
es := eestream.NewRSScheme(fc, size/n)
|
|
|
|
|
2018-07-16 20:22:34 +01:00
|
|
|
TestLoop:
|
2018-07-03 09:35:01 +01:00
|
|
|
for i, tt := range []struct {
|
2018-09-18 05:39:06 +01:00
|
|
|
nodes []*pb.Node
|
2018-07-03 09:35:01 +01:00
|
|
|
min int
|
|
|
|
mbm int
|
2018-08-02 16:12:19 +01:00
|
|
|
badInput bool
|
2018-07-03 09:35:01 +01:00
|
|
|
errs []error
|
|
|
|
errString string
|
|
|
|
}{
|
2018-09-18 05:39:06 +01:00
|
|
|
{[]*pb.Node{}, 0, 0, true, []error{},
|
2018-09-11 05:52:14 +01:00
|
|
|
fmt.Sprintf("ecclient error: number of nodes (0) do not match total count (%v) of erasure scheme", n)},
|
2018-09-18 05:39:06 +01:00
|
|
|
{[]*pb.Node{node0, node1, node2, node3}, 0, -1, true,
|
2018-07-03 09:35:01 +01:00
|
|
|
[]error{nil, nil, nil, nil},
|
|
|
|
"eestream error: negative max buffer memory"},
|
2018-09-18 05:39:06 +01:00
|
|
|
{[]*pb.Node{node0, node1, node0, node3}, 0, 0, true,
|
2018-08-02 16:12:19 +01:00
|
|
|
[]error{nil, nil, nil, nil},
|
|
|
|
"ecclient error: duplicated nodes are not allowed"},
|
2018-09-18 05:39:06 +01:00
|
|
|
{[]*pb.Node{node0, node1, node2, node3}, 0, 0, false,
|
2018-07-03 09:35:01 +01:00
|
|
|
[]error{nil, nil, nil, nil}, ""},
|
2018-09-18 05:39:06 +01:00
|
|
|
{[]*pb.Node{node0, node1, node2, node3}, 0, 0, false,
|
2018-07-03 09:35:01 +01:00
|
|
|
[]error{nil, ErrDialFailed, nil, nil},
|
2018-09-26 15:23:33 +01:00
|
|
|
"ecclient error: successful puts (3) less than repair threshold (4)"},
|
2018-09-18 05:39:06 +01:00
|
|
|
{[]*pb.Node{node0, node1, node2, node3}, 0, 0, false,
|
2018-07-03 09:35:01 +01:00
|
|
|
[]error{nil, ErrOpFailed, nil, nil},
|
2018-09-26 15:23:33 +01:00
|
|
|
"ecclient error: successful puts (3) less than repair threshold (4)"},
|
2018-09-18 05:39:06 +01:00
|
|
|
{[]*pb.Node{node0, node1, node2, node3}, 2, 0, false,
|
2018-07-03 09:35:01 +01:00
|
|
|
[]error{nil, ErrDialFailed, nil, nil}, ""},
|
2018-09-18 05:39:06 +01:00
|
|
|
{[]*pb.Node{node0, node1, node2, node3}, 2, 0, false,
|
2018-07-03 09:35:01 +01:00
|
|
|
[]error{ErrOpFailed, ErrDialFailed, nil, ErrDialFailed},
|
2018-09-26 15:23:33 +01:00
|
|
|
"ecclient error: successful puts (1) less than repair threshold (2)"},
|
2018-10-16 16:53:25 +01:00
|
|
|
{[]*pb.Node{nil, nil, node2, node3}, 0, 0, false,
|
|
|
|
[]error{nil, nil, nil, nil}, ""},
|
2018-07-03 09:35:01 +01:00
|
|
|
} {
|
|
|
|
errTag := fmt.Sprintf("Test case #%d", i)
|
|
|
|
|
2018-11-06 17:49:17 +00:00
|
|
|
id := psclient.NewPieceID()
|
2018-07-03 09:35:01 +01:00
|
|
|
ttl := time.Now()
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
errs := make(map[*pb.Node]error, len(tt.nodes))
|
2018-07-03 09:35:01 +01:00
|
|
|
for i, n := range tt.nodes {
|
|
|
|
errs[n] = tt.errs[i]
|
|
|
|
}
|
|
|
|
|
2018-11-06 17:49:17 +00:00
|
|
|
clients := make(map[*pb.Node]psclient.Client, len(tt.nodes))
|
2018-07-03 09:35:01 +01:00
|
|
|
for _, n := range tt.nodes {
|
2018-10-16 16:53:25 +01:00
|
|
|
if n == nil || tt.badInput {
|
|
|
|
continue
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
2018-11-29 18:39:27 +00:00
|
|
|
derivedID, err := id.Derive(n.Id.Bytes())
|
2018-10-16 16:53:25 +01:00
|
|
|
if !assert.NoError(t, err, errTag) {
|
|
|
|
continue TestLoop
|
|
|
|
}
|
|
|
|
ps := NewMockPSClient(ctrl)
|
|
|
|
gomock.InOrder(
|
2018-10-17 12:40:11 +01:00
|
|
|
ps.EXPECT().Put(gomock.Any(), derivedID, gomock.Any(), ttl, gomock.Any(), gomock.Any()).Return(errs[n]).
|
2018-11-06 17:49:17 +00:00
|
|
|
Do(func(ctx context.Context, id psclient.PieceID, data io.Reader, ttl time.Time, ba *pb.PayerBandwidthAllocation, authorization *pb.SignedMessage) {
|
2018-10-16 16:53:25 +01:00
|
|
|
// simulate that the mocked piece store client is reading the data
|
|
|
|
_, err := io.Copy(ioutil.Discard, data)
|
|
|
|
assert.NoError(t, err, errTag)
|
|
|
|
}),
|
|
|
|
ps.EXPECT().Close().Return(nil),
|
|
|
|
)
|
2018-11-06 17:49:17 +00:00
|
|
|
clients[n] = ps
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
|
|
|
rs, err := eestream.NewRedundancyStrategy(es, tt.min, 0)
|
|
|
|
if !assert.NoError(t, err, errTag) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
r := io.LimitReader(rand.Reader, int64(size))
|
2018-11-06 17:49:17 +00:00
|
|
|
ec := ecClient{newPSClientFunc: mockNewPSClient(clients), memoryLimit: tt.mbm}
|
2018-10-16 16:53:25 +01:00
|
|
|
|
2018-10-30 16:24:46 +00:00
|
|
|
successfulNodes, err := ec.Put(ctx, tt.nodes, rs, id, r, ttl, nil, nil)
|
2018-07-03 09:35:01 +01:00
|
|
|
|
|
|
|
if tt.errString != "" {
|
|
|
|
assert.EqualError(t, err, tt.errString, errTag)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err, errTag)
|
2018-09-27 11:45:19 +01:00
|
|
|
assert.Equal(t, len(tt.nodes), len(successfulNodes), errTag)
|
|
|
|
for i := range tt.nodes {
|
|
|
|
if tt.errs[i] != nil {
|
|
|
|
assert.Nil(t, successfulNodes[i], errTag)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, tt.nodes[i], successfulNodes[i], errTag)
|
|
|
|
}
|
|
|
|
}
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-06 17:49:17 +00:00
|
|
|
func mockNewPSClient(clients map[*pb.Node]psclient.Client) psClientFunc {
|
|
|
|
return func(_ context.Context, _ transport.Client, n *pb.Node, _ int) (psclient.Client, error) {
|
|
|
|
c, ok := clients[n]
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrDialFailed
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 09:35:01 +01:00
|
|
|
func TestGet(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
2018-08-06 15:24:30 +01:00
|
|
|
size := 32 * 1024
|
|
|
|
k := 2
|
|
|
|
n := 4
|
|
|
|
fc, err := infectious.NewFEC(k, n)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
es := eestream.NewRSScheme(fc, size/n)
|
|
|
|
|
2018-07-16 20:22:34 +01:00
|
|
|
TestLoop:
|
2018-07-03 09:35:01 +01:00
|
|
|
for i, tt := range []struct {
|
2018-09-18 05:39:06 +01:00
|
|
|
nodes []*pb.Node
|
2018-07-03 09:35:01 +01:00
|
|
|
mbm int
|
|
|
|
errs []error
|
|
|
|
errString string
|
|
|
|
}{
|
2018-09-18 05:39:06 +01:00
|
|
|
{[]*pb.Node{}, 0, []error{}, "ecclient error: " +
|
2018-11-02 15:22:01 +00:00
|
|
|
fmt.Sprintf("number of nodes (0) do not match minimum required count (%v) of erasure scheme", k)},
|
2018-09-18 05:39:06 +01:00
|
|
|
{[]*pb.Node{node0, node1, node2, node3}, -1,
|
2018-07-03 09:35:01 +01:00
|
|
|
[]error{nil, nil, nil, nil},
|
|
|
|
"eestream error: negative max buffer memory"},
|
2018-09-18 05:39:06 +01:00
|
|
|
{[]*pb.Node{node0, node1, node2, node3}, 0,
|
2018-07-03 09:35:01 +01:00
|
|
|
[]error{nil, nil, nil, nil}, ""},
|
2018-09-18 05:39:06 +01:00
|
|
|
{[]*pb.Node{node0, node1, node2, node3}, 0,
|
2018-07-03 09:35:01 +01:00
|
|
|
[]error{nil, ErrDialFailed, nil, nil}, ""},
|
2018-09-18 05:39:06 +01:00
|
|
|
{[]*pb.Node{node0, node1, node2, node3}, 0,
|
2018-07-03 09:35:01 +01:00
|
|
|
[]error{nil, ErrOpFailed, nil, nil}, ""},
|
2018-09-18 05:39:06 +01:00
|
|
|
{[]*pb.Node{node0, node1, node2, node3}, 0,
|
2018-09-08 14:52:19 +01:00
|
|
|
[]error{ErrOpFailed, ErrDialFailed, nil, ErrDialFailed}, ""},
|
2018-09-18 05:39:06 +01:00
|
|
|
{[]*pb.Node{node0, node1, node2, node3}, 0,
|
2018-09-08 14:52:19 +01:00
|
|
|
[]error{ErrDialFailed, ErrOpFailed, ErrOpFailed, ErrDialFailed}, ""},
|
2018-09-27 11:45:19 +01:00
|
|
|
{[]*pb.Node{nil, nil, node2, node3}, 0,
|
|
|
|
[]error{nil, nil, nil, nil}, ""},
|
2018-07-03 09:35:01 +01:00
|
|
|
} {
|
|
|
|
errTag := fmt.Sprintf("Test case #%d", i)
|
|
|
|
|
2018-11-06 17:49:17 +00:00
|
|
|
id := psclient.NewPieceID()
|
2018-07-03 09:35:01 +01:00
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
errs := make(map[*pb.Node]error, len(tt.nodes))
|
2018-07-03 09:35:01 +01:00
|
|
|
for i, n := range tt.nodes {
|
|
|
|
errs[n] = tt.errs[i]
|
|
|
|
}
|
|
|
|
|
2018-11-06 17:49:17 +00:00
|
|
|
clients := make(map[*pb.Node]psclient.Client, len(tt.nodes))
|
2018-07-03 09:35:01 +01:00
|
|
|
for _, n := range tt.nodes {
|
2018-09-08 14:52:19 +01:00
|
|
|
if errs[n] == ErrOpFailed {
|
2018-11-29 18:39:27 +00:00
|
|
|
derivedID, err := id.Derive(n.Id.Bytes())
|
2018-07-16 20:22:34 +01:00
|
|
|
if !assert.NoError(t, err, errTag) {
|
|
|
|
continue TestLoop
|
|
|
|
}
|
2018-07-03 09:35:01 +01:00
|
|
|
ps := NewMockPSClient(ctrl)
|
2018-10-17 12:40:11 +01:00
|
|
|
ps.EXPECT().Get(gomock.Any(), derivedID, int64(size/k), gomock.Any(), gomock.Any()).Return(ranger.ByteRanger(nil), errs[n])
|
2018-11-06 17:49:17 +00:00
|
|
|
clients[n] = ps
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
|
|
|
}
|
2018-11-06 17:49:17 +00:00
|
|
|
ec := ecClient{newPSClientFunc: mockNewPSClient(clients), memoryLimit: tt.mbm}
|
2018-10-30 16:24:46 +00:00
|
|
|
rr, err := ec.Get(ctx, tt.nodes, es, id, int64(size), nil, nil)
|
2018-09-08 14:52:19 +01:00
|
|
|
if err == nil {
|
|
|
|
_, err := rr.Range(ctx, 0, 0)
|
|
|
|
assert.NoError(t, err, errTag)
|
|
|
|
}
|
2018-07-03 09:35:01 +01:00
|
|
|
if tt.errString != "" {
|
|
|
|
assert.EqualError(t, err, tt.errString, errTag)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err, errTag)
|
|
|
|
assert.NotNil(t, rr, errTag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDelete(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
2018-07-16 20:22:34 +01:00
|
|
|
TestLoop:
|
2018-07-03 09:35:01 +01:00
|
|
|
for i, tt := range []struct {
|
2018-09-18 05:39:06 +01:00
|
|
|
nodes []*pb.Node
|
2018-07-03 09:35:01 +01:00
|
|
|
errs []error
|
|
|
|
errString string
|
|
|
|
}{
|
2018-09-18 05:39:06 +01:00
|
|
|
{[]*pb.Node{}, []error{}, ""},
|
|
|
|
{[]*pb.Node{node0}, []error{nil}, ""},
|
|
|
|
{[]*pb.Node{node0}, []error{ErrDialFailed}, dialFailed},
|
|
|
|
{[]*pb.Node{node0}, []error{ErrOpFailed}, opFailed},
|
|
|
|
{[]*pb.Node{node0, node1}, []error{nil, nil}, ""},
|
|
|
|
{[]*pb.Node{node0, node1}, []error{ErrDialFailed, nil}, ""},
|
|
|
|
{[]*pb.Node{node0, node1}, []error{nil, ErrOpFailed}, ""},
|
|
|
|
{[]*pb.Node{node0, node1}, []error{ErrDialFailed, ErrDialFailed}, dialFailed},
|
|
|
|
{[]*pb.Node{node0, node1}, []error{ErrOpFailed, ErrOpFailed}, opFailed},
|
2018-09-27 11:45:19 +01:00
|
|
|
{[]*pb.Node{nil, node1}, []error{nil, nil}, ""},
|
|
|
|
{[]*pb.Node{nil, nil}, []error{nil, nil}, ""},
|
2018-07-03 09:35:01 +01:00
|
|
|
} {
|
|
|
|
errTag := fmt.Sprintf("Test case #%d", i)
|
|
|
|
|
2018-11-06 17:49:17 +00:00
|
|
|
id := psclient.NewPieceID()
|
2018-07-03 09:35:01 +01:00
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
errs := make(map[*pb.Node]error, len(tt.nodes))
|
2018-07-03 09:35:01 +01:00
|
|
|
for i, n := range tt.nodes {
|
|
|
|
errs[n] = tt.errs[i]
|
|
|
|
}
|
|
|
|
|
2018-11-06 17:49:17 +00:00
|
|
|
clients := make(map[*pb.Node]psclient.Client, len(tt.nodes))
|
2018-07-03 09:35:01 +01:00
|
|
|
for _, n := range tt.nodes {
|
2018-09-27 11:45:19 +01:00
|
|
|
if n != nil && errs[n] != ErrDialFailed {
|
2018-11-29 18:39:27 +00:00
|
|
|
derivedID, err := id.Derive(n.Id.Bytes())
|
2018-07-16 20:22:34 +01:00
|
|
|
if !assert.NoError(t, err, errTag) {
|
|
|
|
continue TestLoop
|
|
|
|
}
|
2018-07-03 09:35:01 +01:00
|
|
|
ps := NewMockPSClient(ctrl)
|
|
|
|
gomock.InOrder(
|
2018-10-17 12:40:11 +01:00
|
|
|
ps.EXPECT().Delete(gomock.Any(), derivedID, gomock.Any()).Return(errs[n]),
|
2018-08-20 16:11:54 +01:00
|
|
|
ps.EXPECT().Close().Return(nil),
|
2018-07-03 09:35:01 +01:00
|
|
|
)
|
2018-11-06 17:49:17 +00:00
|
|
|
clients[n] = ps
|
2018-07-03 09:35:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-06 17:49:17 +00:00
|
|
|
ec := ecClient{newPSClientFunc: mockNewPSClient(clients)}
|
2018-10-17 12:40:11 +01:00
|
|
|
err := ec.Delete(ctx, tt.nodes, id, nil)
|
2018-07-03 09:35:01 +01:00
|
|
|
|
|
|
|
if tt.errString != "" {
|
|
|
|
assert.EqualError(t, err, tt.errString, errTag)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err, errTag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-02 16:12:19 +01:00
|
|
|
|
|
|
|
func TestUnique(t *testing.T) {
|
|
|
|
for i, tt := range []struct {
|
2018-09-18 05:39:06 +01:00
|
|
|
nodes []*pb.Node
|
2018-08-02 16:12:19 +01:00
|
|
|
unique bool
|
|
|
|
}{
|
|
|
|
{nil, true},
|
2018-09-18 05:39:06 +01:00
|
|
|
{[]*pb.Node{}, true},
|
|
|
|
{[]*pb.Node{node0}, true},
|
|
|
|
{[]*pb.Node{node0, node1}, true},
|
|
|
|
{[]*pb.Node{node0, node0}, false},
|
|
|
|
{[]*pb.Node{node0, node1, node0}, false},
|
|
|
|
{[]*pb.Node{node1, node0, node0}, false},
|
|
|
|
{[]*pb.Node{node0, node0, node1}, false},
|
|
|
|
{[]*pb.Node{node2, node0, node1}, true},
|
|
|
|
{[]*pb.Node{node2, node0, node3, node1}, true},
|
|
|
|
{[]*pb.Node{node2, node0, node2, node1}, false},
|
|
|
|
{[]*pb.Node{node1, node0, node3, node1}, false},
|
2018-08-02 16:12:19 +01:00
|
|
|
} {
|
|
|
|
errTag := fmt.Sprintf("Test case #%d", i)
|
|
|
|
assert.Equal(t, tt.unique, unique(tt.nodes), errTag)
|
|
|
|
}
|
|
|
|
}
|