Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix UUID search with hyphens #3203

Merged
merged 1 commit into from
Sep 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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