From de3bc00a00aa78e970437202d4cac80c07565534 Mon Sep 17 00:00:00 2001 From: Ivo Verberk Date: Thu, 21 Jan 2016 20:53:05 +0100 Subject: [PATCH] Detect a half-byte prefix and display a user-friendly error. --- command/alloc_status.go | 4 ++++ command/alloc_status_test.go | 10 +++++++++- command/monitor.go | 4 ++++ command/monitor_test.go | 11 +++++++++++ command/node_drain.go | 5 +++++ command/node_drain_test.go | 9 +++++++++ command/node_status.go | 5 +++++ command/node_status_test.go | 9 +++++++++ 8 files changed, 56 insertions(+), 1 deletion(-) diff --git a/command/alloc_status.go b/command/alloc_status.go index 3c4728952..77ce75827 100644 --- a/command/alloc_status.go +++ b/command/alloc_status.go @@ -77,6 +77,10 @@ func (c *AllocStatusCommand) Run(args []string) int { // Query the allocation info alloc, _, err := client.Allocations().Info(allocID, nil) if err != nil { + if len(allocID)%2 != 0 { + c.Ui.Error(fmt.Sprintf("Identifier (without hyphens) must be of even length.")) + return 1 + } allocs, _, err := client.Allocations().PrefixList(allocID) if err != nil { c.Ui.Error(fmt.Sprintf("Error querying allocation: %v", err)) diff --git a/command/alloc_status_test.go b/command/alloc_status_test.go index 208cca88f..44a665e46 100644 --- a/command/alloc_status_test.go +++ b/command/alloc_status_test.go @@ -28,7 +28,7 @@ func TestAllocStatusCommand_Fails(t *testing.T) { ui.ErrorWriter.Reset() // Fails on connection failure - if code := cmd.Run([]string{"-address=nope", "foo"}); code != 1 { + if code := cmd.Run([]string{"-address=nope", "foobar"}); code != 1 { t.Fatalf("expected exit code 1, got: %d", code) } if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error querying allocation") { @@ -43,4 +43,12 @@ func TestAllocStatusCommand_Fails(t *testing.T) { if out := ui.ErrorWriter.String(); !strings.Contains(out, "No allocation(s) with prefix or id") { t.Fatalf("expected not found error, got: %s", out) } + + // Fail on uneven identifier length + if code := cmd.Run([]string{"-address=" + url, "26470238-5CF2-438F-8772-DC67CFB0705"}); code != 1 { + t.Fatalf("expected exit 1, got: %d", code) + } + if out := ui.ErrorWriter.String(); !strings.Contains(out, "must be of even length.") { + t.Fatalf("expected even length error, got: %s", out) + } } diff --git a/command/monitor.go b/command/monitor.go index 13d3e227a..9e0b579e9 100644 --- a/command/monitor.go +++ b/command/monitor.go @@ -196,6 +196,10 @@ func (m *monitor) monitor(evalID string, allowPrefix bool) int { m.ui.Error(fmt.Sprintf("No evaluation with id %q found", evalID)) return 1 } + if len(evalID)%2 != 0 { + m.ui.Error(fmt.Sprintf("Identifier (without hyphens) must be of even length.")) + return 1 + } evals, _, err := m.client.Evaluations().PrefixList(evalID) if err != nil { diff --git a/command/monitor_test.go b/command/monitor_test.go index 9482a168f..68b41dc77 100644 --- a/command/monitor_test.go +++ b/command/monitor_test.go @@ -323,6 +323,17 @@ func TestMonitor_MonitorWithPrefix(t *testing.T) { if !strings.Contains(out, "finished with status") { t.Fatalf("missing final status\n\n%s", out) } + + // Test identifiers of uneven length + code = mon.monitor(evalID[:7], true) + if code != 1 { + t.Fatalf("expect exit 1, got: %d", code) + } + out = ui.ErrorWriter.String() + t.Logf("dus: %s", out) + if !strings.Contains(out, "must be of even length.") { + t.Fatalf("expect even length error, got %s", out) + } } func TestMonitor_DumpAllocStatus(t *testing.T) { diff --git a/command/node_drain.go b/command/node_drain.go index fea900228..20809022a 100644 --- a/command/node_drain.go +++ b/command/node_drain.go @@ -71,6 +71,11 @@ func (c *NodeDrainCommand) Run(args []string) int { // Check if node exists node, _, err := client.Nodes().Info(nodeID, nil) if err != nil { + if len(nodeID)%2 != 0 { + c.Ui.Error(fmt.Sprintf("Identifier (without hyphens) must be of even length.")) + return 1 + } + // Exact lookup failed, try with prefix based search nodes, _, err := client.Nodes().PrefixList(nodeID) if err != nil { diff --git a/command/node_drain_test.go b/command/node_drain_test.go index 5d2064b7a..659b59912 100644 --- a/command/node_drain_test.go +++ b/command/node_drain_test.go @@ -61,4 +61,13 @@ func TestNodeDrainCommand_Fails(t *testing.T) { if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) { t.Fatalf("expected help output, got: %s", out) } + ui.ErrorWriter.Reset() + + // Fail on uneven identifier length + if code := cmd.Run([]string{"-address=" + url, "-enable", "1234567-abcd-efab-cdef-123456789abc"}); code != 1 { + t.Fatalf("expected exit 1, got: %d", code) + } + if out := ui.ErrorWriter.String(); !strings.Contains(out, "must be of even length.") { + t.Fatalf("expected even length error, got: %s", out) + } } diff --git a/command/node_status.go b/command/node_status.go index 2e0253513..d8424ce24 100644 --- a/command/node_status.go +++ b/command/node_status.go @@ -110,6 +110,11 @@ func (c *NodeStatusCommand) Run(args []string) int { nodeID := args[0] node, _, err := client.Nodes().Info(nodeID, nil) if err != nil { + if len(nodeID)%2 != 0 { + c.Ui.Error(fmt.Sprintf("Identifier (without hyphens) must be of even length.")) + return 1 + } + // Exact lookup failed, try with prefix based search nodes, _, err := client.Nodes().PrefixList(nodeID) if err != nil { diff --git a/command/node_status_test.go b/command/node_status_test.go index 80c42f0d4..353005679 100644 --- a/command/node_status_test.go +++ b/command/node_status_test.go @@ -138,4 +138,13 @@ func TestNodeStatusCommand_Fails(t *testing.T) { 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() + + // Fail on uneven identifier length + if code := cmd.Run([]string{"-address=" + url, "1234567-abcd-efab-cdef-123456789abc"}); code != 1 { + t.Fatalf("expected exit 1, got: %d", code) + } + if out := ui.ErrorWriter.String(); !strings.Contains(out, "must be of even length.") { + t.Fatalf("expected even length error, got: %s", out) + } }