Detect a half-byte prefix and display a user-friendly error.

This commit is contained in:
Ivo Verberk 2016-01-21 20:53:05 +01:00
parent 33d2b2c8ee
commit d8ac284bdd
8 changed files with 56 additions and 1 deletions

View File

@ -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))

View File

@ -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)
}
}

View File

@ -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 {

View File

@ -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) {

View File

@ -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 {

View File

@ -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)
}
}

View File

@ -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 {

View File

@ -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)
}
}