2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-19 20:39:25 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package overlay_test
|
|
|
|
|
|
|
|
import (
|
2019-05-30 18:35:04 +01:00
|
|
|
"runtime"
|
2018-11-19 20:39:25 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-05-01 14:45:52 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-01-31 18:49:00 +00:00
|
|
|
"github.com/zeebo/errs"
|
2018-11-19 20:39:25 +00:00
|
|
|
|
|
|
|
"storj.io/storj/internal/testcontext"
|
|
|
|
"storj.io/storj/internal/testplanet"
|
2019-01-29 19:42:43 +00:00
|
|
|
"storj.io/storj/pkg/overlay"
|
2019-01-31 18:49:00 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-11-19 20:39:25 +00:00
|
|
|
)
|
|
|
|
|
2019-05-01 14:45:52 +01:00
|
|
|
func TestOffline(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 4, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
service := satellite.Overlay.Service
|
|
|
|
// TODO: handle cleanup
|
|
|
|
|
|
|
|
result, err := service.KnownUnreliableOrOffline(ctx, []storj.NodeID{
|
|
|
|
planet.StorageNodes[0].ID(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, result)
|
|
|
|
|
|
|
|
result, err = service.KnownUnreliableOrOffline(ctx, []storj.NodeID{
|
|
|
|
planet.StorageNodes[0].ID(),
|
|
|
|
planet.StorageNodes[1].ID(),
|
|
|
|
planet.StorageNodes[2].ID(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, result)
|
|
|
|
|
|
|
|
result, err = service.KnownUnreliableOrOffline(ctx, []storj.NodeID{
|
|
|
|
planet.StorageNodes[0].ID(),
|
|
|
|
storj.NodeID{1, 2, 3, 4}, //note that this succeeds by design
|
|
|
|
planet.StorageNodes[2].ID(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2019-05-08 18:59:50 +01:00
|
|
|
require.Len(t, result, 1)
|
|
|
|
require.Equal(t, result[0], storj.NodeID{1, 2, 3, 4})
|
2019-05-01 14:45:52 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-31 18:49:00 +00:00
|
|
|
func TestNodeSelection(t *testing.T) {
|
2019-03-29 12:55:05 +00:00
|
|
|
t.Skip("flaky")
|
2019-02-06 13:32:42 +00:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 10, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
var err error
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
|
|
|
|
// This sets a reputable audit count for a certain number of nodes.
|
|
|
|
for i, node := range planet.StorageNodes {
|
|
|
|
for k := 0; k < i; k++ {
|
2019-03-29 08:53:43 +00:00
|
|
|
_, err := satellite.DB.OverlayCache().UpdateStats(ctx, &overlay.UpdateRequest{
|
|
|
|
NodeID: node.ID(),
|
|
|
|
IsUp: true,
|
|
|
|
AuditSuccess: true,
|
|
|
|
})
|
2019-02-06 13:32:42 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
2019-01-31 18:49:00 +00:00
|
|
|
|
2019-02-06 13:32:42 +00:00
|
|
|
// ensure all storagenodes are in overlay service
|
|
|
|
for _, storageNode := range planet.StorageNodes {
|
2019-04-22 10:07:50 +01:00
|
|
|
err = satellite.Overlay.Service.Put(ctx, storageNode.ID(), storageNode.Local().Node)
|
2019-01-29 19:42:43 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2019-02-06 13:32:42 +00:00
|
|
|
|
|
|
|
type test struct {
|
|
|
|
Preferences overlay.NodeSelectionConfig
|
|
|
|
ExcludeCount int
|
2019-03-23 08:06:11 +00:00
|
|
|
RequestCount int
|
2019-02-06 13:32:42 +00:00
|
|
|
ExpectedCount int
|
|
|
|
ShouldFailWith *errs.Class
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range []test{
|
|
|
|
{ // all reputable nodes, only reputable nodes requested
|
2019-06-13 22:51:18 +01:00
|
|
|
Preferences: testNodeSelectionConfig(0, 0, false),
|
2019-02-06 13:32:42 +00:00
|
|
|
RequestCount: 5,
|
|
|
|
ExpectedCount: 5,
|
2019-01-31 18:49:00 +00:00
|
|
|
},
|
2019-02-06 13:32:42 +00:00
|
|
|
{ // all reputable nodes, reputable and new nodes requested
|
2019-06-13 22:51:18 +01:00
|
|
|
Preferences: testNodeSelectionConfig(0, 1, false),
|
2019-02-06 13:32:42 +00:00
|
|
|
RequestCount: 5,
|
|
|
|
ExpectedCount: 5,
|
2019-01-31 18:49:00 +00:00
|
|
|
},
|
2019-02-06 13:32:42 +00:00
|
|
|
{ // all reputable nodes except one, reputable and new nodes requested
|
2019-06-13 22:51:18 +01:00
|
|
|
Preferences: testNodeSelectionConfig(1, 1, false),
|
2019-02-06 13:32:42 +00:00
|
|
|
RequestCount: 5,
|
|
|
|
ExpectedCount: 6,
|
2019-01-31 18:49:00 +00:00
|
|
|
},
|
2019-02-06 13:32:42 +00:00
|
|
|
{ // 50-50 reputable and new nodes, reputable and new nodes requested (new node ratio 1.0)
|
2019-06-13 22:51:18 +01:00
|
|
|
Preferences: testNodeSelectionConfig(5, 1, false),
|
2019-02-06 13:32:42 +00:00
|
|
|
RequestCount: 2,
|
|
|
|
ExpectedCount: 4,
|
2019-01-31 18:49:00 +00:00
|
|
|
},
|
2019-02-06 13:32:42 +00:00
|
|
|
{ // 50-50 reputable and new nodes, reputable and new nodes requested (new node ratio 0.5)
|
2019-06-13 22:51:18 +01:00
|
|
|
Preferences: testNodeSelectionConfig(5, 0.5, false),
|
2019-02-06 13:32:42 +00:00
|
|
|
RequestCount: 4,
|
|
|
|
ExpectedCount: 6,
|
2019-01-31 18:49:00 +00:00
|
|
|
},
|
2019-02-06 13:32:42 +00:00
|
|
|
{ // all new nodes except one, reputable and new nodes requested (happy path)
|
2019-06-13 22:51:18 +01:00
|
|
|
Preferences: testNodeSelectionConfig(8, 1, false),
|
2019-02-06 13:32:42 +00:00
|
|
|
RequestCount: 1,
|
|
|
|
ExpectedCount: 2,
|
2019-01-31 18:49:00 +00:00
|
|
|
},
|
2019-02-06 13:32:42 +00:00
|
|
|
{ // all new nodes except one, reputable and new nodes requested (not happy path)
|
2019-06-13 22:51:18 +01:00
|
|
|
Preferences: testNodeSelectionConfig(9, 1, false),
|
2019-02-06 13:32:42 +00:00
|
|
|
RequestCount: 2,
|
|
|
|
ExpectedCount: 3,
|
|
|
|
ShouldFailWith: &overlay.ErrNotEnoughNodes,
|
2019-01-31 18:49:00 +00:00
|
|
|
},
|
2019-02-06 13:32:42 +00:00
|
|
|
{ // all new nodes, reputable and new nodes requested
|
2019-06-13 22:51:18 +01:00
|
|
|
Preferences: testNodeSelectionConfig(50, 1, false),
|
2019-02-06 13:32:42 +00:00
|
|
|
RequestCount: 2,
|
|
|
|
ExpectedCount: 2,
|
|
|
|
ShouldFailWith: &overlay.ErrNotEnoughNodes,
|
2019-01-31 18:49:00 +00:00
|
|
|
},
|
2019-02-06 13:32:42 +00:00
|
|
|
{ // audit threshold edge case (1)
|
2019-06-13 22:51:18 +01:00
|
|
|
Preferences: testNodeSelectionConfig(9, 0, false),
|
2019-02-06 13:32:42 +00:00
|
|
|
RequestCount: 1,
|
|
|
|
ExpectedCount: 1,
|
2019-01-31 18:49:00 +00:00
|
|
|
},
|
2019-02-06 13:32:42 +00:00
|
|
|
{ // audit threshold edge case (2)
|
2019-06-13 22:51:18 +01:00
|
|
|
Preferences: testNodeSelectionConfig(0, 1, false),
|
2019-02-06 13:32:42 +00:00
|
|
|
RequestCount: 1,
|
|
|
|
ExpectedCount: 1,
|
2019-01-31 18:49:00 +00:00
|
|
|
},
|
2019-02-06 13:32:42 +00:00
|
|
|
{ // excluded node ids being excluded
|
2019-06-13 22:51:18 +01:00
|
|
|
Preferences: testNodeSelectionConfig(5, 0, false),
|
2019-02-06 13:32:42 +00:00
|
|
|
ExcludeCount: 7,
|
|
|
|
RequestCount: 5,
|
|
|
|
ExpectedCount: 3,
|
|
|
|
ShouldFailWith: &overlay.ErrNotEnoughNodes,
|
2019-01-31 18:49:00 +00:00
|
|
|
},
|
2019-02-06 13:32:42 +00:00
|
|
|
} {
|
|
|
|
t.Logf("#%2d. %+v", i, tt)
|
2019-03-23 08:06:11 +00:00
|
|
|
service := planet.Satellites[0].Overlay.Service
|
2019-02-06 13:32:42 +00:00
|
|
|
|
|
|
|
var excludedNodes []storj.NodeID
|
|
|
|
for _, storageNode := range planet.StorageNodes[:tt.ExcludeCount] {
|
|
|
|
excludedNodes = append(excludedNodes, storageNode.ID())
|
|
|
|
}
|
2019-01-29 19:42:43 +00:00
|
|
|
|
2019-03-23 08:06:11 +00:00
|
|
|
response, err := service.FindStorageNodesWithPreferences(ctx, overlay.FindStorageNodesRequest{
|
|
|
|
FreeBandwidth: 0,
|
|
|
|
FreeDisk: 0,
|
|
|
|
RequestedCount: tt.RequestCount,
|
|
|
|
ExcludedNodes: excludedNodes,
|
|
|
|
}, &tt.Preferences)
|
|
|
|
|
|
|
|
t.Log(len(response), err)
|
2019-02-06 13:32:42 +00:00
|
|
|
if tt.ShouldFailWith != nil {
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, tt.ShouldFailWith.Has(err))
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2019-01-29 19:42:43 +00:00
|
|
|
|
2019-03-23 08:06:11 +00:00
|
|
|
assert.Equal(t, tt.ExpectedCount, len(response))
|
2019-01-29 19:42:43 +00:00
|
|
|
}
|
2019-02-06 13:32:42 +00:00
|
|
|
})
|
2019-01-29 19:42:43 +00:00
|
|
|
}
|
2019-05-22 21:06:27 +01:00
|
|
|
|
|
|
|
func TestDistinctIPs(t *testing.T) {
|
2019-05-30 18:35:04 +01:00
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
t.Skip("Test does not work with macOS")
|
|
|
|
}
|
2019-05-22 21:06:27 +01:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 10, UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
2019-05-30 18:35:04 +01:00
|
|
|
NewIPCount: 3,
|
2019-05-22 21:06:27 +01:00
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
service := satellite.Overlay.Service
|
|
|
|
tests := []struct {
|
|
|
|
nodeCount int
|
|
|
|
duplicateCount int
|
|
|
|
requestCount int
|
|
|
|
preferences overlay.NodeSelectionConfig
|
|
|
|
shouldFailWith *errs.Class
|
|
|
|
}{
|
|
|
|
{ // test only distinct IPs with half new nodes
|
2019-05-30 18:35:04 +01:00
|
|
|
requestCount: 4,
|
2019-06-13 22:51:18 +01:00
|
|
|
preferences: testNodeSelectionConfig(1, 0.5, true),
|
2019-05-22 21:06:27 +01:00
|
|
|
},
|
|
|
|
{ // test not enough distinct IPs
|
2019-06-13 22:51:18 +01:00
|
|
|
requestCount: 7,
|
|
|
|
preferences: testNodeSelectionConfig(0, 0, true),
|
2019-05-22 21:06:27 +01:00
|
|
|
shouldFailWith: &overlay.ErrNotEnoughNodes,
|
|
|
|
},
|
|
|
|
{ // test distinct flag false allows duplicates
|
|
|
|
duplicateCount: 10,
|
|
|
|
requestCount: 5,
|
2019-06-13 22:51:18 +01:00
|
|
|
preferences: testNodeSelectionConfig(0, 0.5, false),
|
2019-05-22 21:06:27 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// This sets a reputable audit count for nodes[8] and nodes[9].
|
|
|
|
for i := 9; i > 7; i-- {
|
|
|
|
_, err := satellite.DB.OverlayCache().UpdateStats(ctx, &overlay.UpdateRequest{
|
|
|
|
NodeID: planet.StorageNodes[i].ID(),
|
|
|
|
IsUp: true,
|
|
|
|
AuditSuccess: true,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
response, err := service.FindStorageNodesWithPreferences(ctx, overlay.FindStorageNodesRequest{
|
|
|
|
FreeBandwidth: 0,
|
|
|
|
FreeDisk: 0,
|
|
|
|
RequestedCount: tt.requestCount,
|
|
|
|
}, &tt.preferences)
|
|
|
|
if tt.shouldFailWith != nil {
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, tt.shouldFailWith.Has(err))
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// assert all IPs are unique
|
|
|
|
if tt.preferences.DistinctIP {
|
|
|
|
ips := make(map[string]bool)
|
|
|
|
for _, n := range response {
|
|
|
|
assert.False(t, ips[n.LastIp])
|
|
|
|
ips[n.LastIp] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, tt.requestCount, len(response))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|