Provide a consistent user experience with prefix based lookups.

* Require at least two characters for identifier
* Automatically strip off the last character in case of uneven length
This commit is contained in:
Ivo Verberk
2016-01-21 22:21:35 +01:00
parent de3bc00a00
commit cc4fea99fc
8 changed files with 83 additions and 26 deletions

View File

@@ -77,10 +77,16 @@ 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."))
if len(allocID) == 1 {
c.Ui.Error(fmt.Sprintf("Identifier must contain at least two characters."))
return 1
}
if len(allocID)%2 == 1 {
// Identifiers must be of even length, so we strip off the last byte
// to provide a consistent user experience.
allocID = allocID[:len(allocID)-1]
}
allocs, _, err := client.Allocations().PrefixList(allocID)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying allocation: %v", err))

View File

@@ -43,12 +43,23 @@ 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)
}
ui.ErrorWriter.Reset()
// Fail on uneven identifier length
if code := cmd.Run([]string{"-address=" + url, "26470238-5CF2-438F-8772-DC67CFB0705"}); code != 1 {
// Fail on identifier with too few characters
if code := cmd.Run([]string{"-address=" + url, "2"}); 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)
if out := ui.ErrorWriter.String(); !strings.Contains(out, "must contain at least two characters.") {
t.Fatalf("expected too few characters error, got: %s", out)
}
ui.ErrorWriter.Reset()
// Identifiers with uneven length should produce a query result
if code := cmd.Run([]string{"-address=" + url, "123"}); code != 1 {
t.Fatalf("expected exit 1, got: %d", code)
}
if out := ui.ErrorWriter.String(); !strings.Contains(out, "No allocation(s) with prefix or id") {
t.Fatalf("expected not found error, got: %s", out)
}
}

View File

@@ -196,10 +196,15 @@ 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."))
if len(evalID) == 1 {
m.ui.Error(fmt.Sprintf("Identifier must contain at least two characters."))
return 1
}
if len(evalID)%2 == 1 {
// Identifiers must be of even length, so we strip off the last byte
// to provide a consistent user experience.
evalID = evalID[:len(evalID)-1]
}
evals, _, err := m.client.Evaluations().PrefixList(evalID)
if err != nil {

View File

@@ -324,16 +324,24 @@ func TestMonitor_MonitorWithPrefix(t *testing.T) {
t.Fatalf("missing final status\n\n%s", out)
}
// Test identifiers of uneven length
code = mon.monitor(evalID[:7], true)
// Fail on identifier with too few characters
code = mon.monitor(evalID[:1], 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)
if out := ui.ErrorWriter.String(); !strings.Contains(out, "must contain at least two characters.") {
t.Fatalf("expected too few characters error, got: %s", out)
}
ui.ErrorWriter.Reset()
code = mon.monitor(evalID[:3], true)
if code != 2 {
t.Fatalf("expect exit 2, got: %d", code)
}
if out := ui.OutputWriter.String(); !strings.Contains(out, "Monitoring evaluation") {
t.Fatalf("expected evaluation monitoring output, got: %s", out)
}
}
func TestMonitor_DumpAllocStatus(t *testing.T) {

View File

@@ -71,10 +71,15 @@ 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."))
if len(nodeID) == 1 {
c.Ui.Error(fmt.Sprintf("Identifier must contain at least two characters."))
return 1
}
if len(nodeID)%2 == 1 {
// Identifiers must be of even length, so we strip off the last byte
// to provide a consistent user experience.
nodeID = nodeID[:len(nodeID)-1]
}
// Exact lookup failed, try with prefix based search
nodes, _, err := client.Nodes().PrefixList(nodeID)

View File

@@ -63,11 +63,20 @@ func TestNodeDrainCommand_Fails(t *testing.T) {
}
ui.ErrorWriter.Reset()
// Fail on uneven identifier length
if code := cmd.Run([]string{"-address=" + url, "-enable", "1234567-abcd-efab-cdef-123456789abc"}); code != 1 {
// Fail on identifier with too few characters
if code := cmd.Run([]string{"-address=" + url, "-enable", "1"}); 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)
if out := ui.ErrorWriter.String(); !strings.Contains(out, "must contain at least two characters.") {
t.Fatalf("expected too few characters error, got: %s", out)
}
ui.ErrorWriter.Reset()
// Identifiers with uneven length should produce a query result
if code := cmd.Run([]string{"-address=" + url, "-enable", "123"}); code != 1 {
t.Fatalf("expected exit 1, got: %d", code)
}
if out := ui.ErrorWriter.String(); !strings.Contains(out, "No node(s) with prefix or id") {
t.Fatalf("expected not exist error, got: %s", out)
}
}

View File

@@ -110,10 +110,15 @@ 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."))
if len(nodeID) == 1 {
c.Ui.Error(fmt.Sprintf("Identifier must contain at least two characters."))
return 1
}
if len(nodeID)%2 == 1 {
// Identifiers must be of even length, so we strip off the last byte
// to provide a consistent user experience.
nodeID = nodeID[:len(nodeID)-1]
}
// Exact lookup failed, try with prefix based search
nodes, _, err := client.Nodes().PrefixList(nodeID)

View File

@@ -104,6 +104,14 @@ func TestNodeStatusCommand_Run(t *testing.T) {
}
ui.OutputWriter.Reset()
// 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)
}
}
func TestNodeStatusCommand_Fails(t *testing.T) {
@@ -140,11 +148,11 @@ func TestNodeStatusCommand_Fails(t *testing.T) {
}
ui.ErrorWriter.Reset()
// Fail on uneven identifier length
if code := cmd.Run([]string{"-address=" + url, "1234567-abcd-efab-cdef-123456789abc"}); code != 1 {
// Fail on identifier with too few characters
if code := cmd.Run([]string{"-address=" + url, "1"}); 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)
if out := ui.ErrorWriter.String(); !strings.Contains(out, "must contain at least two characters.") {
t.Fatalf("expected too few characters error, got: %s", out)
}
}