2018-11-08 16:18:28 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-11-14 01:22:18 +00:00
|
|
|
package tally
|
2018-11-08 16:18:28 +00:00
|
|
|
|
|
|
|
import (
|
2018-12-05 14:03:23 +00:00
|
|
|
"crypto/ecdsa"
|
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-12 21:24:08 +00:00
|
|
|
testidentity "storj.io/storj/internal/identity"
|
2018-12-07 11:55:25 +00:00
|
|
|
"storj.io/storj/internal/testcontext"
|
2018-12-07 09:59:31 +00:00
|
|
|
"storj.io/storj/pkg/bwagreement"
|
2018-12-05 14:03:23 +00:00
|
|
|
"storj.io/storj/pkg/bwagreement/test"
|
2018-11-08 16:18:28 +00:00
|
|
|
"storj.io/storj/pkg/overlay"
|
|
|
|
"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()
|
|
|
|
|
2018-12-05 14:03:23 +00:00
|
|
|
pointerdb := pointerdb.NewServer(teststore.New(), &overlay.Cache{}, zap.NewNop(), pointerdb.Config{}, nil)
|
|
|
|
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-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
|
|
|
|
2018-12-14 14:27:21 +00:00
|
|
|
tally := newTally(zap.NewNop(), db.Accounting(), db.BandwidthAgreement(), pointerdb, overlayServer, 0, time.Second)
|
2018-12-05 14:03:23 +00:00
|
|
|
|
|
|
|
err = tally.Query(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQueryWithBw(t *testing.T) {
|
2018-12-07 11:55:25 +00:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2018-12-05 14:03:23 +00:00
|
|
|
pointerdb := pointerdb.NewServer(teststore.New(), &overlay.Cache{}, zap.NewNop(), pointerdb.Config{}, nil)
|
|
|
|
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)
|
|
|
|
assert.NoError(t, db.CreateTables())
|
2018-12-07 09:59:31 +00:00
|
|
|
|
2018-12-14 14:27:21 +00:00
|
|
|
bwDb := db.BandwidthAgreement()
|
|
|
|
tally := newTally(zap.NewNop(), db.Accounting(), bwDb, pointerdb, overlayServer, 0, time.Second)
|
2018-12-05 14:03:23 +00:00
|
|
|
|
|
|
|
//get a private key
|
|
|
|
fiC, err := testidentity.NewTestIdentity()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
k, ok := fiC.Key.(*ecdsa.PrivateKey)
|
|
|
|
assert.True(t, ok)
|
|
|
|
//generate an agreement with the key
|
|
|
|
pba, err := test.GeneratePayerBandwidthAllocation(pb.PayerBandwidthAllocation_GET, k)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
rba, err := test.GenerateRenterBandwidthAllocation(pba, k)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
//save to db
|
2018-12-07 09:59:31 +00:00
|
|
|
err = bwDb.CreateAgreement(ctx, bwagreement.Agreement{Signature: rba.GetSignature(), Agreement: rba.GetData()})
|
2018-12-05 14:03:23 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
//check the db
|
|
|
|
err = tally.Query(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|