open-vault/command/monitor_test.go
Josh Black 6e92c8cbd2
Add a new "vault monitor" command (#8477)
Add a new "vault monitor" command

Co-authored-by: ncabatoff <ncabatoff@hashicorp.com>
Co-authored-by: Calvin Leung Huang <cleung2010@gmail.com>
Co-authored-by: Jeff Mitchell <jeffrey.mitchell@gmail.com>
2020-05-21 13:07:50 -07:00

100 lines
1.6 KiB
Go

package command
import (
"strings"
"sync/atomic"
"testing"
"time"
"github.com/hashicorp/vault/helper/testhelpers"
"github.com/mitchellh/cli"
)
func testMonitorCommand(tb testing.TB) (*cli.MockUi, *MonitorCommand) {
tb.Helper()
ui := cli.NewMockUi()
return ui, &MonitorCommand{
BaseCommand: &BaseCommand{
UI: ui,
},
}
}
func TestMonitorCommand_Run(t *testing.T) {
t.Parallel()
cases := []struct {
name string
args []string
out string
code int64
}{
{
"valid",
[]string{
"-log-level=debug",
},
"",
0,
},
{
"too_many_args",
[]string{
"-log-level=debug",
"foo",
},
"Too many arguments",
1,
},
{
"unknown_log_level",
[]string{
"-log-level=haha",
},
"haha is an unknown log level",
1,
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
client, closer := testVaultServer(t)
defer closer()
var code int64
shutdownCh := make(chan struct{})
ui, cmd := testMonitorCommand(t)
cmd.client = client
cmd.ShutdownCh = shutdownCh
stopCh := testhelpers.GenerateDebugLogs(t, client)
go func() {
atomic.StoreInt64(&code, int64(cmd.Run(tc.args)))
}()
select {
case <-time.After(3 * time.Second):
stopCh <- struct{}{}
close(shutdownCh)
}
if atomic.LoadInt64(&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.Fatalf("expected %q to contain %q", combined, tc.out)
}
<-stopCh
})
}
}