2015-03-14 03:17:55 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2017-09-05 04:05:27 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
2015-03-14 03:17:55 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
2017-09-08 02:03:12 +00:00
|
|
|
func testOperatorUnsealCommand(tb testing.TB) (*cli.MockUi, *OperatorUnsealCommand) {
|
2017-09-05 04:05:27 +00:00
|
|
|
tb.Helper()
|
2015-03-14 03:17:55 +00:00
|
|
|
|
2017-09-05 04:05:27 +00:00
|
|
|
ui := cli.NewMockUi()
|
2017-09-08 02:03:12 +00:00
|
|
|
return ui, &OperatorUnsealCommand{
|
2017-09-05 04:05:27 +00:00
|
|
|
BaseCommand: &BaseCommand{
|
|
|
|
UI: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 02:03:12 +00:00
|
|
|
func TestOperatorUnsealCommand_Run(t *testing.T) {
|
2017-09-05 04:05:27 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
t.Run("error_non_terminal", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2015-03-14 03:17:55 +00:00
|
|
|
|
2017-09-08 02:03:12 +00:00
|
|
|
ui, cmd := testOperatorUnsealCommand(t)
|
2017-09-05 04:05:27 +00:00
|
|
|
cmd.testOutput = ioutil.Discard
|
|
|
|
|
|
|
|
code := cmd.Run(nil)
|
|
|
|
if exp := 1; code != exp {
|
|
|
|
t.Errorf("expected %d to be %d", code, exp)
|
2017-01-17 20:43:10 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 04:05:27 +00:00
|
|
|
expected := "is not a terminal"
|
|
|
|
combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
|
|
|
|
if !strings.Contains(combined, expected) {
|
|
|
|
t.Errorf("expected %q to contain %q", combined, expected)
|
2017-01-17 20:43:10 +00:00
|
|
|
}
|
2017-09-05 04:05:27 +00:00
|
|
|
})
|
2015-03-14 03:17:55 +00:00
|
|
|
|
2017-09-05 04:05:27 +00:00
|
|
|
t.Run("reset", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2015-04-13 01:39:41 +00:00
|
|
|
|
2017-09-05 04:05:27 +00:00
|
|
|
client, keys, closer := testVaultServerUnseal(t)
|
|
|
|
defer closer()
|
2015-04-13 01:39:41 +00:00
|
|
|
|
2017-09-05 04:05:27 +00:00
|
|
|
// Seal so we can unseal
|
|
|
|
if err := client.Sys().Seal(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-13 01:39:41 +00:00
|
|
|
|
2017-09-05 04:05:27 +00:00
|
|
|
// Enter an unseal key
|
|
|
|
if _, err := client.Sys().Unseal(keys[0]); err != nil {
|
|
|
|
t.Fatal(err)
|
2017-01-17 20:43:10 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 02:03:12 +00:00
|
|
|
ui, cmd := testOperatorUnsealCommand(t)
|
2017-09-05 04:05:27 +00:00
|
|
|
cmd.client = client
|
|
|
|
cmd.testOutput = ioutil.Discard
|
|
|
|
|
|
|
|
// Reset and check output
|
|
|
|
code := cmd.Run([]string{
|
|
|
|
"-reset",
|
|
|
|
})
|
|
|
|
if exp := 0; code != exp {
|
|
|
|
t.Errorf("expected %d to be %d", code, exp)
|
2017-01-17 20:43:10 +00:00
|
|
|
}
|
2017-09-22 00:51:12 +00:00
|
|
|
expected := "0/3"
|
2017-09-05 04:05:27 +00:00
|
|
|
combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
|
|
|
|
if !strings.Contains(combined, expected) {
|
|
|
|
t.Errorf("expected %q to contain %q", combined, expected)
|
|
|
|
}
|
|
|
|
})
|
2015-04-13 01:39:41 +00:00
|
|
|
|
2017-09-05 04:05:27 +00:00
|
|
|
t.Run("full", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, keys, closer := testVaultServerUnseal(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
// Seal so we can unseal
|
|
|
|
if err := client.Sys().Seal(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-09-22 00:51:12 +00:00
|
|
|
for _, key := range keys {
|
2017-09-08 02:03:12 +00:00
|
|
|
ui, cmd := testOperatorUnsealCommand(t)
|
2017-09-05 04:05:27 +00:00
|
|
|
cmd.client = client
|
|
|
|
cmd.testOutput = ioutil.Discard
|
|
|
|
|
|
|
|
// Reset and check output
|
|
|
|
code := cmd.Run([]string{
|
|
|
|
key,
|
|
|
|
})
|
|
|
|
if exp := 0; code != exp {
|
2017-09-22 00:51:12 +00:00
|
|
|
t.Errorf("expected %d to be %d: %s", code, exp, ui.ErrorWriter.String())
|
2017-09-05 04:05:27 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-22 00:51:12 +00:00
|
|
|
|
|
|
|
status, err := client.Sys().SealStatus()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if status.Sealed {
|
|
|
|
t.Error("expected unsealed")
|
|
|
|
}
|
2017-09-05 04:05:27 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("communication_failure", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServerBad(t)
|
|
|
|
defer closer()
|
|
|
|
|
2017-09-08 02:03:12 +00:00
|
|
|
ui, cmd := testOperatorUnsealCommand(t)
|
2017-09-05 04:05:27 +00:00
|
|
|
cmd.client = client
|
|
|
|
|
|
|
|
code := cmd.Run([]string{
|
|
|
|
"abcd",
|
|
|
|
})
|
|
|
|
if exp := 2; code != exp {
|
|
|
|
t.Errorf("expected %d to be %d", code, exp)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := "Error unsealing: "
|
|
|
|
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 02:03:12 +00:00
|
|
|
_, cmd := testOperatorUnsealCommand(t)
|
2017-09-05 04:05:27 +00:00
|
|
|
assertNoTabs(t, cmd)
|
|
|
|
})
|
2015-04-13 01:39:41 +00:00
|
|
|
}
|