2023-03-28 19:12:30 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 19:12:30 +00:00
|
|
|
|
2017-10-17 11:06:40 +00:00
|
|
|
package inspect
|
2016-10-31 23:37:27 +00:00
|
|
|
|
|
|
|
import (
|
2020-10-09 19:57:29 +00:00
|
|
|
"flag"
|
2022-11-10 16:26:01 +00:00
|
|
|
"os"
|
2020-04-24 22:18:56 +00:00
|
|
|
"path/filepath"
|
2016-10-31 23:37:27 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mitchellh/cli"
|
2020-10-09 19:57:29 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-10-31 23:37:27 +00:00
|
|
|
)
|
|
|
|
|
2020-10-09 19:57:29 +00:00
|
|
|
// update allows golden files to be updated based on the current output.
|
|
|
|
var update = flag.Bool("update", false, "update golden files")
|
|
|
|
|
|
|
|
// golden reads and optionally writes the expected data to the golden file,
|
|
|
|
// returning the contents as a string.
|
|
|
|
func golden(t *testing.T, name, got string) string {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
golden := filepath.Join("testdata", name+".golden")
|
|
|
|
if *update && got != "" {
|
2022-11-10 16:26:01 +00:00
|
|
|
err := os.WriteFile(golden, []byte(got), 0644)
|
2020-10-09 19:57:29 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-11-10 16:26:01 +00:00
|
|
|
expected, err := os.ReadFile(golden)
|
2020-10-09 19:57:29 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return string(expected)
|
|
|
|
}
|
|
|
|
|
2018-03-19 16:56:00 +00:00
|
|
|
func TestSnapshotInspectCommand_noTabs(t *testing.T) {
|
2017-10-17 14:07:48 +00:00
|
|
|
t.Parallel()
|
2017-10-17 07:03:41 +00:00
|
|
|
if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') {
|
2017-10-17 13:44:20 +00:00
|
|
|
t.Fatal("help has tabs")
|
2017-10-17 07:03:41 +00:00
|
|
|
}
|
2016-10-31 23:37:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSnapshotInspectCommand_Validation(t *testing.T) {
|
2017-10-17 14:07:48 +00:00
|
|
|
t.Parallel()
|
2017-10-17 07:03:41 +00:00
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
2016-10-31 23:37:27 +00:00
|
|
|
|
|
|
|
cases := map[string]struct {
|
|
|
|
args []string
|
|
|
|
output string
|
|
|
|
}{
|
|
|
|
"no file": {
|
|
|
|
[]string{},
|
|
|
|
"Missing FILE argument",
|
|
|
|
},
|
|
|
|
"extra args": {
|
|
|
|
[]string{"foo", "bar", "baz"},
|
|
|
|
"Too many arguments",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
|
|
|
// Ensure our buffer is always clear
|
|
|
|
if ui.ErrorWriter != nil {
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
}
|
|
|
|
if ui.OutputWriter != nil {
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
code := c.Run(tc.args)
|
|
|
|
if code == 0 {
|
|
|
|
t.Errorf("%s: expected non-zero exit", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
output := ui.ErrorWriter.String()
|
|
|
|
if !strings.Contains(output, tc.output) {
|
|
|
|
t.Errorf("%s: expected %q to contain %q", name, output, tc.output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-17 12:04:51 +00:00
|
|
|
func TestSnapshotInspectCommand(t *testing.T) {
|
2016-10-31 23:37:27 +00:00
|
|
|
|
2020-10-09 19:57:29 +00:00
|
|
|
filepath := "./testdata/backup.snap"
|
2016-10-31 23:37:27 +00:00
|
|
|
|
|
|
|
// Inspect the snapshot
|
2017-10-17 07:03:41 +00:00
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
2020-10-09 19:57:29 +00:00
|
|
|
args := []string{filepath}
|
2016-10-31 23:37:27 +00:00
|
|
|
|
2017-02-10 01:00:38 +00:00
|
|
|
code := c.Run(args)
|
2016-10-31 23:37:27 +00:00
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:57:29 +00:00
|
|
|
want := golden(t, t.Name(), ui.OutputWriter.String())
|
|
|
|
require.Equal(t, want, ui.OutputWriter.String())
|
2016-10-31 23:37:27 +00:00
|
|
|
}
|
2020-11-05 17:01:44 +00:00
|
|
|
|
2020-11-10 17:18:21 +00:00
|
|
|
func TestSnapshotInspectKVDetailsCommand(t *testing.T) {
|
2020-11-05 17:01:44 +00:00
|
|
|
|
2020-11-10 17:18:21 +00:00
|
|
|
filepath := "./testdata/backupWithKV.snap"
|
2020-11-05 17:01:44 +00:00
|
|
|
|
|
|
|
// Inspect the snapshot
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
2020-11-10 17:18:21 +00:00
|
|
|
args := []string{"-kvdetails", filepath}
|
2020-11-05 17:01:44 +00:00
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
want := golden(t, t.Name(), ui.OutputWriter.String())
|
|
|
|
require.Equal(t, want, ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
|
2020-11-10 17:18:21 +00:00
|
|
|
func TestSnapshotInspectKVDetailsDepthCommand(t *testing.T) {
|
2020-11-05 17:01:44 +00:00
|
|
|
|
2020-11-10 17:18:21 +00:00
|
|
|
filepath := "./testdata/backupWithKV.snap"
|
2020-11-05 17:01:44 +00:00
|
|
|
|
|
|
|
// Inspect the snapshot
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
2020-11-10 17:18:21 +00:00
|
|
|
args := []string{"-kvdetails", "-kvdepth", "3", filepath}
|
2020-11-05 17:01:44 +00:00
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
want := golden(t, t.Name(), ui.OutputWriter.String())
|
|
|
|
require.Equal(t, want, ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
|
2020-11-10 17:18:21 +00:00
|
|
|
func TestSnapshotInspectKVDetailsDepthFilterCommand(t *testing.T) {
|
2020-11-05 17:01:44 +00:00
|
|
|
|
2020-11-10 17:18:21 +00:00
|
|
|
filepath := "./testdata/backupWithKV.snap"
|
2020-11-05 17:01:44 +00:00
|
|
|
|
|
|
|
// Inspect the snapshot
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
2020-11-10 17:18:21 +00:00
|
|
|
args := []string{"-kvdetails", "-kvdepth", "3", "-kvfilter", "vault/logical", filepath}
|
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
want := golden(t, t.Name(), ui.OutputWriter.String())
|
|
|
|
require.Equal(t, want, ui.OutputWriter.String())
|
|
|
|
}
|
2021-04-23 15:17:08 +00:00
|
|
|
|
|
|
|
// TestSnapshotInspectCommandRaw test reading a snaphost directly from a raft
|
|
|
|
// data dir.
|
|
|
|
func TestSnapshotInspectCommandRaw(t *testing.T) {
|
|
|
|
filepath := "./testdata/raw/state.bin"
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
args := []string{filepath}
|
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
want := golden(t, t.Name(), ui.OutputWriter.String())
|
|
|
|
require.Equal(t, want, ui.OutputWriter.String())
|
|
|
|
}
|
2021-04-23 19:48:10 +00:00
|
|
|
|
|
|
|
func TestSnapshotInspectInvalidFile(t *testing.T) {
|
|
|
|
// Attempt to open a non-snapshot file.
|
|
|
|
filepath := "./testdata/TestSnapshotInspectCommand.golden"
|
|
|
|
|
|
|
|
// Inspect the snapshot
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
args := []string{filepath}
|
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
// Just check it was an error code returned and not a panic - originally this
|
|
|
|
// would panic.
|
|
|
|
if code == 0 {
|
|
|
|
t.Fatalf("should return an error code")
|
|
|
|
}
|
|
|
|
}
|