2019-03-27 10:24:35 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package orders_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"testing"
|
2019-04-01 21:14:58 +01:00
|
|
|
"time"
|
2019-03-27 10:24:35 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"storj.io/storj/internal/memory"
|
|
|
|
"storj.io/storj/internal/testcontext"
|
|
|
|
"storj.io/storj/internal/testplanet"
|
2019-04-01 21:14:58 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
2019-03-27 10:24:35 +00:00
|
|
|
)
|
|
|
|
|
2019-04-01 21:14:58 +01:00
|
|
|
func TestSendingReceivingOrders(t *testing.T) {
|
|
|
|
// test happy path
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 6, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
2019-03-27 10:24:35 +00:00
|
|
|
|
2019-04-01 21:14:58 +01:00
|
|
|
for _, storageNode := range planet.StorageNodes {
|
|
|
|
storageNode.Storage2.Sender.Loop.Pause()
|
|
|
|
}
|
2019-03-27 10:24:35 +00:00
|
|
|
|
2019-04-01 21:14:58 +01:00
|
|
|
expectedData := make([]byte, 50*memory.KiB)
|
|
|
|
_, err := rand.Read(expectedData)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = planet.Uplinks[0].Upload(ctx, planet.Satellites[0], "testbucket", "test/path", expectedData)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
sumBeforeSend := 0
|
|
|
|
for _, storageNode := range planet.StorageNodes {
|
|
|
|
infos, err := storageNode.DB.Orders().ListUnsent(ctx, 10)
|
|
|
|
require.NoError(t, err)
|
|
|
|
sumBeforeSend += len(infos)
|
|
|
|
}
|
|
|
|
require.NotZero(t, sumBeforeSend)
|
|
|
|
|
|
|
|
sumUnsent := 0
|
|
|
|
sumArchived := 0
|
|
|
|
|
|
|
|
for _, storageNode := range planet.StorageNodes {
|
|
|
|
storageNode.Storage2.Sender.Loop.TriggerWait()
|
|
|
|
|
|
|
|
infos, err := storageNode.DB.Orders().ListUnsent(ctx, 10)
|
|
|
|
require.NoError(t, err)
|
|
|
|
sumUnsent += len(infos)
|
2019-03-27 10:24:35 +00:00
|
|
|
|
2019-04-01 21:14:58 +01:00
|
|
|
archivedInfos, err := storageNode.DB.Orders().ListArchived(ctx, sumBeforeSend)
|
|
|
|
require.NoError(t, err)
|
|
|
|
sumArchived += len(archivedInfos)
|
|
|
|
}
|
2019-03-27 10:24:35 +00:00
|
|
|
|
2019-04-01 21:14:58 +01:00
|
|
|
require.Zero(t, sumUnsent)
|
|
|
|
require.Equal(t, sumBeforeSend, sumArchived)
|
|
|
|
})
|
|
|
|
}
|
2019-03-27 10:24:35 +00:00
|
|
|
|
2019-04-01 21:14:58 +01:00
|
|
|
func TestUnableToSendOrders(t *testing.T) {
|
|
|
|
// test sending when satellite is unavailable
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 6, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
for _, storageNode := range planet.StorageNodes {
|
|
|
|
storageNode.Storage2.Sender.Loop.Pause()
|
|
|
|
}
|
2019-03-27 10:24:35 +00:00
|
|
|
|
2019-04-01 21:14:58 +01:00
|
|
|
expectedData := make([]byte, 50*memory.KiB)
|
|
|
|
_, err := rand.Read(expectedData)
|
2019-03-27 10:24:35 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-04-01 21:14:58 +01:00
|
|
|
err = planet.Uplinks[0].Upload(ctx, planet.Satellites[0], "testbucket", "test/path", expectedData)
|
|
|
|
require.NoError(t, err)
|
2019-03-27 10:24:35 +00:00
|
|
|
|
2019-04-01 21:14:58 +01:00
|
|
|
sumBeforeSend := 0
|
|
|
|
for _, storageNode := range planet.StorageNodes {
|
|
|
|
infos, err := storageNode.DB.Orders().ListUnsent(ctx, 10)
|
|
|
|
require.NoError(t, err)
|
|
|
|
sumBeforeSend += len(infos)
|
|
|
|
}
|
|
|
|
require.NotZero(t, sumBeforeSend)
|
2019-03-27 10:24:35 +00:00
|
|
|
|
2019-04-01 21:14:58 +01:00
|
|
|
err = planet.StopPeer(planet.Satellites[0])
|
2019-03-27 10:24:35 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-04-01 21:14:58 +01:00
|
|
|
sumUnsent := 0
|
|
|
|
sumArchived := 0
|
|
|
|
for _, storageNode := range planet.StorageNodes {
|
|
|
|
storageNode.Storage2.Sender.Loop.TriggerWait()
|
|
|
|
|
|
|
|
infos, err := storageNode.DB.Orders().ListUnsent(ctx, 10)
|
|
|
|
require.NoError(t, err)
|
|
|
|
sumUnsent += len(infos)
|
|
|
|
|
|
|
|
archivedInfos, err := storageNode.DB.Orders().ListArchived(ctx, sumBeforeSend)
|
|
|
|
require.NoError(t, err)
|
|
|
|
sumArchived += len(archivedInfos)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Zero(t, sumArchived)
|
|
|
|
require.Equal(t, sumBeforeSend, sumUnsent)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUploadDownloadBandwidth(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 6, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
2019-04-09 20:12:58 +01:00
|
|
|
hourBeforeTest := time.Now().UTC().Add(-time.Hour)
|
2019-04-01 21:14:58 +01:00
|
|
|
|
|
|
|
for _, storageNode := range planet.StorageNodes {
|
|
|
|
storageNode.Storage2.Sender.Loop.Pause()
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedData := make([]byte, 50*memory.KiB)
|
|
|
|
_, err := rand.Read(expectedData)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = planet.Uplinks[0].Upload(ctx, planet.Satellites[0], "testbucket", "test/path", expectedData)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
data, err := planet.Uplinks[0].Download(ctx, planet.Satellites[0], "testbucket", "test/path")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expectedData, data)
|
|
|
|
|
|
|
|
var expectedBucketBandwidth int64
|
|
|
|
expectedStorageBandwidth := make(map[storj.NodeID]int64)
|
|
|
|
for _, storageNode := range planet.StorageNodes {
|
|
|
|
infos, err := storageNode.DB.Orders().ListUnsent(ctx, 10)
|
|
|
|
require.NoError(t, err)
|
|
|
|
if len(infos) > 0 {
|
|
|
|
for _, info := range infos {
|
|
|
|
expectedBucketBandwidth += info.Order.Amount
|
|
|
|
expectedStorageBandwidth[storageNode.ID()] += info.Order.Amount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, storageNode := range planet.StorageNodes {
|
|
|
|
storageNode.Storage2.Sender.Loop.TriggerWait()
|
|
|
|
}
|
|
|
|
|
|
|
|
projects, err := planet.Satellites[0].DB.Console().Projects().GetAll(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ordersDB := planet.Satellites[0].DB.Orders()
|
|
|
|
bucketID := storj.JoinPaths(projects[0].ID.String(), "testbucket")
|
|
|
|
|
2019-04-09 20:12:58 +01:00
|
|
|
bucketBandwidth, err := ordersDB.GetBucketBandwidth(ctx, []byte(bucketID), hourBeforeTest, time.Now().UTC())
|
2019-03-27 10:24:35 +00:00
|
|
|
require.NoError(t, err)
|
2019-04-01 21:14:58 +01:00
|
|
|
require.Equal(t, expectedBucketBandwidth, bucketBandwidth)
|
2019-03-27 10:24:35 +00:00
|
|
|
|
2019-04-01 21:14:58 +01:00
|
|
|
for _, storageNode := range planet.StorageNodes {
|
2019-04-09 20:12:58 +01:00
|
|
|
nodeBandwidth, err := ordersDB.GetStorageNodeBandwidth(ctx, storageNode.ID(), hourBeforeTest, time.Now().UTC())
|
2019-04-01 21:14:58 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expectedStorageBandwidth[storageNode.ID()], nodeBandwidth)
|
|
|
|
}
|
|
|
|
})
|
2019-03-27 10:24:35 +00:00
|
|
|
}
|