From c8f793a5692c5e5cddbde35b038cf5ef916a92b8 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Wed, 13 Sep 2017 10:28:42 -0700 Subject: [PATCH] Fix UUID search with hyphens This PR fixes: * UUID lookup with hyphens and odd length. The math was wrong. There is now a test that ranges over all possible values. * Fixes an unreported issue that could be hit when a job has more than 4 hyphens in it as UUID lookup doesn't allow that. Fixes https://github.com/hashicorp/nomad/issues/3141 --- nomad/search_endpoint.go | 4 +++- nomad/search_endpoint_test.go | 41 +++++++++++++++-------------------- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/nomad/search_endpoint.go b/nomad/search_endpoint.go index d1723fdf0bd6..1404a4f41d8a 100644 --- a/nomad/search_endpoint.go +++ b/nomad/search_endpoint.go @@ -102,7 +102,7 @@ func roundUUIDDownIfOdd(prefix string, context structs.Context) string { if l%2 == 0 { return prefix } - return prefix[:l-1] + return prefix[:len(prefix)-1] } // PrefixSearch is used to list matches for a given prefix, and returns @@ -135,6 +135,8 @@ func (s *Search) PrefixSearch(args *structs.SearchRequest, // this case we want to ignore. case strings.Contains(e, "Invalid UUID: encoding/hex"): case strings.Contains(e, "UUID have 36 characters"): + case strings.Contains(e, "must be even length"): + case strings.Contains(e, "UUID should have maximum of 4"): default: return err } diff --git a/nomad/search_endpoint_test.go b/nomad/search_endpoint_test.go index 0f9af9d3602e..5846bab430a0 100644 --- a/nomad/search_endpoint_test.go +++ b/nomad/search_endpoint_test.go @@ -61,7 +61,7 @@ func TestSearch_PrefixSearch_Job(t *testing.T) { func TestSearch_PrefixSearch_All_JobWithHyphen(t *testing.T) { assert := assert.New(t) - prefix := "example-test" + prefix := "example-test-------" // Assert that a job with more than 4 hyphens works t.Parallel() s := testServer(t, func(c *Config) { @@ -88,7 +88,6 @@ func TestSearch_PrefixSearch_All_JobWithHyphen(t *testing.T) { } req := &structs.SearchRequest{ - Prefix: "example-", Context: structs.All, QueryOptions: structs.QueryOptions{ Region: "global", @@ -96,14 +95,15 @@ func TestSearch_PrefixSearch_All_JobWithHyphen(t *testing.T) { }, } - var resp structs.SearchResponse - if err := msgpackrpc.CallWithCodec(codec, "Search.PrefixSearch", req, &resp); err != nil { - t.Fatalf("err: %v", err) + // req.Prefix = "example-te": 9 + for i := 1; i < len(prefix); i++ { + req.Prefix = prefix[:i] + var resp structs.SearchResponse + assert.Nil(msgpackrpc.CallWithCodec(codec, "Search.PrefixSearch", req, &resp)) + assert.Equal(1, len(resp.Matches[structs.Jobs])) + assert.Equal(job.ID, resp.Matches[structs.Jobs][0]) + assert.EqualValues(jobIndex, resp.Index) } - - assert.Equal(1, len(resp.Matches[structs.Jobs])) - assert.Equal(job.ID, resp.Matches[structs.Jobs][0]) - assert.EqualValues(jobIndex, resp.Index) } func TestSearch_PrefixSearch_All_LongJob(t *testing.T) { @@ -312,7 +312,7 @@ func TestSearch_PrefixSearch_Allocation(t *testing.T) { assert.Equal(uint64(90), resp.Index) } -func TestSearch_PrefixSearch_All_UUID_EvenPrefix(t *testing.T) { +func TestSearch_PrefixSearch_All_UUID(t *testing.T) { assert := assert.New(t) t.Parallel() s := testServer(t, func(c *Config) { @@ -345,11 +345,7 @@ func TestSearch_PrefixSearch_All_UUID_EvenPrefix(t *testing.T) { t.Fatalf("err: %v", err) } - prefix := alloc.ID[:13] - t.Log(prefix) - req := &structs.SearchRequest{ - Prefix: prefix, Context: structs.All, QueryOptions: structs.QueryOptions{ Region: "global", @@ -357,16 +353,15 @@ func TestSearch_PrefixSearch_All_UUID_EvenPrefix(t *testing.T) { }, } - var resp structs.SearchResponse - if err := msgpackrpc.CallWithCodec(codec, "Search.PrefixSearch", req, &resp); err != nil { - t.Fatalf("err: %v", err) + for i := 1; i < len(alloc.ID); i++ { + req.Prefix = alloc.ID[:i] + var resp structs.SearchResponse + assert.Nil(msgpackrpc.CallWithCodec(codec, "Search.PrefixSearch", req, &resp)) + assert.Equal(1, len(resp.Matches[structs.Allocs])) + assert.Equal(alloc.ID, resp.Matches[structs.Allocs][0]) + assert.Equal(resp.Truncations[structs.Allocs], false) + assert.EqualValues(1002, resp.Index) } - - assert.Equal(1, len(resp.Matches[structs.Allocs])) - assert.Equal(alloc.ID, resp.Matches[structs.Allocs][0]) - assert.Equal(resp.Truncations[structs.Allocs], false) - - assert.EqualValues(1002, resp.Index) } func TestSearch_PrefixSearch_Node(t *testing.T) {