Skip to content

Commit

Permalink
Search handles prefix longer than allowed UUIDs
Browse files Browse the repository at this point in the history
This PR fixes an issue in which the Search endpoint would error if the
prefix was longer than the allowed 36 characters of a UUID.

Fixes #3134
  • Loading branch information
dadgar committed Aug 30, 2017
1 parent aa9bb33 commit 0583d03
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
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 0583d03

Please sign in to comment.