2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-08 16:18:28 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-01-23 19:58:44 +00:00
|
|
|
package tally_test
|
2018-11-08 16:18:28 +00:00
|
|
|
|
|
|
|
import (
|
2019-01-10 11:41:57 +00:00
|
|
|
"context"
|
2018-11-08 16:18:28 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-02-01 18:50:12 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-12-07 09:59:31 +00:00
|
|
|
|
2018-12-07 11:55:25 +00:00
|
|
|
"storj.io/storj/internal/testcontext"
|
2019-02-01 18:50:12 +00:00
|
|
|
"storj.io/storj/internal/testplanet"
|
2019-01-25 23:06:38 +00:00
|
|
|
"storj.io/storj/pkg/bwagreement/testbwagreement"
|
2018-11-08 16:18:28 +00:00
|
|
|
"storj.io/storj/pkg/pb"
|
2019-02-01 18:50:12 +00:00
|
|
|
"storj.io/storj/pkg/piecestore/psserver/psdb"
|
2018-11-08 16:18:28 +00:00
|
|
|
)
|
|
|
|
|
2019-02-01 18:50:12 +00:00
|
|
|
func TestQueryNoAgreements(t *testing.T) {
|
2019-02-05 14:23:45 +00:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 1, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
2019-02-01 18:50:12 +00:00
|
|
|
tally := planet.Satellites[0].Accounting.Tally
|
|
|
|
_, bwTotals, err := tally.QueryBW(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, bwTotals, 0)
|
|
|
|
})
|
2018-12-05 14:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestQueryWithBw(t *testing.T) {
|
2019-02-05 14:23:45 +00:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 1, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
sendGeneratedAgreements(ctx, t, planet)
|
2019-02-01 18:50:12 +00:00
|
|
|
tally := planet.Satellites[0].Accounting.Tally
|
|
|
|
tallyEnd, bwTotals, err := tally.QueryBW(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, bwTotals, 1)
|
2019-02-05 14:23:45 +00:00
|
|
|
|
2019-02-01 18:50:12 +00:00
|
|
|
for id, nodeTotals := range bwTotals {
|
|
|
|
require.Len(t, nodeTotals, 5)
|
|
|
|
for _, total := range nodeTotals {
|
|
|
|
require.Equal(t, planet.StorageNodes[0].Identity.ID, id)
|
|
|
|
require.Equal(t, int64(1000), total)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = tally.SaveBWRaw(ctx, tallyEnd, bwTotals)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
2019-01-10 11:41:57 +00:00
|
|
|
}
|
2018-12-27 09:56:25 +00:00
|
|
|
|
2019-02-05 14:23:45 +00:00
|
|
|
func sendGeneratedAgreements(ctx context.Context, t *testing.T, planet *testplanet.Planet) {
|
2019-02-01 18:50:12 +00:00
|
|
|
satID := planet.Satellites[0].Identity
|
|
|
|
upID := planet.Uplinks[0].Identity
|
|
|
|
snID := planet.StorageNodes[0].Identity
|
|
|
|
sender := planet.StorageNodes[0].Agreements.Sender
|
2019-02-05 14:23:45 +00:00
|
|
|
actions := []pb.BandwidthAction{
|
|
|
|
pb.BandwidthAction_PUT,
|
|
|
|
pb.BandwidthAction_GET,
|
|
|
|
pb.BandwidthAction_GET_AUDIT,
|
|
|
|
pb.BandwidthAction_GET_REPAIR,
|
|
|
|
pb.BandwidthAction_PUT_REPAIR,
|
|
|
|
}
|
|
|
|
|
2019-02-01 18:50:12 +00:00
|
|
|
agreements := make([]*psdb.Agreement, len(actions))
|
|
|
|
for i, action := range actions {
|
|
|
|
pba, err := testbwagreement.GeneratePayerBandwidthAllocation(action, satID, upID, time.Hour)
|
|
|
|
require.NoError(t, err)
|
|
|
|
rba, err := testbwagreement.GenerateRenterBandwidthAllocation(pba, snID.ID, upID, 1000)
|
|
|
|
require.NoError(t, err)
|
|
|
|
agreements[i] = &psdb.Agreement{Agreement: *rba}
|
|
|
|
}
|
|
|
|
|
2019-02-05 14:23:45 +00:00
|
|
|
sender.SendAgreementsToSatellite(ctx, satID.ID, agreements)
|
2018-12-05 14:03:23 +00:00
|
|
|
}
|