2018-07-19 23:57:22 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-12-12 15:40:33 +00:00
|
|
|
package node_test
|
2018-07-19 15:48:08 +01:00
|
|
|
|
|
|
|
import (
|
2018-12-12 15:40:33 +00:00
|
|
|
"fmt"
|
2018-07-19 15:48:08 +01:00
|
|
|
"testing"
|
2018-12-12 15:40:33 +00:00
|
|
|
"time"
|
2018-07-19 15:48:08 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-12-12 15:40:33 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2018-07-19 15:48:08 +01:00
|
|
|
|
2018-10-29 14:16:36 +00:00
|
|
|
"storj.io/storj/internal/testcontext"
|
2018-12-12 15:40:33 +00:00
|
|
|
"storj.io/storj/internal/testplanet"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-12-12 15:40:33 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
"storj.io/storj/pkg/utils"
|
2018-07-19 15:48:08 +01:00
|
|
|
)
|
|
|
|
|
2018-12-12 15:40:33 +00:00
|
|
|
func TestClient(t *testing.T) {
|
2018-10-29 14:16:36 +00:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2018-12-12 15:40:33 +00:00
|
|
|
planet, err := testplanet.New(t, 1, 4, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2018-07-19 15:48:08 +01:00
|
|
|
}
|
2018-12-12 15:40:33 +00:00
|
|
|
defer ctx.Check(planet.Shutdown)
|
2018-07-19 15:48:08 +01:00
|
|
|
|
2018-12-12 15:40:33 +00:00
|
|
|
planet.Start(ctx)
|
2018-10-08 16:09:37 +01:00
|
|
|
|
2018-12-12 15:40:33 +00:00
|
|
|
time.Sleep(2 * time.Second)
|
2018-10-08 16:09:37 +01:00
|
|
|
|
2018-12-12 15:40:33 +00:00
|
|
|
peers := []*testplanet.Node{}
|
|
|
|
peers = append(peers, planet.Satellites...)
|
|
|
|
peers = append(peers, planet.StorageNodes...)
|
2018-07-19 15:48:08 +01:00
|
|
|
|
2018-12-12 15:40:33 +00:00
|
|
|
{ // Ping
|
|
|
|
client, err := planet.StorageNodes[0].NewNodeClient()
|
2018-08-24 05:01:03 +01:00
|
|
|
assert.NoError(t, err)
|
2018-12-12 15:40:33 +00:00
|
|
|
defer ctx.Check(client.Disconnect)
|
|
|
|
|
|
|
|
var group errgroup.Group
|
|
|
|
|
|
|
|
for i := range peers {
|
|
|
|
peer := peers[i]
|
|
|
|
group.Go(func() error {
|
|
|
|
pinged, err := client.Ping(ctx, peer.Info)
|
|
|
|
var pingErr error
|
|
|
|
if !pinged {
|
|
|
|
pingErr = fmt.Errorf("ping to %s should have succeeded", peer.ID())
|
|
|
|
}
|
|
|
|
return utils.CombineErrors(pingErr, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
defer ctx.Check(group.Wait)
|
|
|
|
}
|
2018-08-24 05:01:03 +01:00
|
|
|
|
2018-12-12 15:40:33 +00:00
|
|
|
{ // Lookup
|
|
|
|
client, err := planet.StorageNodes[1].NewNodeClient()
|
2018-07-19 15:48:08 +01:00
|
|
|
assert.NoError(t, err)
|
2018-12-12 15:40:33 +00:00
|
|
|
defer ctx.Check(client.Disconnect)
|
2018-07-19 15:48:08 +01:00
|
|
|
|
2018-12-12 15:40:33 +00:00
|
|
|
var group errgroup.Group
|
2018-07-19 15:48:08 +01:00
|
|
|
|
2018-12-12 15:40:33 +00:00
|
|
|
for i := range peers {
|
|
|
|
peer := peers[i]
|
|
|
|
group.Go(func() error {
|
|
|
|
for _, target := range peers {
|
|
|
|
errTag := fmt.Errorf("lookup peer:%s target:%s", peer.ID(), target.ID())
|
2018-10-29 14:16:36 +00:00
|
|
|
|
2018-12-12 15:40:33 +00:00
|
|
|
results, err := client.Lookup(ctx, peer.Info, target.Info)
|
|
|
|
if err != nil {
|
|
|
|
return utils.CombineErrors(errTag, err)
|
|
|
|
}
|
2018-10-26 17:38:22 +01:00
|
|
|
|
2018-12-12 15:40:33 +00:00
|
|
|
if containsResult(results, target.ID()) {
|
|
|
|
continue
|
|
|
|
}
|
2018-10-26 17:38:22 +01:00
|
|
|
|
2018-12-12 15:40:33 +00:00
|
|
|
// with small network we expect to return everything
|
|
|
|
if len(results) != planet.Size() {
|
|
|
|
return utils.CombineErrors(errTag, fmt.Errorf("expected %d got %d: %s", planet.Size(), len(results), pb.NodesToIDs(results)))
|
|
|
|
}
|
2018-10-26 17:38:22 +01:00
|
|
|
|
2018-12-12 15:40:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2018-10-26 17:38:22 +01:00
|
|
|
|
2018-12-12 15:40:33 +00:00
|
|
|
defer ctx.Check(group.Wait)
|
2018-08-24 05:01:03 +01:00
|
|
|
}
|
2018-10-26 17:38:22 +01:00
|
|
|
|
2018-12-12 15:40:33 +00:00
|
|
|
{ // Lookup
|
|
|
|
client, err := planet.StorageNodes[2].NewNodeClient()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer ctx.Check(client.Disconnect)
|
|
|
|
|
|
|
|
targets := []storj.NodeID{
|
|
|
|
{}, // empty target
|
|
|
|
{255}, // non-empty
|
|
|
|
}
|
|
|
|
|
|
|
|
var group errgroup.Group
|
|
|
|
|
|
|
|
for i := range targets {
|
|
|
|
target := targets[i]
|
|
|
|
for i := range peers {
|
|
|
|
peer := peers[i]
|
|
|
|
group.Go(func() error {
|
|
|
|
errTag := fmt.Errorf("invalid lookup peer:%s target:%s", peer.ID(), target)
|
|
|
|
|
|
|
|
results, err := client.Lookup(ctx, peer.Info, pb.Node{Id: target})
|
|
|
|
if err != nil {
|
|
|
|
return utils.CombineErrors(errTag, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// with small network we expect to return everything
|
|
|
|
if len(results) != planet.Size() {
|
|
|
|
return utils.CombineErrors(errTag, fmt.Errorf("expected %d got %d: %s", planet.Size(), len(results), pb.NodesToIDs(results)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
defer ctx.Check(group.Wait)
|
2018-08-24 05:01:03 +01:00
|
|
|
}
|
2018-07-19 15:48:08 +01:00
|
|
|
}
|
|
|
|
|
2018-12-12 15:40:33 +00:00
|
|
|
func containsResult(nodes []*pb.Node, target storj.NodeID) bool {
|
|
|
|
for _, node := range nodes {
|
|
|
|
if node.Id == target {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2018-10-26 17:38:22 +01:00
|
|
|
}
|