From 0583d032eedaeb6379f3e9793c893562f9bb1a70 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Wed, 30 Aug 2017 10:51:05 -0700 Subject: [PATCH 1/2] 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 --- nomad/search_endpoint.go | 6 ++++- nomad/search_endpoint_test.go | 43 +++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/nomad/search_endpoint.go b/nomad/search_endpoint.go index 8399172d9c3f..749c25488b92 100644 --- a/nomad/search_endpoint.go +++ b/nomad/search_endpoint.go @@ -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 { diff --git a/nomad/search_endpoint_test.go b/nomad/search_endpoint_test.go index 462255f12232..a63244682070 100644 --- a/nomad/search_endpoint_test.go +++ b/nomad/search_endpoint_test.go @@ -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) From d76b5bedba4b528b3fcd95760849a72d64a0d3eb Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Wed, 30 Aug 2017 10:54:19 -0700 Subject: [PATCH 2/2] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b2fb4696ffe..65e7b1ffd3b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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]