open-vault/command/revoke_test.go

135 lines
2.4 KiB
Go
Raw Normal View History

2015-04-01 02:21:02 +00:00
package command
import (
2017-09-05 04:04:05 +00:00
"strings"
2015-04-01 02:21:02 +00:00
"testing"
2017-09-05 04:04:05 +00:00
"github.com/hashicorp/vault/api"
2015-04-01 02:21:02 +00:00
"github.com/mitchellh/cli"
)
2017-09-05 04:04:05 +00:00
func testRevokeCommand(tb testing.TB) (*cli.MockUi, *RevokeCommand) {
tb.Helper()
2015-04-01 02:21:02 +00:00
2017-09-05 04:04:05 +00:00
ui := cli.NewMockUi()
return ui, &RevokeCommand{
BaseCommand: &BaseCommand{
UI: ui,
2015-04-01 02:21:02 +00:00
},
}
2017-09-05 04:04:05 +00:00
}
2015-04-01 02:21:02 +00:00
2017-09-05 04:04:05 +00:00
func TestRevokeCommand_Run(t *testing.T) {
t.Parallel()
2015-04-01 02:21:02 +00:00
2017-09-05 04:04:05 +00:00
cases := []struct {
name string
args []string
out string
code int
}{
{
"force_without_prefix",
[]string{"-force"},
"requires also specifying -prefix",
1,
},
{
"single",
nil,
"Success",
0,
},
{
"force_prefix",
[]string{"-force", "-prefix"},
"Success",
0,
},
{
"prefix",
[]string{"-prefix"},
"Success",
0,
},
2015-04-01 02:21:02 +00:00
}
2017-09-05 04:04:05 +00:00
t.Run("validations", func(t *testing.T) {
t.Parallel()
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
client, closer := testVaultServer(t)
defer closer()
if err := client.Sys().Mount("secret-leased", &api.MountInput{
Type: "generic-leased",
}); err != nil {
t.Fatal(err)
}
path := "secret-leased/revoke/" + tc.name
data := map[string]interface{}{
"key": "value",
"lease": "1m",
}
if _, err := client.Logical().Write(path, data); err != nil {
t.Fatal(err)
}
secret, err := client.Logical().Read(path)
if err != nil {
t.Fatal(err)
}
ui, cmd := testRevokeCommand(t)
cmd.client = client
tc.args = append(tc.args, secret.LeaseID)
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)
}
})
}
})
t.Run("communication_failure", func(t *testing.T) {
t.Parallel()
client, closer := testVaultServerBad(t)
defer closer()
ui, cmd := testRevokeCommand(t)
cmd.client = client
code := cmd.Run([]string{
"foo/bar",
})
if exp := 2; code != exp {
t.Errorf("expected %d to be %d", code, exp)
}
expected := "Error revoking lease foo/bar: "
combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
if !strings.Contains(combined, expected) {
t.Errorf("expected %q to contain %q", combined, expected)
}
})
t.Run("no_tabs", func(t *testing.T) {
t.Parallel()
_, cmd := testRevokeCommand(t)
assertNoTabs(t, cmd)
})
2015-04-01 02:21:02 +00:00
}