2018-06-22 14:33:57 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package overlay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2018-10-29 14:16:36 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2018-06-22 14:33:57 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"google.golang.org/grpc"
|
2018-10-08 16:09:37 +01:00
|
|
|
"storj.io/storj/pkg/node"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-09-11 08:27:12 +01:00
|
|
|
"storj.io/storj/storage"
|
2018-06-22 14:33:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFindStorageNodes(t *testing.T) {
|
2018-09-07 10:42:04 +01:00
|
|
|
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
2018-06-22 14:33:57 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-10-12 22:10:50 +01:00
|
|
|
fid, err := node.NewFullIdentity(ctx, 12, 4)
|
2018-06-22 14:33:57 +01:00
|
|
|
assert.NoError(t, err)
|
2018-10-12 22:10:50 +01:00
|
|
|
fid2, err := node.NewFullIdentity(ctx, 12, 4)
|
2018-06-22 14:33:57 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-09-11 08:27:12 +01:00
|
|
|
srv := NewMockServer([]storage.ListItem{
|
|
|
|
{
|
2018-10-12 22:10:50 +01:00
|
|
|
Key: storage.Key(fid.ID.String()),
|
2018-10-29 14:16:36 +00:00
|
|
|
Value: newNodeStorageValue(t, "127.0.0.1:9090"),
|
2018-09-11 08:27:12 +01:00
|
|
|
}, {
|
2018-10-12 22:10:50 +01:00
|
|
|
Key: storage.Key(fid2.ID.String()),
|
2018-10-29 14:16:36 +00:00
|
|
|
Value: newNodeStorageValue(t, "127.0.0.1:9090"),
|
2018-09-11 08:27:12 +01:00
|
|
|
},
|
|
|
|
})
|
2018-06-22 14:33:57 +01:00
|
|
|
assert.NotNil(t, srv)
|
|
|
|
|
2018-09-11 14:13:25 +01:00
|
|
|
go func() { assert.NoError(t, srv.Serve(lis)) }()
|
2018-06-22 14:33:57 +01:00
|
|
|
defer srv.Stop()
|
|
|
|
|
|
|
|
address := lis.Addr().String()
|
2018-11-06 17:49:17 +00:00
|
|
|
c, err := NewTestClient(address)
|
2018-06-22 14:33:57 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
r, err := c.FindStorageNodes(context.Background(), &pb.FindStorageNodesRequest{Opts: &pb.OverlayOptions{Amount: 2}})
|
2018-06-22 14:33:57 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, r)
|
|
|
|
|
|
|
|
assert.Len(t, r.Nodes, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOverlayLookup(t *testing.T) {
|
2018-09-07 10:42:04 +01:00
|
|
|
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
2018-06-22 14:33:57 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-10-12 22:10:50 +01:00
|
|
|
fid, err := node.NewFullIdentity(ctx, 12, 4)
|
2018-06-22 14:33:57 +01:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-09-11 08:27:12 +01:00
|
|
|
srv := NewMockServer([]storage.ListItem{
|
|
|
|
{
|
2018-10-12 22:10:50 +01:00
|
|
|
Key: storage.Key(fid.ID.String()),
|
2018-10-29 14:16:36 +00:00
|
|
|
Value: newNodeStorageValue(t, "127.0.0.1:9090"),
|
2018-09-11 08:27:12 +01:00
|
|
|
},
|
|
|
|
})
|
2018-09-11 14:13:25 +01:00
|
|
|
go func() { assert.NoError(t, srv.Serve(lis)) }()
|
2018-06-22 14:33:57 +01:00
|
|
|
defer srv.Stop()
|
|
|
|
|
|
|
|
address := lis.Addr().String()
|
2018-11-06 17:49:17 +00:00
|
|
|
c, err := NewTestClient(address)
|
2018-06-22 14:33:57 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-10-12 22:10:50 +01:00
|
|
|
r, err := c.Lookup(context.Background(), &pb.LookupRequest{NodeID: fid.ID.String()})
|
2018-06-22 14:33:57 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, r)
|
|
|
|
}
|
2018-09-11 05:52:14 +01:00
|
|
|
|
|
|
|
func TestOverlayBulkLookup(t *testing.T) {
|
2018-09-11 08:27:12 +01:00
|
|
|
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
2018-09-11 05:52:14 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-10-12 22:10:50 +01:00
|
|
|
fid, err := node.NewFullIdentity(ctx, 12, 4)
|
2018-09-11 05:52:14 +01:00
|
|
|
assert.NoError(t, err)
|
2018-10-12 22:10:50 +01:00
|
|
|
fid2, err := node.NewFullIdentity(ctx, 12, 4)
|
2018-09-11 05:52:14 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-09-11 08:27:12 +01:00
|
|
|
srv := NewMockServer([]storage.ListItem{
|
|
|
|
{
|
2018-10-12 22:10:50 +01:00
|
|
|
Key: storage.Key(fid.ID.String()),
|
2018-10-29 14:16:36 +00:00
|
|
|
Value: newNodeStorageValue(t, "127.0.0.1:9090"),
|
2018-09-11 08:27:12 +01:00
|
|
|
},
|
|
|
|
})
|
2018-09-11 14:13:25 +01:00
|
|
|
go func() { assert.NoError(t, srv.Serve(lis)) }()
|
2018-09-11 05:52:14 +01:00
|
|
|
defer srv.Stop()
|
|
|
|
|
|
|
|
address := lis.Addr().String()
|
2018-11-06 17:49:17 +00:00
|
|
|
c, err := NewTestClient(address)
|
2018-09-11 05:52:14 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-10-12 22:10:50 +01:00
|
|
|
req1 := &pb.LookupRequest{NodeID: fid.ID.String()}
|
|
|
|
req2 := &pb.LookupRequest{NodeID: fid2.ID.String()}
|
2018-09-18 05:39:06 +01:00
|
|
|
rs := &pb.LookupRequests{Lookuprequest: []*pb.LookupRequest{req1, req2}}
|
2018-09-11 05:52:14 +01:00
|
|
|
r, err := c.BulkLookup(context.Background(), rs)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, r)
|
|
|
|
}
|
2018-10-29 14:16:36 +00:00
|
|
|
|
|
|
|
// newNodeStorageValue provides a convient way to create a node as a storage.Value for testing purposes
|
|
|
|
func newNodeStorageValue(t *testing.T, address string) storage.Value {
|
|
|
|
na := &pb.Node{Id: "", Address: &pb.NodeAddress{Transport: pb.NodeTransport_TCP_TLS_GRPC, Address: address}}
|
|
|
|
d, err := proto.Marshal(na)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
return d
|
|
|
|
}
|
2018-11-06 17:49:17 +00:00
|
|
|
|
|
|
|
func NewTestClient(address string) (pb.OverlayClient, error) {
|
|
|
|
conn, err := grpc.Dial(address, grpc.WithInsecure())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return pb.NewOverlayClient(conn), nil
|
|
|
|
}
|