2018-07-19 23:57:22 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-07-19 15:48:08 +01:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
"github.com/golang/mock/gomock"
|
2018-07-19 15:48:08 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2018-10-29 14:16:36 +00:00
|
|
|
"storj.io/storj/internal/testcontext"
|
2018-10-08 16:09:37 +01:00
|
|
|
"storj.io/storj/pkg/dht/mocks"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-08-24 05:01:03 +01:00
|
|
|
"storj.io/storj/pkg/provider"
|
2018-07-19 15:48:08 +01:00
|
|
|
)
|
|
|
|
|
2018-08-24 05:01:03 +01:00
|
|
|
var ctx = context.Background()
|
|
|
|
|
2018-07-19 15:48:08 +01:00
|
|
|
func TestLookup(t *testing.T) {
|
2018-10-29 14:16:36 +00:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2018-07-19 15:48:08 +01:00
|
|
|
cases := []struct {
|
2018-09-18 05:39:06 +01:00
|
|
|
self pb.Node
|
|
|
|
to pb.Node
|
|
|
|
find pb.Node
|
2018-08-27 18:28:16 +01:00
|
|
|
expectedErr error
|
2018-07-19 15:48:08 +01:00
|
|
|
}{
|
|
|
|
{
|
2018-10-08 16:09:37 +01:00
|
|
|
self: pb.Node{Id: "hello", Address: &pb.NodeAddress{Address: ":7070"}},
|
|
|
|
to: pb.Node{Id: "hello", Address: &pb.NodeAddress{Address: ":8080"}},
|
|
|
|
find: pb.Node{Id: "hello", Address: &pb.NodeAddress{Address: ":9090"}},
|
2018-07-19 15:48:08 +01:00
|
|
|
expectedErr: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range cases {
|
2018-09-07 10:42:04 +01:00
|
|
|
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
2018-07-19 15:48:08 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-10-26 17:38:22 +01:00
|
|
|
id := newTestIdentity(t)
|
2018-11-09 22:08:33 +00:00
|
|
|
v.to = pb.Node{Id: id.ID.String(), Address: &pb.NodeAddress{Address: lis.Addr().String()}}
|
|
|
|
|
2018-10-26 17:38:22 +01:00
|
|
|
srv, mock, err := newTestServer(ctx, &mockNodeServer{queryCalled: 0}, id)
|
2018-08-24 05:01:03 +01:00
|
|
|
assert.NoError(t, err)
|
2018-10-29 14:16:36 +00:00
|
|
|
|
|
|
|
ctx.Go(func() error { return srv.Serve(lis) })
|
2018-07-19 15:48:08 +01:00
|
|
|
defer srv.Stop()
|
2018-10-29 14:16:36 +00:00
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
|
|
|
|
mdht := mock_dht.NewMockDHT(ctrl)
|
|
|
|
mrt := mock_dht.NewMockRoutingTable(ctrl)
|
|
|
|
|
|
|
|
mdht.EXPECT().GetRoutingTable(gomock.Any()).Return(mrt, nil)
|
|
|
|
mrt.EXPECT().ConnectionSuccess(gomock.Any()).Return(nil)
|
2018-07-19 15:48:08 +01:00
|
|
|
|
2018-11-01 15:48:43 +00:00
|
|
|
ca, err := provider.NewTestCA(ctx)
|
2018-08-24 05:01:03 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
identity, err := ca.NewIdentity()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
nc, err := NewNodeClient(identity, v.self, mdht)
|
2018-07-19 15:48:08 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-08-24 05:01:03 +01:00
|
|
|
_, err = nc.Lookup(ctx, v.to, v.find)
|
2018-07-19 15:48:08 +01:00
|
|
|
assert.Equal(t, v.expectedErr, err)
|
2018-10-26 17:38:22 +01:00
|
|
|
assert.Equal(t, 1, mock.(*mockNodeServer).queryCalled)
|
2018-07-19 15:48:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-26 17:38:22 +01:00
|
|
|
func TestPing(t *testing.T) {
|
2018-10-29 14:16:36 +00:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2018-10-26 17:38:22 +01:00
|
|
|
cases := []struct {
|
|
|
|
self pb.Node
|
|
|
|
toID string
|
|
|
|
toIdentity *provider.FullIdentity
|
|
|
|
expectedErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
self: pb.Node{Id: "hello", Address: &pb.NodeAddress{Address: ":7070"}},
|
|
|
|
toID: "",
|
|
|
|
toIdentity: newTestIdentity(t),
|
|
|
|
expectedErr: nil,
|
|
|
|
},
|
2018-08-24 05:01:03 +01:00
|
|
|
}
|
2018-10-26 17:38:22 +01:00
|
|
|
|
|
|
|
for _, v := range cases {
|
|
|
|
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// new mock DHT for node client
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
mdht := mock_dht.NewMockDHT(ctrl)
|
|
|
|
// set up a node server
|
|
|
|
srv := NewServer(mdht)
|
|
|
|
|
|
|
|
msrv, _, err := newTestServer(ctx, srv, v.toIdentity)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// start gRPC server
|
2018-10-29 14:16:36 +00:00
|
|
|
ctx.Go(func() error { return msrv.Serve(lis) })
|
2018-10-26 17:38:22 +01:00
|
|
|
defer msrv.Stop()
|
|
|
|
|
|
|
|
nc, err := NewNodeClient(v.toIdentity, v.self, mdht)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
id := ID(v.toIdentity.ID)
|
|
|
|
ok, err := nc.Ping(ctx, pb.Node{Id: id.String(), Address: &pb.NodeAddress{Address: lis.Addr().String()}})
|
|
|
|
assert.Equal(t, v.expectedErr, err)
|
|
|
|
assert.Equal(t, ok, true)
|
2018-08-24 05:01:03 +01:00
|
|
|
}
|
2018-10-26 17:38:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTestServer(ctx context.Context, ns pb.NodesServer, identity *provider.FullIdentity) (*grpc.Server, pb.NodesServer, error) {
|
2018-08-24 05:01:03 +01:00
|
|
|
identOpt, err := identity.ServerOption()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
grpcServer := grpc.NewServer(identOpt)
|
2018-10-26 17:38:22 +01:00
|
|
|
pb.RegisterNodesServer(grpcServer, ns)
|
2018-07-19 15:48:08 +01:00
|
|
|
|
2018-10-26 17:38:22 +01:00
|
|
|
return grpcServer, ns, nil
|
2018-07-19 15:48:08 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockNodeServer struct {
|
|
|
|
queryCalled int
|
2018-10-30 18:04:29 +00:00
|
|
|
pingCalled int
|
2018-07-19 15:48:08 +01:00
|
|
|
}
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
func (mn *mockNodeServer) Query(ctx context.Context, req *pb.QueryRequest) (*pb.QueryResponse, error) {
|
2018-07-19 15:48:08 +01:00
|
|
|
mn.queryCalled++
|
2018-09-18 05:39:06 +01:00
|
|
|
return &pb.QueryResponse{}, nil
|
2018-07-19 15:48:08 +01:00
|
|
|
}
|
2018-09-11 08:27:12 +01:00
|
|
|
|
2018-10-30 18:04:29 +00:00
|
|
|
func (mn *mockNodeServer) Ping(ctx context.Context, req *pb.PingRequest) (*pb.PingResponse, error) {
|
|
|
|
mn.pingCalled++
|
|
|
|
return &pb.PingResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2018-10-26 17:38:22 +01:00
|
|
|
func newTestIdentity(t *testing.T) *provider.FullIdentity {
|
2018-11-01 15:48:43 +00:00
|
|
|
ca, err := provider.NewTestCA(ctx)
|
2018-10-26 17:38:22 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
identity, err := ca.NewIdentity()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
return identity
|
|
|
|
}
|