open-vault/command/lease_revoke_test.go

135 lines
2.5 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-08 01:59:31 +00:00
func testLeaseRevokeCommand(tb testing.TB) (*cli.MockUi, *LeaseRevokeCommand) {
2017-09-05 04:04:05 +00:00
tb.Helper()
2015-04-01 02:21:02 +00:00
2017-09-05 04:04:05 +00:00
ui := cli.NewMockUi()
2017-09-08 01:59:31 +00:00
return ui, &LeaseRevokeCommand{
2017-09-05 04:04:05 +00:00
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-08 01:59:31 +00:00
func TestLeaseRevokeCommand_Run(t *testing.T) {
2017-09-05 04:04:05 +00:00
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)
}
2017-09-08 01:59:31 +00:00
ui, cmd := testLeaseRevokeCommand(t)
2017-09-05 04:04:05 +00:00
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()
2017-09-08 01:59:31 +00:00
ui, cmd := testLeaseRevokeCommand(t)
2017-09-05 04:04:05 +00:00
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()
2017-09-08 01:59:31 +00:00
_, cmd := testLeaseRevokeCommand(t)
2017-09-05 04:04:05 +00:00
assertNoTabs(t, cmd)
})
2015-04-01 02:21:02 +00:00
}