2016-02-12 18:21:42 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2018-05-23 20:45:17 +00:00
|
|
|
"bytes"
|
2020-09-23 17:30:01 +00:00
|
|
|
"fmt"
|
2018-02-12 23:12:16 +00:00
|
|
|
"os"
|
2016-02-12 18:21:42 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2020-12-12 00:50:19 +00:00
|
|
|
"time"
|
2016-04-01 17:16:05 +00:00
|
|
|
|
|
|
|
"github.com/ghodss/yaml"
|
|
|
|
"github.com/hashicorp/vault/api"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/jsonutil"
|
2016-02-12 18:21:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type mockUi struct {
|
|
|
|
t *testing.T
|
|
|
|
SampleData string
|
2023-01-26 16:47:42 +00:00
|
|
|
outputData *string
|
2016-02-12 18:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockUi) Ask(_ string) (string, error) {
|
|
|
|
m.t.FailNow()
|
|
|
|
return "", nil
|
|
|
|
}
|
2021-04-08 16:43:39 +00:00
|
|
|
|
2016-02-12 18:21:42 +00:00
|
|
|
func (m mockUi) AskSecret(_ string) (string, error) {
|
|
|
|
m.t.FailNow()
|
|
|
|
return "", nil
|
|
|
|
}
|
2023-01-26 16:47:42 +00:00
|
|
|
func (m mockUi) Output(s string) { *m.outputData = s }
|
2017-09-05 04:01:44 +00:00
|
|
|
func (m mockUi) Info(s string) { m.t.Log(s) }
|
|
|
|
func (m mockUi) Error(s string) { m.t.Log(s) }
|
|
|
|
func (m mockUi) Warn(s string) { m.t.Log(s) }
|
2016-02-12 18:21:42 +00:00
|
|
|
|
|
|
|
func TestJsonFormatter(t *testing.T) {
|
2018-02-12 23:12:16 +00:00
|
|
|
os.Setenv(EnvVaultFormat, "json")
|
2023-01-26 16:47:42 +00:00
|
|
|
var output string
|
|
|
|
ui := mockUi{t: t, SampleData: "something", outputData: &output}
|
2018-02-12 23:12:16 +00:00
|
|
|
if err := outputWithFormat(ui, nil, ui); err != 0 {
|
2016-02-12 18:21:42 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
var newUi mockUi
|
2016-07-06 16:25:40 +00:00
|
|
|
if err := jsonutil.DecodeJSON([]byte(output), &newUi); err != nil {
|
2016-02-12 18:21:42 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if newUi.SampleData != ui.SampleData {
|
|
|
|
t.Fatalf(`values not equal ("%s" != "%s")`,
|
|
|
|
newUi.SampleData,
|
|
|
|
ui.SampleData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestYamlFormatter(t *testing.T) {
|
2018-02-12 23:12:16 +00:00
|
|
|
os.Setenv(EnvVaultFormat, "yaml")
|
2023-01-26 16:47:42 +00:00
|
|
|
var output string
|
|
|
|
ui := mockUi{t: t, SampleData: "something", outputData: &output}
|
2018-02-12 23:12:16 +00:00
|
|
|
if err := outputWithFormat(ui, nil, ui); err != 0 {
|
2016-02-12 18:21:42 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
var newUi mockUi
|
|
|
|
err := yaml.Unmarshal([]byte(output), &newUi)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if newUi.SampleData != ui.SampleData {
|
|
|
|
t.Fatalf(`values not equal ("%s" != "%s")`,
|
|
|
|
newUi.SampleData,
|
|
|
|
ui.SampleData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTableFormatter(t *testing.T) {
|
2018-02-12 23:12:16 +00:00
|
|
|
os.Setenv(EnvVaultFormat, "table")
|
2023-01-26 16:47:42 +00:00
|
|
|
var output string
|
|
|
|
ui := mockUi{t: t, outputData: &output}
|
2020-09-23 17:30:01 +00:00
|
|
|
|
|
|
|
// Testing secret formatting
|
2016-02-12 18:21:42 +00:00
|
|
|
s := api.Secret{Data: map[string]interface{}{"k": "something"}}
|
2018-02-12 23:12:16 +00:00
|
|
|
if err := outputWithFormat(ui, &s, &s); err != 0 {
|
2016-02-12 18:21:42 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(output, "something") {
|
2016-06-20 19:26:33 +00:00
|
|
|
t.Fatal("did not find 'something'")
|
2016-02-12 18:21:42 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-12 23:12:16 +00:00
|
|
|
|
2020-09-23 17:30:01 +00:00
|
|
|
// TestStatusFormat tests to verify that the embedded struct
|
|
|
|
// SealStatusOutput ignores omitEmpty fields and prints out
|
|
|
|
// fields in the embedded struct explicitly. It also checks the spacing,
|
|
|
|
// indentation, and delimiters of table formatting explicitly.
|
|
|
|
func TestStatusFormat(t *testing.T) {
|
2023-01-26 16:47:42 +00:00
|
|
|
var output string
|
|
|
|
ui := mockUi{t: t, outputData: &output}
|
2020-09-23 17:30:01 +00:00
|
|
|
os.Setenv(EnvVaultFormat, "table")
|
|
|
|
|
|
|
|
statusHA := getMockStatusData(false)
|
|
|
|
statusOmitEmpty := getMockStatusData(true)
|
|
|
|
|
|
|
|
// Testing that HA fields are formatted properly for table.
|
|
|
|
// All fields (including new HA fields) are expected
|
|
|
|
if err := outputWithFormat(ui, nil, statusHA); err != 0 {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-01-27 18:06:34 +00:00
|
|
|
expectedOutputString := `Key Value
|
2020-09-23 17:30:01 +00:00
|
|
|
--- -----
|
|
|
|
Recovery Seal Type type
|
|
|
|
Initialized true
|
|
|
|
Sealed true
|
|
|
|
Total Recovery Shares 2
|
|
|
|
Threshold 1
|
|
|
|
Unseal Progress 3/1
|
|
|
|
Unseal Nonce nonce
|
|
|
|
Seal Migration in Progress true
|
|
|
|
Version version
|
2022-04-19 18:28:08 +00:00
|
|
|
Build Date build date
|
2020-09-23 17:30:01 +00:00
|
|
|
Storage Type storage type
|
|
|
|
Cluster Name cluster name
|
|
|
|
Cluster ID cluster id
|
|
|
|
HA Enabled true
|
|
|
|
Raft Committed Index 3
|
|
|
|
Raft Applied Index 4
|
2022-11-09 16:04:36 +00:00
|
|
|
Last WAL 2
|
|
|
|
Warnings [warning]`
|
2020-09-23 17:30:01 +00:00
|
|
|
|
|
|
|
if expectedOutputString != output {
|
|
|
|
fmt.Printf("%s\n%+v\n %s\n%+v\n", "output found was: ", output, "versus", expectedOutputString)
|
|
|
|
t.Fatal("format output for status does not match expected format. Check print statements above.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Testing that omitEmpty fields are omitted from status
|
|
|
|
// no HA fields are expected, except HA Enabled
|
|
|
|
if err := outputWithFormat(ui, nil, statusOmitEmpty); err != 0 {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-01-27 18:06:34 +00:00
|
|
|
expectedOutputString = `Key Value
|
2020-09-23 17:30:01 +00:00
|
|
|
--- -----
|
|
|
|
Recovery Seal Type type
|
|
|
|
Initialized true
|
|
|
|
Sealed true
|
|
|
|
Total Recovery Shares 2
|
|
|
|
Threshold 1
|
|
|
|
Unseal Progress 3/1
|
|
|
|
Unseal Nonce nonce
|
|
|
|
Seal Migration in Progress true
|
|
|
|
Version version
|
2022-04-19 18:28:08 +00:00
|
|
|
Build Date build date
|
2020-09-23 17:30:01 +00:00
|
|
|
Storage Type n/a
|
|
|
|
HA Enabled false`
|
|
|
|
|
|
|
|
if expectedOutputString != output {
|
|
|
|
fmt.Printf("%s\n%+v\n %s\n%+v\n", "output found was: ", output, "versus", expectedOutputString)
|
|
|
|
t.Fatal("format output for status does not match expected format. Check print statements above.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getMockStatusData outputs a SealStatusOutput struct from format.go to be used
|
|
|
|
// for testing. The emptyfields parameter specifies whether the struct will be
|
|
|
|
// initialized with all the omitempty fields as empty or not.
|
|
|
|
func getMockStatusData(emptyFields bool) SealStatusOutput {
|
|
|
|
var status SealStatusOutput
|
|
|
|
var sealStatusResponseMock api.SealStatusResponse
|
|
|
|
if !emptyFields {
|
|
|
|
sealStatusResponseMock = api.SealStatusResponse{
|
|
|
|
Type: "type",
|
|
|
|
Initialized: true,
|
|
|
|
Sealed: true,
|
|
|
|
T: 1,
|
|
|
|
N: 2,
|
|
|
|
Progress: 3,
|
|
|
|
Nonce: "nonce",
|
|
|
|
Version: "version",
|
2022-04-19 18:28:08 +00:00
|
|
|
BuildDate: "build date",
|
2020-09-23 17:30:01 +00:00
|
|
|
Migration: true,
|
|
|
|
ClusterName: "cluster name",
|
|
|
|
ClusterID: "cluster id",
|
|
|
|
RecoverySeal: true,
|
|
|
|
StorageType: "storage type",
|
2022-11-09 16:04:36 +00:00
|
|
|
Warnings: []string{"warning"},
|
2020-09-23 17:30:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// must initialize this struct without explicit field names due to embedding
|
|
|
|
status = SealStatusOutput{
|
|
|
|
sealStatusResponseMock,
|
|
|
|
true, // HAEnabled
|
|
|
|
true, // IsSelf
|
2020-12-12 00:50:19 +00:00
|
|
|
time.Time{}.UTC(), // ActiveTime
|
2020-09-23 17:30:01 +00:00
|
|
|
"leader address", // LeaderAddress
|
|
|
|
"leader cluster address", // LeaderClusterAddress
|
|
|
|
true, // PerfStandby
|
|
|
|
1, // PerfStandbyLastRemoteWAL
|
|
|
|
2, // LastWAL
|
|
|
|
3, // RaftCommittedIndex
|
|
|
|
4, // RaftAppliedIndex
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sealStatusResponseMock = api.SealStatusResponse{
|
|
|
|
Type: "type",
|
|
|
|
Initialized: true,
|
|
|
|
Sealed: true,
|
|
|
|
T: 1,
|
|
|
|
N: 2,
|
|
|
|
Progress: 3,
|
|
|
|
Nonce: "nonce",
|
|
|
|
Version: "version",
|
2022-04-19 18:28:08 +00:00
|
|
|
BuildDate: "build date",
|
2020-09-23 17:30:01 +00:00
|
|
|
Migration: true,
|
|
|
|
ClusterName: "",
|
|
|
|
ClusterID: "",
|
|
|
|
RecoverySeal: true,
|
|
|
|
StorageType: "",
|
|
|
|
}
|
|
|
|
|
|
|
|
// must initialize this struct without explicit field names due to embedding
|
|
|
|
status = SealStatusOutput{
|
|
|
|
sealStatusResponseMock,
|
2020-12-12 00:50:19 +00:00
|
|
|
false, // HAEnabled
|
|
|
|
false, // IsSelf
|
|
|
|
time.Time{}.UTC(), // ActiveTime
|
|
|
|
"", // LeaderAddress
|
|
|
|
"", // LeaderClusterAddress
|
|
|
|
false, // PerfStandby
|
|
|
|
0, // PerfStandbyLastRemoteWAL
|
|
|
|
0, // LastWAL
|
|
|
|
0, // RaftCommittedIndex
|
|
|
|
0, // RaftAppliedIndex
|
2020-09-23 17:30:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return status
|
|
|
|
}
|
|
|
|
|
2018-02-12 23:12:16 +00:00
|
|
|
func Test_Format_Parsing(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
os.Setenv(EnvVaultCLINoColor, "")
|
|
|
|
os.Setenv(EnvVaultFormat, "")
|
|
|
|
}()
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
out string
|
|
|
|
code int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"format",
|
2018-05-23 20:45:17 +00:00
|
|
|
[]string{"token", "renew", "-format", "json"},
|
2018-02-12 23:12:16 +00:00
|
|
|
"{",
|
|
|
|
0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"format_bad",
|
2018-05-23 20:45:17 +00:00
|
|
|
[]string{"token", "renew", "-format", "nope-not-real"},
|
2018-02-12 23:12:16 +00:00
|
|
|
"Invalid output format",
|
|
|
|
1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
2018-05-23 20:45:17 +00:00
|
|
|
stdout := bytes.NewBuffer(nil)
|
|
|
|
stderr := bytes.NewBuffer(nil)
|
|
|
|
runOpts := &RunOptions{
|
|
|
|
Stdout: stdout,
|
|
|
|
Stderr: stderr,
|
|
|
|
Client: client,
|
|
|
|
}
|
|
|
|
|
2018-02-12 23:12:16 +00:00
|
|
|
// Login with the token so we can renew-self.
|
|
|
|
token, _ := testTokenAndAccessor(t, client)
|
|
|
|
client.SetToken(token)
|
|
|
|
|
2018-05-23 20:45:17 +00:00
|
|
|
code := RunCustom(tc.args, runOpts)
|
2018-02-12 23:12:16 +00:00
|
|
|
if code != tc.code {
|
|
|
|
t.Errorf("expected %d to be %d", code, tc.code)
|
|
|
|
}
|
|
|
|
|
2018-05-23 20:45:17 +00:00
|
|
|
combined := stdout.String() + stderr.String()
|
2018-02-12 23:12:16 +00:00
|
|
|
if !strings.Contains(combined, tc.out) {
|
|
|
|
t.Errorf("expected %q to contain %q", combined, tc.out)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|