Files
opencloud/services/storage-users/pkg/command/revisions_test.go
T
Pascal Bleser 498e5030e7 feat: 1419: let a dry run of the revisions cleanup keep a cache of its own
A dry run reads the metadata of every file it looks at. With a file
metadata cache shared with the server, e.g. 'nats-js-kv', it filled
that cache with entries for all of them, for a run that changes
nothing.

A dry run now always keeps its file metadata cache in its own memory,
whatever is configured. It doesn't need the shared cache: it deletes
nothing the server would have to see, and a cache of its own starts
empty, so it reads the metadata from disk. A real run still uses the
configured cache, which must be shared.

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

459 lines
20 KiB
Go

package command
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/opencloud-eu/reva/v3/pkg/storage/pkg/decomposedfs/revisioncleanup"
"github.com/test-go/testify/require"
"github.com/opencloud-eu/opencloud/pkg/shared"
opencloudstorage "github.com/opencloud-eu/opencloud/pkg/storage"
"github.com/opencloud-eu/opencloud/services/storage-users/pkg/config"
"github.com/opencloud-eu/opencloud/services/storage-users/pkg/config/defaults"
)
// revisionsCleanupOptionsFor parses the flags of the revisions cleanup command
func revisionsCleanupOptionsFor(t *testing.T, cfg *config.Config, args ...string) (revisionsCleanupOptions, error) {
t.Helper()
cmd, f := revisionsCleanupCommand(cfg)
require.NoError(t, cmd.ParseFlags(args))
return f.options(cfg, cmd.Flags().Changed)
}
func revisionsCleanupConfig() *config.Config {
return &config.Config{
Driver: "decomposed",
FilemetadataCache: config.FilemetadataCache{Store: "nats-js-kv"},
RevisionsCleanup: config.RevisionsCleanup{
Strategy: "auto, 90d",
Async: true,
AsyncWorkers: 4,
AsyncQueueSize: 100,
AsyncQueueFull: "drop",
AsyncQueueFullTimeout: 5 * time.Second,
},
}
}
func TestRevisionsCleanupOptions(t *testing.T) {
t.Run("the configuration is the default", func(t *testing.T) {
o, err := revisionsCleanupOptionsFor(t, revisionsCleanupConfig(), "--all")
require.NoError(t, err)
require.True(t, o.request.All)
require.Equal(t, "auto, 90d", o.request.Strategy)
// except for a full queue: the command waits for room as long as it takes
require.Equal(t, &revisioncleanup.AsyncOptions{Enabled: true, Workers: 4, QueueSize: 100, QueueFull: revisioncleanup.QueueFullBlock}, o.request.Async)
require.False(t, o.request.DryRun)
require.False(t, o.request.Shuffle)
require.Zero(t, o.timeout)
})
t.Run("flags override the configuration", func(t *testing.T) {
o, err := revisionsCleanupOptionsFor(t, revisionsCleanupConfig(),
"--space", "storage$space1!space1", "--space", "space2", "--strategy", "10",
"--async=false", "--workers", "0", "--queue-size", "7", "--queue-full", "drop", "--queue-full-timeout", "30s",
"--dry-run", "--shuffle", "--timeout", "1h", "--verbose", "--json")
require.NoError(t, err)
require.Equal(t, []string{"space1", "space2"}, o.request.SpaceIDs, "the space ids as the APIs show them are accepted")
require.Equal(t, "10", o.request.Strategy)
require.Equal(t, &revisioncleanup.AsyncOptions{Enabled: false, Workers: 0, QueueSize: 7, QueueFull: revisioncleanup.QueueFullDrop, QueueFullTimeout: 30 * time.Second}, o.request.Async)
require.True(t, o.request.DryRun)
require.True(t, o.request.Shuffle)
require.Equal(t, time.Hour, o.timeout)
require.True(t, o.verbose)
require.True(t, o.json)
})
t.Run("short flags", func(t *testing.T) {
o, err := revisionsCleanupOptionsFor(t, revisionsCleanupConfig(), "-anvj", "-s", "10", "-t", "30m")
require.NoError(t, err)
require.True(t, o.request.All)
require.True(t, o.request.DryRun)
require.True(t, o.verbose)
require.True(t, o.json)
require.Equal(t, "10", o.request.Strategy)
require.Equal(t, 30*time.Minute, o.timeout)
cfg := revisionsCleanupConfig()
cfg.Driver = "posix"
o, err = revisionsCleanupOptionsFor(t, cfg, "-p", "/var/lib/opencloud/users/einstein")
require.NoError(t, err)
require.Equal(t, "/var/lib/opencloud/users/einstein", o.request.FSPath)
})
t.Run("a file metadata cache local to each process", func(t *testing.T) {
for _, storeType := range []string{"memory", "ocmem"} {
cfg := revisionsCleanupConfig()
cfg.FilemetadataCache.Store = storeType
_, err := revisionsCleanupOptionsFor(t, cfg, "--all")
require.Error(t, err, storeType)
require.Contains(t, err.Error(), "'nats-js-kv'", "the error names the fix")
require.Contains(t, err.Error(), "--allow-local-cache", "the error names the override")
_, err = revisionsCleanupOptionsFor(t, cfg, "--all", "--allow-local-cache")
require.NoError(t, err, "with the override, %s", storeType)
_, err = revisionsCleanupOptionsFor(t, cfg, "--all", "--dry-run")
require.NoError(t, err, "a dry run deletes nothing, %s", storeType)
}
for _, storeType := range []string{"nats-js-kv", "redis-sentinel", "noop"} {
cfg := revisionsCleanupConfig()
cfg.FilemetadataCache.Store = storeType
_, err := revisionsCleanupOptionsFor(t, cfg, "--all")
require.NoError(t, err, storeType)
}
})
t.Run("a path within a space", func(t *testing.T) {
o, err := revisionsCleanupOptionsFor(t, revisionsCleanupConfig(), "--space", "space1", "--path", "/Documents")
require.NoError(t, err)
require.Equal(t, "/Documents", o.request.Path)
})
t.Run("a path on disk with posix", func(t *testing.T) {
cfg := revisionsCleanupConfig()
cfg.Driver = "posix"
o, err := revisionsCleanupOptionsFor(t, cfg, "--fs-path", "/var/lib/opencloud/users/einstein")
require.NoError(t, err)
require.Equal(t, "/var/lib/opencloud/users/einstein", o.request.FSPath)
})
for _, tc := range []struct {
name string
strategy string
args []string
err string
}{
{name: "nothing to clean up", err: "no space"},
{name: "the id of a file as a space", args: []string{"--space", "storage$space1!node1"}, err: "it is the id of a file or folder within space \"space1\""},
{name: "a storage id without a space id", args: []string{"--space", "storage$"}, err: "it has no space id"},
{name: "a file id without a space id", args: []string{"--space", "storage$!node1"}, err: "it has no space id"},
{name: "a path without a space", args: []string{"--path", "/Documents"}, err: "--path requires --space"},
{name: "all and a space", args: []string{"--all", "--space", "space1"}, err: "not both"},
{name: "a path in two spaces", args: []string{"--space", "space1", "--space", "space2", "--path", "/Documents"}, err: "exactly one space"},
{name: "a path on disk and a space", args: []string{"--fs-path", "/data", "--space", "space1"}, err: "can't be combined"},
{name: "a path on disk without posix", args: []string{"--fs-path", "/data"}, err: "only supported by the 'posix' driver"},
{name: "the cleanup is disabled", strategy: "disabled", args: []string{"--all"}, err: "--strategy"},
{name: "an invalid strategy", args: []string{"--all", "--strategy", "whenever"}, err: "invalid strategy"},
{name: "a negative timeout", args: []string{"--all", "--timeout", "-1s"}, err: "invalid timeout"},
} {
t.Run("rejects "+tc.name, func(t *testing.T) {
cfg := revisionsCleanupConfig()
if tc.strategy != "" {
cfg.RevisionsCleanup.Strategy = tc.strategy
}
_, err := revisionsCleanupOptionsFor(t, cfg, tc.args...)
require.Error(t, err)
require.Contains(t, err.Error(), tc.err)
})
}
}
// fakeOnDemand reports the given results, and then waits for the end of ctx if wait is set
type fakeOnDemand struct {
results []revisioncleanup.Result
wait bool
err error
request revisioncleanup.Request
}
func (f *fakeOnDemand) CleanupRevisionsUnchecked(ctx context.Context, req revisioncleanup.Request, report func(revisioncleanup.Result)) error {
f.request = req
for _, r := range f.results {
r.DryRun = req.DryRun
report(r)
}
if f.wait {
<-ctx.Done()
return ctx.Err()
}
return f.err
}
var (
revisionsCleanupTime = time.Date(2026, 10, 14, 10, 0, 0, 0, time.UTC)
// a file with a deleted and a kept revision
cleanedFile = revisioncleanup.Result{SpaceID: "space1", NodeID: "node1", Path: "/Documents/report.odt", Revisions: []revisioncleanup.RevisionResult{
{Key: "node1.REV.1", Time: revisionsCleanupTime, Size: 1536, Decision: revisioncleanup.Kept, Reason: "kept by \"auto\""},
{Key: "node1.REV.2", Time: revisionsCleanupTime.Add(-100 * 24 * time.Hour), Size: 2048, Decision: revisioncleanup.Deleted, Reason: "deleted by \"90d\""},
}}
// a file with nothing to delete
untouchedFile = revisioncleanup.Result{SpaceID: "space1", NodeID: "node2", Path: "/notes.txt", Revisions: []revisioncleanup.RevisionResult{
{Key: "node2.REV.1", Time: revisionsCleanupTime, Size: 10, Decision: revisioncleanup.Kept, Reason: "kept by \"auto\""},
}}
skippedFile = revisioncleanup.Result{SpaceID: "space2", NodeID: "node3", Path: "/upload.bin", Skipped: "an upload to the file is still being processed"}
failedFile = revisioncleanup.Result{SpaceID: "space2", NodeID: "node4", Path: "/broken.txt", Err: errors.New("could not list revisions")}
)
func runFakeRevisionsCleanup(t *testing.T, f *fakeOnDemand, o revisionsCleanupOptions) (int, string, error) {
t.Helper()
if o.request.Async == nil {
o.request = revisioncleanup.Request{All: true, Strategy: "auto, 90d", DryRun: o.request.DryRun}
}
var out bytes.Buffer
code, err := cleanupRevisions(context.Background(), f, o, &out)
return code, out.String(), err
}
func TestRevisionsCleanupOutput(t *testing.T) {
t.Run("only the files that changed or need attention, and a summary", func(t *testing.T) {
code, out, err := runFakeRevisionsCleanup(t, &fakeOnDemand{results: []revisioncleanup.Result{cleanedFile, untouchedFile, skippedFile}}, revisionsCleanupOptions{})
require.NoError(t, err)
require.Equal(t, 0, code)
require.Equal(t, `space1 /Documents/report.odt: deleted 1 of 2 revisions (2.0 KiB)
space2 /upload.bin: skipped: an upload to the file is still being processed
3 files in 2 spaces: deleted 1 revision (2.0 KiB) of 1 file, 1 skipped, 0 failed
`, out)
})
t.Run("a dry run", func(t *testing.T) {
o := revisionsCleanupOptions{request: revisioncleanup.Request{DryRun: true}}
code, out, err := runFakeRevisionsCleanup(t, &fakeOnDemand{results: []revisioncleanup.Result{cleanedFile}}, o)
require.NoError(t, err)
require.Equal(t, 0, code)
require.Contains(t, out, "space1 /Documents/report.odt: would delete 1 of 2 revisions")
require.Contains(t, out, "would delete 1 revision (2.0 KiB) of 1 file")
})
t.Run("every file and revision with --verbose", func(t *testing.T) {
code, out, err := runFakeRevisionsCleanup(t, &fakeOnDemand{results: []revisioncleanup.Result{cleanedFile, untouchedFile}}, revisionsCleanupOptions{verbose: true})
require.NoError(t, err)
require.Equal(t, 0, code)
// the times are in the local time zone, like the ones in the reasons
local := func(t time.Time) string { return t.Local().Format(time.RFC3339) }
require.Equal(t, `space1 /Documents/report.odt (node node1): deleted 1 of 2 revisions (2.0 KiB)
kept `+local(revisionsCleanupTime)+` 1.5 KiB kept by "auto"
deleted `+local(revisionsCleanupTime.Add(-100*24*time.Hour))+` 2.0 KiB deleted by "90d"
space1 /notes.txt (node node2): deleted 0 of 1 revision (0 B)
kept `+local(revisionsCleanupTime)+` 10 B kept by "auto"
2 files in 1 space: deleted 1 revision (2.0 KiB) of 1 file, 0 skipped, 0 failed
`, out)
})
t.Run("JSON lines", func(t *testing.T) {
code, out, err := runFakeRevisionsCleanup(t, &fakeOnDemand{results: []revisioncleanup.Result{cleanedFile, untouchedFile, failedFile}}, revisionsCleanupOptions{json: true})
require.NoError(t, err)
require.Equal(t, exitFailed, code)
lines := strings.Split(strings.TrimSpace(out), "\n")
require.Len(t, lines, 3, "the untouched file is left out without --verbose")
var file fileJSON
require.NoError(t, json.Unmarshal([]byte(lines[0]), &file))
require.Equal(t, fileJSON{Type: "file", SpaceID: "space1", NodeID: "node1", Path: "/Documents/report.odt", Revisions: 2, Deleted: 1, DeletedBytes: 2048}, file)
require.NoError(t, json.Unmarshal([]byte(lines[1]), &file))
require.Equal(t, "could not list revisions", file.Error)
var summary summaryJSON
require.NoError(t, json.Unmarshal([]byte(lines[2]), &summary))
require.Equal(t, summaryJSON{Type: "summary", Spaces: 2, Files: 3, Cleaned: 1, Deleted: 1, DeletedBytes: 2048, Failed: 1}, summary)
})
t.Run("JSON lines with the revisions with --verbose", func(t *testing.T) {
_, out, err := runFakeRevisionsCleanup(t, &fakeOnDemand{results: []revisioncleanup.Result{cleanedFile}}, revisionsCleanupOptions{json: true, verbose: true})
require.NoError(t, err)
var file fileJSON
require.NoError(t, json.Unmarshal([]byte(strings.Split(out, "\n")[0]), &file))
require.Len(t, file.Details, 2)
require.Equal(t, revisionJSON{Key: "node1.REV.2", Time: revisionsCleanupTime.Add(-100 * 24 * time.Hour), Size: 2048, Decision: "deleted", Reason: "deleted by \"90d\""}, file.Details[1])
})
}
func TestRevisionsCleanupExitCodes(t *testing.T) {
t.Run("a failed file", func(t *testing.T) {
code, out, err := runFakeRevisionsCleanup(t, &fakeOnDemand{results: []revisioncleanup.Result{cleanedFile, failedFile}}, revisionsCleanupOptions{})
require.NoError(t, err)
require.Equal(t, exitFailed, code)
require.Contains(t, out, "space2 /broken.txt: failed: could not list revisions")
})
t.Run("a revision that could not be deleted", func(t *testing.T) {
file := cleanedFile
file.Revisions = append([]revisioncleanup.RevisionResult{}, cleanedFile.Revisions...)
file.Revisions[1].Err = errors.New("permission denied")
code, out, err := runFakeRevisionsCleanup(t, &fakeOnDemand{results: []revisioncleanup.Result{file}}, revisionsCleanupOptions{})
require.NoError(t, err)
require.Equal(t, exitFailed, code)
require.Contains(t, out, "deleted 0 of 2 revisions (0 B), 1 could not be deleted\n node1.REV.2: permission denied\n")
})
t.Run("the cleanup could not run", func(t *testing.T) {
code, _, err := runFakeRevisionsCleanup(t, &fakeOnDemand{err: errors.New("could not find space space1")}, revisionsCleanupOptions{})
require.Error(t, err)
require.Contains(t, err.Error(), "could not find space")
require.Equal(t, 0, code, "the error itself makes the command fail")
})
t.Run("stopped by the timeout", func(t *testing.T) {
code, out, err := runFakeRevisionsCleanup(t, &fakeOnDemand{results: []revisioncleanup.Result{cleanedFile}, wait: true}, revisionsCleanupOptions{timeout: 10 * time.Millisecond})
require.NoError(t, err)
require.Equal(t, exitStopped, code)
require.Contains(t, out, "The cleanup was stopped after the timeout of 10ms, before all files were cleaned up.")
})
t.Run("a context error of the storage is an error, not the timeout", func(t *testing.T) {
// e.g. NATS that doesn't answer in time
storageErr := fmt.Errorf("could not get the id: %w", context.DeadlineExceeded)
code, out, err := runFakeRevisionsCleanup(t, &fakeOnDemand{err: storageErr}, revisionsCleanupOptions{timeout: time.Hour})
require.True(t, errors.Is(err, storageErr), "expected the error of the storage, got %v", err)
require.Equal(t, 0, code, "the error itself makes the command fail")
require.NotContains(t, out, "timeout")
})
t.Run("interrupted while a timeout is set", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
time.AfterFunc(10*time.Millisecond, cancel)
var out bytes.Buffer
o := revisionsCleanupOptions{timeout: time.Hour, request: revisioncleanup.Request{All: true, Strategy: "auto"}}
code, err := cleanupRevisions(ctx, &fakeOnDemand{wait: true}, o, &out)
require.NoError(t, err)
require.Equal(t, exitFailed, code)
require.Contains(t, out.String(), "interrupted")
})
t.Run("stopped by the timeout, with a failed file", func(t *testing.T) {
code, _, err := runFakeRevisionsCleanup(t, &fakeOnDemand{results: []revisioncleanup.Result{failedFile}, wait: true}, revisionsCleanupOptions{timeout: 10 * time.Millisecond})
require.NoError(t, err)
require.Equal(t, exitFailed, code)
})
}
func TestFormatBytes(t *testing.T) {
for in, want := range map[uint64]string{0: "0 B", 1023: "1023 B", 1024: "1.0 KiB", 1536: "1.5 KiB", 5 << 20: "5.0 MiB", 3 << 40: "3.0 TiB"} {
require.Equal(t, want, formatBytes(in))
}
}
func TestNewEventStream(t *testing.T) {
cfg := &config.Config{}
cfg.Events.Addr = "127.0.0.1:9233"
// blocked stands for a NATS that doesn't answer: newStream returns only when the test ends
blocked := func(t *testing.T) func(*config.Config) (string, error) {
release := make(chan struct{})
t.Cleanup(func() { close(release) })
return func(*config.Config) (string, error) {
<-release
return "", errors.New("released")
}
}
t.Run("returns the stream once connected", func(t *testing.T) {
stream, err := newEventStream(context.Background(), cfg, time.Second, func(*config.Config) (string, error) {
return "stream", nil
})
require.NoError(t, err)
require.Equal(t, "stream", stream)
})
t.Run("returns the error of the stream", func(t *testing.T) {
_, err := newEventStream(context.Background(), cfg, time.Second, func(*config.Config) (string, error) {
return "", errors.New("bad credentials")
})
require.EqualError(t, err, "bad credentials")
})
t.Run("gives up after the timeout, and says what it needs", func(t *testing.T) {
start := time.Now()
_, err := newEventStream(context.Background(), cfg, 50*time.Millisecond, blocked(t))
require.Error(t, err)
require.Contains(t, err.Error(), "could not connect to NATS at 127.0.0.1:9233 within 50ms")
require.Contains(t, err.Error(), "a running OpenCloud server or a separate NATS")
require.True(t, time.Since(start) < 5*time.Second, "it should give up long before 5s")
})
t.Run("stops when interrupted", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
time.AfterFunc(50*time.Millisecond, cancel)
_, err := newEventStream(ctx, cfg, time.Minute, blocked(t))
require.True(t, errors.Is(err, context.Canceled), "expected context.Canceled, got %v", err)
require.Contains(t, err.Error(), "interrupted while connecting to NATS")
})
}
func TestStorageOwnerWarning(t *testing.T) {
root := t.TempDir()
t.Run("none for the owner of the storage", func(t *testing.T) {
require.Empty(t, storageOwnerWarning(root, os.Geteuid()))
})
t.Run("another user", func(t *testing.T) {
warning := storageOwnerWarning(root, os.Geteuid()+1)
require.Contains(t, warning, "the storage "+root+" belongs to the user ")
require.Contains(t, warning, "sudo -u")
})
t.Run("none without a storage", func(t *testing.T) {
require.Empty(t, storageOwnerWarning(filepath.Join(root, "missing"), os.Geteuid()+1))
})
}
// TestRevisionsCleanupDriverConfig doesn't create the drivers: the metadata prefix is
// process wide, and the legacy prefix of the aliases would clash with the prefix that
// TestDeleteStaleNodesHonoursMetadataPrefix fixes for the whole package.
func TestRevisionsCleanupDriverConfig(t *testing.T) {
newConfig := func(driver string) *config.Config {
cfg := defaults.FullDefaultConfig()
cfg.Commons = &shared.Commons{GRPCClientTLS: &shared.GRPCClientTLS{}}
cfg.Driver = driver
return cfg
}
for _, tc := range []struct{ driver, effective, prefix string }{
// the configured prefix, empty by default: then reva uses its own default
{"decomposed", "decomposed", ""},
{"decomposeds3", "decomposeds3", ""},
// the deprecated driver names are aliases, see revisionsCleanupDriverConfig
{"ocis", "decomposed", opencloudstorage.LegacyMetadataPrefix()},
{"s3ng", "decomposeds3", opencloudstorage.LegacyMetadataPrefix()},
} {
t.Run(tc.driver, func(t *testing.T) {
driver, driverConfig, err := revisionsCleanupDriverConfig(newConfig(tc.driver), false)
require.NoError(t, err)
require.Equal(t, tc.effective, driver)
require.Equal(t, tc.prefix, driverConfig["metadata_prefix"])
require.Equal(t, false, driverConfig["asyncfileuploads"])
})
}
t.Run("posix neither scans nor watches", func(t *testing.T) {
driver, driverConfig, err := revisionsCleanupDriverConfig(newConfig("posix"), false)
require.NoError(t, err)
require.Equal(t, "posix", driver)
require.Equal(t, false, driverConfig["scan_fs"])
require.Equal(t, false, driverConfig["watch_fs"])
require.Equal(t, false, driverConfig["asyncfileuploads"])
})
t.Run("a dry run keeps a file metadata cache of its own", func(t *testing.T) {
for _, driver := range []string{"decomposed", "decomposeds3", "posix"} {
cfg := newConfig(driver)
cfg.FilemetadataCache.Store = "nats-js-kv"
_, driverConfig, err := revisionsCleanupDriverConfig(cfg, true)
require.NoError(t, err, driver)
require.Equal(t, "memory", driverConfig["filemetadatacache"].(map[string]any)["cache_store"], driver)
_, driverConfig, err = revisionsCleanupDriverConfig(cfg, false)
require.NoError(t, err, driver)
require.Equal(t, "nats-js-kv", driverConfig["filemetadatacache"].(map[string]any)["cache_store"], "a real run uses the configured cache, %s", driver)
}
})
t.Run("unknown driver", func(t *testing.T) {
_, _, err := revisionsCleanupDriverConfig(newConfig("nonexistent"), false)
require.EqualError(t, err, `unknown storage driver "nonexistent"`)
})
}