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"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"go.uber.org/zap"
|
2018-12-07 09:59:31 +00:00
|
|
|
|
2018-12-07 11:55:25 +00:00
|
|
|
"storj.io/storj/internal/testcontext"
|
2019-01-02 17:39:17 +00:00
|
|
|
"storj.io/storj/internal/testidentity"
|
2019-01-07 15:19:05 +00:00
|
|
|
"storj.io/storj/internal/teststorj"
|
2019-01-23 19:58:44 +00:00
|
|
|
"storj.io/storj/pkg/accounting/tally"
|
2018-12-07 09:59:31 +00:00
|
|
|
"storj.io/storj/pkg/bwagreement"
|
2019-01-25 23:06:38 +00:00
|
|
|
"storj.io/storj/pkg/bwagreement/testbwagreement"
|
2019-01-25 18:05:21 +00:00
|
|
|
"storj.io/storj/pkg/identity"
|
2018-11-08 16:18:28 +00:00
|
|
|
"storj.io/storj/pkg/overlay/mocks"
|
|
|
|
"storj.io/storj/pkg/pb"
|
|
|
|
"storj.io/storj/pkg/pointerdb"
|
2018-12-07 09:59:31 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb"
|
2018-11-08 16:18:28 +00:00
|
|
|
"storj.io/storj/storage/teststore"
|
|
|
|
)
|
|
|
|
|
2018-12-05 14:03:23 +00:00
|
|
|
func TestQueryNoAgreements(t *testing.T) {
|
2018-12-07 11:55:25 +00:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2019-01-23 19:58:44 +00:00
|
|
|
// TODO: use testplanet
|
|
|
|
|
2019-01-18 15:10:21 +00:00
|
|
|
service := pointerdb.NewService(zap.NewNop(), teststore.New())
|
2018-12-05 14:03:23 +00:00
|
|
|
overlayServer := mocks.NewOverlay([]*pb.Node{})
|
2018-12-14 14:27:21 +00:00
|
|
|
db, err := satellitedb.NewInMemory()
|
2018-12-05 14:03:23 +00:00
|
|
|
assert.NoError(t, err)
|
2018-12-14 14:27:21 +00:00
|
|
|
defer ctx.Check(db.Close)
|
|
|
|
assert.NoError(t, db.CreateTables())
|
2018-12-07 09:59:31 +00:00
|
|
|
|
2019-01-23 19:58:44 +00:00
|
|
|
tally := tally.New(zap.NewNop(), db.Accounting(), db.BandwidthAgreement(), service, overlayServer, 0, time.Second)
|
2018-12-05 14:03:23 +00:00
|
|
|
|
2019-01-23 19:58:44 +00:00
|
|
|
err = tally.QueryBW(ctx)
|
2018-12-05 14:03:23 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQueryWithBw(t *testing.T) {
|
2018-12-07 11:55:25 +00:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2019-01-23 19:58:44 +00:00
|
|
|
// TODO: use testplanet
|
|
|
|
|
2019-01-18 15:10:21 +00:00
|
|
|
service := pointerdb.NewService(zap.NewNop(), teststore.New())
|
2018-12-05 14:03:23 +00:00
|
|
|
overlayServer := mocks.NewOverlay([]*pb.Node{})
|
2018-12-12 21:24:08 +00:00
|
|
|
|
2018-12-14 14:27:21 +00:00
|
|
|
db, err := satellitedb.NewInMemory()
|
2018-12-07 09:59:31 +00:00
|
|
|
assert.NoError(t, err)
|
2018-12-14 14:27:21 +00:00
|
|
|
defer ctx.Check(db.Close)
|
2018-12-27 09:56:25 +00:00
|
|
|
|
2018-12-14 14:27:21 +00:00
|
|
|
assert.NoError(t, db.CreateTables())
|
2018-12-07 09:59:31 +00:00
|
|
|
|
2018-12-14 14:27:21 +00:00
|
|
|
bwDb := db.BandwidthAgreement()
|
2019-01-23 19:58:44 +00:00
|
|
|
tally := tally.New(zap.NewNop(), db.Accounting(), bwDb, service, overlayServer, 0, time.Second)
|
2018-12-05 14:03:23 +00:00
|
|
|
|
|
|
|
//get a private key
|
2019-01-02 17:39:17 +00:00
|
|
|
fiC, err := testidentity.NewTestIdentity(ctx)
|
2018-12-05 14:03:23 +00:00
|
|
|
assert.NoError(t, err)
|
2018-12-27 09:56:25 +00:00
|
|
|
|
2019-01-28 19:45:25 +00:00
|
|
|
makeBWA(ctx, t, bwDb, fiC, pb.BandwidthAction_PUT)
|
|
|
|
makeBWA(ctx, t, bwDb, fiC, pb.BandwidthAction_GET)
|
|
|
|
makeBWA(ctx, t, bwDb, fiC, pb.BandwidthAction_GET_AUDIT)
|
|
|
|
makeBWA(ctx, t, bwDb, fiC, pb.BandwidthAction_GET_REPAIR)
|
|
|
|
makeBWA(ctx, t, bwDb, fiC, pb.BandwidthAction_PUT_REPAIR)
|
2019-01-10 11:41:57 +00:00
|
|
|
|
|
|
|
//check the db
|
2019-01-23 19:58:44 +00:00
|
|
|
err = tally.QueryBW(ctx)
|
2018-12-05 14:03:23 +00:00
|
|
|
assert.NoError(t, err)
|
2019-01-10 11:41:57 +00:00
|
|
|
}
|
2018-12-27 09:56:25 +00:00
|
|
|
|
2019-01-28 19:45:25 +00:00
|
|
|
func makeBWA(ctx context.Context, t *testing.T, bwDb bwagreement.DB, fiC *identity.FullIdentity, action pb.BandwidthAction) {
|
2019-01-10 11:41:57 +00:00
|
|
|
//generate an agreement with the key
|
2019-01-25 23:06:38 +00:00
|
|
|
pba, err := testbwagreement.GeneratePayerBandwidthAllocation(action, fiC, fiC, time.Hour)
|
2019-01-10 11:41:57 +00:00
|
|
|
assert.NoError(t, err)
|
2019-01-25 23:06:38 +00:00
|
|
|
rba, err := testbwagreement.GenerateRenterBandwidthAllocation(pba, teststorj.NodeIDFromString("StorageNodeID"), fiC, 666)
|
2018-12-05 14:03:23 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
//save to db
|
2019-01-28 19:45:25 +00:00
|
|
|
err = bwDb.CreateAgreement(ctx, rba)
|
2018-12-05 14:03:23 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|