storj/pkg/overlay/client_test.go

221 lines
4.6 KiB
Go
Raw Normal View History

2019-01-24 20:15:10 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package overlay_test
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2018-11-16 16:31:14 +00:00
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testplanet"
"storj.io/storj/pkg/overlay"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/storj"
)
func TestChoose(t *testing.T) {
ctx := testcontext.New(t)
defer ctx.Cleanup()
2019-01-29 19:42:43 +00:00
planet, err := testplanet.New(t, 1, 8, 1)
require.NoError(t, err)
defer ctx.Check(planet.Shutdown)
planet.Start(ctx)
// we wait a second for all the nodes to complete bootstrapping off the satellite
time.Sleep(2 * time.Second)
oc, err := planet.Uplinks[0].DialOverlay(planet.Satellites[0])
if err != nil {
t.Fatal(err)
}
cases := []struct {
2019-01-29 19:42:43 +00:00
limit int
space int64
bandwidth int64
}{
{
2019-01-29 19:42:43 +00:00
limit: 4,
space: 0,
bandwidth: 0,
},
}
for _, v := range cases {
newNodes, err := oc.Choose(ctx, overlay.Options{
2019-01-29 19:42:43 +00:00
Amount: v.limit,
Space: v.space,
})
assert.NoError(t, err)
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)
}
}
}
func TestLookup(t *testing.T) {
2018-11-16 16:31:14 +00:00
ctx := testcontext.New(t)
defer ctx.Cleanup()
planet, err := testplanet.New(t, 1, 4, 1)
require.NoError(t, err)
defer ctx.Check(planet.Shutdown)
planet.Start(ctx)
// we wait a second for all the nodes to complete bootstrapping off the satellite
time.Sleep(2 * time.Second)
oc, err := planet.Uplinks[0].DialOverlay(planet.Satellites[0])
if err != nil {
t.Fatal(err)
}
nid1 := planet.StorageNodes[0].ID()
cases := []struct {
nodeID storj.NodeID
expectErr bool
}{
{
nodeID: nid1,
expectErr: false,
},
{
2018-12-17 18:47:26 +00:00
nodeID: storj.NodeID{1},
expectErr: true,
},
}
for _, v := range cases {
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-11-16 16:31:14 +00:00
func TestBulkLookup(t *testing.T) {
2018-11-16 16:31:14 +00:00
ctx := testcontext.New(t)
defer ctx.Cleanup()
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)
}
nid1 := planet.StorageNodes[0].ID()
nid2 := planet.StorageNodes[1].ID()
nid3 := planet.StorageNodes[2].ID()
cases := []struct {
nodeIDs storj.NodeIDList
expectedCalls int
}{
{
nodeIDs: storj.NodeIDList{nid1, nid2, nid3},
expectedCalls: 1,
},
}
for _, v := range cases {
resNodes, err := oc.BulkLookup(ctx, v.nodeIDs)
assert.NoError(t, err)
for i, n := range resNodes {
assert.Equal(t, n.Id, v.nodeIDs[i])
}
assert.Equal(t, len(resNodes), len(v.nodeIDs))
}
}
2018-11-16 16:31:14 +00:00
func TestBulkLookupV2(t *testing.T) {
2018-11-16 16:31:14 +00:00
ctx := testcontext.New(t)
defer ctx.Cleanup()
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)
}
Satellite Peer (#1034) * add satellite peer * Add overlay * reorganize kademlia * add RunRefresh * add refresh to storagenode.Peer * add discovery * add agreements and metainfo * rename * add datarepair checker * add repair * add todo notes for audit * add testing interface * add into testplanet * fixes * fix compilation errors * fix compilation errors * make testplanet run * remove audit refrences * ensure that audit tests run * dev * checker tests compilable * fix discovery * fix compilation * fix * fix * dev * fix * disable auth * fixes * revert go.mod/sum * fix linter errors * fix * fix copyright * Add address param for SN dashboard (#1076) * Rename storj-sdk to storj-sim (#1078) * Storagenode logs and config improvements (#1075) * Add more info to SN logs * remove config-dir from user config * add output where config was stored * add message for successful connection * fix linter * remove storage.path from user config * resolve config path * move success message to info * log improvements * Remove captplanet (#1070) * pkg/server: include production cert (#1082) Change-Id: Ie8e6fe78550be83c3bd797db7a1e58d37c684792 * Generate Payments Report (#1079) * memory.Size: autoformat sizes based on value entropy (#1081) * Jj/bytes (#1085) * run tally and rollup * sets dev default tally and rollup intervals * nonessential storj-sim edits (#1086) * Closing context doesn't stop storage node (#1084) * Print when cancelled * Close properly * Don't log nil * Don't print error when closing dashboard * Fix panic in inspector if ping fails (#1088) * Consolidate identity management to identity cli commands (#1083) * Consolidate identity management: Move identity cretaion/signing out of storagenode setup command. * fixes * linters * Consolidate identity management: Move identity cretaion/signing out of storagenode setup command. * fixes * sava backups before saving signed certs * add "-prebuilt-test-cmds" test flag * linters * prepare cli tests for travis * linter fixes * more fixes * linter gods * sp/sdk/sim * remove ca.difficulty * remove unused difficulty * return setup to its rightful place * wip travis * Revert "wip travis" This reverts commit 56834849dcf066d3cc0a4f139033fc3f6d7188ca. * typo in travis.yaml * remove tests * remove more * make it only create one identity at a time for consistency * add config-dir for consitency * add identity creation to storj-sim * add flags * simplify * fix nolint and compile * prevent overwrite and pass difficulty, concurrency, and parent creds * goimports
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}}
nodes := []*pb.Node{n1, n2, n3}
for _, n := range nodes {
assert.NoError(t, cache.Put(ctx, n.Id, *n))
}
2018-11-16 16:31:14 +00:00
{ // empty id
_, err := oc.BulkLookup(ctx, storj.NodeIDList{})
2018-11-16 16:31:14 +00:00
assert.Error(t, err)
}
{ // valid ids
idList := storj.NodeIDList{nid1, nid2, nid3}
ns, err := oc.BulkLookup(ctx, idList)
2018-11-16 16:31:14 +00:00
assert.NoError(t, err)
for i, n := range ns {
assert.Equal(t, n.Id, idList[i])
}
}
2018-11-16 16:31:14 +00:00
{ // missing ids
idList := storj.NodeIDList{nid4, nid5}
ns, err := oc.BulkLookup(ctx, idList)
2018-11-16 16:31:14 +00:00
assert.NoError(t, err)
2018-11-16 16:31:14 +00:00
assert.Equal(t, []*pb.Node{nil, nil}, ns)
}
{ // different order and missing
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)
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)
}
}
}
}