2020-04-15 20:20:16 +01:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package expireddeletion_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/common/memory"
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/testrand"
|
|
|
|
"storj.io/storj/private/testplanet"
|
|
|
|
"storj.io/storj/satellite"
|
2020-11-30 12:33:06 +00:00
|
|
|
"storj.io/storj/satellite/metainfo/metabase"
|
2020-04-15 20:20:16 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestExpiredDeletion(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.ExpiredDeletion.Interval = 500 * time.Millisecond
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
upl := planet.Uplinks[0]
|
|
|
|
expiredChore := satellite.Core.ExpiredDeletion.Chore
|
|
|
|
|
2020-11-30 12:33:06 +00:00
|
|
|
expiredChore.Loop.Pause()
|
2020-04-15 20:20:16 +01:00
|
|
|
|
2020-11-30 12:33:06 +00:00
|
|
|
// Upload four objects:
|
|
|
|
// 1. Inline object without expiraton date
|
|
|
|
err := upl.Upload(ctx, satellite, "testbucket", "inline_no_expire", testrand.Bytes(1*memory.KiB))
|
2020-04-15 20:20:16 +01:00
|
|
|
require.NoError(t, err)
|
2020-11-30 12:33:06 +00:00
|
|
|
// 2. Remote object without expiraton date
|
|
|
|
err = upl.Upload(ctx, satellite, "testbucket", "remote_no_expire", testrand.Bytes(8*memory.KiB))
|
2020-04-15 20:20:16 +01:00
|
|
|
require.NoError(t, err)
|
2020-11-30 12:33:06 +00:00
|
|
|
// 3. Inline object with expiraton date
|
|
|
|
err = upl.UploadWithExpiration(ctx, satellite, "testbucket", "inline_expire", testrand.Bytes(1*memory.KiB), time.Now().Add(1*time.Hour))
|
2020-04-15 20:20:16 +01:00
|
|
|
require.NoError(t, err)
|
2020-11-30 12:33:06 +00:00
|
|
|
// 4. Remote object with expiraton date
|
|
|
|
err = upl.UploadWithExpiration(ctx, satellite, "testbucket", "remote_expire", testrand.Bytes(8*memory.KiB), time.Now().Add(1*time.Hour))
|
2020-04-15 20:20:16 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-11-30 12:33:06 +00:00
|
|
|
// Verify that all four objects are in the metabase
|
|
|
|
count := 0
|
|
|
|
err = satellite.Metainfo.Metabase.IterateObjectsAllVersions(ctx,
|
|
|
|
metabase.IterateObjects{
|
|
|
|
ProjectID: upl.Projects[0].ID,
|
|
|
|
BucketName: "testbucket",
|
|
|
|
Status: metabase.Committed,
|
|
|
|
}, func(ctx context.Context, it metabase.ObjectsIterator) error {
|
|
|
|
for it.Next(ctx, &metabase.ObjectEntry{}) {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
2020-04-15 20:20:16 +01:00
|
|
|
require.NoError(t, err)
|
2020-11-30 12:33:06 +00:00
|
|
|
require.EqualValues(t, 4, count)
|
2020-04-15 20:20:16 +01:00
|
|
|
|
2020-11-30 12:33:06 +00:00
|
|
|
// Trigger the next iteration of expired cleanup and wait to finish
|
|
|
|
expiredChore.SetNow(func() time.Time {
|
|
|
|
// Set the Now function to return time after the objects expiration time
|
|
|
|
return time.Now().Add(2 * time.Hour)
|
|
|
|
})
|
2020-04-15 20:20:16 +01:00
|
|
|
expiredChore.Loop.TriggerWait()
|
|
|
|
|
2020-11-30 12:33:06 +00:00
|
|
|
// Verify that only two objects remain in the metabase
|
|
|
|
count = 0
|
|
|
|
err = satellite.Metainfo.Metabase.IterateObjectsAllVersions(ctx,
|
|
|
|
metabase.IterateObjects{
|
|
|
|
ProjectID: upl.Projects[0].ID,
|
|
|
|
BucketName: "testbucket",
|
|
|
|
Status: metabase.Committed,
|
|
|
|
}, func(ctx context.Context, it metabase.ObjectsIterator) error {
|
|
|
|
for it.Next(ctx, &metabase.ObjectEntry{}) {
|
|
|
|
count++
|
2020-04-15 20:20:16 +01:00
|
|
|
}
|
|
|
|
return nil
|
2020-11-30 12:33:06 +00:00
|
|
|
},
|
|
|
|
)
|
2020-04-15 20:20:16 +01:00
|
|
|
require.NoError(t, err)
|
2020-11-30 12:33:06 +00:00
|
|
|
require.EqualValues(t, 2, count)
|
2020-04-15 20:20:16 +01:00
|
|
|
})
|
|
|
|
}
|