2020-05-10 12:23:13 +01:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package consoleapi_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-09-08 16:15:08 +01:00
|
|
|
"go.uber.org/zap"
|
2020-05-10 12:23:13 +01:00
|
|
|
|
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/storj/private/testplanet"
|
2020-09-08 16:15:08 +01:00
|
|
|
"storj.io/storj/satellite"
|
2021-01-14 16:41:36 +00:00
|
|
|
"storj.io/storj/storagenode/payouts/estimatedpayouts"
|
2020-05-10 12:23:13 +01:00
|
|
|
"storj.io/storj/storagenode/pricing"
|
|
|
|
"storj.io/storj/storagenode/reputation"
|
|
|
|
"storj.io/storj/storagenode/storageusage"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
actions = []pb.PieceAction{
|
|
|
|
pb.PieceAction_INVALID,
|
|
|
|
|
|
|
|
pb.PieceAction_PUT,
|
|
|
|
pb.PieceAction_GET,
|
|
|
|
pb.PieceAction_GET_AUDIT,
|
|
|
|
pb.PieceAction_GET_REPAIR,
|
|
|
|
pb.PieceAction_PUT_REPAIR,
|
|
|
|
pb.PieceAction_DELETE,
|
|
|
|
|
|
|
|
pb.PieceAction_PUT,
|
|
|
|
pb.PieceAction_GET,
|
|
|
|
pb.PieceAction_GET_AUDIT,
|
|
|
|
pb.PieceAction_GET_REPAIR,
|
|
|
|
pb.PieceAction_PUT_REPAIR,
|
|
|
|
pb.PieceAction_DELETE,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStorageNodeApi(t *testing.T) {
|
|
|
|
testplanet.Run(t,
|
|
|
|
testplanet.Config{
|
2020-07-09 18:43:06 +01:00
|
|
|
SatelliteCount: 1,
|
2020-05-10 12:23:13 +01:00
|
|
|
StorageNodeCount: 1,
|
2020-09-08 16:15:08 +01:00
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
|
|
|
|
config.Payments.NodeEgressBandwidthPrice = 2000
|
|
|
|
config.Payments.NodeAuditBandwidthPrice = 1000
|
|
|
|
config.Payments.NodeRepairBandwidthPrice = 1000
|
|
|
|
config.Payments.NodeDiskSpacePrice = 150
|
|
|
|
},
|
|
|
|
},
|
2020-05-10 12:23:13 +01:00
|
|
|
},
|
|
|
|
func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
sno := planet.StorageNodes[0]
|
|
|
|
console := sno.Console
|
|
|
|
bandwidthdb := sno.DB.Bandwidth()
|
|
|
|
pricingdb := sno.DB.Pricing()
|
|
|
|
storageusagedb := sno.DB.StorageUsage()
|
|
|
|
reputationdb := sno.DB.Reputation()
|
|
|
|
baseURL := fmt.Sprintf("http://%s/api/sno", console.Listener.Addr())
|
|
|
|
|
2021-03-23 15:39:02 +00:00
|
|
|
// pause nodestats reputation cache because later tests assert a specific joinedat.
|
|
|
|
sno.NodeStats.Cache.Reputation.Pause()
|
|
|
|
|
2022-07-01 10:32:17 +01:00
|
|
|
now := time.Now().UTC()
|
|
|
|
startingPoint := now.Add(-2 * time.Hour)
|
2020-05-10 12:23:13 +01:00
|
|
|
|
|
|
|
for _, action := range actions {
|
2022-07-01 10:32:17 +01:00
|
|
|
err := bandwidthdb.Add(ctx, satellite.ID(), action, 2300000000000, startingPoint)
|
2020-05-10 12:23:13 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
var satellites []storj.NodeID
|
|
|
|
|
2020-07-09 18:43:06 +01:00
|
|
|
satellites = append(satellites, satellite.ID())
|
2020-05-10 12:23:13 +01:00
|
|
|
stamps, _ := makeStorageUsageStamps(satellites)
|
|
|
|
|
|
|
|
err := storageusagedb.Store(ctx, stamps)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-03-23 15:39:02 +00:00
|
|
|
err = reputationdb.Store(ctx, reputation.Stats{
|
|
|
|
SatelliteID: satellite.ID(),
|
2022-07-01 10:32:17 +01:00
|
|
|
JoinedAt: startingPoint.AddDate(0, -2, 0),
|
2021-03-23 15:39:02 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-05-10 12:23:13 +01:00
|
|
|
egressPrice, repairPrice, auditPrice, diskPrice := int64(2000), int64(1000), int64(1000), int64(150)
|
|
|
|
|
|
|
|
err = pricingdb.Store(ctx, pricing.Pricing{
|
|
|
|
SatelliteID: satellite.ID(),
|
|
|
|
EgressBandwidth: egressPrice,
|
|
|
|
RepairBandwidth: repairPrice,
|
|
|
|
AuditBandwidth: auditPrice,
|
|
|
|
DiskSpace: diskPrice,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-07-01 10:32:17 +01:00
|
|
|
t.Run("EstimatedPayout", func(t *testing.T) {
|
2020-05-10 12:23:13 +01:00
|
|
|
// should return estimated payout for both satellites in current month and empty for previous
|
2021-05-14 16:05:42 +01:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("%s/estimated-payout", baseURL), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
res, err := http.DefaultClient.Do(req)
|
2020-05-10 12:23:13 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, res)
|
|
|
|
require.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = res.Body.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-02-26 20:12:35 +00:00
|
|
|
bodyPayout := &estimatedpayouts.EstimatedPayout{}
|
|
|
|
require.NoError(t, json.Unmarshal(body, bodyPayout))
|
|
|
|
|
2022-07-01 10:32:17 +01:00
|
|
|
estimation, err := sno.Console.Service.GetAllSatellitesEstimatedPayout(ctx, now)
|
2020-07-09 18:43:06 +01:00
|
|
|
require.NoError(t, err)
|
2021-02-26 20:12:35 +00:00
|
|
|
expectedPayout := &estimatedpayouts.EstimatedPayout{
|
2021-01-14 16:41:36 +00:00
|
|
|
CurrentMonth: estimation.CurrentMonth,
|
|
|
|
PreviousMonth: estimation.PreviousMonth,
|
|
|
|
CurrentMonthExpectations: estimation.CurrentMonthExpectations,
|
2021-02-26 20:12:35 +00:00
|
|
|
}
|
2020-05-10 12:23:13 +01:00
|
|
|
require.NoError(t, err)
|
2021-02-26 20:12:35 +00:00
|
|
|
|
|
|
|
require.EqualValues(t, expectedPayout, bodyPayout)
|
2020-05-10 12:23:13 +01:00
|
|
|
})
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeStorageUsageStamps creates storage usage stamps and expected summaries for provided satellites.
|
|
|
|
// Creates one entry per day for 30 days with last date as beginning of provided endDate.
|
|
|
|
func makeStorageUsageStamps(satellites []storj.NodeID) ([]storageusage.Stamp, map[storj.NodeID]float64) {
|
|
|
|
var stamps []storageusage.Stamp
|
|
|
|
summary := make(map[storj.NodeID]float64)
|
|
|
|
|
2022-07-01 10:32:17 +01:00
|
|
|
now := time.Now().UTC()
|
|
|
|
currentDay := now.Day()
|
2020-05-10 12:23:13 +01:00
|
|
|
|
|
|
|
for _, satellite := range satellites {
|
2022-07-01 10:32:17 +01:00
|
|
|
for i := 0; i < currentDay; i++ {
|
2020-05-10 12:23:13 +01:00
|
|
|
stamp := storageusage.Stamp{
|
|
|
|
SatelliteID: satellite,
|
|
|
|
AtRestTotal: 30000000000000,
|
2022-07-01 10:32:17 +01:00
|
|
|
IntervalStart: now.Add(time.Hour * -24 * time.Duration(i)),
|
2020-05-10 12:23:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
summary[satellite] += stamp.AtRestTotal
|
|
|
|
stamps = append(stamps, stamp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stamps, summary
|
|
|
|
}
|