2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-06-19 15:00:15 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-12-04 20:18:26 +00:00
|
|
|
package overlay_test
|
2018-06-19 15:00:15 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2018-12-04 20:18:26 +00:00
|
|
|
"time"
|
2018-06-19 15:00:15 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-01-15 16:08:45 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-06-19 15:00:15 +01:00
|
|
|
|
2018-11-16 16:31:14 +00:00
|
|
|
"storj.io/storj/internal/testcontext"
|
2019-01-02 17:39:17 +00:00
|
|
|
"storj.io/storj/internal/testidentity"
|
2018-12-04 20:18:26 +00:00
|
|
|
"storj.io/storj/internal/testplanet"
|
|
|
|
"storj.io/storj/pkg/overlay"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-11-29 18:39:27 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-06-19 15:00:15 +01:00
|
|
|
)
|
|
|
|
|
2018-12-20 13:57:54 +00:00
|
|
|
func TestNewClient(t *testing.T) {
|
2018-11-16 16:31:14 +00:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2018-06-19 15:00:15 +01:00
|
|
|
cases := []struct {
|
|
|
|
address string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
address: "127.0.0.1:8080",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range cases {
|
2018-11-29 18:39:27 +00:00
|
|
|
ca, err := testidentity.NewTestCA(ctx)
|
2018-08-24 05:01:03 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
identity, err := ca.NewIdentity()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-12-20 13:57:54 +00:00
|
|
|
oc, err := overlay.NewClient(identity, v.address)
|
2018-06-19 15:00:15 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.NotNil(t, oc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChoose(t *testing.T) {
|
2019-01-15 16:08:45 +00:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2019-01-29 19:42:43 +00:00
|
|
|
planet, err := testplanet.New(t, 1, 8, 1)
|
2019-01-15 16:08:45 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
planet.Start(ctx)
|
|
|
|
// we wait a second for all the nodes to complete bootstrapping off the satellite
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
defer ctx.Check(planet.Shutdown)
|
|
|
|
|
|
|
|
oc, err := planet.Uplinks[0].DialOverlay(planet.Satellites[0])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-06-19 15:00:15 +01:00
|
|
|
cases := []struct {
|
2019-01-29 19:42:43 +00:00
|
|
|
limit int
|
|
|
|
space int64
|
|
|
|
bandwidth int64
|
2018-06-19 15:00:15 +01:00
|
|
|
}{
|
|
|
|
{
|
2019-01-29 19:42:43 +00:00
|
|
|
limit: 4,
|
|
|
|
space: 0,
|
|
|
|
bandwidth: 0,
|
2018-06-19 15:00:15 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range cases {
|
2018-12-04 20:18:26 +00:00
|
|
|
newNodes, err := oc.Choose(ctx, overlay.Options{
|
2019-01-29 19:42:43 +00:00
|
|
|
Amount: v.limit,
|
|
|
|
Space: v.space,
|
2018-12-04 20:18:26 +00:00
|
|
|
})
|
2018-06-19 15:00:15 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-12-04 20:18:26 +00:00
|
|
|
assert.Len(t, newNodes, v.limit)
|
|
|
|
for _, n := range newNodes {
|
|
|
|
assert.True(t, n.GetRestrictions().GetFreeDisk() >= v.space)
|
|
|
|
assert.True(t, n.GetRestrictions().GetFreeBandwidth() >= v.bandwidth)
|
2018-10-19 15:05:31 +01:00
|
|
|
}
|
2018-06-19 15:00:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLookup(t *testing.T) {
|
2018-11-16 16:31:14 +00:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2019-01-15 16:08:45 +00:00
|
|
|
planet, err := testplanet.New(t, 1, 4, 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
planet.Start(ctx)
|
|
|
|
// we wait a second for all the nodes to complete bootstrapping off the satellite
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
defer ctx.Check(planet.Shutdown)
|
|
|
|
|
|
|
|
oc, err := planet.Uplinks[0].DialOverlay(planet.Satellites[0])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-12-04 20:18:26 +00:00
|
|
|
|
|
|
|
nid1 := planet.StorageNodes[0].ID()
|
|
|
|
|
2018-06-19 15:00:15 +01:00
|
|
|
cases := []struct {
|
2018-12-04 20:18:26 +00:00
|
|
|
nodeID storj.NodeID
|
|
|
|
expectErr bool
|
2018-06-19 15:00:15 +01:00
|
|
|
}{
|
|
|
|
{
|
2018-12-04 20:18:26 +00:00
|
|
|
nodeID: nid1,
|
|
|
|
expectErr: false,
|
|
|
|
},
|
|
|
|
{
|
2018-12-17 18:47:26 +00:00
|
|
|
nodeID: storj.NodeID{1},
|
2018-12-04 20:18:26 +00:00
|
|
|
expectErr: true,
|
2018-06-19 15:00:15 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range cases {
|
2018-12-04 20:18:26 +00:00
|
|
|
n, err := oc.Lookup(ctx, v.nodeID)
|
|
|
|
if v.expectErr {
|
|
|
|
assert.Error(t, err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, n.Id.String(), v.nodeID.String())
|
|
|
|
}
|
2018-06-19 15:00:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-11-16 16:31:14 +00:00
|
|
|
|
2018-09-11 05:52:14 +01:00
|
|
|
func TestBulkLookup(t *testing.T) {
|
2018-11-16 16:31:14 +00:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2019-01-15 16:08:45 +00:00
|
|
|
planet, err := testplanet.New(t, 1, 4, 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
planet.Start(ctx)
|
|
|
|
// we wait a second for all the nodes to complete bootstrapping off the satellite
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
defer ctx.Check(planet.Shutdown)
|
|
|
|
|
|
|
|
oc, err := planet.Uplinks[0].DialOverlay(planet.Satellites[0])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-12-04 20:18:26 +00:00
|
|
|
|
|
|
|
nid1 := planet.StorageNodes[0].ID()
|
|
|
|
nid2 := planet.StorageNodes[1].ID()
|
|
|
|
nid3 := planet.StorageNodes[2].ID()
|
|
|
|
|
2018-09-11 05:52:14 +01:00
|
|
|
cases := []struct {
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeIDs storj.NodeIDList
|
2018-09-11 05:52:14 +01:00
|
|
|
expectedCalls int
|
|
|
|
}{
|
|
|
|
{
|
2018-12-04 20:18:26 +00:00
|
|
|
nodeIDs: storj.NodeIDList{nid1, nid2, nid3},
|
2018-09-11 05:52:14 +01:00
|
|
|
expectedCalls: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, v := range cases {
|
2018-12-04 20:18:26 +00:00
|
|
|
resNodes, err := oc.BulkLookup(ctx, v.nodeIDs)
|
2018-09-11 05:52:14 +01:00
|
|
|
assert.NoError(t, err)
|
2018-12-04 20:18:26 +00:00
|
|
|
for i, n := range resNodes {
|
|
|
|
assert.Equal(t, n.Id, v.nodeIDs[i])
|
|
|
|
}
|
|
|
|
assert.Equal(t, len(resNodes), len(v.nodeIDs))
|
2018-09-11 05:52:14 +01:00
|
|
|
}
|
|
|
|
}
|
2018-11-16 16:31:14 +00:00
|
|
|
|
2018-09-11 05:52:14 +01:00
|
|
|
func TestBulkLookupV2(t *testing.T) {
|
2018-11-16 16:31:14 +00:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
2018-09-11 08:27:12 +01:00
|
|
|
|
2019-01-15 16:08:45 +00:00
|
|
|
planet, err := testplanet.New(t, 1, 4, 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
planet.Start(ctx)
|
|
|
|
// we wait a second for all the nodes to complete bootstrapping off the satellite
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
defer ctx.Check(planet.Shutdown)
|
|
|
|
|
|
|
|
oc, err := planet.Uplinks[0].DialOverlay(planet.Satellites[0])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-09-11 05:52:14 +01:00
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
cache := planet.Satellites[0].Overlay.Service
|
2018-12-17 18:47:26 +00:00
|
|
|
|
|
|
|
nid1 := storj.NodeID{1}
|
|
|
|
nid2 := storj.NodeID{2}
|
|
|
|
nid3 := storj.NodeID{3}
|
|
|
|
nid4 := storj.NodeID{4}
|
|
|
|
nid5 := storj.NodeID{5}
|
|
|
|
|
|
|
|
n1 := &pb.Node{Id: storj.NodeID{1}}
|
|
|
|
n2 := &pb.Node{Id: storj.NodeID{2}}
|
|
|
|
n3 := &pb.Node{Id: storj.NodeID{3}}
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
nodes := []*pb.Node{n1, n2, n3}
|
2018-09-11 05:52:14 +01:00
|
|
|
for _, n := range nodes {
|
2018-12-04 20:18:26 +00:00
|
|
|
assert.NoError(t, cache.Put(ctx, n.Id, *n))
|
2018-09-11 05:52:14 +01:00
|
|
|
}
|
|
|
|
|
2018-11-16 16:31:14 +00:00
|
|
|
{ // empty id
|
2018-11-29 18:39:27 +00:00
|
|
|
_, err := oc.BulkLookup(ctx, storj.NodeIDList{})
|
2018-11-16 16:31:14 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // valid ids
|
2018-12-04 20:18:26 +00:00
|
|
|
idList := storj.NodeIDList{nid1, nid2, nid3}
|
|
|
|
ns, err := oc.BulkLookup(ctx, idList)
|
2018-11-16 16:31:14 +00:00
|
|
|
assert.NoError(t, err)
|
2018-12-04 20:18:26 +00:00
|
|
|
|
|
|
|
for i, n := range ns {
|
|
|
|
assert.Equal(t, n.Id, idList[i])
|
|
|
|
}
|
2018-09-11 05:52:14 +01:00
|
|
|
}
|
2018-11-16 16:31:14 +00:00
|
|
|
|
|
|
|
{ // missing ids
|
2018-12-04 20:18:26 +00:00
|
|
|
idList := storj.NodeIDList{nid4, nid5}
|
|
|
|
ns, err := oc.BulkLookup(ctx, idList)
|
2018-11-16 16:31:14 +00:00
|
|
|
assert.NoError(t, err)
|
2018-12-04 20:18:26 +00:00
|
|
|
|
2018-11-16 16:31:14 +00:00
|
|
|
assert.Equal(t, []*pb.Node{nil, nil}, ns)
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // different order and missing
|
2018-12-04 20:18:26 +00:00
|
|
|
idList := storj.NodeIDList{nid3, nid4, nid1, nid2, nid5}
|
|
|
|
ns, err := oc.BulkLookup(ctx, idList)
|
2018-11-16 16:31:14 +00:00
|
|
|
assert.NoError(t, err)
|
2018-12-04 20:18:26 +00:00
|
|
|
|
|
|
|
expectedNodes := []*pb.Node{n3, nil, n1, n2, nil}
|
|
|
|
for i, n := range ns {
|
|
|
|
if n == nil {
|
|
|
|
assert.Nil(t, expectedNodes[i])
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, n.Id, expectedNodes[i].Id)
|
|
|
|
}
|
|
|
|
}
|
2018-09-11 05:52:14 +01:00
|
|
|
}
|
|
|
|
}
|