check / check (push) Successful in 12m45s
`vaultik snapshot files <snapshot-id> [path] [--json]` downloads and decrypts the snapshot database, as restore does, and prints mode, size, modification time and path for every file at or under the path. A path that matches nothing is an error. It needs the private key. The mode is in Go's file-mode notation, which the README spells out. The table quotes a path that holds a non-printable character. Named `files` because `ls` is already the alias of `snapshot list`. The missing-key error no longer says "for restore", since this command and deep verify return it too. The README opening now states the private-key rule first. Model: opus-5-5
198 lines
5.6 KiB
Go
198 lines
5.6 KiB
Go
package vaultik_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/spf13/afero"
|
|
"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/storage"
|
|
"sneak.berlin/go/vaultik/internal/vaultik"
|
|
)
|
|
|
|
// These tests cover https://git.eeqj.de/sneak/vaultik/issues/435:
|
|
// `snapshot files` lists what a snapshot holds without restoring it.
|
|
|
|
// unprintableFileName is a file name holding a newline and an escape
|
|
// sequence, which the table must not print raw.
|
|
const unprintableFileName = "odd\nname\x1b[31m"
|
|
|
|
// backUpForFileListing backs up the fault source tree, plus a file named
|
|
// unprintableFileName at its top, and returns a reader whose stdout is
|
|
// out, the snapshot ID, and the source directory.
|
|
func backUpForFileListing(
|
|
t *testing.T, out *bytes.Buffer,
|
|
) (*vaultik.Vaultik, string, string) {
|
|
t.Helper()
|
|
|
|
fs := afero.NewOsFs()
|
|
tempDir := t.TempDir()
|
|
dataDir := filepath.Join(tempDir, "src")
|
|
dbPath := filepath.Join(tempDir, "index.sqlite")
|
|
|
|
ctx := context.Background()
|
|
cfg := faultTestConfig()
|
|
|
|
writeFaultSourceTree(t, fs, dataDir)
|
|
require.NoError(t, afero.WriteFile(fs,
|
|
filepath.Join(dataDir, unprintableFileName), []byte("odd"), 0o644))
|
|
|
|
store, err := storage.NewFileStorer(t.TempDir())
|
|
require.NoError(t, err)
|
|
|
|
db, err := database.New(ctx, dbPath)
|
|
require.NoError(t, err)
|
|
|
|
id := fullFaultBackup(ctx, t, fs, store, cfg,
|
|
database.NewRepositories(db), dataDir, dbPath, "files")
|
|
require.NoError(t, db.Close())
|
|
|
|
v := newReaderVaultik(ctx, cfg, store, nil, fs)
|
|
v.Stdout = out
|
|
|
|
return v, id, dataDir
|
|
}
|
|
|
|
func TestSnapshotFilesJSONListsEveryFileWithItsMetadata(t *testing.T) {
|
|
log.Initialize(log.Config{})
|
|
t.Parallel()
|
|
|
|
var out bytes.Buffer
|
|
|
|
v, id, dataDir := backUpForFileListing(t, &out)
|
|
|
|
require.NoError(t, v.ListSnapshotFiles(id, "", true))
|
|
|
|
var entries []vaultik.ListedFile
|
|
|
|
require.NoError(t, json.Unmarshal(out.Bytes(), &entries))
|
|
|
|
byPath := make(map[string]vaultik.ListedFile, len(entries))
|
|
for _, entry := range entries {
|
|
byPath[entry.Path] = entry
|
|
}
|
|
|
|
for _, rel := range []string{"small.txt", "a.bin", "sub", "sub/b.bin", "sub/c.bin"} {
|
|
path := filepath.Join(dataDir, rel)
|
|
|
|
entry, ok := byPath[path]
|
|
require.True(t, ok, "%s must be listed", path)
|
|
|
|
info, err := afero.NewOsFs().Stat(path)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, info.Mode().String(), entry.Mode, path)
|
|
assert.True(t, info.ModTime().Equal(entry.MTime),
|
|
"%s: mtime %v, listed %v", path, info.ModTime(), entry.MTime)
|
|
|
|
if !info.IsDir() {
|
|
assert.Equal(t, info.Size(), entry.Size, path)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSnapshotFilesListsOnlyWhatIsUnderThePath(t *testing.T) {
|
|
log.Initialize(log.Config{})
|
|
t.Parallel()
|
|
|
|
var out bytes.Buffer
|
|
|
|
v, id, dataDir := backUpForFileListing(t, &out)
|
|
|
|
require.NoError(t, v.ListSnapshotFiles(id, filepath.Join(dataDir, "sub"), false))
|
|
|
|
text := out.String()
|
|
assert.Contains(t, text, filepath.Join(dataDir, "sub", "b.bin"))
|
|
assert.Contains(t, text, filepath.Join(dataDir, "sub", "c.bin"))
|
|
assert.NotContains(t, text, filepath.Join(dataDir, "small.txt"))
|
|
assert.True(t, strings.HasPrefix(text, "MODE"), "the table must have a header")
|
|
}
|
|
|
|
// TestSnapshotFilesTableEscapesAnUnprintablePath checks that the table
|
|
// prints a path holding a newline and an escape sequence quoted and
|
|
// escaped on one row: the newline must not start a forged row, and the
|
|
// escape sequence must not reach the terminal.
|
|
func TestSnapshotFilesTableEscapesAnUnprintablePath(t *testing.T) {
|
|
log.Initialize(log.Config{})
|
|
t.Parallel()
|
|
|
|
var out bytes.Buffer
|
|
|
|
v, id, dataDir := backUpForFileListing(t, &out)
|
|
|
|
require.NoError(t, v.ListSnapshotFiles(id, "", false))
|
|
|
|
text := out.String()
|
|
assert.Contains(t, text,
|
|
strconv.Quote(filepath.Join(dataDir, unprintableFileName)))
|
|
assert.NotContains(t, text, "\x1b", "no escape byte may reach the output")
|
|
|
|
for line := range strings.SplitSeq(text, "\n") {
|
|
assert.False(t, strings.HasPrefix(line, "name"),
|
|
"the newline in the name must not start a row: %q", line)
|
|
}
|
|
}
|
|
|
|
// TestSnapshotFilesQuietSuppressesTableNotJSON is the --quiet contract
|
|
// for `snapshot files`: the table is silenced, but the --json document a
|
|
// script depends on is still written.
|
|
func TestSnapshotFilesQuietSuppressesTableNotJSON(t *testing.T) {
|
|
log.Initialize(log.Config{})
|
|
t.Parallel()
|
|
|
|
var out bytes.Buffer
|
|
|
|
v, id, _ := backUpForFileListing(t, &out)
|
|
v.UI.SetQuiet(true)
|
|
|
|
require.NoError(t, v.ListSnapshotFiles(id, "", false))
|
|
require.Empty(t, out.String(), "the table must be silenced under --quiet")
|
|
|
|
require.NoError(t, v.ListSnapshotFiles(id, "", true))
|
|
|
|
var entries []vaultik.ListedFile
|
|
|
|
require.NoError(t, json.Unmarshal(out.Bytes(), &entries))
|
|
assert.NotEmpty(t, entries,
|
|
"the --json document must still be written under --quiet")
|
|
}
|
|
|
|
func TestSnapshotFilesFailsOnAPathThatMatchesNothing(t *testing.T) {
|
|
log.Initialize(log.Config{})
|
|
t.Parallel()
|
|
|
|
var out bytes.Buffer
|
|
|
|
v, id, dataDir := backUpForFileListing(t, &out)
|
|
missing := filepath.Join(dataDir, "typo.txt")
|
|
|
|
err := v.ListSnapshotFiles(id, missing, false)
|
|
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), missing, "the error must name the path")
|
|
assert.Empty(t, out.String(), "nothing must be printed")
|
|
}
|
|
|
|
func TestSnapshotFilesRequiresThePrivateKey(t *testing.T) {
|
|
log.Initialize(log.Config{})
|
|
t.Parallel()
|
|
|
|
var out bytes.Buffer
|
|
|
|
v, id, _ := backUpForFileListing(t, &out)
|
|
v.Config.AgeSecretKey = ""
|
|
|
|
err := v.ListSnapshotFiles(id, "", false)
|
|
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "VAULTIK_AGE_SECRET_KEY")
|
|
}
|