Skip to content

Commit

Permalink
Merge pull request #3203 from hashicorp/b-search-hyphens
Browse files Browse the repository at this point in the history
Fix UUID search with hyphens
  • Loading branch information
dadgar committed Sep 13, 2017
2 parents 81fae05 + c8f793a commit 74d30d4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
4 changes: 3 additions & 1 deletion nomad/search_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
41 changes: 18 additions & 23 deletions nomad/search_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -88,22 +88,22 @@ func TestSearch_PrefixSearch_All_JobWithHyphen(t *testing.T) {
}

req := &structs.SearchRequest{
Prefix: "example-",
Context: structs.All,
QueryOptions: structs.QueryOptions{
Region: "global",
Namespace: job.Namespace,
},
}

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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -345,28 +345,23 @@ 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",
Namespace: eval1.Namespace,
},
}

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) {
Expand Down

0 comments on commit 74d30d4

Please sign in to comment.