satellite/accounting: test if server-side copy affects bandwidth

Doing server-side copy operation should not affect user monthly
bandwidth. This test covers that case.

Fixes https://github.com/storj/storj/issues/4717

Change-Id: I84ffab96b84851f395ea3a34d88f7dba424ec440
This commit is contained in:
Michał Niewrzał 2022-05-09 11:37:28 +02:00 committed by Michal Niewrzal
parent 87ea2a4794
commit 7b253075bb

View File

@ -1209,3 +1209,89 @@ func TestProjectUsage_BandwidthDeadAllocation(t *testing.T) {
require.Equal(t, initialBandwidthUsage, updatedBandwidthUsage+dead)
})
}
// Check if doing a copy is not affecting monthly bandwidth usage. Doing
// server-side copy should not reduce users monthly bandwidth limit.
func TestProjectBandwidthUsageWithCopies(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) {
// this effectively disable live accounting cache
config.LiveAccounting.BandwidthCacheTTL = -1
config.LiveAccounting.AsOfSystemInterval = 0
},
},
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
testObjects := map[string]memory.Size{
"inline": 1 * memory.KiB,
"remote": 10 * memory.KiB,
}
for name, size := range testObjects {
expectedData := testrand.Bytes(size)
err := planet.Uplinks[0].Upload(ctx, planet.Satellites[0], "testbucket", name, expectedData)
require.NoError(t, err)
data, err := planet.Uplinks[0].Download(ctx, planet.Satellites[0], "testbucket", name)
require.NoError(t, err)
require.Equal(t, data, expectedData)
}
// flush allocated bandwidth to DB as we will use DB directly without cache
planet.Satellites[0].Orders.Chore.Loop.TriggerWait()
projectID := planet.Uplinks[0].Projects[0].ID
now := time.Now()
expectedBandwidth, err := planet.Satellites[0].DB.ProjectAccounting().GetProjectBandwidth(ctx, projectID, now.Year(), now.Month(), now.Day(), 0)
require.NoError(t, err)
require.NotZero(t, expectedBandwidth)
project, err := planet.Uplinks[0].OpenProject(ctx, planet.Satellites[0])
require.NoError(t, err)
defer ctx.Check(project.Close)
// make copies
for name := range testObjects {
_, err = project.CopyObject(ctx, "testbucket", name, "testbucket", name+"copy", nil)
require.NoError(t, err)
}
// flush allocated bandwidth to DB as we will use DB directly without cache
planet.Satellites[0].Orders.Chore.Loop.TriggerWait()
bandwidth, err := planet.Satellites[0].DB.ProjectAccounting().GetProjectBandwidth(ctx, projectID, now.Year(), now.Month(), now.Day(), 0)
require.NoError(t, err)
require.Equal(t, expectedBandwidth, bandwidth)
// download copies to verify that used bandwidth will be increased
for name := range testObjects {
_, err = planet.Uplinks[0].Download(ctx, planet.Satellites[0], "testbucket", name+"copy")
require.NoError(t, err)
}
// flush allocated bandwidth to DB as we will use DB directly without cache
planet.Satellites[0].Orders.Chore.Loop.TriggerWait()
bandwidth, err = planet.Satellites[0].DB.ProjectAccounting().GetProjectBandwidth(ctx, projectID, now.Year(), now.Month(), now.Day(), 0)
require.NoError(t, err)
require.Less(t, expectedBandwidth, bandwidth)
// last bandwidth read becomes new expectedBandwidth
expectedBandwidth = bandwidth
// delete ancestors
for name := range testObjects {
_, err = project.DeleteObject(ctx, "testbucket", name)
require.NoError(t, err)
}
// flush allocated bandwidth to DB as we will use DB directly without cache
planet.Satellites[0].Orders.Chore.Loop.TriggerWait()
bandwidth, err = planet.Satellites[0].DB.ProjectAccounting().GetProjectBandwidth(ctx, projectID, now.Year(), now.Month(), now.Day(), 0)
require.NoError(t, err)
require.Equal(t, expectedBandwidth, bandwidth)
})
}