storj/satellite/accounting/projectbwcleanup/chore_test.go
Bill Thorp 328004c0ef satellite/accounting: fix build - time rounding
We were seeing error on the last day of the month with TestProjectAllocatedBandwidthRetainTwo.
This is due to AddDate normalizes its result in the same way that Date does, so, for example,
adding one month to October 31 yields December 1, the normalized form for November 31."

I also fixed a minor UTC issue with this test as well.

Change-Id: I0157873e7befa57810e5f264a922b188890fa46a
2020-08-31 09:37:13 -04:00

88 lines
2.7 KiB
Go

// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package projectbwcleanup_test
import (
"testing"
"time"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"storj.io/common/pb"
"storj.io/common/testcontext"
"storj.io/common/testrand"
"storj.io/storj/private/testplanet"
"storj.io/storj/satellite"
)
var testBytes int64 = 5000
func TestProjectAllocatedBandwidthRetainNegative(t *testing.T) {
// -1 to validate we don't delete the current month
testProjectAllocatedBandwidthRetain(t, -1)
}
func TestProjectAllocatedBandwidthRetainZero(t *testing.T) {
// 0 to validate we don't delete the current month
testProjectAllocatedBandwidthRetain(t, 0)
}
func TestProjectAllocatedBandwidthRetainTwo(t *testing.T) {
testProjectAllocatedBandwidthRetain(t, 2)
}
func testProjectAllocatedBandwidthRetain(t *testing.T, retain int) {
testplanet.Run(t, testplanet.Config{
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 0,
Reconfigure: testplanet.Reconfigure{
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
config.ProjectBWCleanup.RetainMonths = retain
},
},
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
months := retain + 1 // number of months to retain + 1 for current month
// must have at least have the current month
if months < 1 {
months = 1
}
satellite := planet.Satellites[0]
satellite.Accounting.ProjectBWCleanup.Loop.Pause()
ordersDB := satellite.DB.Orders()
projectID := testrand.UUID()
bucketName := testrand.BucketName()
now := time.Now().UTC()
for i := 0; i <= months; i++ {
newDate := time.Date(now.Year(), now.Month()-time.Month(i), 15, 12, 0, 0, 0, time.UTC)
err := ordersDB.UpdateBucketBandwidthAllocation(ctx, projectID, []byte(bucketName), pb.PieceAction_GET, testBytes, newDate)
require.NoError(t, err)
}
for i := 0; i <= months; i++ {
newDate := time.Date(now.Year(), now.Month()-time.Month(i), 15, 12, 0, 0, 0, time.UTC)
bytes, err := satellite.Accounting.ProjectUsage.GetProjectAllocatedBandwidth(ctx, projectID, newDate.Year(), newDate.Month())
require.NoError(t, err)
require.EqualValues(t, testBytes, bytes)
}
satellite.Accounting.ProjectBWCleanup.Loop.TriggerWait()
for i := 0; i <= months; i++ {
newDate := time.Date(now.Year(), now.Month()-time.Month(i), 15, 12, 0, 0, 0, time.UTC)
bytes, err := satellite.Accounting.ProjectUsage.GetProjectAllocatedBandwidth(ctx, projectID, newDate.Year(), newDate.Month())
if i < months || retain < 0 { // there should always be the current month
require.NoError(t, err)
require.EqualValues(t, testBytes, bytes, "Month: %d", i)
} else {
require.NoError(t, err)
require.EqualValues(t, 0, bytes)
}
}
})
}