Skip to content

Commit

Permalink
Merge pull request #3138 from hashicorp/b-status-length
Browse files Browse the repository at this point in the history
Search handles prefix longer than allowed UUIDs
  • Loading branch information
dadgar committed Aug 30, 2017
2 parents d1e2950 + d76b5be commit 98bb9a2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 0.6.3 (Unreleased)

BUG FIXES:
* api: Search handles prefix longer than allowed UUIDs [GH-3138]
* api: Search endpoint handles even UUID prefixes with hyphens [GH-3120]
* cli: All status commands handle even UUID prefixes with hyphens [GH-3122]
* cli: Fix autocompletion of paths that include directories on zsh [GH-3129]
Expand Down
6 changes: 5 additions & 1 deletion nomad/search_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,13 @@ func (s *Search) PrefixSearch(args *structs.SearchRequest,
iter, err := getResourceIter(ctx, roundUUIDDownIfOdd(args.Prefix, args.Context), ws, state)

if err != nil {
e := err.Error()
switch {
// Searching other contexts with job names raises an error, which in
// this case we want to ignore.
if !strings.Contains(err.Error(), "Invalid UUID: encoding/hex") {
case strings.Contains(e, "Invalid UUID: encoding/hex"):
case strings.Contains(e, "UUID have 36 characters"):
default:
return err
}
} else {
Expand Down
43 changes: 43 additions & 0 deletions nomad/search_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nomad

import (
"strconv"
"strings"
"testing"

msgpackrpc "github.com/hashicorp/net-rpc-msgpackrpc"
Expand Down Expand Up @@ -97,6 +98,48 @@ func TestSearch_PrefixSearch_All_JobWithHyphen(t *testing.T) {
assert.EqualValues(jobIndex, resp.Index)
}

func TestSearch_PrefixSearch_All_LongJob(t *testing.T) {
assert := assert.New(t)
prefix := strings.Repeat("a", 100)

t.Parallel()
s := testServer(t, func(c *Config) {
c.NumSchedulers = 0
})

defer s.Shutdown()
codec := rpcClient(t, s)
testutil.WaitForLeader(t, s.RPC)

// Register a job and an allocation
jobID := registerAndVerifyJob(s, t, prefix, 0)
alloc := mock.Alloc()
alloc.JobID = jobID
summary := mock.JobSummary(alloc.JobID)
state := s.fsm.State()

if err := state.UpsertJobSummary(999, summary); err != nil {
t.Fatalf("err: %v", err)
}
if err := state.UpsertAllocs(1000, []*structs.Allocation{alloc}); err != nil {
t.Fatalf("err: %v", err)
}

req := &structs.SearchRequest{
Prefix: prefix,
Context: structs.All,
}

var resp structs.SearchResponse
if err := msgpackrpc.CallWithCodec(codec, "Search.PrefixSearch", req, &resp); err != nil {
t.Fatalf("err: %v", err)
}

assert.Equal(1, len(resp.Matches[structs.Jobs]))
assert.Equal(jobID, resp.Matches[structs.Jobs][0])
assert.EqualValues(jobIndex, resp.Index)
}

// truncate should limit results to 20
func TestSearch_PrefixSearch_Truncate(t *testing.T) {
assert := assert.New(t)
Expand Down

0 comments on commit 98bb9a2

Please sign in to comment.