2020-02-17 18:22:24 +00:00
|
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
|
|
package accounting_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-01-09 16:51:59 +00:00
|
|
|
|
"go.uber.org/zap"
|
2020-02-17 18:22:24 +00:00
|
|
|
|
|
|
|
|
|
"storj.io/common/memory"
|
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
|
"storj.io/common/testrand"
|
|
|
|
|
"storj.io/storj/private/testplanet"
|
2020-01-09 16:51:59 +00:00
|
|
|
|
"storj.io/storj/satellite"
|
|
|
|
|
"storj.io/storj/satellite/accounting"
|
2020-02-17 18:22:24 +00:00
|
|
|
|
)
|
|
|
|
|
|
2020-03-11 15:52:28 +00:00
|
|
|
|
func TestBilling_InlineFiles(t *testing.T) {
|
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 1, UplinkCount: 1,
|
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
|
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
|
|
|
|
|
config.Orders.FlushBatchSize = 1
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
|
const (
|
|
|
|
|
bucketName = "testbucket"
|
|
|
|
|
firstPath = "path"
|
|
|
|
|
secondPath = "another_path"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
satelliteSys = planet.Satellites[0]
|
|
|
|
|
uplink = planet.Uplinks[0]
|
|
|
|
|
projectID = uplink.ProjectID[satelliteSys.ID()]
|
|
|
|
|
since = time.Now()
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
satelliteSys.Accounting.Tally.Loop.Pause()
|
|
|
|
|
|
|
|
|
|
// Prepare two inline segments for the Uplink to upload
|
|
|
|
|
firstSegment := testrand.Bytes(2 * memory.KiB)
|
|
|
|
|
secondSegment := testrand.Bytes(3 * memory.KiB)
|
|
|
|
|
|
|
|
|
|
err := uplink.Upload(ctx, satelliteSys, bucketName, firstPath, firstSegment)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
err = uplink.Upload(ctx, satelliteSys, bucketName, secondPath, secondSegment)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
_, err = uplink.Download(ctx, satelliteSys, bucketName, firstPath)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// We need to call tally twice, it calculates the estimated time
|
|
|
|
|
// using the difference in the generation time of the two tallies
|
|
|
|
|
satelliteSys.Accounting.Tally.Loop.TriggerWait()
|
|
|
|
|
|
|
|
|
|
usage := getProjectTotal(ctx, t, planet, 0, projectID, since)
|
|
|
|
|
|
|
|
|
|
// Usage should be > 0
|
|
|
|
|
require.NotZero(t, usage.ObjectCount)
|
|
|
|
|
require.NotZero(t, usage.Storage)
|
|
|
|
|
require.NotZero(t, usage.Egress)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-09 16:51:59 +00:00
|
|
|
|
func TestBilling_FilesAfterDeletion(t *testing.T) {
|
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 4, UplinkCount: 1,
|
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
|
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
|
|
|
|
|
config.Orders.FlushBatchSize = 1
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
|
const (
|
|
|
|
|
bucketName = "testbucket"
|
|
|
|
|
filePath = "test/path"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
satelliteSys = planet.Satellites[0]
|
|
|
|
|
uplink = planet.Uplinks[0]
|
|
|
|
|
projectID = uplink.ProjectID[satelliteSys.ID()]
|
|
|
|
|
since = time.Now()
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
satelliteSys.Accounting.Tally.Loop.Pause()
|
|
|
|
|
|
|
|
|
|
// Prepare some data for the Uplink to upload
|
|
|
|
|
uploadData := testrand.Bytes(5 * memory.KiB)
|
|
|
|
|
|
|
|
|
|
err := uplink.Upload(ctx, satelliteSys, bucketName, filePath, uploadData)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// We need to call tally twice, it calculates the estimated time
|
|
|
|
|
// using the difference in the generation time of the two tallies
|
|
|
|
|
satelliteSys.Accounting.Tally.Loop.TriggerWait()
|
|
|
|
|
|
|
|
|
|
// Get usage for uploaded file before we delete it
|
|
|
|
|
usageBefore := getProjectTotal(ctx, t, planet, 0, projectID, since)
|
|
|
|
|
|
|
|
|
|
// ObjectCount and Storage should be > 0
|
|
|
|
|
require.NotZero(t, usageBefore.ObjectCount)
|
|
|
|
|
require.NotZero(t, usageBefore.Storage)
|
|
|
|
|
require.Zero(t, usageBefore.Egress)
|
|
|
|
|
|
|
|
|
|
err = uplink.DeleteObject(ctx, satelliteSys, bucketName, filePath)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
err = uplink.DeleteBucket(ctx, satelliteSys, bucketName)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Get usage after file was deleted
|
|
|
|
|
usageAfter := getProjectTotal(ctx, t, planet, 0, projectID, since)
|
|
|
|
|
|
|
|
|
|
// Verify data is correct. We don’t bill for the data after deleting objects, usage should be equal
|
|
|
|
|
require.Equal(t, usageBefore.ObjectCount, usageAfter.ObjectCount, "Object count should be equal")
|
|
|
|
|
require.Equal(t, usageBefore.Storage, usageAfter.Storage, "Storage should be equal")
|
|
|
|
|
require.Zero(t, usageAfter.Egress, "Egress should be 0")
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestBilling_TrafficAfterFileDeletion(t *testing.T) {
|
2020-02-24 11:07:34 +00:00
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 4, UplinkCount: 1,
|
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
|
Satellite: testplanet.ReconfigureRS(2, 3, 4, 4),
|
|
|
|
|
},
|
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
|
const (
|
|
|
|
|
bucketName = "testbucket"
|
|
|
|
|
filePath = "test/path"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
satelliteSys = planet.Satellites[0]
|
|
|
|
|
uplink = planet.Uplinks[0]
|
|
|
|
|
projectID = uplink.ProjectID[satelliteSys.ID()]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
data := testrand.Bytes(5 * memory.KiB)
|
|
|
|
|
err := uplink.Upload(ctx, satelliteSys, bucketName, filePath, data)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
_, err = uplink.Download(ctx, satelliteSys, bucketName, filePath)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
err = uplink.DeleteObject(ctx, satelliteSys, bucketName, filePath)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
err = uplink.DeleteBucket(ctx, satelliteSys, bucketName)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Check that download traffic gets billed even if the file and bucket was deleted
|
2020-01-09 16:51:59 +00:00
|
|
|
|
usage := getProjectTotal(ctx, t, planet, 0, projectID, time.Now().Add(-30*time.Millisecond))
|
|
|
|
|
require.NotZero(t, usage.Egress, "Egress should not be empty")
|
2020-02-24 11:07:34 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-17 18:22:24 +00:00
|
|
|
|
func TestBilling_DownloadAndNoUploadTraffic(t *testing.T) {
|
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 4, UplinkCount: 1,
|
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
|
Satellite: testplanet.ReconfigureRS(2, 3, 4, 4),
|
|
|
|
|
},
|
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
|
const (
|
|
|
|
|
bucketName = "a-bucket"
|
|
|
|
|
objectKey = "object-filename"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
satelliteSys := planet.Satellites[0]
|
|
|
|
|
// Make sure that we don't have interference with billed repair traffic
|
|
|
|
|
// in case of a bug. There is a specific test to verify that the repair
|
|
|
|
|
// traffic isn't billed.
|
|
|
|
|
satelliteSys.Audit.Chore.Loop.Stop()
|
|
|
|
|
satelliteSys.Repair.Repairer.Loop.Stop()
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
uplnk = planet.Uplinks[0]
|
|
|
|
|
projectID = uplnk.ProjectID[satelliteSys.ID()]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
since := time.Now().Add(-10 * time.Hour)
|
2020-01-09 16:51:59 +00:00
|
|
|
|
usage := getProjectTotal(ctx, t, planet, 0, projectID, since)
|
|
|
|
|
require.Zero(t, usage.Egress, "billed usage")
|
2020-02-17 18:22:24 +00:00
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
data := testrand.Bytes(10 * memory.KiB)
|
|
|
|
|
err := uplnk.Upload(ctx, satelliteSys, bucketName, objectKey, data)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-09 16:51:59 +00:00
|
|
|
|
usage = getProjectTotal(ctx, t, planet, 0, projectID, since)
|
|
|
|
|
require.Zero(t, usage.Egress, "billed usage")
|
2020-02-17 18:22:24 +00:00
|
|
|
|
|
|
|
|
|
_, err := uplnk.Download(ctx, satelliteSys, bucketName, objectKey)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
2020-01-09 16:51:59 +00:00
|
|
|
|
usage = getProjectTotal(ctx, t, planet, 0, projectID, since)
|
|
|
|
|
require.NotZero(t, usage.Egress, "billed usage")
|
2020-02-17 18:22:24 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-09 16:51:59 +00:00
|
|
|
|
// getProjectTotal returns used egress, storage, objectCount for the
|
2020-02-17 18:22:24 +00:00
|
|
|
|
// projectID in the satellite referenced by satelliteIdx index.
|
2020-01-09 16:51:59 +00:00
|
|
|
|
func getProjectTotal(
|
2020-02-17 18:22:24 +00:00
|
|
|
|
ctx context.Context, t *testing.T, planet *testplanet.Planet, satelliteIdx int,
|
|
|
|
|
projectID uuid.UUID, since time.Time,
|
2020-01-09 16:51:59 +00:00
|
|
|
|
) *accounting.ProjectUsage {
|
2020-02-17 18:22:24 +00:00
|
|
|
|
t.Helper()
|
|
|
|
|
|
2020-01-09 16:51:59 +00:00
|
|
|
|
// Wait for the SNs endpoints to finish their work
|
2020-03-05 11:34:12 +00:00
|
|
|
|
require.NoError(t, planet.WaitForStorageNodeEndpoints(ctx))
|
|
|
|
|
|
2020-01-09 16:51:59 +00:00
|
|
|
|
// Calculate the usage used for upload
|
2020-02-17 18:22:24 +00:00
|
|
|
|
for _, sn := range planet.StorageNodes {
|
|
|
|
|
sn.Storage2.Orders.Sender.TriggerWait()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sat := planet.Satellites[satelliteIdx]
|
|
|
|
|
{
|
|
|
|
|
rollout := sat.Core.Accounting.ReportedRollupChore
|
|
|
|
|
require.NoError(t, rollout.RunOnce(ctx, since))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sat.Accounting.Tally.Loop.TriggerWait()
|
|
|
|
|
|
2020-01-09 16:51:59 +00:00
|
|
|
|
usage, err := sat.DB.ProjectAccounting().GetProjectTotal(ctx, projectID, since, time.Now())
|
2020-02-17 18:22:24 +00:00
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
2020-01-09 16:51:59 +00:00
|
|
|
|
return usage
|
2020-02-17 18:22:24 +00:00
|
|
|
|
}
|