2017-07-28 21:48:15 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2017-08-14 03:51:47 +00:00
|
|
|
"strconv"
|
2017-08-30 17:51:05 +00:00
|
|
|
"strings"
|
2017-08-14 03:51:47 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-08-10 19:24:11 +00:00
|
|
|
msgpackrpc "github.com/hashicorp/net-rpc-msgpackrpc"
|
2017-10-10 22:04:23 +00:00
|
|
|
"github.com/hashicorp/nomad/acl"
|
2017-07-28 21:48:15 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2017-08-04 14:18:46 +00:00
|
|
|
const jobIndex = 1000
|
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
func registerAndVerifyJob(s *Server, t *testing.T, prefix string, counter int) *structs.Job {
|
2017-07-28 21:48:15 +00:00
|
|
|
job := mock.Job()
|
2017-08-01 21:06:52 +00:00
|
|
|
job.ID = prefix + strconv.Itoa(counter)
|
2017-07-28 21:48:15 +00:00
|
|
|
state := s.fsm.State()
|
2017-08-04 19:28:12 +00:00
|
|
|
if err := state.UpsertJob(jobIndex, job); err != nil {
|
2017-07-28 21:48:15 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
return job
|
2017-07-28 21:48:15 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 17:43:50 +00:00
|
|
|
func TestSearch_PrefixSearch_Job(t *testing.T) {
|
2017-08-04 19:28:12 +00:00
|
|
|
assert := assert.New(t)
|
2017-08-01 21:06:52 +00:00
|
|
|
prefix := "aaaaaaaa-e8f7-fd38-c855-ab94ceb8970"
|
|
|
|
|
2017-07-28 21:48:15 +00:00
|
|
|
t.Parallel()
|
|
|
|
s := testServer(t, func(c *Config) {
|
2017-08-03 14:47:20 +00:00
|
|
|
c.NumSchedulers = 0
|
2017-07-28 21:48:15 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
codec := rpcClient(t, s)
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
job := registerAndVerifyJob(s, t, prefix, 0)
|
2017-07-28 21:48:15 +00:00
|
|
|
|
2017-08-10 19:24:11 +00:00
|
|
|
req := &structs.SearchRequest{
|
2017-08-02 15:57:19 +00:00
|
|
|
Prefix: prefix,
|
2017-08-11 21:21:35 +00:00
|
|
|
Context: structs.Jobs,
|
2017-09-07 23:56:15 +00:00
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
Namespace: job.Namespace,
|
|
|
|
},
|
2017-07-28 21:48:15 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 19:24:11 +00:00
|
|
|
var resp structs.SearchResponse
|
2017-08-11 14:44:46 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Search.PrefixSearch", req, &resp); err != nil {
|
2017-07-28 21:48:15 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-11 21:21:35 +00:00
|
|
|
assert.Equal(1, len(resp.Matches[structs.Jobs]))
|
2017-09-07 23:56:15 +00:00
|
|
|
assert.Equal(job.ID, resp.Matches[structs.Jobs][0])
|
2017-08-04 19:28:12 +00:00
|
|
|
assert.Equal(uint64(jobIndex), resp.Index)
|
2017-07-28 21:48:15 +00:00
|
|
|
}
|
2017-08-01 21:28:53 +00:00
|
|
|
|
2017-10-10 22:04:23 +00:00
|
|
|
func TestSearch_PrefixSearch_ACL(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
jobID := "aaaaaaaa-e8f7-fd38-c855-ab94ceb8970"
|
|
|
|
|
|
|
|
t.Parallel()
|
|
|
|
s, root := testACLServer(t, func(c *Config) {
|
|
|
|
c.NumSchedulers = 0
|
|
|
|
})
|
|
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
codec := rpcClient(t, s)
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
state := s.fsm.State()
|
|
|
|
|
|
|
|
job := registerAndVerifyJob(s, t, jobID, 0)
|
|
|
|
assert.Nil(state.UpsertNode(1001, mock.Node()))
|
|
|
|
|
|
|
|
req := &structs.SearchRequest{
|
|
|
|
Prefix: "",
|
|
|
|
Context: structs.Jobs,
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
Namespace: job.Namespace,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try without a token and expect failure
|
|
|
|
{
|
|
|
|
var resp structs.SearchResponse
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Search.PrefixSearch", req, &resp)
|
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with an invalid token and expect failure
|
|
|
|
{
|
|
|
|
invalidToken := mock.CreatePolicyAndToken(t, state, 1003, "test-invalid",
|
|
|
|
mock.NamespacePolicy(structs.DefaultNamespace, "", []string{acl.NamespaceCapabilityListJobs}))
|
2017-10-12 22:16:33 +00:00
|
|
|
req.AuthToken = invalidToken.SecretID
|
2017-10-10 22:04:23 +00:00
|
|
|
var resp structs.SearchResponse
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Search.PrefixSearch", req, &resp)
|
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with a node:read token and expect failure due to Jobs being the context
|
|
|
|
{
|
|
|
|
validToken := mock.CreatePolicyAndToken(t, state, 1005, "test-invalid2", mock.NodePolicy(acl.PolicyRead))
|
2017-10-12 22:16:33 +00:00
|
|
|
req.AuthToken = validToken.SecretID
|
2017-10-10 22:04:23 +00:00
|
|
|
var resp structs.SearchResponse
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Search.PrefixSearch", req, &resp)
|
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with a node:read token and expect success due to All context
|
|
|
|
{
|
|
|
|
validToken := mock.CreatePolicyAndToken(t, state, 1007, "test-valid", mock.NodePolicy(acl.PolicyRead))
|
|
|
|
req.Context = structs.All
|
2017-10-12 22:16:33 +00:00
|
|
|
req.AuthToken = validToken.SecretID
|
2017-10-10 22:04:23 +00:00
|
|
|
var resp structs.SearchResponse
|
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Search.PrefixSearch", req, &resp))
|
|
|
|
assert.Equal(uint64(1001), resp.Index)
|
|
|
|
assert.Len(resp.Matches[structs.Nodes], 1)
|
|
|
|
|
|
|
|
// Jobs filtered out since token only has access to node:read
|
|
|
|
assert.Len(resp.Matches[structs.Jobs], 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with a valid token for namespace:read-job
|
|
|
|
{
|
|
|
|
validToken := mock.CreatePolicyAndToken(t, state, 1009, "test-valid2",
|
|
|
|
mock.NamespacePolicy(structs.DefaultNamespace, "", []string{acl.NamespaceCapabilityReadJob}))
|
2017-10-12 22:16:33 +00:00
|
|
|
req.AuthToken = validToken.SecretID
|
2017-10-10 22:04:23 +00:00
|
|
|
var resp structs.SearchResponse
|
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Search.PrefixSearch", req, &resp))
|
|
|
|
assert.Len(resp.Matches[structs.Jobs], 1)
|
|
|
|
assert.Equal(job.ID, resp.Matches[structs.Jobs][0])
|
|
|
|
|
2017-10-12 01:05:27 +00:00
|
|
|
// Index of job - not node - because node context is filtered out
|
|
|
|
assert.Equal(uint64(1000), resp.Index)
|
|
|
|
|
2017-10-10 22:04:23 +00:00
|
|
|
// Nodes filtered out since token only has access to namespace:read-job
|
|
|
|
assert.Len(resp.Matches[structs.Nodes], 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with a valid token for node:read and namespace:read-job
|
|
|
|
{
|
|
|
|
validToken := mock.CreatePolicyAndToken(t, state, 1011, "test-valid3", strings.Join([]string{
|
|
|
|
mock.NamespacePolicy(structs.DefaultNamespace, "", []string{acl.NamespaceCapabilityReadJob}),
|
|
|
|
mock.NodePolicy(acl.PolicyRead),
|
|
|
|
}, "\n"))
|
2017-10-12 22:16:33 +00:00
|
|
|
req.AuthToken = validToken.SecretID
|
2017-10-10 22:04:23 +00:00
|
|
|
var resp structs.SearchResponse
|
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Search.PrefixSearch", req, &resp))
|
|
|
|
assert.Len(resp.Matches[structs.Jobs], 1)
|
|
|
|
assert.Equal(job.ID, resp.Matches[structs.Jobs][0])
|
|
|
|
assert.Len(resp.Matches[structs.Nodes], 1)
|
2017-10-12 01:05:27 +00:00
|
|
|
assert.Equal(uint64(1001), resp.Index)
|
2017-10-10 22:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try with a management token
|
|
|
|
{
|
2017-10-12 22:16:33 +00:00
|
|
|
req.AuthToken = root.SecretID
|
2017-10-10 22:04:23 +00:00
|
|
|
var resp structs.SearchResponse
|
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Search.PrefixSearch", req, &resp))
|
|
|
|
assert.Equal(uint64(1001), resp.Index)
|
|
|
|
assert.Len(resp.Matches[structs.Jobs], 1)
|
|
|
|
assert.Equal(job.ID, resp.Matches[structs.Jobs][0])
|
|
|
|
assert.Len(resp.Matches[structs.Nodes], 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-29 16:21:18 +00:00
|
|
|
func TestSearch_PrefixSearch_All_JobWithHyphen(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2017-09-13 17:28:42 +00:00
|
|
|
prefix := "example-test-------" // Assert that a job with more than 4 hyphens works
|
2017-08-29 16:21:18 +00:00
|
|
|
|
|
|
|
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
|
2017-09-07 23:56:15 +00:00
|
|
|
job := registerAndVerifyJob(s, t, prefix, 0)
|
2017-08-29 16:21:18 +00:00
|
|
|
alloc := mock.Alloc()
|
2017-09-07 23:56:15 +00:00
|
|
|
alloc.JobID = job.ID
|
|
|
|
alloc.Namespace = job.Namespace
|
2017-08-29 16:21:18 +00:00
|
|
|
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{
|
|
|
|
Context: structs.All,
|
2017-09-07 23:56:15 +00:00
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
Namespace: job.Namespace,
|
|
|
|
},
|
2017-08-29 16:21:18 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 17:28:42 +00:00
|
|
|
// 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)
|
2017-08-30 17:51:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2017-09-07 23:56:15 +00:00
|
|
|
job := registerAndVerifyJob(s, t, prefix, 0)
|
2017-08-30 17:51:05 +00:00
|
|
|
alloc := mock.Alloc()
|
2017-09-07 23:56:15 +00:00
|
|
|
alloc.JobID = job.ID
|
2017-08-30 17:51:05 +00:00
|
|
|
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,
|
2017-09-07 23:56:15 +00:00
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
Namespace: job.Namespace,
|
|
|
|
},
|
2017-08-30 17:51:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var resp structs.SearchResponse
|
2017-08-29 16:21:18 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Search.PrefixSearch", req, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(1, len(resp.Matches[structs.Jobs]))
|
2017-09-07 23:56:15 +00:00
|
|
|
assert.Equal(job.ID, resp.Matches[structs.Jobs][0])
|
2017-08-29 16:21:18 +00:00
|
|
|
assert.EqualValues(jobIndex, resp.Index)
|
|
|
|
}
|
|
|
|
|
2017-08-03 14:47:20 +00:00
|
|
|
// truncate should limit results to 20
|
2017-08-11 14:44:46 +00:00
|
|
|
func TestSearch_PrefixSearch_Truncate(t *testing.T) {
|
2017-08-04 19:28:12 +00:00
|
|
|
assert := assert.New(t)
|
2017-08-01 21:28:53 +00:00
|
|
|
prefix := "aaaaaaaa-e8f7-fd38-c855-ab94ceb8970"
|
|
|
|
|
|
|
|
t.Parallel()
|
|
|
|
s := testServer(t, func(c *Config) {
|
2017-08-03 14:47:20 +00:00
|
|
|
c.NumSchedulers = 0
|
2017-08-01 21:28:53 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
codec := rpcClient(t, s)
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
var job *structs.Job
|
2017-08-01 21:28:53 +00:00
|
|
|
for counter := 0; counter < 25; counter++ {
|
2017-09-07 23:56:15 +00:00
|
|
|
job = registerAndVerifyJob(s, t, prefix, counter)
|
2017-08-01 21:28:53 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 19:24:11 +00:00
|
|
|
req := &structs.SearchRequest{
|
2017-08-02 15:57:19 +00:00
|
|
|
Prefix: prefix,
|
2017-08-11 21:21:35 +00:00
|
|
|
Context: structs.Jobs,
|
2017-09-07 23:56:15 +00:00
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
Namespace: job.Namespace,
|
|
|
|
},
|
2017-08-01 21:28:53 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 19:24:11 +00:00
|
|
|
var resp structs.SearchResponse
|
2017-08-11 14:44:46 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Search.PrefixSearch", req, &resp); err != nil {
|
2017-08-01 21:28:53 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-11 21:21:35 +00:00
|
|
|
assert.Equal(20, len(resp.Matches[structs.Jobs]))
|
|
|
|
assert.Equal(resp.Truncations[structs.Jobs], true)
|
2017-08-04 19:28:12 +00:00
|
|
|
assert.Equal(uint64(jobIndex), resp.Index)
|
2017-08-01 21:28:53 +00:00
|
|
|
}
|
2017-08-02 17:21:58 +00:00
|
|
|
|
2017-08-15 17:43:50 +00:00
|
|
|
func TestSearch_PrefixSearch_AllWithJob(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
prefix := "aaaaaaaa-e8f7-fd38-c855-ab94ceb8970"
|
|
|
|
|
|
|
|
t.Parallel()
|
|
|
|
s := testServer(t, func(c *Config) {
|
|
|
|
c.NumSchedulers = 0
|
|
|
|
})
|
|
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
codec := rpcClient(t, s)
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
job := registerAndVerifyJob(s, t, prefix, 0)
|
2017-08-15 17:43:50 +00:00
|
|
|
|
|
|
|
eval1 := mock.Eval()
|
2017-09-07 23:56:15 +00:00
|
|
|
eval1.ID = job.ID
|
2017-08-15 17:43:50 +00:00
|
|
|
s.fsm.State().UpsertEvals(2000, []*structs.Evaluation{eval1})
|
|
|
|
|
|
|
|
req := &structs.SearchRequest{
|
|
|
|
Prefix: prefix,
|
|
|
|
Context: structs.All,
|
2017-09-07 23:56:15 +00:00
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
Namespace: job.Namespace,
|
|
|
|
},
|
2017-08-15 17:43:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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]))
|
2017-09-07 23:56:15 +00:00
|
|
|
assert.Equal(job.ID, resp.Matches[structs.Jobs][0])
|
2017-08-15 17:43:50 +00:00
|
|
|
|
|
|
|
assert.Equal(1, len(resp.Matches[structs.Evals]))
|
|
|
|
assert.Equal(eval1.ID, resp.Matches[structs.Evals][0])
|
|
|
|
}
|
|
|
|
|
2017-08-11 14:44:46 +00:00
|
|
|
func TestSearch_PrefixSearch_Evals(t *testing.T) {
|
2017-08-04 19:28:12 +00:00
|
|
|
assert := assert.New(t)
|
2017-08-02 17:21:58 +00:00
|
|
|
t.Parallel()
|
|
|
|
s := testServer(t, func(c *Config) {
|
2017-08-03 14:47:20 +00:00
|
|
|
c.NumSchedulers = 0
|
2017-08-02 17:21:58 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
codec := rpcClient(t, s)
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
|
|
|
eval1 := mock.Eval()
|
2017-08-04 14:18:46 +00:00
|
|
|
s.fsm.State().UpsertEvals(2000, []*structs.Evaluation{eval1})
|
2017-08-02 17:21:58 +00:00
|
|
|
|
|
|
|
prefix := eval1.ID[:len(eval1.ID)-2]
|
|
|
|
|
2017-08-10 19:24:11 +00:00
|
|
|
req := &structs.SearchRequest{
|
2017-08-02 17:21:58 +00:00
|
|
|
Prefix: prefix,
|
2017-08-11 21:21:35 +00:00
|
|
|
Context: structs.Evals,
|
2017-09-07 23:56:15 +00:00
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
Namespace: eval1.Namespace,
|
|
|
|
},
|
2017-08-02 17:21:58 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 19:24:11 +00:00
|
|
|
var resp structs.SearchResponse
|
2017-08-11 14:44:46 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Search.PrefixSearch", req, &resp); err != nil {
|
2017-08-02 17:21:58 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-11 21:21:35 +00:00
|
|
|
assert.Equal(1, len(resp.Matches[structs.Evals]))
|
|
|
|
assert.Equal(eval1.ID, resp.Matches[structs.Evals][0])
|
|
|
|
assert.Equal(resp.Truncations[structs.Evals], false)
|
2017-08-03 22:39:56 +00:00
|
|
|
|
2017-08-04 19:28:12 +00:00
|
|
|
assert.Equal(uint64(2000), resp.Index)
|
2017-08-02 17:21:58 +00:00
|
|
|
}
|
2017-08-03 14:28:38 +00:00
|
|
|
|
2017-08-11 14:44:46 +00:00
|
|
|
func TestSearch_PrefixSearch_Allocation(t *testing.T) {
|
2017-08-04 19:28:12 +00:00
|
|
|
assert := assert.New(t)
|
2017-08-03 14:28:38 +00:00
|
|
|
t.Parallel()
|
|
|
|
s := testServer(t, func(c *Config) {
|
|
|
|
c.NumSchedulers = 0
|
|
|
|
})
|
|
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
codec := rpcClient(t, s)
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
summary := mock.JobSummary(alloc.JobID)
|
|
|
|
state := s.fsm.State()
|
|
|
|
|
|
|
|
if err := state.UpsertJobSummary(999, summary); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-08-04 14:18:46 +00:00
|
|
|
if err := state.UpsertAllocs(90, []*structs.Allocation{alloc}); err != nil {
|
2017-08-03 14:28:38 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix := alloc.ID[:len(alloc.ID)-2]
|
|
|
|
|
2017-08-10 19:24:11 +00:00
|
|
|
req := &structs.SearchRequest{
|
2017-08-03 14:28:38 +00:00
|
|
|
Prefix: prefix,
|
2017-08-11 21:21:35 +00:00
|
|
|
Context: structs.Allocs,
|
2017-09-07 23:56:15 +00:00
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
Namespace: alloc.Namespace,
|
|
|
|
},
|
2017-08-03 14:28:38 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 19:24:11 +00:00
|
|
|
var resp structs.SearchResponse
|
2017-08-11 14:44:46 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Search.PrefixSearch", req, &resp); err != nil {
|
2017-08-03 14:28:38 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-11 21:21:35 +00:00
|
|
|
assert.Equal(1, len(resp.Matches[structs.Allocs]))
|
|
|
|
assert.Equal(alloc.ID, resp.Matches[structs.Allocs][0])
|
|
|
|
assert.Equal(resp.Truncations[structs.Allocs], false)
|
2017-08-03 22:39:56 +00:00
|
|
|
|
2017-08-04 19:28:12 +00:00
|
|
|
assert.Equal(uint64(90), resp.Index)
|
2017-08-03 14:28:38 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 17:28:42 +00:00
|
|
|
func TestSearch_PrefixSearch_All_UUID(t *testing.T) {
|
2017-08-29 16:21:18 +00:00
|
|
|
assert := assert.New(t)
|
|
|
|
t.Parallel()
|
|
|
|
s := testServer(t, func(c *Config) {
|
|
|
|
c.NumSchedulers = 0
|
|
|
|
})
|
|
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
codec := rpcClient(t, s)
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
node := mock.Node()
|
|
|
|
if err := state.UpsertNode(1001, node); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
eval1 := mock.Eval()
|
|
|
|
eval1.ID = node.ID
|
|
|
|
if err := state.UpsertEvals(1002, []*structs.Evaluation{eval1}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &structs.SearchRequest{
|
|
|
|
Context: structs.All,
|
2017-09-07 23:56:15 +00:00
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
Namespace: eval1.Namespace,
|
|
|
|
},
|
2017-08-29 16:21:18 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 17:28:42 +00:00
|
|
|
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)
|
2017-08-29 16:21:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-11 14:44:46 +00:00
|
|
|
func TestSearch_PrefixSearch_Node(t *testing.T) {
|
2017-08-04 19:28:12 +00:00
|
|
|
assert := assert.New(t)
|
2017-08-03 14:28:38 +00:00
|
|
|
t.Parallel()
|
|
|
|
s := testServer(t, func(c *Config) {
|
|
|
|
c.NumSchedulers = 0
|
|
|
|
})
|
|
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
codec := rpcClient(t, s)
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
|
|
|
state := s.fsm.State()
|
|
|
|
node := mock.Node()
|
|
|
|
|
|
|
|
if err := state.UpsertNode(100, node); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix := node.ID[:len(node.ID)-2]
|
|
|
|
|
2017-08-10 19:24:11 +00:00
|
|
|
req := &structs.SearchRequest{
|
2017-08-03 14:28:38 +00:00
|
|
|
Prefix: prefix,
|
2017-08-11 21:21:35 +00:00
|
|
|
Context: structs.Nodes,
|
2017-09-07 23:56:15 +00:00
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
Namespace: structs.DefaultNamespace,
|
|
|
|
},
|
2017-08-03 14:28:38 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 19:24:11 +00:00
|
|
|
var resp structs.SearchResponse
|
2017-08-11 14:44:46 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Search.PrefixSearch", req, &resp); err != nil {
|
2017-08-03 14:28:38 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-11 21:21:35 +00:00
|
|
|
assert.Equal(1, len(resp.Matches[structs.Nodes]))
|
|
|
|
assert.Equal(node.ID, resp.Matches[structs.Nodes][0])
|
|
|
|
assert.Equal(false, resp.Truncations[structs.Nodes])
|
2017-08-04 14:18:46 +00:00
|
|
|
|
2017-08-04 19:28:12 +00:00
|
|
|
assert.Equal(uint64(100), resp.Index)
|
2017-08-03 14:28:38 +00:00
|
|
|
}
|
2017-08-03 14:47:20 +00:00
|
|
|
|
2017-08-18 20:06:25 +00:00
|
|
|
func TestSearch_PrefixSearch_Deployment(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
t.Parallel()
|
|
|
|
s := testServer(t, func(c *Config) {
|
|
|
|
c.NumSchedulers = 0
|
|
|
|
})
|
|
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
codec := rpcClient(t, s)
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
|
|
|
deployment := mock.Deployment()
|
|
|
|
s.fsm.State().UpsertDeployment(2000, deployment)
|
|
|
|
|
|
|
|
prefix := deployment.ID[:len(deployment.ID)-2]
|
|
|
|
|
|
|
|
req := &structs.SearchRequest{
|
|
|
|
Prefix: prefix,
|
|
|
|
Context: structs.Deployments,
|
2017-09-07 23:56:15 +00:00
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
Namespace: deployment.Namespace,
|
|
|
|
},
|
2017-08-18 20:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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.Deployments]))
|
|
|
|
assert.Equal(deployment.ID, resp.Matches[structs.Deployments][0])
|
|
|
|
assert.Equal(resp.Truncations[structs.Deployments], false)
|
|
|
|
|
|
|
|
assert.Equal(uint64(2000), resp.Index)
|
|
|
|
}
|
|
|
|
|
2017-08-11 14:44:46 +00:00
|
|
|
func TestSearch_PrefixSearch_AllContext(t *testing.T) {
|
2017-08-04 19:28:12 +00:00
|
|
|
assert := assert.New(t)
|
2017-08-03 15:59:27 +00:00
|
|
|
t.Parallel()
|
|
|
|
s := testServer(t, func(c *Config) {
|
|
|
|
c.NumSchedulers = 0
|
|
|
|
})
|
|
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
codec := rpcClient(t, s)
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
|
|
|
state := s.fsm.State()
|
|
|
|
node := mock.Node()
|
|
|
|
|
|
|
|
if err := state.UpsertNode(100, node); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
eval1 := mock.Eval()
|
|
|
|
eval1.ID = node.ID
|
|
|
|
if err := state.UpsertEvals(1000, []*structs.Evaluation{eval1}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix := node.ID[:len(node.ID)-2]
|
|
|
|
|
2017-08-10 19:24:11 +00:00
|
|
|
req := &structs.SearchRequest{
|
2017-08-03 15:59:27 +00:00
|
|
|
Prefix: prefix,
|
2017-08-11 21:21:35 +00:00
|
|
|
Context: structs.All,
|
2017-09-07 23:56:15 +00:00
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
Namespace: eval1.Namespace,
|
|
|
|
},
|
2017-08-03 15:59:27 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 19:24:11 +00:00
|
|
|
var resp structs.SearchResponse
|
2017-08-11 14:44:46 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Search.PrefixSearch", req, &resp); err != nil {
|
2017-08-03 15:59:27 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-11 21:21:35 +00:00
|
|
|
assert.Equal(1, len(resp.Matches[structs.Nodes]))
|
|
|
|
assert.Equal(1, len(resp.Matches[structs.Evals]))
|
2017-08-03 15:59:27 +00:00
|
|
|
|
2017-08-11 21:21:35 +00:00
|
|
|
assert.Equal(node.ID, resp.Matches[structs.Nodes][0])
|
|
|
|
assert.Equal(eval1.ID, resp.Matches[structs.Evals][0])
|
2017-08-03 22:39:56 +00:00
|
|
|
|
2017-08-04 20:14:41 +00:00
|
|
|
assert.Equal(uint64(1000), resp.Index)
|
2017-08-03 22:39:56 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 14:16:24 +00:00
|
|
|
// Tests that the top 20 matches are returned when no prefix is set
|
2017-08-11 14:44:46 +00:00
|
|
|
func TestSearch_PrefixSearch_NoPrefix(t *testing.T) {
|
2017-08-04 19:28:12 +00:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2017-08-03 22:39:56 +00:00
|
|
|
prefix := "aaaaaaaa-e8f7-fd38-c855-ab94ceb8970"
|
|
|
|
|
|
|
|
t.Parallel()
|
|
|
|
s := testServer(t, func(c *Config) {
|
|
|
|
c.NumSchedulers = 0
|
|
|
|
})
|
|
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
codec := rpcClient(t, s)
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
job := registerAndVerifyJob(s, t, prefix, 0)
|
2017-08-03 22:39:56 +00:00
|
|
|
|
2017-08-10 19:24:11 +00:00
|
|
|
req := &structs.SearchRequest{
|
2017-08-03 22:39:56 +00:00
|
|
|
Prefix: "",
|
2017-08-11 21:21:35 +00:00
|
|
|
Context: structs.Jobs,
|
2017-09-07 23:56:15 +00:00
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
Namespace: job.Namespace,
|
|
|
|
},
|
2017-08-03 22:39:56 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 19:24:11 +00:00
|
|
|
var resp structs.SearchResponse
|
2017-08-11 14:44:46 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Search.PrefixSearch", req, &resp); err != nil {
|
2017-08-03 22:39:56 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-11 21:21:35 +00:00
|
|
|
assert.Equal(1, len(resp.Matches[structs.Jobs]))
|
2017-09-07 23:56:15 +00:00
|
|
|
assert.Equal(job.ID, resp.Matches[structs.Jobs][0])
|
2017-08-04 19:28:12 +00:00
|
|
|
assert.Equal(uint64(jobIndex), resp.Index)
|
2017-08-03 22:39:56 +00:00
|
|
|
}
|
|
|
|
|
2017-08-08 20:29:02 +00:00
|
|
|
// Tests that the zero matches are returned when a prefix has no matching
|
|
|
|
// results
|
2017-08-11 14:44:46 +00:00
|
|
|
func TestSearch_PrefixSearch_NoMatches(t *testing.T) {
|
2017-08-04 19:28:12 +00:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2017-08-03 22:39:56 +00:00
|
|
|
prefix := "aaaaaaaa-e8f7-fd38-c855-ab94ceb8970"
|
|
|
|
|
|
|
|
t.Parallel()
|
|
|
|
s := testServer(t, func(c *Config) {
|
|
|
|
c.NumSchedulers = 0
|
|
|
|
})
|
|
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
codec := rpcClient(t, s)
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
2017-08-10 19:24:11 +00:00
|
|
|
req := &structs.SearchRequest{
|
2017-08-03 22:39:56 +00:00
|
|
|
Prefix: prefix,
|
2017-08-11 21:21:35 +00:00
|
|
|
Context: structs.Jobs,
|
2017-09-07 23:56:15 +00:00
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
Namespace: structs.DefaultNamespace,
|
|
|
|
},
|
2017-08-03 22:39:56 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 19:24:11 +00:00
|
|
|
var resp structs.SearchResponse
|
2017-08-11 14:44:46 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Search.PrefixSearch", req, &resp); err != nil {
|
2017-08-03 22:39:56 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-11 21:21:35 +00:00
|
|
|
assert.Equal(0, len(resp.Matches[structs.Jobs]))
|
2017-08-04 19:28:12 +00:00
|
|
|
assert.Equal(uint64(0), resp.Index)
|
2017-08-03 15:59:27 +00:00
|
|
|
}
|
2017-08-08 20:29:02 +00:00
|
|
|
|
|
|
|
// Prefixes can only be looked up if their length is a power of two. For
|
|
|
|
// prefixes which are an odd length, use the length-1 characters.
|
2017-08-11 14:44:46 +00:00
|
|
|
func TestSearch_PrefixSearch_RoundDownToEven(t *testing.T) {
|
2017-08-08 20:29:02 +00:00
|
|
|
assert := assert.New(t)
|
2017-08-10 14:13:35 +00:00
|
|
|
id1 := "aaafaaaa-e8f7-fd38-c855-ab94ceb89"
|
2017-08-10 14:48:02 +00:00
|
|
|
id2 := "aaafeaaa-e8f7-fd38-c855-ab94ceb89"
|
|
|
|
prefix := "aaafa"
|
2017-08-08 20:29:02 +00:00
|
|
|
|
|
|
|
t.Parallel()
|
|
|
|
s := testServer(t, func(c *Config) {
|
|
|
|
c.NumSchedulers = 0
|
|
|
|
})
|
|
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
codec := rpcClient(t, s)
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
job := registerAndVerifyJob(s, t, id1, 0)
|
2017-08-10 14:13:35 +00:00
|
|
|
registerAndVerifyJob(s, t, id2, 50)
|
2017-08-08 20:29:02 +00:00
|
|
|
|
2017-08-10 19:24:11 +00:00
|
|
|
req := &structs.SearchRequest{
|
2017-08-08 20:29:02 +00:00
|
|
|
Prefix: prefix,
|
2017-08-11 21:21:35 +00:00
|
|
|
Context: structs.Jobs,
|
2017-09-07 23:56:15 +00:00
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
Namespace: job.Namespace,
|
|
|
|
},
|
2017-08-08 20:29:02 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 19:24:11 +00:00
|
|
|
var resp structs.SearchResponse
|
2017-08-11 14:44:46 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Search.PrefixSearch", req, &resp); err != nil {
|
2017-08-08 20:29:02 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-11 21:21:35 +00:00
|
|
|
assert.Equal(1, len(resp.Matches[structs.Jobs]))
|
2017-09-07 23:56:15 +00:00
|
|
|
assert.Equal(job.ID, resp.Matches[structs.Jobs][0])
|
2017-08-08 20:29:02 +00:00
|
|
|
}
|