2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-09-11 18:10:20 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2015-09-11 18:10:20 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2022-08-18 13:51:53 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2015-09-11 18:10:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAgentInfoCommand_Implements(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-09-11 18:10:20 +00:00
|
|
|
var _ cli.Command = &AgentInfoCommand{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgentInfoCommand_Run(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-07-21 04:07:32 +00:00
|
|
|
srv, _, url := testServer(t, false, nil)
|
2022-12-21 14:23:58 +00:00
|
|
|
defer srv.Shutdown()
|
2015-09-12 21:50:05 +00:00
|
|
|
|
2020-10-05 14:07:41 +00:00
|
|
|
ui := cli.NewMockUi()
|
2015-09-14 20:13:52 +00:00
|
|
|
cmd := &AgentInfoCommand{Meta: Meta{Ui: ui}}
|
2015-09-12 21:50:05 +00:00
|
|
|
|
2015-09-14 20:13:52 +00:00
|
|
|
code := cmd.Run([]string{"-address=" + url})
|
2022-08-18 13:51:53 +00:00
|
|
|
must.Zero(t, code)
|
2015-09-12 21:50:05 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 14:42:46 +00:00
|
|
|
func TestAgentInfoCommand_Run_JSON(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2021-01-13 14:42:46 +00:00
|
|
|
srv, _, url := testServer(t, false, nil)
|
2022-12-21 14:23:58 +00:00
|
|
|
defer srv.Shutdown()
|
2021-01-13 14:42:46 +00:00
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := &AgentInfoCommand{Meta: Meta{Ui: ui}}
|
|
|
|
|
|
|
|
code := cmd.Run([]string{"-address=" + url, "-json"})
|
2022-08-18 13:51:53 +00:00
|
|
|
must.Zero(t, code)
|
|
|
|
|
|
|
|
out := ui.OutputWriter.String()
|
|
|
|
must.StrContains(t, out, `"config"`)
|
2021-01-13 14:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgentInfoCommand_Run_Gotemplate(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2021-01-13 14:42:46 +00:00
|
|
|
srv, _, url := testServer(t, false, nil)
|
2022-12-21 14:23:58 +00:00
|
|
|
defer srv.Shutdown()
|
2021-01-13 14:42:46 +00:00
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := &AgentInfoCommand{Meta: Meta{Ui: ui}}
|
|
|
|
|
|
|
|
code := cmd.Run([]string{"-address=" + url, "-t", "{{.Stats.raft}}"})
|
2022-08-18 13:51:53 +00:00
|
|
|
must.Zero(t, code)
|
|
|
|
|
|
|
|
out := ui.OutputWriter.String()
|
|
|
|
must.StrContains(t, out, "last_log_index")
|
2021-01-13 14:42:46 +00:00
|
|
|
}
|
|
|
|
|
2015-09-12 21:50:05 +00:00
|
|
|
func TestAgentInfoCommand_Fails(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-10-05 14:07:41 +00:00
|
|
|
ui := cli.NewMockUi()
|
2015-09-14 20:13:52 +00:00
|
|
|
cmd := &AgentInfoCommand{Meta: Meta{Ui: ui}}
|
2015-09-12 21:50:05 +00:00
|
|
|
|
|
|
|
// Fails on misuse
|
2022-08-18 13:51:53 +00:00
|
|
|
code := cmd.Run([]string{"some", "bad", "args"})
|
|
|
|
must.One(t, code)
|
|
|
|
|
|
|
|
out := ui.ErrorWriter.String()
|
|
|
|
must.StrContains(t, out, commandErrorText(cmd))
|
|
|
|
|
2015-09-12 23:36:44 +00:00
|
|
|
ui.ErrorWriter.Reset()
|
2015-09-12 21:50:05 +00:00
|
|
|
|
|
|
|
// Fails on connection failure
|
2022-08-18 13:51:53 +00:00
|
|
|
code = cmd.Run([]string{"-address=nope"})
|
|
|
|
must.One(t, code)
|
|
|
|
|
|
|
|
out = ui.ErrorWriter.String()
|
|
|
|
must.StrContains(t, out, "Error querying agent info")
|
2015-09-11 18:10:20 +00:00
|
|
|
}
|