Module path changed from git.eeqj.de/sneak/vaultik to sneak.berlin/go/vaultik (vanity redirect). All imports, ldflags, Dockerfile, goreleaser config, and docs updated. App data/config directories now use plain "vaultik" instead of the reverse-DNS name. README: - New copy-pasteable quickstart at top: go install, config init, age keypair, config set for key + file:// destination, home backup - All command names in command details are code-quoted - config set/get gained sequence index support (age_recipients.0) so lists are settable from the CLI - Dockerfile build is CGO_ENABLED=0 to match the pure-Go build
257 lines
7.7 KiB
Go
257 lines
7.7 KiB
Go
package vaultik_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"database/sql"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"sneak.berlin/go/vaultik/internal/database"
|
|
"sneak.berlin/go/vaultik/internal/log"
|
|
"sneak.berlin/go/vaultik/internal/types"
|
|
"sneak.berlin/go/vaultik/internal/vaultik"
|
|
)
|
|
|
|
// setupPurgeTest creates a Vaultik instance with an in-memory database and mock
|
|
// storage pre-populated with the given snapshot IDs. Each snapshot is marked as
|
|
// completed. Remote metadata stubs are created so syncWithRemote keeps them.
|
|
func setupPurgeTest(t *testing.T, snapshotIDs []string) *vaultik.Vaultik {
|
|
t.Helper()
|
|
log.Initialize(log.Config{})
|
|
|
|
ctx := context.Background()
|
|
db, err := database.New(ctx, ":memory:")
|
|
require.NoError(t, err)
|
|
t.Cleanup(func() { _ = db.Close() })
|
|
|
|
repos := database.NewRepositories(db)
|
|
mockStorage := NewMockStorer()
|
|
|
|
// Insert each snapshot into the DB and create remote metadata stubs.
|
|
// Use timestamps parsed from snapshot IDs for realistic ordering.
|
|
for _, id := range snapshotIDs {
|
|
// Parse timestamp from the snapshot ID
|
|
parts := strings.Split(id, "_")
|
|
timestampStr := parts[len(parts)-1]
|
|
startedAt, err := time.Parse(time.RFC3339, timestampStr)
|
|
require.NoError(t, err, "parsing timestamp from snapshot ID %q", id)
|
|
|
|
completedAt := startedAt.Add(5 * time.Minute)
|
|
snap := &database.Snapshot{
|
|
ID: types.SnapshotID(id),
|
|
Hostname: "testhost",
|
|
VaultikVersion: "test",
|
|
StartedAt: startedAt,
|
|
CompletedAt: &completedAt,
|
|
}
|
|
err = repos.WithTx(ctx, func(ctx context.Context, tx *sql.Tx) error {
|
|
return repos.Snapshots.Create(ctx, tx, snap)
|
|
})
|
|
require.NoError(t, err, "creating snapshot %s", id)
|
|
|
|
// Create remote metadata stub so syncWithRemote keeps it
|
|
metadataKey := "metadata/" + id + "/manifest.json.zst"
|
|
err = mockStorage.Put(ctx, metadataKey, strings.NewReader("stub"))
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
stdout := &bytes.Buffer{}
|
|
stderr := &bytes.Buffer{}
|
|
stdin := &bytes.Buffer{}
|
|
|
|
v := &vaultik.Vaultik{
|
|
Storage: mockStorage,
|
|
Repositories: repos,
|
|
DB: db,
|
|
Stdout: stdout,
|
|
Stderr: stderr,
|
|
Stdin: stdin,
|
|
}
|
|
v.SetContext(ctx)
|
|
|
|
return v
|
|
}
|
|
|
|
// listRemainingSnapshots returns IDs of all completed snapshots in the database.
|
|
func listRemainingSnapshots(t *testing.T, v *vaultik.Vaultik) []string {
|
|
t.Helper()
|
|
ctx := context.Background()
|
|
dbSnaps, err := v.Repositories.Snapshots.ListRecent(ctx, 10000)
|
|
require.NoError(t, err)
|
|
|
|
var ids []string
|
|
for _, s := range dbSnaps {
|
|
if s.CompletedAt != nil {
|
|
ids = append(ids, s.ID.String())
|
|
}
|
|
}
|
|
return ids
|
|
}
|
|
|
|
func TestPurgeKeepLatest_PerName(t *testing.T) {
|
|
// Create snapshots for two different names: "home" and "system".
|
|
// With per-name --keep-latest, the latest of each should be kept.
|
|
snapshotIDs := []string{
|
|
"testhost_system_2026-01-01T00:00:00Z",
|
|
"testhost_home_2026-01-01T01:00:00Z",
|
|
"testhost_system_2026-01-01T02:00:00Z",
|
|
"testhost_home_2026-01-01T03:00:00Z",
|
|
"testhost_system_2026-01-01T04:00:00Z",
|
|
}
|
|
|
|
v := setupPurgeTest(t, snapshotIDs)
|
|
|
|
err := v.PurgeSnapshotsWithOptions(&vaultik.SnapshotPurgeOptions{
|
|
KeepLatest: true,
|
|
Force: true,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
remaining := listRemainingSnapshots(t, v)
|
|
|
|
// Should keep the latest of each name
|
|
assert.Len(t, remaining, 2, "should keep exactly 2 snapshots (one per name)")
|
|
assert.Contains(t, remaining, "testhost_system_2026-01-01T04:00:00Z", "should keep latest system")
|
|
assert.Contains(t, remaining, "testhost_home_2026-01-01T03:00:00Z", "should keep latest home")
|
|
}
|
|
|
|
func TestPurgeKeepLatest_SingleName(t *testing.T) {
|
|
// All snapshots have the same name — keep-latest should keep exactly one.
|
|
snapshotIDs := []string{
|
|
"testhost_home_2026-01-01T00:00:00Z",
|
|
"testhost_home_2026-01-01T01:00:00Z",
|
|
"testhost_home_2026-01-01T02:00:00Z",
|
|
}
|
|
|
|
v := setupPurgeTest(t, snapshotIDs)
|
|
|
|
err := v.PurgeSnapshotsWithOptions(&vaultik.SnapshotPurgeOptions{
|
|
KeepLatest: true,
|
|
Force: true,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
remaining := listRemainingSnapshots(t, v)
|
|
assert.Len(t, remaining, 1)
|
|
assert.Contains(t, remaining, "testhost_home_2026-01-01T02:00:00Z", "should keep the newest")
|
|
}
|
|
|
|
func TestPurgeKeepLatest_WithNameFilter(t *testing.T) {
|
|
// Use --name to filter purge to only "home" snapshots.
|
|
// "system" snapshots should be untouched.
|
|
snapshotIDs := []string{
|
|
"testhost_system_2026-01-01T00:00:00Z",
|
|
"testhost_home_2026-01-01T01:00:00Z",
|
|
"testhost_system_2026-01-01T02:00:00Z",
|
|
"testhost_home_2026-01-01T03:00:00Z",
|
|
"testhost_home_2026-01-01T04:00:00Z",
|
|
}
|
|
|
|
v := setupPurgeTest(t, snapshotIDs)
|
|
|
|
err := v.PurgeSnapshotsWithOptions(&vaultik.SnapshotPurgeOptions{
|
|
KeepLatest: true,
|
|
Force: true,
|
|
Names: []string{"home"},
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
remaining := listRemainingSnapshots(t, v)
|
|
|
|
// 2 system snapshots untouched + 1 latest home = 3
|
|
assert.Len(t, remaining, 3)
|
|
assert.Contains(t, remaining, "testhost_system_2026-01-01T00:00:00Z")
|
|
assert.Contains(t, remaining, "testhost_system_2026-01-01T02:00:00Z")
|
|
assert.Contains(t, remaining, "testhost_home_2026-01-01T04:00:00Z")
|
|
}
|
|
|
|
func TestPurgeKeepLatest_NoSnapshots(t *testing.T) {
|
|
v := setupPurgeTest(t, nil)
|
|
|
|
err := v.PurgeSnapshotsWithOptions(&vaultik.SnapshotPurgeOptions{
|
|
KeepLatest: true,
|
|
Force: true,
|
|
})
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func TestPurgeKeepLatest_NameFilterNoMatch(t *testing.T) {
|
|
snapshotIDs := []string{
|
|
"testhost_system_2026-01-01T00:00:00Z",
|
|
"testhost_system_2026-01-01T01:00:00Z",
|
|
}
|
|
|
|
v := setupPurgeTest(t, snapshotIDs)
|
|
|
|
err := v.PurgeSnapshotsWithOptions(&vaultik.SnapshotPurgeOptions{
|
|
KeepLatest: true,
|
|
Force: true,
|
|
Names: []string{"nonexistent"},
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
// All snapshots should remain — the name filter matched nothing
|
|
remaining := listRemainingSnapshots(t, v)
|
|
assert.Len(t, remaining, 2)
|
|
}
|
|
|
|
func TestPurgeOlderThan_WithNameFilter(t *testing.T) {
|
|
// Snapshots with different names and timestamps.
|
|
// --older-than should apply only to the named subset when --name is used.
|
|
snapshotIDs := []string{
|
|
"testhost_system_2020-01-01T00:00:00Z",
|
|
"testhost_home_2020-01-01T00:00:00Z",
|
|
"testhost_system_2026-01-01T00:00:00Z",
|
|
"testhost_home_2026-01-01T00:00:00Z",
|
|
}
|
|
|
|
v := setupPurgeTest(t, snapshotIDs)
|
|
|
|
// Purge only "home" snapshots older than 365 days
|
|
err := v.PurgeSnapshotsWithOptions(&vaultik.SnapshotPurgeOptions{
|
|
OlderThan: "365d",
|
|
Force: true,
|
|
Names: []string{"home"},
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
remaining := listRemainingSnapshots(t, v)
|
|
|
|
// Old system stays (not filtered by name), old home deleted, recent ones stay
|
|
assert.Len(t, remaining, 3)
|
|
assert.Contains(t, remaining, "testhost_system_2020-01-01T00:00:00Z")
|
|
assert.Contains(t, remaining, "testhost_system_2026-01-01T00:00:00Z")
|
|
assert.Contains(t, remaining, "testhost_home_2026-01-01T00:00:00Z")
|
|
}
|
|
|
|
func TestPurgeKeepLatest_ThreeNames(t *testing.T) {
|
|
// Three different snapshot names with multiple snapshots each.
|
|
snapshotIDs := []string{
|
|
"testhost_home_2026-01-01T00:00:00Z",
|
|
"testhost_system_2026-01-01T01:00:00Z",
|
|
"testhost_media_2026-01-01T02:00:00Z",
|
|
"testhost_home_2026-01-01T03:00:00Z",
|
|
"testhost_system_2026-01-01T04:00:00Z",
|
|
"testhost_media_2026-01-01T05:00:00Z",
|
|
"testhost_home_2026-01-01T06:00:00Z",
|
|
}
|
|
|
|
v := setupPurgeTest(t, snapshotIDs)
|
|
|
|
err := v.PurgeSnapshotsWithOptions(&vaultik.SnapshotPurgeOptions{
|
|
KeepLatest: true,
|
|
Force: true,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
remaining := listRemainingSnapshots(t, v)
|
|
assert.Len(t, remaining, 3, "should keep one per name")
|
|
assert.Contains(t, remaining, "testhost_home_2026-01-01T06:00:00Z")
|
|
assert.Contains(t, remaining, "testhost_system_2026-01-01T04:00:00Z")
|
|
assert.Contains(t, remaining, "testhost_media_2026-01-01T05:00:00Z")
|
|
}
|