Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
52def7b6ab | ||
|
|
dd3667c8e9 | ||
|
|
30285c7a8c |
@@ -712,8 +712,10 @@ the `NO_COLOR` environment variable is unset (https://no-color.org/).
|
|||||||
reports and tables (`version`, `info`, `remote info`, `snapshot
|
reports and tables (`version`, `info`, `remote info`, `snapshot
|
||||||
verify`, `snapshot list`) but never the machine-consumed `config get`
|
verify`, `snapshot list`) but never the machine-consumed `config get`
|
||||||
value or the `--json` documents, which a script depends on. The
|
value or the `--json` documents, which a script depends on. The
|
||||||
`database delete` confirmation prompt is also written this way and
|
confirmation prompts of `database delete`, `prune`, `snapshot purge`
|
||||||
always shown: it is an interactive exchange the operator must see.
|
and `snapshot remove`, each with what it asks to delete, are also
|
||||||
|
written this way and always shown: they are an interactive exchange
|
||||||
|
the operator must see.
|
||||||
|
|
||||||
`internal/ui` writes to stdout; it is the output the user asked for. The
|
`internal/ui` writes to stdout; it is the output the user asked for. The
|
||||||
exceptions are the startup banner and the error a failed command ends
|
exceptions are the startup banner and the error a failed command ends
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ require (
|
|||||||
github.com/spf13/cobra v1.10.1
|
github.com/spf13/cobra v1.10.1
|
||||||
github.com/spf13/pflag v1.0.10
|
github.com/spf13/pflag v1.0.10
|
||||||
github.com/stretchr/testify v1.11.1
|
github.com/stretchr/testify v1.11.1
|
||||||
|
go.uber.org/dig v1.19.0
|
||||||
go.uber.org/fx v1.24.0
|
go.uber.org/fx v1.24.0
|
||||||
golang.org/x/sync v0.18.0
|
golang.org/x/sync v0.18.0
|
||||||
golang.org/x/sys v0.38.0
|
golang.org/x/sys v0.38.0
|
||||||
@@ -256,7 +257,6 @@ require (
|
|||||||
go.opentelemetry.io/otel/metric v1.38.0 // indirect
|
go.opentelemetry.io/otel/metric v1.38.0 // indirect
|
||||||
go.opentelemetry.io/otel/trace v1.38.0 // indirect
|
go.opentelemetry.io/otel/trace v1.38.0 // indirect
|
||||||
go.shabbyrobe.org/gocovmerge v0.0.0-20230507111327-fa4f82cfbf4d // indirect
|
go.shabbyrobe.org/gocovmerge v0.0.0-20230507111327-fa4f82cfbf4d // indirect
|
||||||
go.uber.org/dig v1.19.0 // indirect
|
|
||||||
go.uber.org/multierr v1.11.0 // indirect
|
go.uber.org/multierr v1.11.0 // indirect
|
||||||
go.uber.org/zap v1.27.0 // indirect
|
go.uber.org/zap v1.27.0 // indirect
|
||||||
go.yaml.in/yaml/v2 v2.4.3 // indirect
|
go.yaml.in/yaml/v2 v2.4.3 // indirect
|
||||||
|
|||||||
+6
-10
@@ -8,12 +8,12 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/adrg/xdg"
|
"github.com/adrg/xdg"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"go.uber.org/dig"
|
||||||
"go.uber.org/fx"
|
"go.uber.org/fx"
|
||||||
"sneak.berlin/go/vaultik/internal/config"
|
"sneak.berlin/go/vaultik/internal/config"
|
||||||
"sneak.berlin/go/vaultik/internal/database"
|
"sneak.berlin/go/vaultik/internal/database"
|
||||||
@@ -146,16 +146,12 @@ func (e *startupError) Error() string {
|
|||||||
// could not build arguments for function "Y" (file:line): failed to build U:
|
// could not build arguments for function "Y" (file:line): failed to build U:
|
||||||
// received non-nil error from function "Z" (file:line): <real error>
|
// received non-nil error from function "Z" (file:line): <real error>
|
||||||
//
|
//
|
||||||
// Users care about the real error, not the DI plumbing. We strip everything
|
// Users care about the real error, not the DI plumbing. dig.RootCause,
|
||||||
// up through the last "): " (which is always the close-paren of an fx
|
// from the library fx is built on, unwraps those layers and returns the
|
||||||
// function-location annotation followed by the wrapped error).
|
// real error whole. Cutting the text at "): " instead would also cut a
|
||||||
|
// real error that contains it, such as the blob_size_limit one.
|
||||||
func cleanStartupError(err error) error {
|
func cleanStartupError(err error) error {
|
||||||
msg := err.Error()
|
return &startupError{msg: dig.RootCause(err).Error()}
|
||||||
if idx := strings.LastIndex(msg, "): "); idx >= 0 {
|
|
||||||
msg = msg[idx+3:]
|
|
||||||
}
|
|
||||||
|
|
||||||
return &startupError{msg: msg}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunApp starts the fx application, blocks until it is asked to stop, and
|
// RunApp starts the fx application, blocks until it is asked to stop, and
|
||||||
|
|||||||
+47
-40
@@ -2,58 +2,65 @@ package cli //nolint:testpackage // needs access to unexported cleanStartupError
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"go.uber.org/fx"
|
||||||
|
"sneak.berlin/go/vaultik/internal/config"
|
||||||
"sneak.berlin/go/vaultik/internal/pidlock"
|
"sneak.berlin/go/vaultik/internal/pidlock"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TestCleanStartupError starts a real fx app whose config has a
|
||||||
|
// blob_size_limit below the minimum. fx puts annotations naming the
|
||||||
|
// functions it was building in front of the config error; only those
|
||||||
|
// may be removed, although the config error itself contains "): ".
|
||||||
func TestCleanStartupError(t *testing.T) {
|
func TestCleanStartupError(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
tests := []struct {
|
path := filepath.Join(t.TempDir(), "config.yml")
|
||||||
name string
|
|
||||||
in string
|
err := os.WriteFile(path, []byte("snapshots:\n"+
|
||||||
want string
|
" test:\n"+
|
||||||
}{
|
" paths: [/tmp/vaultik-test-source]\n"+
|
||||||
{
|
"storage_url: file:///tmp/vaultik-test-storage\n"+
|
||||||
name: "real fx error chain",
|
"chunk_size: 10MB\n"+
|
||||||
in: `could not build arguments for function ` +
|
"blob_size_limit: 30MB\n"), 0o600)
|
||||||
`"sneak.berlin/go/vaultik/internal/cli".newSnapshotCreateCommand.func1.1 ` +
|
if err != nil {
|
||||||
`(/Users/user/dev/vaultik/internal/cli/snapshot.go:71): ` +
|
t.Fatalf("write config: %v", err)
|
||||||
`failed to build *vaultik.Vaultik: ` +
|
|
||||||
`could not build arguments for function ` +
|
|
||||||
`"sneak.berlin/go/vaultik/internal/vaultik".New ` +
|
|
||||||
`(/Users/user/dev/vaultik/internal/vaultik/vaultik.go:59): ` +
|
|
||||||
`failed to build storage.Storer: ` +
|
|
||||||
`received non-nil error from function ` +
|
|
||||||
`"sneak.berlin/go/vaultik/internal/storage".NewStorer ` +
|
|
||||||
`(/Users/user/dev/vaultik/internal/storage/module.go:23): ` +
|
|
||||||
`creating base path: mkdir /Volumes/BACKUPS: permission denied`,
|
|
||||||
want: `creating base path: mkdir /Volumes/BACKUPS: permission denied`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "no fx wrapping",
|
|
||||||
in: "plain error",
|
|
||||||
want: "plain error",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "single fx wrapping",
|
|
||||||
in: `received non-nil error from function "foo" (file.go:1): underlying problem`,
|
|
||||||
want: "underlying problem",
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
app := fx.New(
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
fx.NopLogger,
|
||||||
t.Parallel()
|
fx.Supply(config.Path(path)),
|
||||||
|
config.Module,
|
||||||
|
fx.Invoke(func(*config.Config) {}),
|
||||||
|
)
|
||||||
|
|
||||||
//nolint:err113 // test constructs errors from table input
|
err = app.Start(t.Context())
|
||||||
got := cleanStartupError(errors.New(tt.in)).Error()
|
if err == nil {
|
||||||
if got != tt.want {
|
t.Fatal("app started; want a blob_size_limit error")
|
||||||
t.Errorf("got %q, want %q", got, tt.want)
|
}
|
||||||
}
|
|
||||||
})
|
want := "failed to load config: invalid config: blob_size_limit must " +
|
||||||
|
"be at least the largest chunk the chunker can emit (chunk_size " +
|
||||||
|
"times the FastCDC size spread): need at least 40000000 bytes"
|
||||||
|
|
||||||
|
got := cleanStartupError(err).Error()
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("got %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestCleanStartupErrorNotFromFx checks that an error fx did not wrap is
|
||||||
|
// shown unchanged.
|
||||||
|
func TestCleanStartupErrorNotFromFx(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
//nolint:err113 // a one-off error standing in for a startup failure
|
||||||
|
got := cleanStartupError(errors.New("plain error")).Error()
|
||||||
|
if got != "plain error" {
|
||||||
|
t.Errorf("got %q, want %q", got, "plain error")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,123 @@
|
|||||||
|
package cli //nolint:testpackage // shares runEntry and the hermetic config helpers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
// flagCron is used to build the argument vectors below.
|
||||||
|
const flagCron = "--cron"
|
||||||
|
|
||||||
|
// plantUnreferencedBlob writes a blob that no snapshot references to the
|
||||||
|
// destination store at storeDir, so that a prune deletes it. Returns its
|
||||||
|
// path.
|
||||||
|
func plantUnreferencedBlob(t *testing.T, storeDir string) string {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
hash := strings.Repeat("b", 64)
|
||||||
|
path := filepath.Join(storeDir, "blobs", hash[:2], hash[2:4], hash)
|
||||||
|
|
||||||
|
require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o750))
|
||||||
|
require.NoError(t, os.WriteFile(path, []byte("unreferenced"), 0o600))
|
||||||
|
|
||||||
|
return path
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestEntrySnapshotCreatePruneIsSilent checks that a successful
|
||||||
|
// `snapshot create --prune` under --cron or --quiet prints nothing on
|
||||||
|
// stdout or stderr, whether or not the prune finds a blob to delete.
|
||||||
|
//
|
||||||
|
//nolint:paralleltest // replaces os.Args, os.Stdout, os.Stderr and the xdg globals
|
||||||
|
func TestEntrySnapshotCreatePruneIsSilent(t *testing.T) {
|
||||||
|
for _, testCase := range []struct {
|
||||||
|
name string
|
||||||
|
args []string
|
||||||
|
plantBlob bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "cron, no blob to delete",
|
||||||
|
args: []string{cmdSnapshot, cmdCreate, flagCron, flagPrune},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "cron, blob to delete",
|
||||||
|
args: []string{cmdSnapshot, cmdCreate, flagCron, flagPrune},
|
||||||
|
plantBlob: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "cron with keep-newer-than",
|
||||||
|
args: []string{
|
||||||
|
cmdSnapshot, cmdCreate, flagCron, flagPrune,
|
||||||
|
flagKeepNewerThan, "4w",
|
||||||
|
},
|
||||||
|
plantBlob: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "quiet",
|
||||||
|
args: []string{flagQuiet, cmdSnapshot, cmdCreate, flagPrune},
|
||||||
|
plantBlob: true,
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
t.Run(testCase.name, func(t *testing.T) {
|
||||||
|
configPath, storeDir := writeBackupConfig(t)
|
||||||
|
|
||||||
|
var blob string
|
||||||
|
if testCase.plantBlob {
|
||||||
|
blob = plantUnreferencedBlob(t, storeDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
code, stdout, stderr := runEntry(t,
|
||||||
|
append([]string{flagConfig, configPath}, testCase.args...)...)
|
||||||
|
|
||||||
|
assert.Equal(t, 0, code)
|
||||||
|
assert.Empty(t, stdout)
|
||||||
|
assert.Empty(t, stderr)
|
||||||
|
|
||||||
|
if testCase.plantBlob {
|
||||||
|
assert.NoFileExists(t, blob, "the prune must delete the blob")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestEntryPruneQuietIsSilent checks that a successful `prune --force`
|
||||||
|
// under --quiet prints nothing on stdout or stderr, both when there is
|
||||||
|
// nothing to remove and when it removes a stale local snapshot record
|
||||||
|
// and an unreferenced blob.
|
||||||
|
//
|
||||||
|
//nolint:paralleltest // replaces os.Args, os.Stdout, os.Stderr and the xdg globals
|
||||||
|
func TestEntryPruneQuietIsSilent(t *testing.T) {
|
||||||
|
for _, testCase := range []struct {
|
||||||
|
name string
|
||||||
|
staleRecordAndBlob bool
|
||||||
|
}{
|
||||||
|
{name: "nothing to remove"},
|
||||||
|
{name: "stale record and blob to remove", staleRecordAndBlob: true},
|
||||||
|
} {
|
||||||
|
t.Run(testCase.name, func(t *testing.T) {
|
||||||
|
configPath := writeHermeticPruneConfig(t, testCase.staleRecordAndBlob)
|
||||||
|
|
||||||
|
var blob string
|
||||||
|
if testCase.staleRecordAndBlob {
|
||||||
|
// writeHermeticPruneConfig puts the store beside the config.
|
||||||
|
blob = plantUnreferencedBlob(t,
|
||||||
|
filepath.Join(filepath.Dir(configPath), "store"))
|
||||||
|
}
|
||||||
|
|
||||||
|
code, stdout, stderr := runEntry(t,
|
||||||
|
flagConfig, configPath, flagQuiet, cmdPrune, flagForce)
|
||||||
|
|
||||||
|
assert.Equal(t, 0, code)
|
||||||
|
assert.Empty(t, stdout)
|
||||||
|
assert.Empty(t, stderr)
|
||||||
|
|
||||||
|
if testCase.staleRecordAndBlob {
|
||||||
|
assert.NoFileExists(t, blob, "the prune must delete the blob")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -204,7 +204,7 @@ func (v *Vaultik) PruneBlobs(opts *PruneOptions) error {
|
|||||||
return v.outputPruneBlobsJSON(result)
|
return v.outputPruneBlobsJSON(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
v.printlnStdout("No unreferenced blobs to remove.")
|
v.UI.Infof("No unreferenced blobs to remove.")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -212,12 +212,17 @@ func (v *Vaultik) PruneBlobs(opts *PruneOptions) error {
|
|||||||
log.Info("Found unreferenced blobs",
|
log.Info("Found unreferenced blobs",
|
||||||
"count", len(unreferencedBlobs), "total_size", ubytes(totalSize))
|
"count", len(unreferencedBlobs), "total_size", ubytes(totalSize))
|
||||||
|
|
||||||
if !opts.JSON {
|
if opts.Force && !opts.JSON {
|
||||||
v.stdoutf("Found %d unreferenced blob(s) totaling %s\n",
|
v.UI.Infof("Found %d unreferenced blob(s) totaling %s.",
|
||||||
len(unreferencedBlobs), ubytes(totalSize))
|
len(unreferencedBlobs), ubytes(totalSize))
|
||||||
}
|
}
|
||||||
|
|
||||||
if !opts.Force && !opts.JSON {
|
if !opts.Force && !opts.JSON {
|
||||||
|
// The count, the prompt and its answer bypass the UI: they are an
|
||||||
|
// interactive exchange the operator must see, so --quiet does not
|
||||||
|
// silence them.
|
||||||
|
v.stdoutf("Found %d unreferenced blob(s) totaling %s.\n",
|
||||||
|
len(unreferencedBlobs), ubytes(totalSize))
|
||||||
v.stdoutf("\nDelete %d unreferenced blob(s)? [y/N] ", len(unreferencedBlobs))
|
v.stdoutf("\nDelete %d unreferenced blob(s)? [y/N] ", len(unreferencedBlobs))
|
||||||
|
|
||||||
var confirm string
|
var confirm string
|
||||||
@@ -244,7 +249,7 @@ func (v *Vaultik) PruneBlobs(opts *PruneOptions) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
v.stdoutf("\nDeleted %d blob(s) totaling %s\n",
|
v.UI.Infof("Deleted %d blob(s) totaling %s.",
|
||||||
result.BlobsDeleted, ubytes(result.BytesFreed))
|
result.BlobsDeleted, ubytes(result.BytesFreed))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import (
|
|||||||
"sneak.berlin/go/vaultik/internal/log"
|
"sneak.berlin/go/vaultik/internal/log"
|
||||||
"sneak.berlin/go/vaultik/internal/snapshot"
|
"sneak.berlin/go/vaultik/internal/snapshot"
|
||||||
"sneak.berlin/go/vaultik/internal/types"
|
"sneak.berlin/go/vaultik/internal/types"
|
||||||
|
"sneak.berlin/go/vaultik/internal/ui"
|
||||||
"sneak.berlin/go/vaultik/internal/vaultik"
|
"sneak.berlin/go/vaultik/internal/vaultik"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -58,13 +59,16 @@ func setupConsistencyTest(
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stdout := &bytes.Buffer{}
|
||||||
|
|
||||||
v := &vaultik.Vaultik{
|
v := &vaultik.Vaultik{
|
||||||
Storage: mockStorage,
|
Storage: mockStorage,
|
||||||
Repositories: repos,
|
Repositories: repos,
|
||||||
DB: db,
|
DB: db,
|
||||||
Stdout: &bytes.Buffer{},
|
Stdout: stdout,
|
||||||
Stderr: &bytes.Buffer{},
|
Stderr: &bytes.Buffer{},
|
||||||
Stdin: &bytes.Buffer{},
|
Stdin: &bytes.Buffer{},
|
||||||
|
UI: ui.NewWithColor(stdout, false),
|
||||||
}
|
}
|
||||||
v.SetContext(ctx)
|
v.SetContext(ctx)
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import (
|
|||||||
"sneak.berlin/go/vaultik/internal/log"
|
"sneak.berlin/go/vaultik/internal/log"
|
||||||
"sneak.berlin/go/vaultik/internal/snapshot"
|
"sneak.berlin/go/vaultik/internal/snapshot"
|
||||||
"sneak.berlin/go/vaultik/internal/types"
|
"sneak.berlin/go/vaultik/internal/types"
|
||||||
|
"sneak.berlin/go/vaultik/internal/ui"
|
||||||
"sneak.berlin/go/vaultik/internal/vaultik"
|
"sneak.berlin/go/vaultik/internal/vaultik"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -86,6 +87,7 @@ func setupPurgeTest(
|
|||||||
Stdout: stdout,
|
Stdout: stdout,
|
||||||
Stderr: stderr,
|
Stderr: stderr,
|
||||||
Stdin: stdin,
|
Stdin: stdin,
|
||||||
|
UI: ui.NewWithColor(stdout, false),
|
||||||
}
|
}
|
||||||
v.SetContext(ctx)
|
v.SetContext(ctx)
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,148 @@
|
|||||||
|
package vaultik_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"sneak.berlin/go/vaultik/internal/log"
|
||||||
|
"sneak.berlin/go/vaultik/internal/ui"
|
||||||
|
"sneak.berlin/go/vaultik/internal/vaultik"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestPurgeSnapshots_QuietSuppressesReport checks that `snapshot purge
|
||||||
|
// --keep-latest --force` under --quiet deletes the older snapshot and
|
||||||
|
// prints nothing.
|
||||||
|
func TestPurgeSnapshots_QuietSuppressesReport(t *testing.T) {
|
||||||
|
log.Initialize(log.Config{})
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
v := setupPurgeTest(t, snapHostname, []string{snapHomeT0, snapHomeT1})
|
||||||
|
|
||||||
|
var out bytes.Buffer
|
||||||
|
|
||||||
|
v.Stdout = &out
|
||||||
|
v.UI = ui.NewWithColor(&out, false)
|
||||||
|
v.UI.SetQuiet(true)
|
||||||
|
|
||||||
|
require.NoError(t, v.PurgeSnapshotsWithOptions(&vaultik.SnapshotPurgeOptions{
|
||||||
|
KeepLatest: true,
|
||||||
|
Force: true,
|
||||||
|
}))
|
||||||
|
|
||||||
|
assert.Equal(t, []string{snapHomeT1}, listRemainingSnapshots(t, v))
|
||||||
|
assert.Empty(t, out.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestPurgeSnapshots_QuietPromptListsSnapshots checks that `snapshot
|
||||||
|
// purge --keep-latest` without --force still lists the snapshots its
|
||||||
|
// prompt asks to delete under --quiet.
|
||||||
|
func TestPurgeSnapshots_QuietPromptListsSnapshots(t *testing.T) {
|
||||||
|
log.Initialize(log.Config{})
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
v := setupPurgeTest(t, snapHostname, []string{snapHomeT0, snapHomeT1})
|
||||||
|
|
||||||
|
var out bytes.Buffer
|
||||||
|
|
||||||
|
v.Stdout = &out
|
||||||
|
v.UI = ui.NewWithColor(&out, false)
|
||||||
|
v.UI.SetQuiet(true)
|
||||||
|
|
||||||
|
// Stdin is empty, so the prompt reads no answer and cancels.
|
||||||
|
require.NoError(t, v.PurgeSnapshotsWithOptions(&vaultik.SnapshotPurgeOptions{
|
||||||
|
KeepLatest: true,
|
||||||
|
}))
|
||||||
|
|
||||||
|
assert.Contains(t, out.String(), snapHomeT0)
|
||||||
|
assert.Contains(t, out.String(), "Delete 1 snapshot(s)? [y/N]")
|
||||||
|
assert.Len(t, listRemainingSnapshots(t, v), 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestPruneBlobs_QuietPromptShowsBlobTotal checks that `prune` without
|
||||||
|
// --force still states the count and total size of the blobs its prompt
|
||||||
|
// asks to delete under --quiet.
|
||||||
|
func TestPruneBlobs_QuietPromptShowsBlobTotal(t *testing.T) {
|
||||||
|
log.Initialize(log.Config{})
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
env := newListEnv(t)
|
||||||
|
env.v.UI.SetQuiet(true)
|
||||||
|
|
||||||
|
// The store holds no manifest, so nothing references this blob.
|
||||||
|
addBlob(t, env.store.testStorer, testBlobHashA)
|
||||||
|
|
||||||
|
// Stdin is empty, so the prompt reads no answer and cancels.
|
||||||
|
require.NoError(t, env.v.PruneBlobs(&vaultik.PruneOptions{}))
|
||||||
|
|
||||||
|
assert.Contains(t, env.stdout.String(),
|
||||||
|
"Found 1 unreferenced blob(s) totaling")
|
||||||
|
assert.Contains(t, env.stdout.String(),
|
||||||
|
"Delete 1 unreferenced blob(s)? [y/N]")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestRemoveSnapshot_QuietSuppressesReport checks that `snapshot remove`
|
||||||
|
// under --quiet prints nothing with --dry-run or with --force, and that
|
||||||
|
// the --json document still prints.
|
||||||
|
func TestRemoveSnapshot_QuietSuppressesReport(t *testing.T) {
|
||||||
|
log.Initialize(log.Config{})
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
newQuietRemover := func() (*vaultik.TestVaultik, *testStorer) {
|
||||||
|
store := newTestStorer()
|
||||||
|
addManifest(t, store, "snapshot-001", nil)
|
||||||
|
|
||||||
|
tv := vaultik.NewForTesting(store)
|
||||||
|
tv.Repositories = newLocalIndex(t, "snapshot-001")
|
||||||
|
tv.UI.SetQuiet(true)
|
||||||
|
|
||||||
|
return tv, store
|
||||||
|
}
|
||||||
|
|
||||||
|
tv, store := newQuietRemover()
|
||||||
|
|
||||||
|
_, err := tv.RemoveSnapshot("snapshot-001",
|
||||||
|
&vaultik.RemoveOptions{DryRun: true})
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Empty(t, tv.Stdout.String(),
|
||||||
|
"--dry-run must print nothing under --quiet")
|
||||||
|
|
||||||
|
_, err = tv.RemoveSnapshot("snapshot-001",
|
||||||
|
&vaultik.RemoveOptions{Force: true})
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.False(t, store.hasKey(remoteKeyPath("snapshot-001")))
|
||||||
|
assert.Empty(t, tv.Stdout.String(),
|
||||||
|
"--force must print nothing under --quiet")
|
||||||
|
|
||||||
|
tv, _ = newQuietRemover()
|
||||||
|
|
||||||
|
_, err = tv.RemoveSnapshot("snapshot-001",
|
||||||
|
&vaultik.RemoveOptions{JSON: true})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
var result vaultik.RemoveResult
|
||||||
|
|
||||||
|
require.NoError(t, json.Unmarshal(tv.Stdout.Bytes(), &result),
|
||||||
|
"the --json document must still print under --quiet")
|
||||||
|
assert.True(t, result.RemoteRemoved)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestNukeRemote_QuietSuppressesReport checks that `remote nuke --force`
|
||||||
|
// under --quiet prints nothing while it removes a snapshot's metadata
|
||||||
|
// and its blobs.
|
||||||
|
func TestNukeRemote_QuietSuppressesReport(t *testing.T) {
|
||||||
|
log.Initialize(log.Config{})
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
storeDir := filepath.Join(t.TempDir(), "store")
|
||||||
|
v, _, out := backUpToFileDestination(ctx, t, storeDir)
|
||||||
|
v.UI.SetQuiet(true)
|
||||||
|
|
||||||
|
require.NoError(t, v.NukeRemote(true))
|
||||||
|
assert.Empty(t, out.String())
|
||||||
|
}
|
||||||
@@ -350,7 +350,7 @@ func TestRemoveSnapshot_DryRun(t *testing.T) {
|
|||||||
assert.True(t, store.hasKey("blobs/aa/aa/"+blobA))
|
assert.True(t, store.hasKey("blobs/aa/aa/"+blobA))
|
||||||
assert.True(t, store.hasKey(remoteKeyPath("snapshot-001")))
|
assert.True(t, store.hasKey(remoteKeyPath("snapshot-001")))
|
||||||
|
|
||||||
assert.Contains(t, tv.Stdout.String(), "[Dry run - no changes made]")
|
assert.Contains(t, tv.Stdout.String(), "Dry run: no changes made.")
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestRemoveSnapshot_UnknownIDIsError checks that an ID naming no
|
// TestRemoveSnapshot_UnknownIDIsError checks that an ID naming no
|
||||||
@@ -504,7 +504,7 @@ func TestRemoveAllSnapshots_DryRun(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal(t, initialCount, store.keyCount())
|
assert.Equal(t, initialCount, store.keyCount())
|
||||||
|
|
||||||
assert.Contains(t, tv.Stdout.String(), "[Dry run - no changes made]")
|
assert.Contains(t, tv.Stdout.String(), "Dry run: no changes made.")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRemoveAllSnapshots_NoSnapshots(t *testing.T) {
|
func TestRemoveAllSnapshots_NoSnapshots(t *testing.T) {
|
||||||
|
|||||||
@@ -574,7 +574,7 @@ func (v *Vaultik) PurgeSnapshotsWithOptions(opts *SnapshotPurgeOptions) error {
|
|||||||
|
|
||||||
if len(toDelete) == 0 {
|
if len(toDelete) == 0 {
|
||||||
if !opts.Quiet {
|
if !opts.Quiet {
|
||||||
v.printlnStdout("No snapshots to delete")
|
v.UI.Infof("No snapshots to delete.")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -631,19 +631,19 @@ func selectSnapshotsToPurge(
|
|||||||
func (v *Vaultik) confirmAndExecutePurge(
|
func (v *Vaultik) confirmAndExecutePurge(
|
||||||
toDelete []SnapshotInfo, force, quiet bool,
|
toDelete []SnapshotInfo, force, quiet bool,
|
||||||
) error {
|
) error {
|
||||||
if !quiet {
|
// Confirm unless --force is used. The list, the prompt and its answer
|
||||||
|
// bypass the UI: they are an interactive exchange the operator must
|
||||||
|
// see, so --quiet does not silence them.
|
||||||
|
if !force {
|
||||||
v.stdoutf("The following snapshots will be deleted:\n\n")
|
v.stdoutf("The following snapshots will be deleted:\n\n")
|
||||||
|
|
||||||
for _, snap := range toDelete {
|
for _, snap := range toDelete {
|
||||||
v.stdoutf(" %s (%s, %s)\n",
|
v.stdoutf(" %s (%s, %s)\n",
|
||||||
snap.ID,
|
snap.ID,
|
||||||
snap.Timestamp.Format("2006-01-02 15:04:05"),
|
snap.Timestamp.Format(time.DateTime),
|
||||||
ubytes(snap.CompressedSize))
|
ubytes(snap.CompressedSize))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Confirm unless --force is used
|
|
||||||
if !force {
|
|
||||||
v.stdoutf("\nDelete %d snapshot(s)? [y/N] ", len(toDelete))
|
v.stdoutf("\nDelete %d snapshot(s)? [y/N] ", len(toDelete))
|
||||||
|
|
||||||
var confirm string
|
var confirm string
|
||||||
@@ -661,7 +661,16 @@ func (v *Vaultik) confirmAndExecutePurge(
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
} else if !quiet {
|
} else if !quiet {
|
||||||
v.stdoutf("\nDeleting %d snapshot(s) (--force specified)\n", len(toDelete))
|
v.UI.Infof("The following snapshots will be deleted:")
|
||||||
|
|
||||||
|
for _, snap := range toDelete {
|
||||||
|
v.UI.Detailf("%s (%s, %s)",
|
||||||
|
v.UI.Snapshot(snap.ID.String()),
|
||||||
|
snap.Timestamp.Format(time.DateTime),
|
||||||
|
ubytes(snap.CompressedSize))
|
||||||
|
}
|
||||||
|
|
||||||
|
v.UI.Infof("Deleting %d snapshot(s) (--force specified).", len(toDelete))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete snapshots (both local and remote)
|
// Delete snapshots (both local and remote)
|
||||||
@@ -694,9 +703,9 @@ func (v *Vaultik) confirmAndExecutePurge(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !quiet {
|
if !quiet {
|
||||||
v.stdoutf("Deleted %d snapshot(s)\n", len(toDelete))
|
v.UI.Infof("Deleted %d snapshot(s).", len(toDelete))
|
||||||
v.printlnStdout(
|
v.UI.Noticef("Run '%s' to clean up unreferenced remote blobs.",
|
||||||
"\nNote: Run 'vaultik prune' to clean up unreferenced remote blobs.")
|
pruneCommandHint)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -1069,7 +1078,7 @@ func (v *Vaultik) CleanupLocalSnapshots(opts *PruneOptions) error {
|
|||||||
log.Info("Removing stale local snapshot record", "snapshot_id", id)
|
log.Info("Removing stale local snapshot record", "snapshot_id", id)
|
||||||
|
|
||||||
if !opts.JSON {
|
if !opts.JSON {
|
||||||
v.stdoutf("Removing stale local record: %s\n", id)
|
v.UI.Infof("Removing stale local record: %s.", v.UI.Snapshot(id))
|
||||||
}
|
}
|
||||||
|
|
||||||
err = v.deleteSnapshotFromLocalDB(id)
|
err = v.deleteSnapshotFromLocalDB(id)
|
||||||
@@ -1092,9 +1101,9 @@ func (v *Vaultik) CleanupLocalSnapshots(opts *PruneOptions) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if removed == 0 {
|
if removed == 0 {
|
||||||
v.printlnStdout("No stale local snapshots found.")
|
v.UI.Infof("No stale local snapshots found.")
|
||||||
} else {
|
} else {
|
||||||
v.stdoutf("Removed %d stale local snapshot record(s).\n", removed)
|
v.UI.Infof("Removed %d stale local snapshot record(s).", removed)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -1273,14 +1282,15 @@ func (v *Vaultik) RemoveSnapshot(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if inLocalIndex {
|
if inLocalIndex {
|
||||||
v.stdoutf("Removed snapshot '%s' from local database\n", snapshotID)
|
v.UI.Infof("Removed snapshot '%s' from local database.",
|
||||||
|
v.UI.Snapshot(snapshotID))
|
||||||
}
|
}
|
||||||
|
|
||||||
if result.RemoteRemoved {
|
if result.RemoteRemoved {
|
||||||
v.printlnStdout("Removed snapshot metadata from remote storage")
|
v.UI.Infof("Removed snapshot metadata from remote storage.")
|
||||||
v.stdoutf("\nNote: The removed snapshot's blobs remain on the remote. "+
|
v.UI.Noticef("The removed snapshot's blobs remain on the remote. "+
|
||||||
"Run '%s' to delete any blobs no longer referenced by any "+
|
"Run '%s' to delete any blobs no longer referenced by any "+
|
||||||
"remaining remote snapshot.\n", pruneCommandHint)
|
"remaining remote snapshot.", pruneCommandHint)
|
||||||
}
|
}
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
@@ -1336,21 +1346,23 @@ func (v *Vaultik) removeSnapshotDryRun(
|
|||||||
return result, v.outputRemoveJSON(result)
|
return result, v.outputRemoveJSON(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
v.stdoutf("Would remove snapshot: %s\n", snapshotID)
|
v.UI.Infof("Would remove snapshot: %s.", v.UI.Snapshot(snapshotID))
|
||||||
|
|
||||||
if !opts.LocalOnly {
|
if !opts.LocalOnly {
|
||||||
v.printlnStdout("Would also remove snapshot metadata from " +
|
v.UI.Infof("Would also remove snapshot metadata from " +
|
||||||
"remote storage (blobs untouched)")
|
"remote storage (blobs untouched).")
|
||||||
}
|
}
|
||||||
|
|
||||||
v.printlnStdout("[Dry run - no changes made]")
|
v.UI.Noticef("Dry run: no changes made.")
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// confirmRemoveSnapshot prompts for confirmation before removing a
|
// confirmRemoveSnapshot prompts for confirmation before removing a
|
||||||
// snapshot. Returns false (treating EOF/read errors as "no") unless the
|
// snapshot. Returns false (treating EOF/read errors as "no") unless the
|
||||||
// user answers "y".
|
// user answers "y". The prompt and its answer bypass the UI: they are an
|
||||||
|
// interactive exchange the operator must see, so --quiet does not
|
||||||
|
// silence them.
|
||||||
func (v *Vaultik) confirmRemoveSnapshot(snapshotID string, opts *RemoveOptions) bool {
|
func (v *Vaultik) confirmRemoveSnapshot(snapshotID string, opts *RemoveOptions) bool {
|
||||||
if opts.LocalOnly {
|
if opts.LocalOnly {
|
||||||
v.stdoutf("Remove snapshot '%s' from local database "+
|
v.stdoutf("Remove snapshot '%s' from local database "+
|
||||||
@@ -1456,7 +1468,7 @@ func (v *Vaultik) RemoveAllSnapshots(opts *RemoveOptions) (*RemoveResult, error)
|
|||||||
|
|
||||||
if len(localSnaps) == 0 && len(orphanRemoteKeys) == 0 {
|
if len(localSnaps) == 0 && len(orphanRemoteKeys) == 0 {
|
||||||
if !opts.JSON {
|
if !opts.JSON {
|
||||||
v.printlnStdout("No snapshots found")
|
v.UI.Infof("No snapshots found.")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &RemoveResult{}, nil
|
return &RemoveResult{}, nil
|
||||||
@@ -1561,27 +1573,27 @@ func (v *Vaultik) handleRemoveAllDryRun(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !opts.JSON {
|
if !opts.JSON {
|
||||||
v.stdoutf("Would remove %d local snapshot(s):\n", len(localSnaps))
|
v.UI.Infof("Would remove %d local snapshot(s):", len(localSnaps))
|
||||||
|
|
||||||
for _, id := range localSnaps {
|
for _, id := range localSnaps {
|
||||||
v.stdoutf(" %s\n", id)
|
v.UI.Detailf("%s", v.UI.Snapshot(id))
|
||||||
}
|
}
|
||||||
|
|
||||||
if !opts.LocalOnly {
|
if !opts.LocalOnly {
|
||||||
if len(orphanRemoteKeys) > 0 {
|
if len(orphanRemoteKeys) > 0 {
|
||||||
v.stdoutf("Would also remove %d orphan remote snapshot key(s):\n",
|
v.UI.Infof("Would also remove %d orphan remote snapshot key(s):",
|
||||||
len(orphanRemoteKeys))
|
len(orphanRemoteKeys))
|
||||||
|
|
||||||
for _, key := range orphanRemoteKeys {
|
for _, key := range orphanRemoteKeys {
|
||||||
v.stdoutf(" %s\n", key)
|
v.UI.Detailf("%s", key)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
v.printlnStdout("Would also remove snapshot metadata from " +
|
v.UI.Infof("Would also remove snapshot metadata from " +
|
||||||
"remote storage (blobs untouched)")
|
"remote storage (blobs untouched).")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
v.printlnStdout("[Dry run - no changes made]")
|
v.UI.Noticef("Dry run: no changes made.")
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.JSON {
|
if opts.JSON {
|
||||||
@@ -1630,10 +1642,10 @@ func (v *Vaultik) executeRemoveAll(
|
|||||||
return result, v.outputRemoveJSON(result)
|
return result, v.outputRemoveJSON(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
v.stdoutf("Removed %d snapshot(s)\n", len(result.SnapshotsRemoved))
|
v.UI.Infof("Removed %d snapshot(s).", len(result.SnapshotsRemoved))
|
||||||
|
|
||||||
if !opts.LocalOnly && result.RemoteRemoved {
|
if !opts.LocalOnly && result.RemoteRemoved {
|
||||||
v.printlnStdout("Removed snapshot metadata from remote storage")
|
v.UI.Infof("Removed snapshot metadata from remote storage.")
|
||||||
}
|
}
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user