2017-07-28 21:48:15 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hashicorp/net-rpc-msgpackrpc"
|
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2017-08-01 21:06:52 +00:00
|
|
|
"strconv"
|
2017-07-28 21:48:15 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2017-08-04 14:18:46 +00:00
|
|
|
const jobIndex = 1000
|
|
|
|
|
2017-08-01 21:06:52 +00:00
|
|
|
func registerAndVerifyJob(s *Server, t *testing.T, prefix string, counter int) string {
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
return job.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourcesEndpoint_List(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-08-01 21:06:52 +00:00
|
|
|
jobID := registerAndVerifyJob(s, t, prefix, 0)
|
2017-07-28 21:48:15 +00:00
|
|
|
|
2017-08-04 22:18:49 +00:00
|
|
|
req := &structs.ResourceListRequest{
|
2017-08-02 15:57:19 +00:00
|
|
|
Prefix: prefix,
|
2017-08-04 14:18:46 +00:00
|
|
|
Context: "jobs",
|
2017-07-28 21:48:15 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 22:18:49 +00:00
|
|
|
var resp structs.ResourceListResponse
|
2017-07-28 21:48:15 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Resources.List", req, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-04 19:28:12 +00:00
|
|
|
assert.Equal(1, len(resp.Matches["jobs"]))
|
|
|
|
assert.Equal(jobID, resp.Matches["jobs"][0])
|
|
|
|
assert.Equal(uint64(jobIndex), resp.Index)
|
2017-07-28 21:48:15 +00:00
|
|
|
}
|
2017-08-01 21:28:53 +00:00
|
|
|
|
2017-08-03 14:47:20 +00:00
|
|
|
// truncate should limit results to 20
|
|
|
|
func TestResourcesEndpoint_List_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)
|
|
|
|
|
|
|
|
for counter := 0; counter < 25; counter++ {
|
|
|
|
registerAndVerifyJob(s, t, prefix, counter)
|
|
|
|
}
|
|
|
|
|
2017-08-04 22:18:49 +00:00
|
|
|
req := &structs.ResourceListRequest{
|
2017-08-02 15:57:19 +00:00
|
|
|
Prefix: prefix,
|
2017-08-04 14:18:46 +00:00
|
|
|
Context: "jobs",
|
2017-08-01 21:28:53 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 22:18:49 +00:00
|
|
|
var resp structs.ResourceListResponse
|
2017-08-01 21:28:53 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Resources.List", req, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-04 19:28:12 +00:00
|
|
|
assert.Equal(20, len(resp.Matches["jobs"]))
|
|
|
|
assert.Equal(resp.Truncations["jobs"], true)
|
|
|
|
assert.Equal(uint64(jobIndex), resp.Index)
|
2017-08-01 21:28:53 +00:00
|
|
|
}
|
2017-08-02 17:21:58 +00:00
|
|
|
|
2017-08-03 14:47:20 +00:00
|
|
|
func TestResourcesEndpoint_List_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-04 22:18:49 +00:00
|
|
|
req := &structs.ResourceListRequest{
|
2017-08-02 17:21:58 +00:00
|
|
|
Prefix: prefix,
|
2017-08-04 14:18:46 +00:00
|
|
|
Context: "evals",
|
2017-08-02 17:21:58 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 22:18:49 +00:00
|
|
|
var resp structs.ResourceListResponse
|
2017-08-02 17:21:58 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Resources.List", req, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-04 19:28:12 +00:00
|
|
|
assert.Equal(1, len(resp.Matches["evals"]))
|
|
|
|
assert.Equal(eval1.ID, resp.Matches["evals"][0])
|
|
|
|
assert.Equal(resp.Truncations["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
|
|
|
|
|
|
|
func TestResourcesEndpoint_List_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-04 22:18:49 +00:00
|
|
|
req := &structs.ResourceListRequest{
|
2017-08-03 14:28:38 +00:00
|
|
|
Prefix: prefix,
|
2017-08-04 14:18:46 +00:00
|
|
|
Context: "allocs",
|
2017-08-03 14:28:38 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 22:18:49 +00:00
|
|
|
var resp structs.ResourceListResponse
|
2017-08-03 14:28:38 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Resources.List", req, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-04 19:28:12 +00:00
|
|
|
assert.Equal(1, len(resp.Matches["allocs"]))
|
|
|
|
assert.Equal(alloc.ID, resp.Matches["allocs"][0])
|
|
|
|
assert.Equal(resp.Truncations["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
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourcesEndpoint_List_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-04 22:18:49 +00:00
|
|
|
req := &structs.ResourceListRequest{
|
2017-08-03 14:28:38 +00:00
|
|
|
Prefix: prefix,
|
2017-08-04 14:18:46 +00:00
|
|
|
Context: "nodes",
|
2017-08-03 14:28:38 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 22:18:49 +00:00
|
|
|
var resp structs.ResourceListResponse
|
2017-08-03 14:28:38 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Resources.List", req, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-04 19:28:12 +00:00
|
|
|
assert.Equal(1, len(resp.Matches["nodes"]))
|
|
|
|
assert.Equal(node.ID, resp.Matches["nodes"][0])
|
|
|
|
assert.Equal(false, resp.Truncations["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
|
|
|
|
|
|
|
func TestResourcesEndpoint_List_InvalidContext(t *testing.T) {
|
2017-08-04 19:28:12 +00:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2017-08-03 14:47:20 +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-08-04 22:18:49 +00:00
|
|
|
req := &structs.ResourceListRequest{
|
2017-08-03 14:47:20 +00:00
|
|
|
Prefix: "anyPrefix",
|
|
|
|
Context: "invalid",
|
|
|
|
}
|
|
|
|
|
2017-08-04 22:18:49 +00:00
|
|
|
var resp structs.ResourceListResponse
|
2017-08-03 14:47:20 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Resources.List", req, &resp)
|
2017-08-04 22:18:49 +00:00
|
|
|
assert.Equal(err.Error(), "context must be one of [allocs nodes jobs evals]; got \"invalid\"")
|
2017-08-03 22:39:56 +00:00
|
|
|
|
2017-08-04 19:28:12 +00:00
|
|
|
assert.Equal(uint64(0), resp.Index)
|
2017-08-03 14:47:20 +00:00
|
|
|
}
|
2017-08-03 15:59:27 +00:00
|
|
|
|
|
|
|
func TestResourcesEndpoint_List_NoContext(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-04 22:18:49 +00:00
|
|
|
req := &structs.ResourceListRequest{
|
2017-08-03 15:59:27 +00:00
|
|
|
Prefix: prefix,
|
|
|
|
Context: "",
|
|
|
|
}
|
|
|
|
|
2017-08-04 22:18:49 +00:00
|
|
|
var resp structs.ResourceListResponse
|
2017-08-03 15:59:27 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Resources.List", req, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-04 19:28:12 +00:00
|
|
|
assert.Equal(1, len(resp.Matches["nodes"]))
|
|
|
|
assert.Equal(1, len(resp.Matches["evals"]))
|
2017-08-03 15:59:27 +00:00
|
|
|
|
2017-08-04 19:28:12 +00:00
|
|
|
assert.Equal(node.ID, resp.Matches["nodes"][0])
|
|
|
|
assert.Equal(eval1.ID, resp.Matches["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-03 22:39:56 +00:00
|
|
|
func TestResourcesEndpoint_List_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)
|
|
|
|
|
|
|
|
jobID := registerAndVerifyJob(s, t, prefix, 0)
|
|
|
|
|
2017-08-04 22:18:49 +00:00
|
|
|
req := &structs.ResourceListRequest{
|
2017-08-03 22:39:56 +00:00
|
|
|
Prefix: "",
|
2017-08-04 14:18:46 +00:00
|
|
|
Context: "jobs",
|
2017-08-03 22:39:56 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 22:18:49 +00:00
|
|
|
var resp structs.ResourceListResponse
|
2017-08-03 22:39:56 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Resources.List", req, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-04 19:28:12 +00:00
|
|
|
assert.Equal(1, len(resp.Matches["jobs"]))
|
|
|
|
assert.Equal(jobID, resp.Matches["jobs"][0])
|
|
|
|
assert.Equal(uint64(jobIndex), resp.Index)
|
2017-08-03 22:39:56 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 19:28:12 +00:00
|
|
|
//// Tests that the zero matches are returned when a prefix has no matching
|
|
|
|
//// results
|
2017-08-03 22:39:56 +00:00
|
|
|
func TestResourcesEndpoint_List_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-04 22:18:49 +00:00
|
|
|
req := &structs.ResourceListRequest{
|
2017-08-03 22:39:56 +00:00
|
|
|
Prefix: prefix,
|
2017-08-04 14:18:46 +00:00
|
|
|
Context: "jobs",
|
2017-08-03 22:39:56 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 22:18:49 +00:00
|
|
|
var resp structs.ResourceListResponse
|
2017-08-03 22:39:56 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Resources.List", req, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-04 19:28:12 +00:00
|
|
|
assert.Equal(0, len(resp.Matches["jobs"]))
|
|
|
|
assert.Equal(uint64(0), resp.Index)
|
2017-08-03 15:59:27 +00:00
|
|
|
}
|