From c91f8da7f05dead5fa023cb37a14f051ca9fac84 Mon Sep 17 00:00:00 2001 From: Danielle Lancashire Date: Tue, 10 Dec 2019 13:03:37 +0100 Subject: [PATCH] command: error when no node is found for `monitor` Currently `nomad monitor -node-id` will panic when a node-id does not match any nodes, as there is no empty result bounds checking. Here we return an error to the user when no nodes are found. --- command/agent_monitor.go | 5 +++++ command/agent_monitor_test.go | 11 ++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/command/agent_monitor.go b/command/agent_monitor.go index 8fd2fdf19eb7..1d11792ddb50 100644 --- a/command/agent_monitor.go +++ b/command/agent_monitor.go @@ -106,6 +106,11 @@ func (c *MonitorCommand) Run(args []string) int { return 1 } + if len(nodes) == 0 { + c.Ui.Error(fmt.Sprintf("No node(s) with prefix or id %q found", nodeID)) + return 1 + } + if len(nodes) > 1 { out := formatNodeStubList(nodes, false) c.Ui.Output(fmt.Sprintf("Prefix matched multiple nodes\n\n%s", out)) diff --git a/command/agent_monitor_test.go b/command/agent_monitor_test.go index 24109b280486..3a31fc6c652d 100644 --- a/command/agent_monitor_test.go +++ b/command/agent_monitor_test.go @@ -14,7 +14,7 @@ func TestMonitorCommand_Implements(t *testing.T) { func TestMonitorCommand_Fails(t *testing.T) { t.Parallel() - srv, _, _ := testServer(t, false, nil) + srv, _, url := testServer(t, false, nil) defer srv.Shutdown() ui := new(cli.MockUi) @@ -33,4 +33,13 @@ func TestMonitorCommand_Fails(t *testing.T) { if code := cmd.Run([]string{"-address=nope"}); code != 1 { t.Fatalf("exepected exit code 1, got: %d", code) } + + // Fails on nonexistent node + if code := cmd.Run([]string{"-address=" + url, "-node-id=12345678-abcd-efab-cdef-123456789abc"}); code != 1 { + t.Fatalf("expected exit 1, got: %d", code) + } + if out := ui.ErrorWriter.String(); !strings.Contains(out, "No node(s) with prefix") { + t.Fatalf("expected not found error, got: %s", out) + } + ui.ErrorWriter.Reset() }