satellite/accounting: Add test not billing after deletion

New test added to be sure that we don`t bill for the data after deleting it.

Change-Id: Ifb5931ce28f6b0294aeb16311164675a17f11917
This commit is contained in:
Michal Niewrzal 2020-01-09 17:51:59 +01:00 committed by igaass
parent 173cb1e484
commit aed8dea625

View File

@ -10,14 +10,74 @@ import (
"github.com/skyrings/skyring-common/tools/uuid" "github.com/skyrings/skyring-common/tools/uuid"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"go.uber.org/zap"
"storj.io/common/memory" "storj.io/common/memory"
"storj.io/common/testcontext" "storj.io/common/testcontext"
"storj.io/common/testrand" "storj.io/common/testrand"
"storj.io/storj/private/testplanet" "storj.io/storj/private/testplanet"
"storj.io/storj/satellite"
"storj.io/storj/satellite/accounting"
) )
func TestBillingTrafficAfterFileDeletion(t *testing.T) { 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 dont 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) {
testplanet.Run(t, testplanet.Config{ testplanet.Run(t, testplanet.Config{
SatelliteCount: 1, StorageNodeCount: 4, UplinkCount: 1, SatelliteCount: 1, StorageNodeCount: 4, UplinkCount: 1,
Reconfigure: testplanet.Reconfigure{ Reconfigure: testplanet.Reconfigure{
@ -49,8 +109,8 @@ func TestBillingTrafficAfterFileDeletion(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
// Check that download traffic gets billed even if the file and bucket was deleted // Check that download traffic gets billed even if the file and bucket was deleted
bandwidth := getTotalProjectBandwidth(ctx, t, planet, 0, projectID, time.Now().Add(-3*time.Hour)) usage := getProjectTotal(ctx, t, planet, 0, projectID, time.Now().Add(-30*time.Millisecond))
require.NotZero(t, bandwidth, "Egress should not be empty") require.NotZero(t, usage.Egress, "Egress should not be empty")
}) })
} }
@ -79,8 +139,8 @@ func TestBilling_DownloadAndNoUploadTraffic(t *testing.T) {
) )
since := time.Now().Add(-10 * time.Hour) since := time.Now().Add(-10 * time.Hour)
bandwidth := getTotalProjectBandwidth(ctx, t, planet, 0, projectID, since) usage := getProjectTotal(ctx, t, planet, 0, projectID, since)
require.Zero(t, bandwidth, "billed bandwidth") require.Zero(t, usage.Egress, "billed usage")
{ {
data := testrand.Bytes(10 * memory.KiB) data := testrand.Bytes(10 * memory.KiB)
@ -88,29 +148,29 @@ func TestBilling_DownloadAndNoUploadTraffic(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
} }
bandwidth = getTotalProjectBandwidth(ctx, t, planet, 0, projectID, since) usage = getProjectTotal(ctx, t, planet, 0, projectID, since)
require.Zero(t, bandwidth, "billed bandwidth") require.Zero(t, usage.Egress, "billed usage")
_, err := uplnk.Download(ctx, satelliteSys, bucketName, objectKey) _, err := uplnk.Download(ctx, satelliteSys, bucketName, objectKey)
require.NoError(t, err) require.NoError(t, err)
bandwidth = getTotalProjectBandwidth(ctx, t, planet, 0, projectID, since) usage = getProjectTotal(ctx, t, planet, 0, projectID, since)
require.NotZero(t, bandwidth, "billed bandwidth") require.NotZero(t, usage.Egress, "billed usage")
}) })
} }
// getTotalProjectBandwidth returns the total used egress bandwidth for the // getProjectTotal returns used egress, storage, objectCount for the
// projectID in the satellite referenced by satelliteIdx index. // projectID in the satellite referenced by satelliteIdx index.
func getTotalProjectBandwidth( func getProjectTotal(
ctx context.Context, t *testing.T, planet *testplanet.Planet, satelliteIdx int, ctx context.Context, t *testing.T, planet *testplanet.Planet, satelliteIdx int,
projectID uuid.UUID, since time.Time, projectID uuid.UUID, since time.Time,
) int64 { ) *accounting.ProjectUsage {
t.Helper() t.Helper()
// Wait for the SNs endpoints to finish thir work // Wait for the SNs endpoints to finish their work
require.NoError(t, planet.WaitForStorageNodeEndpoints(ctx)) require.NoError(t, planet.WaitForStorageNodeEndpoints(ctx))
// Calculate the bandwidth used for upload // Calculate the usage used for upload
for _, sn := range planet.StorageNodes { for _, sn := range planet.StorageNodes {
sn.Storage2.Orders.Sender.TriggerWait() sn.Storage2.Orders.Sender.TriggerWait()
} }
@ -123,8 +183,8 @@ func getTotalProjectBandwidth(
sat.Accounting.Tally.Loop.TriggerWait() sat.Accounting.Tally.Loop.TriggerWait()
bandwidth, err := sat.DB.ProjectAccounting().GetProjectTotal(ctx, projectID, since, time.Now()) usage, err := sat.DB.ProjectAccounting().GetProjectTotal(ctx, projectID, since, time.Now())
require.NoError(t, err) require.NoError(t, err)
return bandwidth.Egress return usage
} }