open-nomad/command/agent_info_test.go
Seth Hoenig c5d36eaa2f cleanup: fixing warnings and refactoring of command package, part 2
This PR continues the cleanup of the command package, removing linter
warnings, refactoring to use helpers, making tests easier to read, etc.
2022-08-18 09:43:39 -05:00

79 lines
1.7 KiB
Go

package command
import (
"testing"
"github.com/hashicorp/nomad/ci"
"github.com/mitchellh/cli"
"github.com/shoenig/test/must"
)
func TestAgentInfoCommand_Implements(t *testing.T) {
ci.Parallel(t)
var _ cli.Command = &AgentInfoCommand{}
}
func TestAgentInfoCommand_Run(t *testing.T) {
ci.Parallel(t)
srv, _, url := testServer(t, false, nil)
defer stopTestAgent(srv)
ui := cli.NewMockUi()
cmd := &AgentInfoCommand{Meta: Meta{Ui: ui}}
code := cmd.Run([]string{"-address=" + url})
must.Zero(t, code)
}
func TestAgentInfoCommand_Run_JSON(t *testing.T) {
ci.Parallel(t)
srv, _, url := testServer(t, false, nil)
defer stopTestAgent(srv)
ui := cli.NewMockUi()
cmd := &AgentInfoCommand{Meta: Meta{Ui: ui}}
code := cmd.Run([]string{"-address=" + url, "-json"})
must.Zero(t, code)
out := ui.OutputWriter.String()
must.StrContains(t, out, `"config"`)
}
func TestAgentInfoCommand_Run_Gotemplate(t *testing.T) {
ci.Parallel(t)
srv, _, url := testServer(t, false, nil)
defer stopTestAgent(srv)
ui := cli.NewMockUi()
cmd := &AgentInfoCommand{Meta: Meta{Ui: ui}}
code := cmd.Run([]string{"-address=" + url, "-t", "{{.Stats.raft}}"})
must.Zero(t, code)
out := ui.OutputWriter.String()
must.StrContains(t, out, "last_log_index")
}
func TestAgentInfoCommand_Fails(t *testing.T) {
ci.Parallel(t)
ui := cli.NewMockUi()
cmd := &AgentInfoCommand{Meta: Meta{Ui: ui}}
// Fails on misuse
code := cmd.Run([]string{"some", "bad", "args"})
must.One(t, code)
out := ui.ErrorWriter.String()
must.StrContains(t, out, commandErrorText(cmd))
ui.ErrorWriter.Reset()
// Fails on connection failure
code = cmd.Run([]string{"-address=nope"})
must.One(t, code)
out = ui.ErrorWriter.String()
must.StrContains(t, out, "Error querying agent info")
}