Search handles prefix longer than allowed UUIDs
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 https://github.com/hashicorp/nomad/issues/3134
This commit is contained in:
parent
6aaffa30f0
commit
346d76dec3
|
@ -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 {
|
||||
|
|
|
@ -2,6 +2,7 @@ package nomad
|
|||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
msgpackrpc "github.com/hashicorp/net-rpc-msgpackrpc"
|
||||
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue