2015-09-06 22:34:28 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2015-09-06 22:46:45 +00:00
|
|
|
"reflect"
|
2015-09-06 22:34:28 +00:00
|
|
|
"testing"
|
2015-10-29 02:25:39 +00:00
|
|
|
"time"
|
2015-09-06 22:34:28 +00:00
|
|
|
|
2019-01-15 19:46:12 +00:00
|
|
|
msgpackrpc "github.com/hashicorp/net-rpc-msgpackrpc"
|
2017-09-15 00:24:51 +00:00
|
|
|
"github.com/hashicorp/nomad/acl"
|
2018-02-21 18:58:04 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2018-01-19 00:05:20 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2015-09-06 22:34:28 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2017-09-15 00:24:51 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-02-21 18:58:04 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-09-06 22:34:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAllocEndpoint_List(t *testing.T) {
|
2017-07-23 22:04:38 +00:00
|
|
|
t.Parallel()
|
2018-01-12 01:00:30 +00:00
|
|
|
s1 := TestServer(t, nil)
|
2015-09-06 22:34:28 +00:00
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the register request
|
|
|
|
alloc := mock.Alloc()
|
2016-07-21 21:43:21 +00:00
|
|
|
summary := mock.JobSummary(alloc.JobID)
|
2015-09-06 22:34:28 +00:00
|
|
|
state := s1.fsm.State()
|
2016-07-21 21:43:21 +00:00
|
|
|
|
|
|
|
if err := state.UpsertJobSummary(999, summary); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if err := state.UpsertAllocs(1000, []*structs.Allocation{alloc}); err != nil {
|
2015-09-06 22:34:28 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-12-24 10:46:59 +00:00
|
|
|
// Lookup the allocations
|
2015-09-06 22:34:28 +00:00
|
|
|
get := &structs.AllocListRequest{
|
2017-09-07 23:56:15 +00:00
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
Namespace: structs.DefaultNamespace,
|
|
|
|
},
|
2015-09-06 22:34:28 +00:00
|
|
|
}
|
|
|
|
var resp structs.AllocListResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Alloc.List", get, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Index != 1000 {
|
|
|
|
t.Fatalf("Bad index: %d %d", resp.Index, 1000)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.Allocations) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", resp.Allocations)
|
|
|
|
}
|
|
|
|
if resp.Allocations[0].ID != alloc.ID {
|
|
|
|
t.Fatalf("bad: %#v", resp.Allocations[0])
|
2015-12-24 10:46:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup the allocations by prefix
|
|
|
|
get = &structs.AllocListRequest{
|
2017-09-07 23:56:15 +00:00
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
Namespace: structs.DefaultNamespace,
|
|
|
|
Prefix: alloc.ID[:4],
|
|
|
|
},
|
2015-12-24 10:46:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var resp2 structs.AllocListResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Alloc.List", get, &resp2); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp2.Index != 1000 {
|
|
|
|
t.Fatalf("Bad index: %d %d", resp2.Index, 1000)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp2.Allocations) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", resp2.Allocations)
|
|
|
|
}
|
|
|
|
if resp2.Allocations[0].ID != alloc.ID {
|
|
|
|
t.Fatalf("bad: %#v", resp2.Allocations[0])
|
2015-09-06 22:34:28 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-06 22:46:45 +00:00
|
|
|
|
2017-09-15 00:24:51 +00:00
|
|
|
func TestAllocEndpoint_List_ACL(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-01-12 01:00:30 +00:00
|
|
|
s1, root := TestACLServer(t, nil)
|
2017-09-15 00:24:51 +00:00
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
// Create the alloc
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
allocs := []*structs.Allocation{alloc}
|
|
|
|
summary := mock.JobSummary(alloc.JobID)
|
|
|
|
state := s1.fsm.State()
|
|
|
|
|
|
|
|
assert.Nil(state.UpsertJobSummary(999, summary), "UpsertJobSummary")
|
|
|
|
assert.Nil(state.UpsertAllocs(1000, allocs), "UpsertAllocs")
|
|
|
|
|
|
|
|
stubAllocs := []*structs.AllocListStub{alloc.Stub()}
|
|
|
|
stubAllocs[0].CreateIndex = 1000
|
|
|
|
stubAllocs[0].ModifyIndex = 1000
|
|
|
|
|
|
|
|
// Create the namespace policy and tokens
|
2017-10-04 22:08:10 +00:00
|
|
|
validToken := mock.CreatePolicyAndToken(t, state, 1001, "test-valid",
|
|
|
|
mock.NamespacePolicy(structs.DefaultNamespace, "", []string{acl.NamespaceCapabilityReadJob}))
|
|
|
|
invalidToken := mock.CreatePolicyAndToken(t, state, 1003, "test-invalid",
|
|
|
|
mock.NamespacePolicy(structs.DefaultNamespace, "", []string{acl.NamespaceCapabilityListJobs}))
|
2017-09-15 00:24:51 +00:00
|
|
|
|
|
|
|
// Lookup the allocs without a token and expect failure
|
|
|
|
get := &structs.AllocListRequest{
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
Namespace: structs.DefaultNamespace,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var resp structs.AllocListResponse
|
|
|
|
assert.NotNil(msgpackrpc.CallWithCodec(codec, "Alloc.List", get, &resp), "RPC")
|
|
|
|
|
|
|
|
// Try with a valid token
|
2017-10-12 22:16:33 +00:00
|
|
|
get.AuthToken = validToken.SecretID
|
2017-09-15 00:24:51 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Alloc.List", get, &resp), "RPC")
|
|
|
|
assert.EqualValues(resp.Index, 1000, "resp.Index")
|
|
|
|
assert.Equal(stubAllocs, resp.Allocations, "Returned alloc list not equal")
|
|
|
|
|
|
|
|
// Try with a invalid token
|
2017-10-12 22:16:33 +00:00
|
|
|
get.AuthToken = invalidToken.SecretID
|
2017-09-15 00:24:51 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Alloc.List", get, &resp)
|
|
|
|
assert.NotNil(err, "RPC")
|
|
|
|
assert.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
|
|
|
|
|
|
|
// Try with a root token
|
2017-10-12 22:16:33 +00:00
|
|
|
get.AuthToken = root.SecretID
|
2017-09-15 00:24:51 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Alloc.List", get, &resp), "RPC")
|
|
|
|
assert.EqualValues(resp.Index, 1000, "resp.Index")
|
|
|
|
assert.Equal(stubAllocs, resp.Allocations, "Returned alloc list not equal")
|
|
|
|
}
|
|
|
|
|
2015-10-30 02:00:02 +00:00
|
|
|
func TestAllocEndpoint_List_Blocking(t *testing.T) {
|
2017-07-23 22:04:38 +00:00
|
|
|
t.Parallel()
|
2018-01-12 01:00:30 +00:00
|
|
|
s1 := TestServer(t, nil)
|
2015-10-29 02:25:39 +00:00
|
|
|
defer s1.Shutdown()
|
|
|
|
state := s1.fsm.State()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the alloc
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
|
2016-07-21 21:43:21 +00:00
|
|
|
summary := mock.JobSummary(alloc.JobID)
|
|
|
|
if err := state.UpsertJobSummary(1, summary); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-10-29 02:25:39 +00:00
|
|
|
// Upsert alloc triggers watches
|
|
|
|
time.AfterFunc(100*time.Millisecond, func() {
|
|
|
|
if err := state.UpsertAllocs(2, []*structs.Allocation{alloc}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
req := &structs.AllocListRequest{
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
2017-09-07 23:56:15 +00:00
|
|
|
Namespace: structs.DefaultNamespace,
|
2015-10-29 02:25:39 +00:00
|
|
|
MinQueryIndex: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
start := time.Now()
|
|
|
|
var resp structs.AllocListResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Alloc.List", req, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-30 15:27:47 +00:00
|
|
|
if elapsed := time.Since(start); elapsed < 100*time.Millisecond {
|
2015-10-29 02:25:39 +00:00
|
|
|
t.Fatalf("should block (returned in %s) %#v", elapsed, resp)
|
|
|
|
}
|
|
|
|
if resp.Index != 2 {
|
|
|
|
t.Fatalf("Bad index: %d %d", resp.Index, 2)
|
|
|
|
}
|
|
|
|
if len(resp.Allocations) != 1 || resp.Allocations[0].ID != alloc.ID {
|
|
|
|
t.Fatalf("bad: %#v", resp.Allocations)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client updates trigger watches
|
|
|
|
alloc2 := mock.Alloc()
|
|
|
|
alloc2.ID = alloc.ID
|
|
|
|
alloc2.ClientStatus = structs.AllocClientStatusRunning
|
|
|
|
time.AfterFunc(100*time.Millisecond, func() {
|
2016-07-25 21:11:32 +00:00
|
|
|
state.UpsertJobSummary(3, mock.JobSummary(alloc2.JobID))
|
|
|
|
if err := state.UpdateAllocsFromClient(4, []*structs.Allocation{alloc2}); err != nil {
|
2015-10-29 02:25:39 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-07-25 21:11:32 +00:00
|
|
|
req.MinQueryIndex = 3
|
2015-10-29 02:25:39 +00:00
|
|
|
start = time.Now()
|
|
|
|
var resp2 structs.AllocListResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Alloc.List", req, &resp2); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-30 15:27:47 +00:00
|
|
|
if elapsed := time.Since(start); elapsed < 100*time.Millisecond {
|
2015-10-29 02:25:39 +00:00
|
|
|
t.Fatalf("should block (returned in %s) %#v", elapsed, resp2)
|
|
|
|
}
|
2016-07-25 21:11:32 +00:00
|
|
|
if resp2.Index != 4 {
|
|
|
|
t.Fatalf("Bad index: %d %d", resp2.Index, 4)
|
2015-10-29 02:25:39 +00:00
|
|
|
}
|
|
|
|
if len(resp2.Allocations) != 1 || resp.Allocations[0].ID != alloc.ID ||
|
|
|
|
resp2.Allocations[0].ClientStatus != structs.AllocClientStatusRunning {
|
|
|
|
t.Fatalf("bad: %#v", resp2.Allocations)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-06 22:46:45 +00:00
|
|
|
func TestAllocEndpoint_GetAlloc(t *testing.T) {
|
2017-07-23 22:04:38 +00:00
|
|
|
t.Parallel()
|
2018-01-12 01:00:30 +00:00
|
|
|
s1 := TestServer(t, nil)
|
2015-09-06 22:46:45 +00:00
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the register request
|
2018-01-19 00:05:20 +00:00
|
|
|
prevAllocID := uuid.Generate()
|
2015-09-06 22:46:45 +00:00
|
|
|
alloc := mock.Alloc()
|
2018-01-19 00:05:20 +00:00
|
|
|
alloc.RescheduleTracker = &structs.RescheduleTracker{
|
|
|
|
Events: []*structs.RescheduleEvent{
|
|
|
|
{RescheduleTime: time.Now().UTC().UnixNano(), PrevNodeID: "boom", PrevAllocID: prevAllocID},
|
|
|
|
},
|
|
|
|
}
|
2015-09-06 22:46:45 +00:00
|
|
|
state := s1.fsm.State()
|
2016-07-25 21:11:32 +00:00
|
|
|
state.UpsertJobSummary(999, mock.JobSummary(alloc.JobID))
|
2015-09-07 03:47:42 +00:00
|
|
|
err := state.UpsertAllocs(1000, []*structs.Allocation{alloc})
|
2015-09-06 22:46:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-09-15 00:43:27 +00:00
|
|
|
// Lookup the alloc
|
2015-09-06 22:46:45 +00:00
|
|
|
get := &structs.AllocSpecificRequest{
|
|
|
|
AllocID: alloc.ID,
|
2015-09-14 01:18:40 +00:00
|
|
|
QueryOptions: structs.QueryOptions{Region: "global"},
|
2015-09-06 22:46:45 +00:00
|
|
|
}
|
|
|
|
var resp structs.SingleAllocResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Alloc.GetAlloc", get, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Index != 1000 {
|
|
|
|
t.Fatalf("Bad index: %d %d", resp.Index, 1000)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(alloc, resp.Alloc) {
|
|
|
|
t.Fatalf("bad: %#v", resp.Alloc)
|
|
|
|
}
|
|
|
|
}
|
2015-10-29 23:04:53 +00:00
|
|
|
|
2017-09-15 00:43:27 +00:00
|
|
|
func TestAllocEndpoint_GetAlloc_ACL(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-01-12 01:00:30 +00:00
|
|
|
s1, root := TestACLServer(t, nil)
|
2017-09-15 00:43:27 +00:00
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
// Create the alloc
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
allocs := []*structs.Allocation{alloc}
|
|
|
|
summary := mock.JobSummary(alloc.JobID)
|
|
|
|
state := s1.fsm.State()
|
|
|
|
|
|
|
|
assert.Nil(state.UpsertJobSummary(999, summary), "UpsertJobSummary")
|
|
|
|
assert.Nil(state.UpsertAllocs(1000, allocs), "UpsertAllocs")
|
|
|
|
|
|
|
|
// Create the namespace policy and tokens
|
2017-10-04 22:08:10 +00:00
|
|
|
validToken := mock.CreatePolicyAndToken(t, state, 1001, "test-valid",
|
|
|
|
mock.NamespacePolicy(structs.DefaultNamespace, "", []string{acl.NamespaceCapabilityReadJob}))
|
|
|
|
invalidToken := mock.CreatePolicyAndToken(t, state, 1003, "test-invalid",
|
|
|
|
mock.NamespacePolicy(structs.DefaultNamespace, "", []string{acl.NamespaceCapabilityListJobs}))
|
2017-09-15 00:43:27 +00:00
|
|
|
|
|
|
|
get := &structs.AllocSpecificRequest{
|
|
|
|
AllocID: alloc.ID,
|
|
|
|
QueryOptions: structs.QueryOptions{Region: "global"},
|
|
|
|
}
|
|
|
|
|
2017-10-12 23:27:33 +00:00
|
|
|
// Lookup the alloc without a token and expect failure
|
|
|
|
{
|
|
|
|
var resp structs.SingleAllocResponse
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Alloc.GetAlloc", get, &resp)
|
|
|
|
assert.Equal(structs.ErrPermissionDenied.Error(), err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with a valid ACL token
|
|
|
|
{
|
|
|
|
get.AuthToken = validToken.SecretID
|
|
|
|
var resp structs.SingleAllocResponse
|
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Alloc.GetAlloc", get, &resp), "RPC")
|
|
|
|
assert.EqualValues(resp.Index, 1000, "resp.Index")
|
|
|
|
assert.Equal(alloc, resp.Alloc, "Returned alloc not equal")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with a valid Node.SecretID
|
|
|
|
{
|
|
|
|
node := mock.Node()
|
|
|
|
assert.Nil(state.UpsertNode(1005, node))
|
|
|
|
get.AuthToken = node.SecretID
|
|
|
|
var resp structs.SingleAllocResponse
|
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Alloc.GetAlloc", get, &resp), "RPC")
|
|
|
|
assert.EqualValues(resp.Index, 1000, "resp.Index")
|
|
|
|
assert.Equal(alloc, resp.Alloc, "Returned alloc not equal")
|
|
|
|
}
|
2017-09-15 00:43:27 +00:00
|
|
|
|
|
|
|
// Try with a invalid token
|
2017-10-12 23:27:33 +00:00
|
|
|
{
|
|
|
|
get.AuthToken = invalidToken.SecretID
|
|
|
|
var resp structs.SingleAllocResponse
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Alloc.GetAlloc", get, &resp)
|
|
|
|
assert.NotNil(err, "RPC")
|
|
|
|
assert.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
|
|
|
}
|
2017-09-15 00:43:27 +00:00
|
|
|
|
|
|
|
// Try with a root token
|
2017-10-12 23:27:33 +00:00
|
|
|
{
|
|
|
|
get.AuthToken = root.SecretID
|
|
|
|
var resp structs.SingleAllocResponse
|
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Alloc.GetAlloc", get, &resp), "RPC")
|
|
|
|
assert.EqualValues(resp.Index, 1000, "resp.Index")
|
|
|
|
assert.Equal(alloc, resp.Alloc, "Returned alloc not equal")
|
|
|
|
}
|
2017-09-15 00:43:27 +00:00
|
|
|
}
|
|
|
|
|
2015-10-30 02:00:02 +00:00
|
|
|
func TestAllocEndpoint_GetAlloc_Blocking(t *testing.T) {
|
2017-07-23 22:04:38 +00:00
|
|
|
t.Parallel()
|
2018-01-12 01:00:30 +00:00
|
|
|
s1 := TestServer(t, nil)
|
2015-10-29 23:04:53 +00:00
|
|
|
defer s1.Shutdown()
|
|
|
|
state := s1.fsm.State()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the allocs
|
|
|
|
alloc1 := mock.Alloc()
|
|
|
|
alloc2 := mock.Alloc()
|
|
|
|
|
|
|
|
// First create an unrelated alloc
|
|
|
|
time.AfterFunc(100*time.Millisecond, func() {
|
2016-07-25 21:11:32 +00:00
|
|
|
state.UpsertJobSummary(99, mock.JobSummary(alloc1.JobID))
|
2015-10-30 02:00:02 +00:00
|
|
|
err := state.UpsertAllocs(100, []*structs.Allocation{alloc1})
|
2015-10-29 23:04:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Create the alloc we are watching later
|
|
|
|
time.AfterFunc(200*time.Millisecond, func() {
|
2017-02-08 06:10:33 +00:00
|
|
|
state.UpsertJobSummary(199, mock.JobSummary(alloc2.JobID))
|
2015-10-30 02:00:02 +00:00
|
|
|
err := state.UpsertAllocs(200, []*structs.Allocation{alloc2})
|
2015-10-29 23:04:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-02-01 21:57:35 +00:00
|
|
|
// Lookup the allocs
|
2015-10-29 23:04:53 +00:00
|
|
|
get := &structs.AllocSpecificRequest{
|
|
|
|
AllocID: alloc2.ID,
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
2017-02-08 06:10:33 +00:00
|
|
|
MinQueryIndex: 150,
|
2015-10-29 23:04:53 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
var resp structs.SingleAllocResponse
|
|
|
|
start := time.Now()
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Alloc.GetAlloc", get, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-30 15:27:47 +00:00
|
|
|
if elapsed := time.Since(start); elapsed < 200*time.Millisecond {
|
2015-10-29 23:04:53 +00:00
|
|
|
t.Fatalf("should block (returned in %s) %#v", elapsed, resp)
|
|
|
|
}
|
2015-10-30 02:00:02 +00:00
|
|
|
if resp.Index != 200 {
|
|
|
|
t.Fatalf("Bad index: %d %d", resp.Index, 200)
|
2015-10-29 23:04:53 +00:00
|
|
|
}
|
|
|
|
if resp.Alloc == nil || resp.Alloc.ID != alloc2.ID {
|
|
|
|
t.Fatalf("bad: %#v", resp.Alloc)
|
|
|
|
}
|
|
|
|
}
|
2016-02-01 21:57:35 +00:00
|
|
|
|
|
|
|
func TestAllocEndpoint_GetAllocs(t *testing.T) {
|
2017-07-23 22:04:38 +00:00
|
|
|
t.Parallel()
|
2018-01-12 01:00:30 +00:00
|
|
|
s1 := TestServer(t, nil)
|
2016-02-01 21:57:35 +00:00
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the register request
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc2 := mock.Alloc()
|
|
|
|
state := s1.fsm.State()
|
2016-07-25 21:11:32 +00:00
|
|
|
state.UpsertJobSummary(998, mock.JobSummary(alloc.JobID))
|
|
|
|
state.UpsertJobSummary(999, mock.JobSummary(alloc2.JobID))
|
2016-02-01 21:57:35 +00:00
|
|
|
err := state.UpsertAllocs(1000, []*structs.Allocation{alloc, alloc2})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup the allocs
|
|
|
|
get := &structs.AllocsGetRequest{
|
2017-01-10 21:25:52 +00:00
|
|
|
AllocIDs: []string{alloc.ID, alloc2.ID},
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
},
|
2016-02-01 21:57:35 +00:00
|
|
|
}
|
|
|
|
var resp structs.AllocsGetResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Alloc.GetAllocs", get, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Index != 1000 {
|
|
|
|
t.Fatalf("Bad index: %d %d", resp.Index, 1000)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.Allocs) != 2 {
|
|
|
|
t.Fatalf("bad: %#v", resp.Allocs)
|
|
|
|
}
|
|
|
|
|
2018-03-12 18:26:37 +00:00
|
|
|
// Lookup nonexistent allocs.
|
2016-02-01 21:57:35 +00:00
|
|
|
get = &structs.AllocsGetRequest{
|
|
|
|
AllocIDs: []string{"foo"},
|
|
|
|
QueryOptions: structs.QueryOptions{Region: "global"},
|
|
|
|
}
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Alloc.GetAllocs", get, &resp); err == nil {
|
|
|
|
t.Fatalf("expect error")
|
|
|
|
}
|
|
|
|
}
|
2017-01-10 21:25:52 +00:00
|
|
|
|
|
|
|
func TestAllocEndpoint_GetAllocs_Blocking(t *testing.T) {
|
2017-07-23 22:04:38 +00:00
|
|
|
t.Parallel()
|
2018-01-12 01:00:30 +00:00
|
|
|
s1 := TestServer(t, nil)
|
2017-01-10 21:25:52 +00:00
|
|
|
defer s1.Shutdown()
|
|
|
|
state := s1.fsm.State()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the allocs
|
|
|
|
alloc1 := mock.Alloc()
|
|
|
|
alloc2 := mock.Alloc()
|
|
|
|
|
|
|
|
// First create an unrelated alloc
|
|
|
|
time.AfterFunc(100*time.Millisecond, func() {
|
|
|
|
state.UpsertJobSummary(99, mock.JobSummary(alloc1.JobID))
|
|
|
|
err := state.UpsertAllocs(100, []*structs.Allocation{alloc1})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Create the alloc we are watching later
|
|
|
|
time.AfterFunc(200*time.Millisecond, func() {
|
|
|
|
state.UpsertJobSummary(199, mock.JobSummary(alloc2.JobID))
|
|
|
|
err := state.UpsertAllocs(200, []*structs.Allocation{alloc2})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Lookup the allocs
|
|
|
|
get := &structs.AllocsGetRequest{
|
|
|
|
AllocIDs: []string{alloc1.ID, alloc2.ID},
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
MinQueryIndex: 150,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var resp structs.AllocsGetResponse
|
|
|
|
start := time.Now()
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Alloc.GetAllocs", get, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if elapsed := time.Since(start); elapsed < 200*time.Millisecond {
|
|
|
|
t.Fatalf("should block (returned in %s) %#v", elapsed, resp)
|
|
|
|
}
|
|
|
|
if resp.Index != 200 {
|
|
|
|
t.Fatalf("Bad index: %d %d", resp.Index, 200)
|
|
|
|
}
|
|
|
|
if len(resp.Allocs) != 2 {
|
|
|
|
t.Fatalf("bad: %#v", resp.Allocs)
|
|
|
|
}
|
|
|
|
}
|
2018-02-21 18:58:04 +00:00
|
|
|
|
2018-02-23 01:38:44 +00:00
|
|
|
func TestAllocEndpoint_UpdateDesiredTransition(t *testing.T) {
|
2018-02-21 18:58:04 +00:00
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
s1, _ := TestACLServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the register request
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc2 := mock.Alloc()
|
|
|
|
state := s1.fsm.State()
|
|
|
|
require.Nil(state.UpsertJobSummary(998, mock.JobSummary(alloc.JobID)))
|
|
|
|
require.Nil(state.UpsertJobSummary(999, mock.JobSummary(alloc2.JobID)))
|
|
|
|
require.Nil(state.UpsertAllocs(1000, []*structs.Allocation{alloc, alloc2}))
|
|
|
|
|
2018-02-23 01:38:44 +00:00
|
|
|
t1 := &structs.DesiredTransition{
|
2018-02-21 18:58:04 +00:00
|
|
|
Migrate: helper.BoolToPtr(true),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the allocs desired status
|
2018-02-23 01:38:44 +00:00
|
|
|
get := &structs.AllocUpdateDesiredTransitionRequest{
|
|
|
|
Allocs: map[string]*structs.DesiredTransition{
|
2018-02-21 18:58:04 +00:00
|
|
|
alloc.ID: t1,
|
|
|
|
alloc2.ID: t1,
|
|
|
|
},
|
2018-02-23 01:38:44 +00:00
|
|
|
Evals: []*structs.Evaluation{
|
|
|
|
{
|
|
|
|
ID: uuid.Generate(),
|
|
|
|
Namespace: alloc.Namespace,
|
|
|
|
Priority: alloc.Job.Priority,
|
|
|
|
Type: alloc.Job.Type,
|
|
|
|
TriggeredBy: structs.EvalTriggerNodeDrain,
|
|
|
|
JobID: alloc.Job.ID,
|
|
|
|
JobModifyIndex: alloc.Job.ModifyIndex,
|
|
|
|
Status: structs.EvalStatusPending,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: uuid.Generate(),
|
|
|
|
Namespace: alloc2.Namespace,
|
|
|
|
Priority: alloc2.Job.Priority,
|
|
|
|
Type: alloc2.Job.Type,
|
|
|
|
TriggeredBy: structs.EvalTriggerNodeDrain,
|
|
|
|
JobID: alloc2.Job.ID,
|
|
|
|
JobModifyIndex: alloc2.Job.ModifyIndex,
|
|
|
|
Status: structs.EvalStatusPending,
|
|
|
|
},
|
|
|
|
},
|
2018-02-21 18:58:04 +00:00
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Region: "global",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try without permissions
|
|
|
|
var resp structs.GenericResponse
|
2018-02-23 01:38:44 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Alloc.UpdateDesiredTransition", get, &resp)
|
2018-02-21 18:58:04 +00:00
|
|
|
require.NotNil(err)
|
|
|
|
require.True(structs.IsErrPermissionDenied(err))
|
|
|
|
|
|
|
|
// Try with permissions
|
|
|
|
get.WriteRequest.AuthToken = s1.getLeaderAcl()
|
|
|
|
var resp2 structs.GenericResponse
|
2018-02-23 01:38:44 +00:00
|
|
|
require.Nil(msgpackrpc.CallWithCodec(codec, "Alloc.UpdateDesiredTransition", get, &resp2))
|
2018-02-21 18:58:04 +00:00
|
|
|
require.NotZero(resp2.Index)
|
|
|
|
|
|
|
|
// Look up the allocations
|
|
|
|
out1, err := state.AllocByID(nil, alloc.ID)
|
|
|
|
require.Nil(err)
|
|
|
|
out2, err := state.AllocByID(nil, alloc.ID)
|
|
|
|
require.Nil(err)
|
2018-02-23 01:38:44 +00:00
|
|
|
e1, err := state.EvalByID(nil, get.Evals[0].ID)
|
|
|
|
require.Nil(err)
|
|
|
|
e2, err := state.EvalByID(nil, get.Evals[1].ID)
|
|
|
|
require.Nil(err)
|
2018-02-21 18:58:04 +00:00
|
|
|
|
2018-02-23 01:38:44 +00:00
|
|
|
require.NotNil(out1.DesiredTransition.Migrate)
|
|
|
|
require.NotNil(out2.DesiredTransition.Migrate)
|
|
|
|
require.NotNil(e1)
|
|
|
|
require.NotNil(e2)
|
|
|
|
require.True(*out1.DesiredTransition.Migrate)
|
|
|
|
require.True(*out2.DesiredTransition.Migrate)
|
2018-02-21 18:58:04 +00:00
|
|
|
}
|