2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-09-12 20:55:51 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2015-09-15 18:20:08 +00:00
|
|
|
"fmt"
|
2015-09-12 23:36:44 +00:00
|
|
|
"strings"
|
2015-09-12 20:55:51 +00:00
|
|
|
"testing"
|
2018-05-02 23:19:15 +00:00
|
|
|
"time"
|
2015-09-12 20:55:51 +00:00
|
|
|
|
2018-05-02 23:19:15 +00:00
|
|
|
"github.com/hashicorp/nomad/api"
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2017-07-21 04:07:32 +00:00
|
|
|
"github.com/hashicorp/nomad/command/agent"
|
2015-09-15 18:20:08 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2015-09-12 20:55:51 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2017-08-15 18:30:23 +00:00
|
|
|
"github.com/posener/complete"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2015-09-12 20:55:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNodeStatusCommand_Implements(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-09-12 20:55:51 +00:00
|
|
|
var _ cli.Command = &NodeStatusCommand{}
|
|
|
|
}
|
2015-09-12 23:36:44 +00:00
|
|
|
|
2016-03-29 19:36:24 +00:00
|
|
|
func TestNodeStatusCommand_Self(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2016-03-29 19:36:24 +00:00
|
|
|
// Start in dev mode so we get a node registration
|
2017-07-21 04:07:32 +00:00
|
|
|
srv, client, url := testServer(t, true, func(c *agent.Config) {
|
2016-03-29 19:36:24 +00:00
|
|
|
c.NodeName = "mynode"
|
|
|
|
})
|
2017-07-21 04:07:32 +00:00
|
|
|
defer srv.Shutdown()
|
2016-03-29 19:36:24 +00:00
|
|
|
|
2020-10-05 14:07:41 +00:00
|
|
|
ui := cli.NewMockUi()
|
2016-03-29 19:36:24 +00:00
|
|
|
cmd := &NodeStatusCommand{Meta: Meta{Ui: ui}}
|
|
|
|
|
|
|
|
// Wait for a node to appear
|
|
|
|
var nodeID string
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
nodes, _, err := client.Nodes().List(nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if len(nodes) == 0 {
|
|
|
|
return false, fmt.Errorf("missing node")
|
|
|
|
}
|
|
|
|
nodeID = nodes[0].ID
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Query self node
|
|
|
|
if code := cmd.Run([]string{"-address=" + url, "-self"}); code != 0 {
|
|
|
|
t.Fatalf("expected exit 0, got: %d", code)
|
|
|
|
}
|
|
|
|
out := ui.OutputWriter.String()
|
|
|
|
if !strings.Contains(out, "mynode") {
|
|
|
|
t.Fatalf("expect to find mynode, got: %s", out)
|
|
|
|
}
|
2017-07-07 20:55:39 +00:00
|
|
|
if !strings.Contains(out, "No allocations placed") {
|
2016-03-29 19:36:24 +00:00
|
|
|
t.Fatalf("should not dump allocations")
|
|
|
|
}
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
|
|
|
|
// Request full id output
|
|
|
|
if code := cmd.Run([]string{"-address=" + url, "-self", "-verbose"}); code != 0 {
|
|
|
|
t.Fatalf("expected exit 0, got: %d", code)
|
|
|
|
}
|
|
|
|
out = ui.OutputWriter.String()
|
|
|
|
if !strings.Contains(out, nodeID) {
|
|
|
|
t.Fatalf("expected full node id %q, got: %s", nodeID, out)
|
|
|
|
}
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
}
|
|
|
|
|
2015-09-12 23:36:44 +00:00
|
|
|
func TestNodeStatusCommand_Run(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-09-15 18:20:08 +00:00
|
|
|
// Start in dev mode so we get a node registration
|
2017-07-21 04:07:32 +00:00
|
|
|
srv, client, url := testServer(t, true, func(c *agent.Config) {
|
2015-09-15 18:20:08 +00:00
|
|
|
c.NodeName = "mynode"
|
|
|
|
})
|
2017-07-21 04:07:32 +00:00
|
|
|
defer srv.Shutdown()
|
2015-09-12 23:36:44 +00:00
|
|
|
|
2020-10-05 14:07:41 +00:00
|
|
|
ui := cli.NewMockUi()
|
2015-09-14 20:13:52 +00:00
|
|
|
cmd := &NodeStatusCommand{Meta: Meta{Ui: ui}}
|
2015-09-12 23:36:44 +00:00
|
|
|
|
2015-09-15 18:20:08 +00:00
|
|
|
// Wait for a node to appear
|
|
|
|
var nodeID string
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
nodes, _, err := client.Nodes().List(nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if len(nodes) == 0 {
|
|
|
|
return false, fmt.Errorf("missing node")
|
|
|
|
}
|
|
|
|
nodeID = nodes[0].ID
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
})
|
|
|
|
|
2015-09-12 23:36:44 +00:00
|
|
|
// Query all node statuses
|
2015-09-14 20:13:52 +00:00
|
|
|
if code := cmd.Run([]string{"-address=" + url}); code != 0 {
|
2015-09-12 23:36:44 +00:00
|
|
|
t.Fatalf("expected exit 0, got: %d", code)
|
|
|
|
}
|
2015-09-15 18:20:08 +00:00
|
|
|
out := ui.OutputWriter.String()
|
|
|
|
if !strings.Contains(out, "mynode") {
|
|
|
|
t.Fatalf("expect to find mynode, got: %s", out)
|
|
|
|
}
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
|
|
|
|
// Query a single node
|
|
|
|
if code := cmd.Run([]string{"-address=" + url, nodeID}); code != 0 {
|
|
|
|
t.Fatalf("expected exit 0, got: %d", code)
|
|
|
|
}
|
|
|
|
out = ui.OutputWriter.String()
|
|
|
|
if !strings.Contains(out, "mynode") {
|
|
|
|
t.Fatalf("expect to find mynode, got: %s", out)
|
|
|
|
}
|
|
|
|
ui.OutputWriter.Reset()
|
2015-09-12 23:36:44 +00:00
|
|
|
|
2015-09-15 18:20:08 +00:00
|
|
|
// Query single node in short view
|
|
|
|
if code := cmd.Run([]string{"-address=" + url, "-short", nodeID}); code != 0 {
|
|
|
|
t.Fatalf("expected exit 0, got: %d", code)
|
|
|
|
}
|
|
|
|
out = ui.OutputWriter.String()
|
|
|
|
if !strings.Contains(out, "mynode") {
|
|
|
|
t.Fatalf("expect to find mynode, got: %s", out)
|
|
|
|
}
|
2017-07-07 20:55:39 +00:00
|
|
|
if !strings.Contains(out, "No allocations placed") {
|
2015-09-15 18:20:08 +00:00
|
|
|
t.Fatalf("should not dump allocations")
|
2015-09-12 23:36:44 +00:00
|
|
|
}
|
2015-12-22 22:44:33 +00:00
|
|
|
|
2017-08-29 17:09:30 +00:00
|
|
|
// Query a single node based on a prefix that is even without the hyphen
|
|
|
|
if code := cmd.Run([]string{"-address=" + url, nodeID[:13]}); code != 0 {
|
2015-12-22 22:44:33 +00:00
|
|
|
t.Fatalf("expected exit 0, got: %d", code)
|
|
|
|
}
|
|
|
|
out = ui.OutputWriter.String()
|
|
|
|
if !strings.Contains(out, "mynode") {
|
|
|
|
t.Fatalf("expect to find mynode, got: %s", out)
|
|
|
|
}
|
2019-10-04 17:06:12 +00:00
|
|
|
if !strings.Contains(out, nodeID) {
|
|
|
|
t.Fatalf("expected node id %q, got: %s", nodeID, out)
|
2016-01-14 20:57:43 +00:00
|
|
|
}
|
2015-12-22 22:44:33 +00:00
|
|
|
ui.OutputWriter.Reset()
|
2016-01-14 20:57:43 +00:00
|
|
|
|
|
|
|
// Request full id output
|
2016-01-15 22:32:38 +00:00
|
|
|
if code := cmd.Run([]string{"-address=" + url, "-verbose", nodeID[:4]}); code != 0 {
|
2016-01-14 20:57:43 +00:00
|
|
|
t.Fatalf("expected exit 0, got: %d", code)
|
|
|
|
}
|
|
|
|
out = ui.OutputWriter.String()
|
|
|
|
if !strings.Contains(out, nodeID) {
|
|
|
|
t.Fatalf("expected full node id %q, got: %s", nodeID, out)
|
|
|
|
}
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
|
2016-01-21 21:21:35 +00:00
|
|
|
// Identifiers with uneven length should produce a query result
|
|
|
|
if code := cmd.Run([]string{"-address=" + url, nodeID[:3]}); code != 0 {
|
|
|
|
t.Fatalf("expected exit 0, got: %d", code)
|
|
|
|
}
|
|
|
|
out = ui.OutputWriter.String()
|
|
|
|
if !strings.Contains(out, "mynode") {
|
|
|
|
t.Fatalf("expect to find mynode, got: %s", out)
|
|
|
|
}
|
2015-09-12 23:36:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeStatusCommand_Fails(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)
|
|
|
|
defer srv.Shutdown()
|
2015-09-12 23:36:44 +00:00
|
|
|
|
2020-10-05 14:07:41 +00:00
|
|
|
ui := cli.NewMockUi()
|
2015-09-14 20:13:52 +00:00
|
|
|
cmd := &NodeStatusCommand{Meta: Meta{Ui: ui}}
|
2015-09-12 23:36:44 +00:00
|
|
|
|
|
|
|
// Fails on misuse
|
|
|
|
if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
|
|
|
|
t.Fatalf("expected exit code 1, got: %d", code)
|
|
|
|
}
|
2018-04-18 17:51:17 +00:00
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
|
2015-09-12 23:36:44 +00:00
|
|
|
t.Fatalf("expected help output, got: %s", out)
|
|
|
|
}
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
|
|
|
|
// Fails on connection failure
|
2015-09-14 20:13:52 +00:00
|
|
|
if code := cmd.Run([]string{"-address=nope"}); code != 1 {
|
2015-09-12 23:36:44 +00:00
|
|
|
t.Fatalf("expected exit code 1, got: %d", code)
|
|
|
|
}
|
2015-09-14 20:13:52 +00:00
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error querying node status") {
|
2015-09-12 23:36:44 +00:00
|
|
|
t.Fatalf("expected failed query error, got: %s", out)
|
|
|
|
}
|
2015-12-22 22:44:33 +00:00
|
|
|
ui.ErrorWriter.Reset()
|
2015-09-12 23:36:44 +00:00
|
|
|
|
2018-03-12 18:26:37 +00:00
|
|
|
// Fails on nonexistent node
|
2016-01-14 20:57:43 +00:00
|
|
|
if code := cmd.Run([]string{"-address=" + url, "12345678-abcd-efab-cdef-123456789abc"}); code != 1 {
|
2015-09-12 23:36:44 +00:00
|
|
|
t.Fatalf("expected exit 1, got: %d", code)
|
|
|
|
}
|
2015-12-24 10:46:59 +00:00
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "No node(s) with prefix") {
|
2015-09-12 23:36:44 +00:00
|
|
|
t.Fatalf("expected not found error, got: %s", out)
|
|
|
|
}
|
2016-01-21 19:53:05 +00:00
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
|
2016-01-21 21:21:35 +00:00
|
|
|
// Fail on identifier with too few characters
|
|
|
|
if code := cmd.Run([]string{"-address=" + url, "1"}); code != 1 {
|
2016-01-21 19:53:05 +00:00
|
|
|
t.Fatalf("expected exit 1, got: %d", code)
|
|
|
|
}
|
2016-01-21 21:21:35 +00:00
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "must contain at least two characters.") {
|
|
|
|
t.Fatalf("expected too few characters error, got: %s", out)
|
2016-01-21 19:53:05 +00:00
|
|
|
}
|
2016-08-06 12:38:41 +00:00
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
|
|
|
|
// Failed on both -json and -t options are specified
|
|
|
|
if code := cmd.Run([]string{"-address=" + url, "-json", "-t", "{{.ID}}"}); code != 1 {
|
|
|
|
t.Fatalf("expected exit 1, got: %d", code)
|
|
|
|
}
|
2017-07-07 02:20:57 +00:00
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Both json and template formatting are not allowed") {
|
2016-08-06 12:38:41 +00:00
|
|
|
t.Fatalf("expected getting formatter error, got: %s", out)
|
|
|
|
}
|
2022-04-05 19:53:43 +00:00
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
|
|
|
|
// Fail if -quiet is passed with -verbose
|
|
|
|
if code := cmd.Run([]string{"-address=" + url, "-quiet", "-verbose"}); code != 1 {
|
|
|
|
t.Fatalf("expected exit 1, got: %d", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "-quiet cannot be used with -verbose or -json") {
|
|
|
|
t.Fatalf("expected getting formatter error, got: %s", out)
|
|
|
|
}
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
|
|
|
|
// Fail if -quiet is passed with -json
|
|
|
|
if code := cmd.Run([]string{"-address=" + url, "-quiet", "-json"}); code != 1 {
|
|
|
|
t.Fatalf("expected exit 1, got: %d", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "-quiet cannot be used with -verbose or -json") {
|
|
|
|
t.Fatalf("expected getting formatter error, got: %s", out)
|
|
|
|
}
|
2015-09-12 23:36:44 +00:00
|
|
|
}
|
2017-08-15 18:30:23 +00:00
|
|
|
|
|
|
|
func TestNodeStatusCommand_AutocompleteArgs(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-08-15 18:30:23 +00:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
srv, client, url := testServer(t, true, nil)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
|
|
|
|
// Wait for a node to appear
|
|
|
|
var nodeID string
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
nodes, _, err := client.Nodes().List(nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if len(nodes) == 0 {
|
|
|
|
return false, fmt.Errorf("missing node")
|
|
|
|
}
|
|
|
|
nodeID = nodes[0].ID
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
})
|
|
|
|
|
2020-10-05 14:07:41 +00:00
|
|
|
ui := cli.NewMockUi()
|
2017-08-15 18:30:23 +00:00
|
|
|
cmd := &NodeStatusCommand{Meta: Meta{Ui: ui, flagAddress: url}}
|
|
|
|
|
|
|
|
prefix := nodeID[:len(nodeID)-5]
|
|
|
|
args := complete.Args{Last: prefix}
|
|
|
|
predictor := cmd.AutocompleteArgs()
|
|
|
|
|
|
|
|
res := predictor.Predict(args)
|
|
|
|
assert.Equal(1, len(res))
|
|
|
|
assert.Equal(nodeID, res[0])
|
|
|
|
}
|
2018-05-02 23:19:15 +00:00
|
|
|
|
|
|
|
func TestNodeStatusCommand_FormatDrain(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-05-02 23:19:15 +00:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
node := &api.Node{}
|
|
|
|
|
|
|
|
assert.Equal("false", formatDrain(node))
|
|
|
|
|
|
|
|
node.DrainStrategy = &api.DrainStrategy{}
|
|
|
|
assert.Equal("true; no deadline", formatDrain(node))
|
|
|
|
|
2018-06-06 20:05:39 +00:00
|
|
|
node.DrainStrategy = &api.DrainStrategy{}
|
|
|
|
node.DrainStrategy.Deadline = -1 * time.Second
|
|
|
|
assert.Equal("true; force drain", formatDrain(node))
|
2018-05-02 23:19:15 +00:00
|
|
|
|
2018-06-06 20:05:39 +00:00
|
|
|
// formatTime special cases Unix(0, 0), so increment by 1
|
|
|
|
node.DrainStrategy = &api.DrainStrategy{}
|
|
|
|
node.DrainStrategy.ForceDeadline = time.Unix(1, 0).UTC()
|
2018-05-02 23:19:15 +00:00
|
|
|
assert.Equal("true; 1970-01-01T00:00:01Z deadline", formatDrain(node))
|
|
|
|
|
|
|
|
node.DrainStrategy.IgnoreSystemJobs = true
|
|
|
|
assert.Equal("true; 1970-01-01T00:00:01Z deadline; ignoring system jobs", formatDrain(node))
|
|
|
|
}
|