Files
opencloud/services/storage-users/pkg/revaconfig/drivers_test.go
T
Pascal Bleser 7676730860 feat: 1419: configure the revision cleanup of the storage-users drivers
Wire reva's revision cleanup options into the 'decomposed', 'decomposeds3'
and 'posix' drivers, in the storage provider and in the data provider.

The options are a single 'revisions_cleanup' section of the storage-users
configuration, next to 'filemetadata_cache' and 'id_cache', as only one
driver is used at a time. Its environment variables:

- STORAGE_USERS_REVISIONS_CLEANUP_STRATEGY: which old revisions to
  delete whenever a file gets a new revision, e.g. 'auto', '30d' or
  '7d, auto, 90d'
- STORAGE_USERS_REVISIONS_CLEANUP_ASYNC: run the cleanup in background
  workers
- STORAGE_USERS_REVISIONS_CLEANUP_ASYNC_WORKERS: number of background
  workers
- STORAGE_USERS_REVISIONS_CLEANUP_ASYNC_QUEUE_SIZE: number of files that
  can wait
- STORAGE_USERS_REVISIONS_CLEANUP_ASYNC_QUEUE_FULL: 'drop' or 'block'
- STORAGE_USERS_REVISIONS_CLEANUP_ASYNC_QUEUE_FULL_TIMEOUT: how long
  'block' waits for room at most, 0 for as long as it takes

The strategy defaults to 'disabled', which keeps all revisions as before.
The other defaults are reva's: background workers, with the number of
workers and the queue size determined automatically, dropping the
cleanup of a file when the queue is full (it is cleaned up with its
next revision), and a timeout of 5s for 'block'.

Other commands that create a driver, 'posixfs scan' and 'uploads
sessions', clean up revisions without background workers: they could
exit before the workers are done, and stop the cleanup of a file
midway.

Assisted-by: Claude Code:claude-opus-5-5
2026-11-23 23:59:43 +01:00

116 lines
4.7 KiB
Go

package revaconfig
import (
"errors"
"testing"
"time"
"github.com/opencloud-eu/reva/v3/pkg/storage/pkg/decomposedfs/options"
"github.com/test-go/testify/require"
"github.com/opencloud-eu/opencloud/pkg/config/envdecode"
"github.com/opencloud-eu/opencloud/pkg/shared"
"github.com/opencloud-eu/opencloud/pkg/storage"
"github.com/opencloud-eu/opencloud/services/storage-users/pkg/config/defaults"
)
func TestMetadataPrefixReachesAllDrivers(t *testing.T) {
t.Setenv("OC_METADATA_PREFIX", "user.foreign.")
cfg := defaults.FullDefaultConfig()
cfg.Commons = &shared.Commons{GRPCClientTLS: &shared.GRPCClientTLS{}}
require.NoError(t, envdecode.Decode(cfg))
for name, driver := range map[string]map[string]any{
"posix": Posix(cfg, false, false),
"decomposed": Decomposed(cfg),
"decomposed no events": DecomposedNoEvents(cfg),
"decomposeds3": DecomposedS3(cfg),
"decomposeds3 no events": DecomposedS3NoEvents(cfg),
} {
require.Equal(t, "user.foreign.", driver["metadata_prefix"], name)
}
}
func TestLegacyDriverAliases(t *testing.T) {
for _, tc := range []struct{ legacy, replacement string }{
{"ocis", "decomposed"},
{"s3ng", "decomposeds3"},
} {
t.Run(tc.legacy, func(t *testing.T) {
cfg := defaults.FullDefaultConfig()
cfg.Commons = &shared.Commons{GRPCClientTLS: &shared.GRPCClientTLS{}}
cfg.GRPC.TLS = &shared.GRPCServiceTLS{}
cfg.Driver = tc.legacy
cfg.Drivers.Decomposed.MetadataPrefix = "user.other."
cfg.Drivers.DecomposedS3.MetadataPrefix = "user.other."
rcfg := StorageUsersConfigFromStruct(cfg)
// Both Reva services must resolve the alias to the replacement driver and
// use the legacy prefix instead of the configured one.
for _, service := range []struct{ protocol, name string }{{"grpc", "storageprovider"}, {"http", "dataprovider"}} {
services := rcfg[service.protocol].(map[string]any)["services"].(map[string]any)
c := services[service.name].(map[string]any)
require.Equal(t, tc.replacement, c["driver"])
drivers := c["drivers"].(map[string]any)
driver := drivers[tc.replacement].(map[string]any)
require.Equal(t, storage.LegacyMetadataPrefix(), driver["metadata_prefix"])
_, present := drivers[tc.legacy]
require.False(t, present)
}
// Resolving an alias must not change the original OpenCloud config.
require.Equal(t, "user.other.", cfg.Drivers.Decomposed.MetadataPrefix)
require.Equal(t, "user.other.", cfg.Drivers.DecomposedS3.MetadataPrefix)
})
}
}
func TestRevisionsCleanupReachesAllDrivers(t *testing.T) {
drivers := func(t *testing.T) map[string]map[string]any {
cfg := defaults.FullDefaultConfig()
cfg.Commons = &shared.Commons{GRPCClientTLS: &shared.GRPCClientTLS{}}
if err := envdecode.Decode(cfg); !errors.Is(err, envdecode.ErrNoTargetFieldsAreSet) {
require.NoError(t, err)
}
return map[string]map[string]any{
"posix": Posix(cfg, false, false),
"decomposed": Decomposed(cfg),
"decomposed no events": DecomposedNoEvents(cfg),
"decomposeds3": DecomposedS3(cfg),
"decomposeds3 no events": DecomposedS3NoEvents(cfg),
}
}
t.Run("defaults keep all revisions", func(t *testing.T) {
for name, driver := range drivers(t) {
o, err := options.New(driver)
require.NoError(t, err, name)
require.Equal(t, "disabled", o.RevisionsCleanupStrategy, name)
require.True(t, o.RevisionsCleanupAsync, name)
require.Equal(t, 0, o.RevisionsCleanupAsyncWorkers, name)
require.Equal(t, 0, o.RevisionsCleanupAsyncQueueSize, name)
require.Equal(t, "drop", o.RevisionsCleanupAsyncQueueFull, name)
require.Equal(t, 5*time.Second, o.RevisionsCleanupAsyncQueueFullTimeout, name)
}
})
t.Run("environment variables", func(t *testing.T) {
t.Setenv("STORAGE_USERS_REVISIONS_CLEANUP_STRATEGY", "7d, auto, 90d")
t.Setenv("STORAGE_USERS_REVISIONS_CLEANUP_ASYNC", "false")
t.Setenv("STORAGE_USERS_REVISIONS_CLEANUP_ASYNC_WORKERS", "3")
t.Setenv("STORAGE_USERS_REVISIONS_CLEANUP_ASYNC_QUEUE_SIZE", "42")
t.Setenv("STORAGE_USERS_REVISIONS_CLEANUP_ASYNC_QUEUE_FULL", "block")
t.Setenv("STORAGE_USERS_REVISIONS_CLEANUP_ASYNC_QUEUE_FULL_TIMEOUT", "1m")
for name, driver := range drivers(t) {
// decoded by reva, so that a key it does not know would show up as a default value
o, err := options.New(driver)
require.NoError(t, err, name)
require.Equal(t, "7d, auto, 90d", o.RevisionsCleanupStrategy, name)
require.False(t, o.RevisionsCleanupAsync, name)
require.Equal(t, 3, o.RevisionsCleanupAsyncWorkers, name)
require.Equal(t, 42, o.RevisionsCleanupAsyncQueueSize, name)
require.Equal(t, "block", o.RevisionsCleanupAsyncQueueFull, name)
require.Equal(t, time.Minute, o.RevisionsCleanupAsyncQueueFullTimeout, name)
}
})
}