34a439a99c
* enable deletion of raws * assert deletion of raws during TestRollupRaws * add some unit tests for DeleteRawBefore
124 lines
3.6 KiB
Go
124 lines
3.6 KiB
Go
// Copyright (C) 2019 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package tally_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"storj.io/storj/internal/testcontext"
|
|
"storj.io/storj/internal/testplanet"
|
|
"storj.io/storj/internal/teststorj"
|
|
"storj.io/storj/pkg/bwagreement/testbwagreement"
|
|
"storj.io/storj/pkg/pb"
|
|
"storj.io/storj/pkg/piecestore/psserver/psdb"
|
|
"storj.io/storj/pkg/storj"
|
|
"storj.io/storj/satellite"
|
|
)
|
|
|
|
func TestQueryNoAgreements(t *testing.T) {
|
|
testplanet.Run(t, testplanet.Config{
|
|
SatelliteCount: 1, StorageNodeCount: 1, UplinkCount: 1,
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
tally := planet.Satellites[0].Accounting.Tally
|
|
_, bwTotals, err := tally.QueryBW(ctx)
|
|
require.NoError(t, err)
|
|
require.Len(t, bwTotals, 0)
|
|
})
|
|
}
|
|
|
|
func TestQueryWithBw(t *testing.T) {
|
|
testplanet.Run(t, testplanet.Config{
|
|
SatelliteCount: 1, StorageNodeCount: 1, UplinkCount: 1,
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
db := planet.Satellites[0].DB
|
|
sendGeneratedAgreements(ctx, t, db, planet)
|
|
|
|
tally := planet.Satellites[0].Accounting.Tally
|
|
tallyEnd, bwTotals, err := tally.QueryBW(ctx)
|
|
require.NoError(t, err)
|
|
require.Len(t, bwTotals, 1)
|
|
|
|
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, time.Now().UTC(), bwTotals)
|
|
require.NoError(t, err)
|
|
})
|
|
}
|
|
|
|
func TestDeleteRawBefore(t *testing.T) {
|
|
tests := []struct {
|
|
createdAt time.Time
|
|
eraseBefore time.Time
|
|
expectedRaws int
|
|
}{
|
|
{
|
|
createdAt: time.Now(),
|
|
eraseBefore: time.Now(),
|
|
expectedRaws: 1,
|
|
},
|
|
{
|
|
createdAt: time.Now(),
|
|
eraseBefore: time.Now().Add(24 * time.Hour),
|
|
expectedRaws: 0,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
testplanet.Run(t, testplanet.Config{
|
|
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 0,
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
id := teststorj.NodeIDFromBytes([]byte{})
|
|
nodeData := make(map[storj.NodeID]float64)
|
|
nodeData[id] = float64(1000)
|
|
|
|
err := planet.Satellites[0].DB.Accounting().SaveAtRestRaw(ctx, tt.createdAt, tt.createdAt, nodeData)
|
|
require.NoError(t, err)
|
|
|
|
err = planet.Satellites[0].DB.Accounting().DeleteRawBefore(ctx, tt.eraseBefore)
|
|
require.NoError(t, err)
|
|
|
|
raws, err := planet.Satellites[0].DB.Accounting().GetRaw(ctx)
|
|
require.NoError(t, err)
|
|
assert.Len(t, raws, tt.expectedRaws)
|
|
})
|
|
}
|
|
}
|
|
|
|
func sendGeneratedAgreements(ctx context.Context, t *testing.T, db satellite.DB, planet *testplanet.Planet) {
|
|
satID := planet.Satellites[0].Identity
|
|
upID := planet.Uplinks[0].Identity
|
|
snID := planet.StorageNodes[0].Identity
|
|
sender := planet.StorageNodes[0].Agreements.Sender
|
|
actions := []pb.BandwidthAction{
|
|
pb.BandwidthAction_PUT,
|
|
pb.BandwidthAction_GET,
|
|
pb.BandwidthAction_GET_AUDIT,
|
|
pb.BandwidthAction_GET_REPAIR,
|
|
pb.BandwidthAction_PUT_REPAIR,
|
|
}
|
|
|
|
agreements := make([]*psdb.Agreement, len(actions))
|
|
for i, action := range actions {
|
|
pba, err := testbwagreement.GenerateOrderLimit(action, satID, upID, time.Hour)
|
|
require.NoError(t, err)
|
|
err = db.CertDB().SavePublicKey(ctx, pba.UplinkId, upID.Leaf.PublicKey)
|
|
assert.NoError(t, err)
|
|
rba, err := testbwagreement.GenerateOrder(pba, snID.ID, upID, 1000)
|
|
require.NoError(t, err)
|
|
agreements[i] = &psdb.Agreement{Agreement: *rba}
|
|
}
|
|
|
|
sender.SettleAgreements(ctx, satID.ID, agreements)
|
|
}
|