2018-06-19 15:00:15 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package overlay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-09-11 05:52:14 +01:00
|
|
|
"fmt"
|
2018-06-19 15:00:15 +01:00
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-09-11 05:52:14 +01:00
|
|
|
"github.com/zeebo/errs"
|
2018-06-19 15:00:15 +01:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2018-07-09 18:43:13 +01:00
|
|
|
"storj.io/storj/pkg/dht"
|
2018-09-11 05:52:14 +01:00
|
|
|
"storj.io/storj/pkg/kademlia"
|
2018-08-24 05:01:03 +01:00
|
|
|
"storj.io/storj/pkg/provider"
|
2018-06-19 15:00:15 +01:00
|
|
|
proto "storj.io/storj/protos/overlay"
|
|
|
|
)
|
|
|
|
|
2018-07-09 18:43:13 +01:00
|
|
|
type mockNodeID struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockNodeID) String() string {
|
|
|
|
return "foobar"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockNodeID) Bytes() []byte {
|
|
|
|
return []byte("foobar")
|
|
|
|
}
|
|
|
|
|
2018-06-19 15:00:15 +01:00
|
|
|
func TestNewOverlayClient(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
address string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
address: "127.0.0.1:8080",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range cases {
|
2018-08-24 05:01:03 +01:00
|
|
|
ca, err := provider.NewCA(ctx, 12, 4)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
identity, err := ca.NewIdentity()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
oc, err := NewOverlayClient(identity, v.address)
|
2018-06-19 15:00:15 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.NotNil(t, oc)
|
|
|
|
assert.NotEmpty(t, oc.client)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChoose(t *testing.T) {
|
|
|
|
cases := []struct {
|
2018-07-23 21:05:02 +01:00
|
|
|
limit int
|
2018-06-19 15:00:15 +01:00
|
|
|
space int64
|
|
|
|
expectedCalls int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
limit: 50,
|
|
|
|
space: 100,
|
|
|
|
expectedCalls: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range cases {
|
2018-09-07 10:42:04 +01:00
|
|
|
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
2018-06-19 15:00:15 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-08-27 18:28:16 +01:00
|
|
|
srv, mock, err := newTestServer(ctx)
|
2018-08-24 05:01:03 +01:00
|
|
|
assert.NoError(t, err)
|
2018-06-19 15:00:15 +01:00
|
|
|
go srv.Serve(lis)
|
|
|
|
defer srv.Stop()
|
|
|
|
|
2018-08-24 05:01:03 +01:00
|
|
|
ca, err := provider.NewCA(ctx, 12, 4)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
identity, err := ca.NewIdentity()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
oc, err := NewOverlayClient(identity, lis.Addr().String())
|
2018-06-19 15:00:15 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.NotNil(t, oc)
|
|
|
|
assert.NotEmpty(t, oc.client)
|
|
|
|
|
2018-08-24 05:01:03 +01:00
|
|
|
_, err = oc.Choose(ctx, v.limit, v.space)
|
2018-06-19 15:00:15 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, mock.FindStorageNodesCalled, v.expectedCalls)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLookup(t *testing.T) {
|
|
|
|
cases := []struct {
|
2018-07-09 18:43:13 +01:00
|
|
|
nodeID dht.NodeID
|
2018-06-19 15:00:15 +01:00
|
|
|
expectedCalls int
|
|
|
|
}{
|
|
|
|
{
|
2018-07-09 18:43:13 +01:00
|
|
|
nodeID: mockNodeID{},
|
2018-06-19 15:00:15 +01:00
|
|
|
expectedCalls: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range cases {
|
2018-09-07 10:42:04 +01:00
|
|
|
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
2018-06-19 15:00:15 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-08-27 18:28:16 +01:00
|
|
|
srv, mock, err := newTestServer(ctx)
|
2018-08-24 05:01:03 +01:00
|
|
|
assert.NoError(t, err)
|
2018-06-19 15:00:15 +01:00
|
|
|
go srv.Serve(lis)
|
|
|
|
defer srv.Stop()
|
|
|
|
|
2018-08-24 05:01:03 +01:00
|
|
|
ca, err := provider.NewCA(ctx, 12, 4)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
identity, err := ca.NewIdentity()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
oc, err := NewOverlayClient(identity, lis.Addr().String())
|
2018-06-19 15:00:15 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.NotNil(t, oc)
|
|
|
|
assert.NotEmpty(t, oc.client)
|
|
|
|
|
2018-08-24 05:01:03 +01:00
|
|
|
_, err = oc.Lookup(ctx, v.nodeID)
|
2018-06-19 15:00:15 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, mock.lookupCalled, v.expectedCalls)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-09-11 05:52:14 +01:00
|
|
|
func TestBulkLookup(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
nodeIDs []dht.NodeID
|
|
|
|
expectedCalls int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
nodeIDs: []dht.NodeID{mockNodeID{}, mockNodeID{}, mockNodeID{}},
|
|
|
|
expectedCalls: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, v := range cases {
|
|
|
|
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", 0))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
srv, mock, err := newTestServer(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
go srv.Serve(lis)
|
|
|
|
defer srv.Stop()
|
|
|
|
|
|
|
|
ca, err := provider.NewCA(ctx, 12, 4)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
identity, err := ca.NewIdentity()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
oc, err := NewOverlayClient(identity, lis.Addr().String())
|
|
|
|
assert.NoError(t, err)
|
2018-06-19 15:00:15 +01:00
|
|
|
|
2018-09-11 05:52:14 +01:00
|
|
|
assert.NotNil(t, oc)
|
|
|
|
assert.NotEmpty(t, oc.client)
|
|
|
|
|
|
|
|
_, err = oc.BulkLookup(ctx, v.nodeIDs)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, mock.bulkLookupCalled, v.expectedCalls)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func TestBulkLookupV2(t *testing.T) {
|
|
|
|
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", 0))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
srv, s, err := newServer(ctx)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
go srv.Serve(lis)
|
|
|
|
defer srv.Stop()
|
|
|
|
|
|
|
|
ca, err := provider.NewCA(ctx, 12, 4)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
identity, err := ca.NewIdentity()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
oc, err := NewOverlayClient(identity, lis.Addr().String())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.NotNil(t, oc)
|
|
|
|
assert.NotEmpty(t, oc.client)
|
|
|
|
n1 := &proto.Node{Id: "n1"}
|
|
|
|
n2 := &proto.Node{Id: "n2"}
|
|
|
|
n3 := &proto.Node{Id: "n3"}
|
|
|
|
nodes := []*proto.Node{n1, n2, n3}
|
|
|
|
for _, n := range nodes {
|
|
|
|
s.cache.Put(n.Id, *n)
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
testID string
|
|
|
|
nodeIDs []dht.NodeID
|
|
|
|
responses []*proto.Node
|
|
|
|
errors *errs.Class
|
|
|
|
}{
|
|
|
|
{testID: "empty id",
|
|
|
|
nodeIDs: []dht.NodeID{},
|
|
|
|
responses: nil,
|
|
|
|
errors: &ClientError,
|
|
|
|
},
|
|
|
|
{testID: "valid ids",
|
|
|
|
nodeIDs: func() []dht.NodeID {
|
|
|
|
id1 := kademlia.StringToNodeID("n1")
|
|
|
|
id2 := kademlia.StringToNodeID("n2")
|
|
|
|
id3 := kademlia.StringToNodeID("n3")
|
|
|
|
return []dht.NodeID{id1, id2, id3}
|
|
|
|
}(),
|
|
|
|
responses: nodes,
|
|
|
|
errors: nil,
|
|
|
|
},
|
|
|
|
{testID: "missing ids",
|
|
|
|
nodeIDs: func() []dht.NodeID {
|
|
|
|
id1 := kademlia.StringToNodeID("n4")
|
|
|
|
id2 := kademlia.StringToNodeID("n5")
|
|
|
|
return []dht.NodeID{id1, id2}
|
|
|
|
}(),
|
|
|
|
responses: []*proto.Node{nil, nil},
|
|
|
|
errors: nil,
|
|
|
|
},
|
|
|
|
{testID: "random order and nil",
|
|
|
|
nodeIDs: func() []dht.NodeID {
|
|
|
|
id1 := kademlia.StringToNodeID("n1")
|
|
|
|
id2 := kademlia.StringToNodeID("n2")
|
|
|
|
id3 := kademlia.StringToNodeID("n3")
|
|
|
|
id4 := kademlia.StringToNodeID("n4")
|
|
|
|
return []dht.NodeID{id2, id1, id3, id4}
|
|
|
|
}(),
|
|
|
|
responses: func() []*proto.Node {
|
|
|
|
return []*proto.Node{nodes[1], nodes[0], nodes[2], nil}
|
|
|
|
}(),
|
|
|
|
errors: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.testID, func(t *testing.T) {
|
|
|
|
ns, err := oc.BulkLookup(ctx, c.nodeIDs)
|
|
|
|
assertErrClass(t, c.errors, err)
|
|
|
|
assert.Equal(t, c.responses, ns)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newServer(ctx context.Context) (*grpc.Server, *Server, error) {
|
|
|
|
ca, err := provider.NewCA(ctx, 12, 4)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
identity, err := ca.NewIdentity()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
identOpt, err := identity.ServerOption()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
grpcServer := grpc.NewServer(identOpt)
|
|
|
|
cache, err := NewRedisOverlayCache("127.0.0.1:6379", "", 1, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
s := &Server{cache: cache}
|
|
|
|
|
|
|
|
proto.RegisterOverlayServer(grpcServer, s)
|
|
|
|
|
|
|
|
return grpcServer, s, nil
|
|
|
|
}
|
2018-08-27 18:28:16 +01:00
|
|
|
func newTestServer(ctx context.Context) (*grpc.Server, *mockOverlayServer, error) {
|
2018-08-24 05:01:03 +01:00
|
|
|
ca, err := provider.NewCA(ctx, 12, 4)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
identity, err := ca.NewIdentity()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
identOpt, err := identity.ServerOption()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
grpcServer := grpc.NewServer(identOpt)
|
2018-06-19 15:00:15 +01:00
|
|
|
mo := &mockOverlayServer{lookupCalled: 0, FindStorageNodesCalled: 0}
|
|
|
|
|
|
|
|
proto.RegisterOverlayServer(grpcServer, mo)
|
|
|
|
|
2018-08-24 05:01:03 +01:00
|
|
|
return grpcServer, mo, nil
|
2018-06-19 15:00:15 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockOverlayServer struct {
|
|
|
|
lookupCalled int
|
2018-09-11 05:52:14 +01:00
|
|
|
bulkLookupCalled int
|
2018-06-19 15:00:15 +01:00
|
|
|
FindStorageNodesCalled int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *mockOverlayServer) Lookup(ctx context.Context, req *proto.LookupRequest) (*proto.LookupResponse, error) {
|
|
|
|
o.lookupCalled++
|
|
|
|
return &proto.LookupResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *mockOverlayServer) FindStorageNodes(ctx context.Context, req *proto.FindStorageNodesRequest) (*proto.FindStorageNodesResponse, error) {
|
|
|
|
o.FindStorageNodesCalled++
|
|
|
|
return &proto.FindStorageNodesResponse{}, nil
|
|
|
|
}
|
2018-09-11 05:52:14 +01:00
|
|
|
|
|
|
|
func (o *mockOverlayServer) BulkLookup(ctx context.Context, reqs *proto.LookupRequests) (*proto.LookupResponses, error) {
|
|
|
|
o.bulkLookupCalled++
|
|
|
|
return &proto.LookupResponses{}, nil
|
|
|
|
}
|