60732577f5
* Use Colored UI if stdout is a tty * Add format options to operator unseal * Add format test on operator unseal * Add -no-color output flag, and use BasicUi if no-color flag is provided * Move seal status formatting logic to OutputSealStatus * Apply no-color to warnings from DeprecatedCommands as well * Add OutputWithFormat to support arbitrary data, add format option to auth list * Add ability to output arbitrary list data on TableFormatter * Clear up switch logic on format * Add format option for list-related commands * Add format option to rest of commands that returns a client API response * Remove initOutputYAML and initOutputJSON, and use OutputWithFormat instead * Remove outputAsYAML and outputAsJSON, and use OutputWithFormat instead * Remove -no-color flag, use env var exclusively to toggle colored output * Fix compile * Remove -no-color flag in main.go * Add missing FlagSetOutputFormat * Fix generate-root/decode test * Migrate init functions to main.go * Add no-color flag back as hidden * Handle non-supported data types for TableFormatter.OutputList * Pull formatting much further up to remove the need to use c.flagFormat (#3950) * Pull formatting much further up to remove the need to use c.flagFormat Also remove OutputWithFormat as the logic can cause issues. * Use const for env var * Minor updates * Remove unnecessary check * Fix SSH output and some tests * Fix tests * Make race detector not run on generate root since it kills Travis these days * Update docs * Update docs * Address review feedback * Handle --format as well as -format
134 lines
2.7 KiB
Go
134 lines
2.7 KiB
Go
package command
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/ghodss/yaml"
|
|
"github.com/hashicorp/vault/api"
|
|
"github.com/hashicorp/vault/helper/jsonutil"
|
|
)
|
|
|
|
var output string
|
|
|
|
type mockUi struct {
|
|
t *testing.T
|
|
SampleData string
|
|
}
|
|
|
|
func (m mockUi) Ask(_ string) (string, error) {
|
|
m.t.FailNow()
|
|
return "", nil
|
|
}
|
|
func (m mockUi) AskSecret(_ string) (string, error) {
|
|
m.t.FailNow()
|
|
return "", nil
|
|
}
|
|
func (m mockUi) Output(s string) { output = s }
|
|
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) }
|
|
|
|
func TestJsonFormatter(t *testing.T) {
|
|
os.Setenv(EnvVaultFormat, "json")
|
|
ui := mockUi{t: t, SampleData: "something"}
|
|
if err := outputWithFormat(ui, nil, ui); err != 0 {
|
|
t.Fatal(err)
|
|
}
|
|
var newUi mockUi
|
|
if err := jsonutil.DecodeJSON([]byte(output), &newUi); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if newUi.SampleData != ui.SampleData {
|
|
t.Fatalf(`values not equal ("%s" != "%s")`,
|
|
newUi.SampleData,
|
|
ui.SampleData)
|
|
}
|
|
}
|
|
|
|
func TestYamlFormatter(t *testing.T) {
|
|
os.Setenv(EnvVaultFormat, "yaml")
|
|
ui := mockUi{t: t, SampleData: "something"}
|
|
if err := outputWithFormat(ui, nil, ui); err != 0 {
|
|
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) {
|
|
os.Setenv(EnvVaultFormat, "table")
|
|
ui := mockUi{t: t}
|
|
s := api.Secret{Data: map[string]interface{}{"k": "something"}}
|
|
if err := outputWithFormat(ui, &s, &s); err != 0 {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(output, "something") {
|
|
t.Fatal("did not find 'something'")
|
|
}
|
|
}
|
|
|
|
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",
|
|
[]string{"-format", "json"},
|
|
"{",
|
|
0,
|
|
},
|
|
{
|
|
"format_bad",
|
|
[]string{"-format", "nope-not-real"},
|
|
"Invalid output format",
|
|
1,
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
client, closer := testVaultServer(t)
|
|
defer closer()
|
|
|
|
// Login with the token so we can renew-self.
|
|
token, _ := testTokenAndAccessor(t, client)
|
|
client.SetToken(token)
|
|
|
|
ui, cmd := testTokenRenewCommand(t)
|
|
cmd.client = client
|
|
|
|
tc.args = setupEnv(tc.args)
|
|
|
|
code := cmd.Run(tc.args)
|
|
if code != tc.code {
|
|
t.Errorf("expected %d to be %d", code, tc.code)
|
|
}
|
|
|
|
combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
|
|
if !strings.Contains(combined, tc.out) {
|
|
t.Errorf("expected %q to contain %q", combined, tc.out)
|
|
}
|
|
})
|
|
}
|
|
}
|