2015-07-06 20:23:11 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2016-02-22 02:51:34 +00:00
|
|
|
"fmt"
|
2015-07-06 21:38:57 +00:00
|
|
|
"reflect"
|
2016-08-16 06:11:57 +00:00
|
|
|
"strings"
|
2015-07-06 20:23:11 +00:00
|
|
|
"testing"
|
2015-08-23 02:17:49 +00:00
|
|
|
"time"
|
2015-07-06 20:23:11 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/net-rpc-msgpackrpc"
|
2015-08-11 21:27:14 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2015-07-06 20:23:11 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2016-08-19 20:13:51 +00:00
|
|
|
vapi "github.com/hashicorp/vault/api"
|
2015-07-06 20:23:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestClientEndpoint_Register(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the register request
|
2015-08-11 21:27:14 +00:00
|
|
|
node := mock.Node()
|
2015-07-07 16:51:42 +00:00
|
|
|
req := &structs.NodeRegisterRequest{
|
2015-07-06 20:23:11 +00:00
|
|
|
Node: node,
|
2015-09-14 01:18:40 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
2015-07-06 20:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp structs.GenericResponse
|
2015-09-07 03:31:32 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.Register", req, &resp); err != nil {
|
2015-07-06 20:23:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-07-06 20:34:32 +00:00
|
|
|
if resp.Index == 0 {
|
|
|
|
t.Fatalf("bad index: %d", resp.Index)
|
|
|
|
}
|
2015-07-06 20:23:11 +00:00
|
|
|
|
|
|
|
// Check for the node in the FSM
|
|
|
|
state := s1.fsm.State()
|
2015-09-07 03:56:38 +00:00
|
|
|
out, err := state.NodeByID(node.ID)
|
2015-07-06 20:23:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("expected node")
|
|
|
|
}
|
2015-07-06 20:34:32 +00:00
|
|
|
if out.CreateIndex != resp.Index {
|
|
|
|
t.Fatalf("index mis-match")
|
|
|
|
}
|
2016-01-30 01:46:44 +00:00
|
|
|
if out.ComputedClass == "" {
|
2016-01-21 01:30:02 +00:00
|
|
|
t.Fatal("ComputedClass not set")
|
|
|
|
}
|
2015-07-06 20:23:11 +00:00
|
|
|
}
|
2015-07-06 20:42:33 +00:00
|
|
|
|
2016-08-16 06:11:57 +00:00
|
|
|
func TestClientEndpoint_Register_NoSecret(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the register request
|
|
|
|
node := mock.Node()
|
|
|
|
node.SecretID = ""
|
|
|
|
req := &structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp structs.GenericResponse
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Node.Register", req, &resp)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "secret") {
|
|
|
|
t.Fatalf("Expecting error regarding missing secret id: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the node to be pre-0.5
|
|
|
|
node.Attributes["nomad.version"] = "0.4.1"
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.Register", req, &resp); err != nil {
|
2016-10-27 05:05:44 +00:00
|
|
|
t.Fatalf("Not expecting err: %v", err)
|
2016-08-16 06:11:57 +00:00
|
|
|
}
|
|
|
|
if resp.Index == 0 {
|
|
|
|
t.Fatalf("bad index: %d", resp.Index)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the node in the FSM
|
|
|
|
state := s1.fsm.State()
|
|
|
|
out, err := state.NodeByID(node.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("expected node")
|
|
|
|
}
|
|
|
|
if out.CreateIndex != resp.Index {
|
|
|
|
t.Fatalf("index mis-match")
|
|
|
|
}
|
|
|
|
if out.ComputedClass == "" {
|
|
|
|
t.Fatal("ComputedClass not set")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-19 17:50:49 +00:00
|
|
|
func TestClientEndpoint_Register_SecretMismatch(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the register request
|
|
|
|
node := mock.Node()
|
|
|
|
req := &structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp structs.GenericResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.Register", req, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the nodes SecretID
|
|
|
|
node.SecretID = structs.GenerateUUID()
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Node.Register", req, &resp)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "Not registering") {
|
|
|
|
t.Fatalf("Expecting error regarding mismatching secret id", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-06 20:42:33 +00:00
|
|
|
func TestClientEndpoint_Deregister(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the register request
|
2015-08-11 21:27:14 +00:00
|
|
|
node := mock.Node()
|
2015-07-07 16:51:42 +00:00
|
|
|
reg := &structs.NodeRegisterRequest{
|
2015-07-06 20:42:33 +00:00
|
|
|
Node: node,
|
2015-09-14 01:18:40 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
2015-07-06 20:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp structs.GenericResponse
|
2015-09-07 03:31:32 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.Register", reg, &resp); err != nil {
|
2015-07-06 20:42:33 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deregister
|
2015-07-07 16:51:42 +00:00
|
|
|
dereg := &structs.NodeDeregisterRequest{
|
2015-07-06 20:42:33 +00:00
|
|
|
NodeID: node.ID,
|
2015-09-14 01:18:40 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
2015-07-06 20:42:33 +00:00
|
|
|
}
|
|
|
|
var resp2 structs.GenericResponse
|
2015-09-07 03:31:32 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.Deregister", dereg, &resp2); err != nil {
|
2015-07-06 20:42:33 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp2.Index == 0 {
|
|
|
|
t.Fatalf("bad index: %d", resp2.Index)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the node in the FSM
|
|
|
|
state := s1.fsm.State()
|
2015-09-07 03:56:38 +00:00
|
|
|
out, err := state.NodeByID(node.ID)
|
2015-07-06 20:42:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatalf("unexpected node")
|
|
|
|
}
|
|
|
|
}
|
2015-07-06 20:50:40 +00:00
|
|
|
|
2016-08-22 20:57:27 +00:00
|
|
|
func TestClientEndpoint_Deregister_Vault(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the register request
|
|
|
|
node := mock.Node()
|
|
|
|
reg := &structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp structs.GenericResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.Register", reg, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap the servers Vault Client
|
|
|
|
tvc := &TestVaultClient{}
|
|
|
|
s1.vault = tvc
|
|
|
|
|
|
|
|
// Put some Vault accessors in the state store for that node
|
|
|
|
state := s1.fsm.State()
|
|
|
|
va1 := mock.VaultAccessor()
|
|
|
|
va1.NodeID = node.ID
|
|
|
|
va2 := mock.VaultAccessor()
|
|
|
|
va2.NodeID = node.ID
|
|
|
|
state.UpsertVaultAccessor(100, []*structs.VaultAccessor{va1, va2})
|
|
|
|
|
|
|
|
// Deregister
|
|
|
|
dereg := &structs.NodeDeregisterRequest{
|
|
|
|
NodeID: node.ID,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
var resp2 structs.GenericResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.Deregister", dereg, &resp2); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp2.Index == 0 {
|
|
|
|
t.Fatalf("bad index: %d", resp2.Index)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the node in the FSM
|
|
|
|
out, err := state.NodeByID(node.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatalf("unexpected node")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the endpoint revoked the tokens
|
|
|
|
if l := len(tvc.RevokedTokens); l != 2 {
|
|
|
|
t.Fatalf("Deregister revoked %d tokens; want 2", l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-06 20:50:40 +00:00
|
|
|
func TestClientEndpoint_UpdateStatus(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the register request
|
2015-08-11 21:27:14 +00:00
|
|
|
node := mock.Node()
|
2015-07-07 16:51:42 +00:00
|
|
|
reg := &structs.NodeRegisterRequest{
|
2015-07-06 20:50:40 +00:00
|
|
|
Node: node,
|
2015-09-14 01:18:40 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
2015-07-06 20:50:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
2015-08-23 00:49:48 +00:00
|
|
|
var resp structs.NodeUpdateResponse
|
2015-09-07 03:31:32 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.Register", reg, &resp); err != nil {
|
2015-07-06 20:50:40 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-08-23 00:49:48 +00:00
|
|
|
// Check for heartbeat interval
|
2015-08-23 01:16:05 +00:00
|
|
|
ttl := resp.HeartbeatTTL
|
2015-08-31 01:10:12 +00:00
|
|
|
if ttl < s1.config.MinHeartbeatTTL || ttl > 2*s1.config.MinHeartbeatTTL {
|
2015-08-23 01:16:05 +00:00
|
|
|
t.Fatalf("bad: %#v", ttl)
|
2015-08-23 00:49:48 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 20:50:40 +00:00
|
|
|
// Update the status
|
2015-07-07 16:51:42 +00:00
|
|
|
dereg := &structs.NodeUpdateStatusRequest{
|
2015-07-06 20:50:40 +00:00
|
|
|
NodeID: node.ID,
|
2015-09-07 02:47:02 +00:00
|
|
|
Status: structs.NodeStatusInit,
|
2015-09-14 01:18:40 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
2015-07-06 20:50:40 +00:00
|
|
|
}
|
2015-08-23 00:49:48 +00:00
|
|
|
var resp2 structs.NodeUpdateResponse
|
2015-09-07 03:31:32 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.UpdateStatus", dereg, &resp2); err != nil {
|
2015-07-06 20:50:40 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp2.Index == 0 {
|
|
|
|
t.Fatalf("bad index: %d", resp2.Index)
|
|
|
|
}
|
|
|
|
|
2015-08-23 00:49:48 +00:00
|
|
|
// Check for heartbeat interval
|
2015-08-23 01:16:05 +00:00
|
|
|
ttl = resp2.HeartbeatTTL
|
2015-08-31 01:10:12 +00:00
|
|
|
if ttl < s1.config.MinHeartbeatTTL || ttl > 2*s1.config.MinHeartbeatTTL {
|
2015-08-23 01:16:05 +00:00
|
|
|
t.Fatalf("bad: %#v", ttl)
|
2015-08-23 00:49:48 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 20:50:40 +00:00
|
|
|
// Check for the node in the FSM
|
|
|
|
state := s1.fsm.State()
|
2015-09-07 03:56:38 +00:00
|
|
|
out, err := state.NodeByID(node.ID)
|
2015-07-06 20:50:40 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("expected node")
|
|
|
|
}
|
|
|
|
if out.ModifyIndex != resp2.Index {
|
|
|
|
t.Fatalf("index mis-match")
|
|
|
|
}
|
|
|
|
}
|
2015-07-06 21:38:57 +00:00
|
|
|
|
2016-08-22 20:57:27 +00:00
|
|
|
func TestClientEndpoint_UpdateStatus_Vault(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the register request
|
|
|
|
node := mock.Node()
|
|
|
|
reg := &structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp structs.NodeUpdateResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.Register", reg, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for heartbeat interval
|
|
|
|
ttl := resp.HeartbeatTTL
|
|
|
|
if ttl < s1.config.MinHeartbeatTTL || ttl > 2*s1.config.MinHeartbeatTTL {
|
|
|
|
t.Fatalf("bad: %#v", ttl)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap the servers Vault Client
|
|
|
|
tvc := &TestVaultClient{}
|
|
|
|
s1.vault = tvc
|
|
|
|
|
|
|
|
// Put some Vault accessors in the state store for that node
|
|
|
|
state := s1.fsm.State()
|
|
|
|
va1 := mock.VaultAccessor()
|
|
|
|
va1.NodeID = node.ID
|
|
|
|
va2 := mock.VaultAccessor()
|
|
|
|
va2.NodeID = node.ID
|
|
|
|
state.UpsertVaultAccessor(100, []*structs.VaultAccessor{va1, va2})
|
|
|
|
|
|
|
|
// Update the status to be down
|
|
|
|
dereg := &structs.NodeUpdateStatusRequest{
|
|
|
|
NodeID: node.ID,
|
|
|
|
Status: structs.NodeStatusDown,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
var resp2 structs.NodeUpdateResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.UpdateStatus", dereg, &resp2); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp2.Index == 0 {
|
|
|
|
t.Fatalf("bad index: %d", resp2.Index)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the endpoint revoked the tokens
|
|
|
|
if l := len(tvc.RevokedTokens); l != 2 {
|
|
|
|
t.Fatalf("Deregister revoked %d tokens; want 2", l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 22:22:02 +00:00
|
|
|
func TestClientEndpoint_Register_GetEvals(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Register a system job.
|
|
|
|
job := mock.SystemJob()
|
|
|
|
state := s1.fsm.State()
|
|
|
|
if err := state.UpsertJob(1, job); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the register request going directly to ready
|
|
|
|
node := mock.Node()
|
|
|
|
node.Status = structs.NodeStatusReady
|
|
|
|
reg := &structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp structs.NodeUpdateResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.Register", reg, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for heartbeat interval
|
|
|
|
ttl := resp.HeartbeatTTL
|
|
|
|
if ttl < s1.config.MinHeartbeatTTL || ttl > 2*s1.config.MinHeartbeatTTL {
|
|
|
|
t.Fatalf("bad: %#v", ttl)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for an eval caused by the system job.
|
|
|
|
if len(resp.EvalIDs) != 1 {
|
|
|
|
t.Fatalf("expected one eval; got %#v", resp.EvalIDs)
|
|
|
|
}
|
|
|
|
|
|
|
|
evalID := resp.EvalIDs[0]
|
|
|
|
eval, err := state.EvalByID(evalID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not get eval %v", evalID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if eval.Type != "system" {
|
|
|
|
t.Fatalf("unexpected eval type; got %v; want %q", eval.Type, "system")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the node in the FSM
|
|
|
|
out, err := state.NodeByID(node.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("expected node")
|
|
|
|
}
|
|
|
|
if out.ModifyIndex != resp.Index {
|
|
|
|
t.Fatalf("index mis-match")
|
|
|
|
}
|
2016-07-25 19:46:18 +00:00
|
|
|
|
|
|
|
// Transistion it to down and then ready
|
|
|
|
node.Status = structs.NodeStatusDown
|
|
|
|
reg = &structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.Register", reg, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.EvalIDs) != 1 {
|
|
|
|
t.Fatalf("expected one eval; got %#v", resp.EvalIDs)
|
|
|
|
}
|
|
|
|
|
|
|
|
node.Status = structs.NodeStatusReady
|
|
|
|
reg = &structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.Register", reg, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.EvalIDs) != 1 {
|
|
|
|
t.Fatalf("expected one eval; got %#v", resp.EvalIDs)
|
|
|
|
}
|
2016-07-21 22:22:02 +00:00
|
|
|
}
|
|
|
|
|
2015-10-20 18:33:37 +00:00
|
|
|
func TestClientEndpoint_UpdateStatus_GetEvals(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Register a system job.
|
|
|
|
job := mock.SystemJob()
|
|
|
|
state := s1.fsm.State()
|
|
|
|
if err := state.UpsertJob(1, job); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the register request
|
|
|
|
node := mock.Node()
|
|
|
|
node.Status = structs.NodeStatusInit
|
|
|
|
reg := &structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp structs.NodeUpdateResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.Register", reg, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for heartbeat interval
|
|
|
|
ttl := resp.HeartbeatTTL
|
|
|
|
if ttl < s1.config.MinHeartbeatTTL || ttl > 2*s1.config.MinHeartbeatTTL {
|
|
|
|
t.Fatalf("bad: %#v", ttl)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the status
|
|
|
|
update := &structs.NodeUpdateStatusRequest{
|
|
|
|
NodeID: node.ID,
|
|
|
|
Status: structs.NodeStatusReady,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
var resp2 structs.NodeUpdateResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.UpdateStatus", update, &resp2); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp2.Index == 0 {
|
|
|
|
t.Fatalf("bad index: %d", resp2.Index)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for an eval caused by the system job.
|
|
|
|
if len(resp2.EvalIDs) != 1 {
|
|
|
|
t.Fatalf("expected one eval; got %#v", resp2.EvalIDs)
|
|
|
|
}
|
|
|
|
|
|
|
|
evalID := resp2.EvalIDs[0]
|
|
|
|
eval, err := state.EvalByID(evalID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not get eval %v", evalID)
|
|
|
|
}
|
|
|
|
|
2015-10-23 23:32:45 +00:00
|
|
|
if eval.Type != "system" {
|
|
|
|
t.Fatalf("unexpected eval type; got %v; want %q", eval.Type, "system")
|
2015-10-20 18:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for heartbeat interval
|
|
|
|
ttl = resp2.HeartbeatTTL
|
|
|
|
if ttl < s1.config.MinHeartbeatTTL || ttl > 2*s1.config.MinHeartbeatTTL {
|
|
|
|
t.Fatalf("bad: %#v", ttl)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the node in the FSM
|
|
|
|
out, err := state.NodeByID(node.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("expected node")
|
|
|
|
}
|
|
|
|
if out.ModifyIndex != resp2.Index {
|
|
|
|
t.Fatalf("index mis-match")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-23 00:49:48 +00:00
|
|
|
func TestClientEndpoint_UpdateStatus_HeartbeatOnly(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
2016-06-01 10:47:19 +00:00
|
|
|
|
|
|
|
s2 := testServer(t, func(c *Config) {
|
|
|
|
c.DevDisableBootstrap = true
|
|
|
|
})
|
|
|
|
defer s2.Shutdown()
|
|
|
|
|
|
|
|
s3 := testServer(t, func(c *Config) {
|
|
|
|
c.DevDisableBootstrap = true
|
|
|
|
})
|
|
|
|
defer s3.Shutdown()
|
|
|
|
servers := []*Server{s1, s2, s3}
|
|
|
|
testJoin(t, s1, s2, s3)
|
|
|
|
|
|
|
|
for _, s := range servers {
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
peers, _ := s.raftPeers.Peers()
|
|
|
|
return len(peers) == 3, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("should have 3 peers")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-23 00:49:48 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the register request
|
|
|
|
node := mock.Node()
|
|
|
|
reg := &structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
2015-09-14 01:18:40 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
2015-08-23 00:49:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp structs.NodeUpdateResponse
|
2015-09-07 03:31:32 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.Register", reg, &resp); err != nil {
|
2015-08-23 00:49:48 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for heartbeat interval
|
2015-08-23 01:16:05 +00:00
|
|
|
ttl := resp.HeartbeatTTL
|
2015-08-31 01:10:12 +00:00
|
|
|
if ttl < s1.config.MinHeartbeatTTL || ttl > 2*s1.config.MinHeartbeatTTL {
|
2015-08-23 01:16:05 +00:00
|
|
|
t.Fatalf("bad: %#v", ttl)
|
2015-08-23 00:49:48 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 18:09:31 +00:00
|
|
|
// Check for heartbeat servers
|
2016-06-01 10:47:19 +00:00
|
|
|
serverAddrs := resp.Servers
|
|
|
|
if len(serverAddrs) == 0 {
|
|
|
|
t.Fatalf("bad: %#v", serverAddrs)
|
2016-05-23 18:09:31 +00:00
|
|
|
}
|
|
|
|
|
2015-08-23 00:49:48 +00:00
|
|
|
// Update the status, static state
|
|
|
|
dereg := &structs.NodeUpdateStatusRequest{
|
|
|
|
NodeID: node.ID,
|
|
|
|
Status: node.Status,
|
2015-09-14 01:18:40 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
2015-08-23 00:49:48 +00:00
|
|
|
}
|
|
|
|
var resp2 structs.NodeUpdateResponse
|
2015-09-07 03:31:32 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.UpdateStatus", dereg, &resp2); err != nil {
|
2015-08-23 00:49:48 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp2.Index != 0 {
|
|
|
|
t.Fatalf("bad index: %d", resp2.Index)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for heartbeat interval
|
2015-08-23 01:16:05 +00:00
|
|
|
ttl = resp2.HeartbeatTTL
|
2015-08-31 01:10:12 +00:00
|
|
|
if ttl < s1.config.MinHeartbeatTTL || ttl > 2*s1.config.MinHeartbeatTTL {
|
2015-08-23 01:16:05 +00:00
|
|
|
t.Fatalf("bad: %#v", ttl)
|
2015-08-23 00:49:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-07 03:00:12 +00:00
|
|
|
func TestClientEndpoint_UpdateDrain(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the register request
|
|
|
|
node := mock.Node()
|
|
|
|
reg := &structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
2015-09-14 01:18:40 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
2015-09-07 03:00:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp structs.NodeUpdateResponse
|
2015-09-07 03:31:32 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.Register", reg, &resp); err != nil {
|
2015-09-07 03:00:12 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the status
|
|
|
|
dereg := &structs.NodeUpdateDrainRequest{
|
|
|
|
NodeID: node.ID,
|
|
|
|
Drain: true,
|
2015-09-14 01:18:40 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
2015-09-07 03:00:12 +00:00
|
|
|
}
|
|
|
|
var resp2 structs.NodeDrainUpdateResponse
|
2015-09-07 03:31:32 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.UpdateDrain", dereg, &resp2); err != nil {
|
2015-09-07 03:00:12 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp2.Index == 0 {
|
|
|
|
t.Fatalf("bad index: %d", resp2.Index)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the node in the FSM
|
|
|
|
state := s1.fsm.State()
|
2015-09-07 03:56:38 +00:00
|
|
|
out, err := state.NodeByID(node.ID)
|
2015-09-07 03:00:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !out.Drain {
|
|
|
|
t.Fatalf("bad: %#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-09 20:11:58 +00:00
|
|
|
// This test ensures that Nomad marks client state of allocations which are in
|
|
|
|
// pending/running state to lost when a node is marked as down.
|
2016-08-09 17:16:17 +00:00
|
|
|
func TestClientEndpoint_Drain_Down(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
2016-08-09 20:11:58 +00:00
|
|
|
// Register a node
|
2016-08-09 17:16:17 +00:00
|
|
|
node := mock.Node()
|
|
|
|
reg := &structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
// Fetch the response
|
|
|
|
var resp structs.NodeUpdateResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.Register", reg, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-08-09 20:11:58 +00:00
|
|
|
// Register a service job
|
2016-08-09 17:16:17 +00:00
|
|
|
var jobResp structs.JobRegisterResponse
|
|
|
|
job := mock.Job()
|
2016-08-09 20:11:58 +00:00
|
|
|
job.TaskGroups[0].Count = 1
|
2016-08-09 17:16:17 +00:00
|
|
|
jobReq := &structs.JobRegisterRequest{
|
|
|
|
Job: job,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Job.Register", jobReq, &jobResp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-08-09 20:11:58 +00:00
|
|
|
// Register a system job
|
|
|
|
var jobResp1 structs.JobRegisterResponse
|
|
|
|
job1 := mock.Job()
|
|
|
|
job1.TaskGroups[0].Count = 1
|
|
|
|
job1.Type = structs.JobTypeSystem
|
|
|
|
jobReq1 := &structs.JobRegisterRequest{
|
|
|
|
Job: job1,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Job.Register", jobReq1, &jobResp1); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the scheduler to create an allocation
|
2016-08-09 17:16:17 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
allocs, err := s1.fsm.state.AllocsByJob(job.ID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2016-08-09 20:11:58 +00:00
|
|
|
allocs1, err := s1.fsm.state.AllocsByJob(job1.ID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return len(allocs) > 0 && len(allocs1) > 0, nil
|
2016-08-09 17:16:17 +00:00
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
|
2016-08-09 20:11:58 +00:00
|
|
|
// Drain the node
|
2016-08-09 17:16:17 +00:00
|
|
|
dereg := &structs.NodeUpdateDrainRequest{
|
|
|
|
NodeID: node.ID,
|
|
|
|
Drain: true,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
var resp2 structs.NodeDrainUpdateResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.UpdateDrain", dereg, &resp2); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-08-09 20:11:58 +00:00
|
|
|
// Mark the node as down
|
2016-08-09 17:16:17 +00:00
|
|
|
node.Status = structs.NodeStatusDown
|
|
|
|
reg = &structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.Register", reg, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the allocation has transitioned to lost
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
summary, err := s1.fsm.state.JobSummaryByID(job.ID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2016-08-09 20:11:58 +00:00
|
|
|
expectedSummary := &structs.JobSummary{
|
2016-08-09 17:16:17 +00:00
|
|
|
JobID: job.ID,
|
|
|
|
Summary: map[string]structs.TaskGroupSummary{
|
|
|
|
"web": structs.TaskGroupSummary{
|
2016-08-09 20:11:58 +00:00
|
|
|
Queued: 1,
|
|
|
|
Lost: 1,
|
2016-08-09 17:16:17 +00:00
|
|
|
},
|
|
|
|
},
|
2016-08-09 20:11:58 +00:00
|
|
|
CreateIndex: jobResp.JobModifyIndex,
|
|
|
|
ModifyIndex: summary.ModifyIndex,
|
2016-08-09 17:16:17 +00:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(summary, expectedSummary) {
|
2016-08-09 20:11:58 +00:00
|
|
|
return false, fmt.Errorf("expected: %#v, actual: %#v", expectedSummary, summary)
|
|
|
|
}
|
|
|
|
|
|
|
|
summary1, err := s1.fsm.state.JobSummaryByID(job1.ID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
expectedSummary1 := &structs.JobSummary{
|
|
|
|
JobID: job1.ID,
|
|
|
|
Summary: map[string]structs.TaskGroupSummary{
|
|
|
|
"web": structs.TaskGroupSummary{
|
|
|
|
Lost: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CreateIndex: jobResp1.JobModifyIndex,
|
|
|
|
ModifyIndex: summary1.ModifyIndex,
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(summary1, expectedSummary1) {
|
|
|
|
return false, fmt.Errorf("expected: %#v, actual: %#v", expectedSummary1, summary1)
|
2016-08-09 17:16:17 +00:00
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-07-06 21:38:57 +00:00
|
|
|
func TestClientEndpoint_GetNode(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the register request
|
2015-08-11 21:27:14 +00:00
|
|
|
node := mock.Node()
|
2015-07-07 16:51:42 +00:00
|
|
|
reg := &structs.NodeRegisterRequest{
|
2015-07-06 21:38:57 +00:00
|
|
|
Node: node,
|
2015-09-14 01:18:40 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
2015-07-06 21:38:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp structs.GenericResponse
|
2015-09-07 03:31:32 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.Register", reg, &resp); err != nil {
|
2015-07-06 21:38:57 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
node.CreateIndex = resp.Index
|
|
|
|
node.ModifyIndex = resp.Index
|
|
|
|
|
|
|
|
// Lookup the node
|
|
|
|
get := &structs.NodeSpecificRequest{
|
|
|
|
NodeID: node.ID,
|
2015-09-14 01:18:40 +00:00
|
|
|
QueryOptions: structs.QueryOptions{Region: "global"},
|
2015-07-06 21:38:57 +00:00
|
|
|
}
|
|
|
|
var resp2 structs.SingleNodeResponse
|
2015-09-07 03:31:32 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.GetNode", get, &resp2); err != nil {
|
2015-07-06 21:38:57 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp2.Index != resp.Index {
|
|
|
|
t.Fatalf("Bad index: %d %d", resp2.Index, resp.Index)
|
|
|
|
}
|
|
|
|
|
2016-01-30 01:46:44 +00:00
|
|
|
if resp2.Node.ComputedClass == "" {
|
2016-01-21 20:21:42 +00:00
|
|
|
t.Fatalf("bad ComputedClass: %#v", resp2.Node)
|
|
|
|
}
|
|
|
|
|
2016-07-25 21:11:32 +00:00
|
|
|
// Update the status updated at value
|
|
|
|
node.StatusUpdatedAt = resp2.Node.StatusUpdatedAt
|
2016-08-16 06:11:57 +00:00
|
|
|
node.SecretID = ""
|
2015-07-06 21:38:57 +00:00
|
|
|
if !reflect.DeepEqual(node, resp2.Node) {
|
2016-07-25 21:11:32 +00:00
|
|
|
t.Fatalf("bad: %#v \n %#v", node, resp2.Node)
|
2015-07-06 21:38:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup non-existing node
|
2016-01-14 20:57:43 +00:00
|
|
|
get.NodeID = "12345678-abcd-efab-cdef-123456789abc"
|
2015-09-07 03:31:32 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.GetNode", get, &resp2); err != nil {
|
2015-07-06 21:38:57 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp2.Index != resp.Index {
|
|
|
|
t.Fatalf("Bad index: %d %d", resp2.Index, resp.Index)
|
|
|
|
}
|
|
|
|
if resp2.Node != nil {
|
|
|
|
t.Fatalf("unexpected node")
|
|
|
|
}
|
|
|
|
}
|
2015-08-06 23:39:20 +00:00
|
|
|
|
2015-10-30 02:00:02 +00:00
|
|
|
func TestClientEndpoint_GetNode_Blocking(t *testing.T) {
|
2015-10-29 22:48:44 +00:00
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
state := s1.fsm.State()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the node
|
|
|
|
node1 := mock.Node()
|
|
|
|
node2 := mock.Node()
|
|
|
|
|
|
|
|
// First create an unrelated node.
|
|
|
|
time.AfterFunc(100*time.Millisecond, func() {
|
2015-10-30 02:00:02 +00:00
|
|
|
if err := state.UpsertNode(100, node1); err != nil {
|
2015-10-29 22:48:44 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Upsert the node we are watching later
|
|
|
|
time.AfterFunc(200*time.Millisecond, func() {
|
2015-10-30 02:00:02 +00:00
|
|
|
if err := state.UpsertNode(200, node2); err != nil {
|
2015-10-29 22:48:44 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Lookup the node
|
2015-10-30 02:00:02 +00:00
|
|
|
req := &structs.NodeSpecificRequest{
|
2015-10-29 22:48:44 +00:00
|
|
|
NodeID: node2.ID,
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
2015-10-30 02:00:02 +00:00
|
|
|
MinQueryIndex: 50,
|
2015-10-29 22:48:44 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
var resp structs.SingleNodeResponse
|
|
|
|
start := time.Now()
|
2015-10-30 02:00:02 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.GetNode", req, &resp); err != nil {
|
2015-10-29 22:48:44 +00:00
|
|
|
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 22:48:44 +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 22:48:44 +00:00
|
|
|
}
|
|
|
|
if resp.Node == nil || resp.Node.ID != node2.ID {
|
|
|
|
t.Fatalf("bad: %#v", resp.Node)
|
|
|
|
}
|
2015-10-30 02:00:02 +00:00
|
|
|
|
|
|
|
// Node update triggers watches
|
|
|
|
time.AfterFunc(100*time.Millisecond, func() {
|
|
|
|
nodeUpdate := mock.Node()
|
|
|
|
nodeUpdate.ID = node2.ID
|
|
|
|
nodeUpdate.Status = structs.NodeStatusDown
|
|
|
|
if err := state.UpsertNode(300, nodeUpdate); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
req.QueryOptions.MinQueryIndex = 250
|
|
|
|
var resp2 structs.SingleNodeResponse
|
|
|
|
start = time.Now()
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.GetNode", 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-30 02:00:02 +00:00
|
|
|
t.Fatalf("should block (returned in %s) %#v", elapsed, resp)
|
|
|
|
}
|
|
|
|
if resp2.Index != 300 {
|
|
|
|
t.Fatalf("Bad index: %d %d", resp2.Index, 300)
|
|
|
|
}
|
|
|
|
if resp2.Node == nil || resp2.Node.Status != structs.NodeStatusDown {
|
|
|
|
t.Fatalf("bad: %#v", resp2.Node)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Node delete triggers watches
|
|
|
|
time.AfterFunc(100*time.Millisecond, func() {
|
|
|
|
if err := state.DeleteNode(400, node2.ID); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
req.QueryOptions.MinQueryIndex = 350
|
|
|
|
var resp3 structs.SingleNodeResponse
|
|
|
|
start = time.Now()
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.GetNode", req, &resp3); 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-30 02:00:02 +00:00
|
|
|
t.Fatalf("should block (returned in %s) %#v", elapsed, resp)
|
|
|
|
}
|
|
|
|
if resp3.Index != 400 {
|
|
|
|
t.Fatalf("Bad index: %d %d", resp2.Index, 400)
|
|
|
|
}
|
|
|
|
if resp3.Node != nil {
|
|
|
|
t.Fatalf("bad: %#v", resp3.Node)
|
|
|
|
}
|
2015-10-29 22:48:44 +00:00
|
|
|
}
|
|
|
|
|
2015-08-23 02:17:49 +00:00
|
|
|
func TestClientEndpoint_GetAllocs(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the register request
|
|
|
|
node := mock.Node()
|
|
|
|
reg := &structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
2015-09-14 01:18:40 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
2015-08-23 02:17:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp structs.GenericResponse
|
2015-09-07 03:31:32 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.Register", reg, &resp); err != nil {
|
2015-08-23 02:17:49 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
node.CreateIndex = resp.Index
|
|
|
|
node.ModifyIndex = resp.Index
|
|
|
|
|
|
|
|
// Inject fake evaluations
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.NodeID = node.ID
|
|
|
|
state := s1.fsm.State()
|
2016-07-25 21:11:32 +00:00
|
|
|
state.UpsertJobSummary(99, mock.JobSummary(alloc.JobID))
|
2015-09-07 03:47:42 +00:00
|
|
|
err := state.UpsertAllocs(100, []*structs.Allocation{alloc})
|
2015-08-23 02:17:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup the allocs
|
|
|
|
get := &structs.NodeSpecificRequest{
|
|
|
|
NodeID: node.ID,
|
2015-09-14 01:18:40 +00:00
|
|
|
QueryOptions: structs.QueryOptions{Region: "global"},
|
2015-08-23 02:17:49 +00:00
|
|
|
}
|
|
|
|
var resp2 structs.NodeAllocsResponse
|
2015-09-07 03:31:32 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.GetAllocs", get, &resp2); err != nil {
|
2015-08-23 02:17:49 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp2.Index != 100 {
|
|
|
|
t.Fatalf("Bad index: %d %d", resp2.Index, 100)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp2.Allocs) != 1 || resp2.Allocs[0].ID != alloc.ID {
|
|
|
|
t.Fatalf("bad: %#v", resp2.Allocs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup non-existing node
|
|
|
|
get.NodeID = "foobarbaz"
|
2015-09-07 03:31:32 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.GetAllocs", get, &resp2); err != nil {
|
2015-08-23 02:17:49 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp2.Index != 100 {
|
|
|
|
t.Fatalf("Bad index: %d %d", resp2.Index, 100)
|
|
|
|
}
|
|
|
|
if len(resp2.Allocs) != 0 {
|
|
|
|
t.Fatalf("unexpected node")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-29 14:29:52 +00:00
|
|
|
func TestClientEndpoint_GetClientAllocs(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the register request
|
|
|
|
node := mock.Node()
|
|
|
|
reg := &structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp structs.GenericResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.Register", reg, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
node.CreateIndex = resp.Index
|
|
|
|
node.ModifyIndex = resp.Index
|
|
|
|
|
|
|
|
// Inject fake evaluations
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.NodeID = node.ID
|
|
|
|
state := s1.fsm.State()
|
2016-07-25 21:11:32 +00:00
|
|
|
state.UpsertJobSummary(99, mock.JobSummary(alloc.JobID))
|
2016-01-29 14:29:52 +00:00
|
|
|
err := state.UpsertAllocs(100, []*structs.Allocation{alloc})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup the allocs
|
|
|
|
get := &structs.NodeSpecificRequest{
|
|
|
|
NodeID: node.ID,
|
2016-08-16 06:11:57 +00:00
|
|
|
SecretID: node.SecretID,
|
2016-01-29 14:29:52 +00:00
|
|
|
QueryOptions: structs.QueryOptions{Region: "global"},
|
|
|
|
}
|
|
|
|
var resp2 structs.NodeClientAllocsResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.GetClientAllocs", get, &resp2); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp2.Index != 100 {
|
|
|
|
t.Fatalf("Bad index: %d %d", resp2.Index, 100)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp2.Allocs) != 1 || resp2.Allocs[alloc.ID] != 100 {
|
|
|
|
t.Fatalf("bad: %#v", resp2.Allocs)
|
|
|
|
}
|
|
|
|
|
2016-08-16 06:11:57 +00:00
|
|
|
// Lookup node with bad SecretID
|
|
|
|
get.SecretID = "foobarbaz"
|
2016-01-29 14:29:52 +00:00
|
|
|
var resp3 structs.NodeClientAllocsResponse
|
2016-08-16 06:11:57 +00:00
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Node.GetClientAllocs", get, &resp3)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "does not match") {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup non-existing node
|
|
|
|
get.NodeID = structs.GenerateUUID()
|
|
|
|
var resp4 structs.NodeClientAllocsResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.GetClientAllocs", get, &resp4); err != nil {
|
2016-01-29 14:29:52 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-08-16 06:11:57 +00:00
|
|
|
if resp4.Index != 100 {
|
2016-01-29 14:29:52 +00:00
|
|
|
t.Fatalf("Bad index: %d %d", resp3.Index, 100)
|
|
|
|
}
|
2016-08-16 06:11:57 +00:00
|
|
|
if len(resp4.Allocs) != 0 {
|
2016-01-29 14:29:52 +00:00
|
|
|
t.Fatalf("unexpected node %#v", resp3.Allocs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientEndpoint_GetClientAllocs_Blocking(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the register request
|
|
|
|
node := mock.Node()
|
|
|
|
reg := &structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp structs.GenericResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.Register", reg, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
node.CreateIndex = resp.Index
|
|
|
|
node.ModifyIndex = resp.Index
|
|
|
|
|
|
|
|
// Inject fake evaluations async
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.NodeID = node.ID
|
|
|
|
state := s1.fsm.State()
|
2016-07-25 21:11:32 +00:00
|
|
|
state.UpsertJobSummary(99, mock.JobSummary(alloc.JobID))
|
2016-01-29 14:29:52 +00:00
|
|
|
start := time.Now()
|
|
|
|
time.AfterFunc(100*time.Millisecond, func() {
|
|
|
|
err := state.UpsertAllocs(100, []*structs.Allocation{alloc})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Lookup the allocs in a blocking query
|
|
|
|
req := &structs.NodeSpecificRequest{
|
2016-08-16 06:11:57 +00:00
|
|
|
NodeID: node.ID,
|
|
|
|
SecretID: node.SecretID,
|
2016-01-29 14:29:52 +00:00
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
MinQueryIndex: 50,
|
|
|
|
MaxQueryTime: time.Second,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var resp2 structs.NodeClientAllocsResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.GetClientAllocs", req, &resp2); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should block at least 100ms
|
|
|
|
if time.Since(start) < 100*time.Millisecond {
|
|
|
|
t.Fatalf("too fast")
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp2.Index != 100 {
|
|
|
|
t.Fatalf("Bad index: %d %d", resp2.Index, 100)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp2.Allocs) != 1 || resp2.Allocs[alloc.ID] != 100 {
|
|
|
|
t.Fatalf("bad: %#v", resp2.Allocs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alloc updates fire watches
|
|
|
|
time.AfterFunc(100*time.Millisecond, func() {
|
|
|
|
allocUpdate := mock.Alloc()
|
|
|
|
allocUpdate.NodeID = alloc.NodeID
|
|
|
|
allocUpdate.ID = alloc.ID
|
|
|
|
allocUpdate.ClientStatus = structs.AllocClientStatusRunning
|
2016-07-25 21:11:32 +00:00
|
|
|
state.UpsertJobSummary(199, mock.JobSummary(allocUpdate.JobID))
|
2016-02-01 21:57:35 +00:00
|
|
|
err := state.UpsertAllocs(200, []*structs.Allocation{allocUpdate})
|
2016-01-29 14:29:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
req.QueryOptions.MinQueryIndex = 150
|
|
|
|
var resp3 structs.NodeClientAllocsResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.GetClientAllocs", req, &resp3); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if time.Since(start) < 100*time.Millisecond {
|
|
|
|
t.Fatalf("too fast")
|
|
|
|
}
|
|
|
|
if resp3.Index != 200 {
|
|
|
|
t.Fatalf("Bad index: %d %d", resp3.Index, 200)
|
|
|
|
}
|
|
|
|
if len(resp3.Allocs) != 1 || resp3.Allocs[alloc.ID] != 200 {
|
|
|
|
t.Fatalf("bad: %#v", resp3.Allocs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-23 02:17:49 +00:00
|
|
|
func TestClientEndpoint_GetAllocs_Blocking(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the register request
|
|
|
|
node := mock.Node()
|
|
|
|
reg := &structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
2015-09-14 01:18:40 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
2015-08-23 02:17:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp structs.GenericResponse
|
2015-09-07 03:31:32 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.Register", reg, &resp); err != nil {
|
2015-08-23 02:17:49 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
node.CreateIndex = resp.Index
|
|
|
|
node.ModifyIndex = resp.Index
|
|
|
|
|
|
|
|
// Inject fake evaluations async
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.NodeID = node.ID
|
|
|
|
state := s1.fsm.State()
|
2016-07-25 21:11:32 +00:00
|
|
|
state.UpsertJobSummary(99, mock.JobSummary(alloc.JobID))
|
2015-08-23 02:17:49 +00:00
|
|
|
start := time.Now()
|
2015-10-30 02:00:02 +00:00
|
|
|
time.AfterFunc(100*time.Millisecond, func() {
|
2015-09-07 03:47:42 +00:00
|
|
|
err := state.UpsertAllocs(100, []*structs.Allocation{alloc})
|
2015-08-23 02:17:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-10-30 02:00:02 +00:00
|
|
|
})
|
2015-08-23 02:17:49 +00:00
|
|
|
|
|
|
|
// Lookup the allocs in a blocking query
|
2015-10-30 02:00:02 +00:00
|
|
|
req := &structs.NodeSpecificRequest{
|
2015-08-23 02:17:49 +00:00
|
|
|
NodeID: node.ID,
|
|
|
|
QueryOptions: structs.QueryOptions{
|
2015-09-14 01:18:40 +00:00
|
|
|
Region: "global",
|
2015-08-23 02:17:49 +00:00
|
|
|
MinQueryIndex: 50,
|
|
|
|
MaxQueryTime: time.Second,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var resp2 structs.NodeAllocsResponse
|
2015-10-30 02:00:02 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.GetAllocs", req, &resp2); err != nil {
|
2015-08-23 02:17:49 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should block at least 100ms
|
|
|
|
if time.Since(start) < 100*time.Millisecond {
|
|
|
|
t.Fatalf("too fast")
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp2.Index != 100 {
|
|
|
|
t.Fatalf("Bad index: %d %d", resp2.Index, 100)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp2.Allocs) != 1 || resp2.Allocs[0].ID != alloc.ID {
|
|
|
|
t.Fatalf("bad: %#v", resp2.Allocs)
|
|
|
|
}
|
2015-10-30 02:00:02 +00:00
|
|
|
|
|
|
|
// Alloc updates fire watches
|
|
|
|
time.AfterFunc(100*time.Millisecond, func() {
|
|
|
|
allocUpdate := mock.Alloc()
|
|
|
|
allocUpdate.NodeID = alloc.NodeID
|
|
|
|
allocUpdate.ID = alloc.ID
|
|
|
|
allocUpdate.ClientStatus = structs.AllocClientStatusRunning
|
2016-07-25 21:11:32 +00:00
|
|
|
state.UpsertJobSummary(199, mock.JobSummary(allocUpdate.JobID))
|
2016-02-22 01:59:12 +00:00
|
|
|
err := state.UpdateAllocsFromClient(200, []*structs.Allocation{allocUpdate})
|
2015-10-30 02:00:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
req.QueryOptions.MinQueryIndex = 150
|
|
|
|
var resp3 structs.NodeAllocsResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.GetAllocs", req, &resp3); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if time.Since(start) < 100*time.Millisecond {
|
|
|
|
t.Fatalf("too fast")
|
|
|
|
}
|
|
|
|
if resp3.Index != 200 {
|
|
|
|
t.Fatalf("Bad index: %d %d", resp3.Index, 200)
|
|
|
|
}
|
|
|
|
if len(resp3.Allocs) != 1 || resp3.Allocs[0].ClientStatus != structs.AllocClientStatusRunning {
|
|
|
|
t.Fatalf("bad: %#v", resp3.Allocs[0])
|
|
|
|
}
|
2015-08-23 02:17:49 +00:00
|
|
|
}
|
|
|
|
|
2015-08-26 01:12:51 +00:00
|
|
|
func TestClientEndpoint_UpdateAlloc(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the register request
|
|
|
|
node := mock.Node()
|
|
|
|
reg := &structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
2015-09-14 01:18:40 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
2015-08-26 01:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp structs.GenericResponse
|
2015-09-07 03:31:32 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.Register", reg, &resp); err != nil {
|
2015-08-26 01:12:51 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inject fake evaluations
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.NodeID = node.ID
|
|
|
|
state := s1.fsm.State()
|
2016-07-25 21:11:32 +00:00
|
|
|
state.UpsertJobSummary(99, mock.JobSummary(alloc.JobID))
|
2015-09-07 03:47:42 +00:00
|
|
|
err := state.UpsertAllocs(100, []*structs.Allocation{alloc})
|
2015-08-26 01:12:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt update
|
|
|
|
clientAlloc := new(structs.Allocation)
|
|
|
|
*clientAlloc = *alloc
|
|
|
|
clientAlloc.ClientStatus = structs.AllocClientStatusFailed
|
|
|
|
|
|
|
|
// Update the alloc
|
|
|
|
update := &structs.AllocUpdateRequest{
|
|
|
|
Alloc: []*structs.Allocation{clientAlloc},
|
2015-09-14 01:18:40 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
2015-08-26 01:12:51 +00:00
|
|
|
}
|
|
|
|
var resp2 structs.NodeAllocsResponse
|
2016-02-22 02:51:34 +00:00
|
|
|
start := time.Now()
|
2015-09-07 03:31:32 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.UpdateAlloc", update, &resp2); err != nil {
|
2015-08-26 01:12:51 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp2.Index == 0 {
|
|
|
|
t.Fatalf("Bad index: %d", resp2.Index)
|
|
|
|
}
|
2016-02-22 02:51:34 +00:00
|
|
|
if diff := time.Since(start); diff < batchUpdateInterval {
|
|
|
|
t.Fatalf("too fast: %v", diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup the alloc
|
|
|
|
out, err := state.AllocByID(alloc.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out.ClientStatus != structs.AllocClientStatusFailed {
|
|
|
|
t.Fatalf("Bad: %#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientEndpoint_BatchUpdate(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the register request
|
|
|
|
node := mock.Node()
|
|
|
|
reg := &structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp structs.GenericResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.Register", reg, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inject fake evaluations
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.NodeID = node.ID
|
|
|
|
state := s1.fsm.State()
|
2016-07-25 21:11:32 +00:00
|
|
|
state.UpsertJobSummary(99, mock.JobSummary(alloc.JobID))
|
2016-02-22 02:51:34 +00:00
|
|
|
err := state.UpsertAllocs(100, []*structs.Allocation{alloc})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt update
|
|
|
|
clientAlloc := new(structs.Allocation)
|
|
|
|
*clientAlloc = *alloc
|
|
|
|
clientAlloc.ClientStatus = structs.AllocClientStatusFailed
|
|
|
|
|
|
|
|
// Call to do the batch update
|
|
|
|
bf := NewBatchFuture()
|
|
|
|
endpoint := s1.endpoints.Node
|
|
|
|
endpoint.batchUpdate(bf, []*structs.Allocation{clientAlloc})
|
|
|
|
if err := bf.Wait(); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if bf.Index() == 0 {
|
|
|
|
t.Fatalf("Bad index: %d", bf.Index())
|
|
|
|
}
|
2015-08-26 01:12:51 +00:00
|
|
|
|
|
|
|
// Lookup the alloc
|
2015-09-07 03:56:38 +00:00
|
|
|
out, err := state.AllocByID(alloc.ID)
|
2015-08-26 01:12:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out.ClientStatus != structs.AllocClientStatusFailed {
|
|
|
|
t.Fatalf("Bad: %#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-22 20:57:27 +00:00
|
|
|
func TestClientEndpoint_UpdateAlloc_Vault(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the register request
|
|
|
|
node := mock.Node()
|
|
|
|
reg := &structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp structs.GenericResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.Register", reg, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap the servers Vault Client
|
|
|
|
tvc := &TestVaultClient{}
|
|
|
|
s1.vault = tvc
|
|
|
|
|
|
|
|
// Inject fake allocation and vault accessor
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.NodeID = node.ID
|
|
|
|
state := s1.fsm.State()
|
|
|
|
state.UpsertJobSummary(99, mock.JobSummary(alloc.JobID))
|
|
|
|
if err := state.UpsertAllocs(100, []*structs.Allocation{alloc}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
va := mock.VaultAccessor()
|
|
|
|
va.NodeID = node.ID
|
|
|
|
va.AllocID = alloc.ID
|
|
|
|
if err := state.UpsertVaultAccessor(101, []*structs.VaultAccessor{va}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt update
|
|
|
|
clientAlloc := new(structs.Allocation)
|
|
|
|
*clientAlloc = *alloc
|
|
|
|
clientAlloc.ClientStatus = structs.AllocClientStatusFailed
|
|
|
|
|
|
|
|
// Update the alloc
|
|
|
|
update := &structs.AllocUpdateRequest{
|
|
|
|
Alloc: []*structs.Allocation{clientAlloc},
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
var resp2 structs.NodeAllocsResponse
|
|
|
|
start := time.Now()
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.UpdateAlloc", update, &resp2); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp2.Index == 0 {
|
|
|
|
t.Fatalf("Bad index: %d", resp2.Index)
|
|
|
|
}
|
|
|
|
if diff := time.Since(start); diff < batchUpdateInterval {
|
|
|
|
t.Fatalf("too fast: %v", diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup the alloc
|
|
|
|
out, err := state.AllocByID(alloc.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out.ClientStatus != structs.AllocClientStatusFailed {
|
|
|
|
t.Fatalf("Bad: %#v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
if l := len(tvc.RevokedTokens); l != 1 {
|
|
|
|
t.Fatalf("Deregister revoked %d tokens; want 1", l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-06 23:39:20 +00:00
|
|
|
func TestClientEndpoint_CreateNodeEvals(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Inject fake evaluations
|
2015-08-11 21:27:14 +00:00
|
|
|
alloc := mock.Alloc()
|
2015-08-06 23:39:20 +00:00
|
|
|
state := s1.fsm.State()
|
2016-07-25 21:11:32 +00:00
|
|
|
state.UpsertJobSummary(1, mock.JobSummary(alloc.JobID))
|
|
|
|
if err := state.UpsertAllocs(2, []*structs.Allocation{alloc}); err != nil {
|
2015-10-20 17:57:53 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inject a fake system job.
|
|
|
|
job := mock.SystemJob()
|
2016-07-25 21:11:32 +00:00
|
|
|
if err := state.UpsertJob(3, job); err != nil {
|
2015-08-06 23:39:20 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create some evaluations
|
2015-09-07 03:31:32 +00:00
|
|
|
ids, index, err := s1.endpoints.Node.createNodeEvals(alloc.NodeID, 1)
|
2015-08-06 23:39:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if index == 0 {
|
|
|
|
t.Fatalf("bad: %d", index)
|
|
|
|
}
|
2015-10-20 17:57:53 +00:00
|
|
|
if len(ids) != 2 {
|
2015-08-06 23:39:20 +00:00
|
|
|
t.Fatalf("bad: %s", ids)
|
|
|
|
}
|
|
|
|
|
2015-10-20 17:57:53 +00:00
|
|
|
// Lookup the evaluations
|
|
|
|
evalByType := make(map[string]*structs.Evaluation, 2)
|
|
|
|
for _, id := range ids {
|
|
|
|
eval, err := state.EvalByID(id)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if eval == nil {
|
|
|
|
t.Fatalf("expected eval")
|
|
|
|
}
|
2015-08-06 23:39:20 +00:00
|
|
|
|
2015-10-20 17:57:53 +00:00
|
|
|
if old, ok := evalByType[eval.Type]; ok {
|
|
|
|
t.Fatalf("multiple evals of the same type: %v and %v", old, eval)
|
|
|
|
}
|
|
|
|
|
|
|
|
evalByType[eval.Type] = eval
|
2015-08-06 23:39:20 +00:00
|
|
|
}
|
2015-10-20 17:57:53 +00:00
|
|
|
|
|
|
|
if len(evalByType) != 2 {
|
|
|
|
t.Fatalf("Expected a service and system job; got %#v", evalByType)
|
2015-08-06 23:39:20 +00:00
|
|
|
}
|
2015-10-20 17:57:53 +00:00
|
|
|
|
|
|
|
// Ensure the evals are correct.
|
|
|
|
for schedType, eval := range evalByType {
|
|
|
|
expPriority := alloc.Job.Priority
|
|
|
|
expJobID := alloc.JobID
|
2015-10-23 23:32:45 +00:00
|
|
|
if schedType == "system" {
|
2015-10-20 17:57:53 +00:00
|
|
|
expPriority = job.Priority
|
|
|
|
expJobID = job.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
if eval.CreateIndex != index {
|
|
|
|
t.Fatalf("CreateIndex mis-match on type %v: %#v", schedType, eval)
|
|
|
|
}
|
|
|
|
if eval.TriggeredBy != structs.EvalTriggerNodeUpdate {
|
|
|
|
t.Fatalf("TriggeredBy incorrect on type %v: %#v", schedType, eval)
|
|
|
|
}
|
|
|
|
if eval.NodeID != alloc.NodeID {
|
|
|
|
t.Fatalf("NodeID incorrect on type %v: %#v", schedType, eval)
|
|
|
|
}
|
|
|
|
if eval.NodeModifyIndex != 1 {
|
|
|
|
t.Fatalf("NodeModifyIndex incorrect on type %v: %#v", schedType, eval)
|
|
|
|
}
|
|
|
|
if eval.Status != structs.EvalStatusPending {
|
|
|
|
t.Fatalf("Status incorrect on type %v: %#v", schedType, eval)
|
|
|
|
}
|
|
|
|
if eval.Priority != expPriority {
|
|
|
|
t.Fatalf("Priority incorrect on type %v: %#v", schedType, eval)
|
|
|
|
}
|
|
|
|
if eval.JobID != expJobID {
|
|
|
|
t.Fatalf("JobID incorrect on type %v: %#v", schedType, eval)
|
|
|
|
}
|
2015-08-06 23:39:20 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-16 01:20:35 +00:00
|
|
|
|
|
|
|
func TestClientEndpoint_Evaluate(t *testing.T) {
|
2015-10-08 22:36:42 +00:00
|
|
|
s1 := testServer(t, func(c *Config) {
|
|
|
|
c.NumSchedulers = 0 // Prevent automatic dequeue
|
|
|
|
})
|
2015-08-16 01:20:35 +00:00
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Inject fake evaluations
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
node := mock.Node()
|
|
|
|
node.ID = alloc.NodeID
|
|
|
|
state := s1.fsm.State()
|
2015-09-07 03:47:42 +00:00
|
|
|
err := state.UpsertNode(1, node)
|
2015-08-16 01:20:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-07-25 21:11:32 +00:00
|
|
|
state.UpsertJobSummary(2, mock.JobSummary(alloc.JobID))
|
|
|
|
err = state.UpsertAllocs(3, []*structs.Allocation{alloc})
|
2015-08-16 01:20:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-evaluate
|
|
|
|
req := &structs.NodeEvaluateRequest{
|
|
|
|
NodeID: alloc.NodeID,
|
2015-09-14 01:18:40 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
2015-08-16 01:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp structs.NodeUpdateResponse
|
2015-09-07 03:31:32 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.Evaluate", req, &resp); err != nil {
|
2015-08-16 01:20:35 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Index == 0 {
|
|
|
|
t.Fatalf("bad index: %d", resp.Index)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create some evaluations
|
|
|
|
ids := resp.EvalIDs
|
|
|
|
if len(ids) != 1 {
|
|
|
|
t.Fatalf("bad: %s", ids)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup the evaluation
|
2015-09-07 03:56:38 +00:00
|
|
|
eval, err := state.EvalByID(ids[0])
|
2015-08-16 01:20:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if eval == nil {
|
|
|
|
t.Fatalf("expected eval")
|
|
|
|
}
|
|
|
|
if eval.CreateIndex != resp.Index {
|
|
|
|
t.Fatalf("index mis-match")
|
|
|
|
}
|
|
|
|
|
|
|
|
if eval.Priority != alloc.Job.Priority {
|
|
|
|
t.Fatalf("bad: %#v", eval)
|
|
|
|
}
|
|
|
|
if eval.Type != alloc.Job.Type {
|
|
|
|
t.Fatalf("bad: %#v", eval)
|
|
|
|
}
|
|
|
|
if eval.TriggeredBy != structs.EvalTriggerNodeUpdate {
|
|
|
|
t.Fatalf("bad: %#v", eval)
|
|
|
|
}
|
|
|
|
if eval.JobID != alloc.JobID {
|
|
|
|
t.Fatalf("bad: %#v", eval)
|
|
|
|
}
|
|
|
|
if eval.NodeID != alloc.NodeID {
|
|
|
|
t.Fatalf("bad: %#v", eval)
|
|
|
|
}
|
|
|
|
if eval.NodeModifyIndex != 1 {
|
|
|
|
t.Fatalf("bad: %#v", eval)
|
|
|
|
}
|
|
|
|
if eval.Status != structs.EvalStatusPending {
|
|
|
|
t.Fatalf("bad: %#v", eval)
|
|
|
|
}
|
|
|
|
}
|
2015-09-06 21:28:29 +00:00
|
|
|
|
|
|
|
func TestClientEndpoint_ListNodes(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the register request
|
|
|
|
node := mock.Node()
|
|
|
|
reg := &structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
2015-09-14 01:18:40 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
2015-09-06 21:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp structs.GenericResponse
|
2015-09-07 03:31:32 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.Register", reg, &resp); err != nil {
|
2015-09-06 21:28:29 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
node.CreateIndex = resp.Index
|
|
|
|
node.ModifyIndex = resp.Index
|
|
|
|
|
|
|
|
// Lookup the node
|
|
|
|
get := &structs.NodeListRequest{
|
2015-09-14 01:18:40 +00:00
|
|
|
QueryOptions: structs.QueryOptions{Region: "global"},
|
2015-09-06 21:28:29 +00:00
|
|
|
}
|
|
|
|
var resp2 structs.NodeListResponse
|
2015-09-07 03:31:32 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.List", get, &resp2); err != nil {
|
2015-09-06 21:28:29 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp2.Index != resp.Index {
|
|
|
|
t.Fatalf("Bad index: %d %d", resp2.Index, resp.Index)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp2.Nodes) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", resp2.Nodes)
|
|
|
|
}
|
|
|
|
if resp2.Nodes[0].ID != node.ID {
|
|
|
|
t.Fatalf("bad: %#v", resp2.Nodes[0])
|
2015-12-24 10:46:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup the node with prefix
|
|
|
|
get = &structs.NodeListRequest{
|
|
|
|
QueryOptions: structs.QueryOptions{Region: "global", Prefix: node.ID[:4]},
|
|
|
|
}
|
|
|
|
var resp3 structs.NodeListResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.List", get, &resp3); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp3.Index != resp.Index {
|
|
|
|
t.Fatalf("Bad index: %d %d", resp3.Index, resp2.Index)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp3.Nodes) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", resp3.Nodes)
|
|
|
|
}
|
|
|
|
if resp3.Nodes[0].ID != node.ID {
|
|
|
|
t.Fatalf("bad: %#v", resp3.Nodes[0])
|
2015-09-06 21:28:29 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-28 18:21:39 +00:00
|
|
|
|
2015-10-30 02:00:02 +00:00
|
|
|
func TestClientEndpoint_ListNodes_Blocking(t *testing.T) {
|
2015-10-28 18:21:39 +00:00
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
2015-10-28 19:29:06 +00:00
|
|
|
state := s1.fsm.State()
|
2015-10-28 18:21:39 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the node
|
|
|
|
node := mock.Node()
|
|
|
|
|
2015-10-28 19:29:06 +00:00
|
|
|
// Node upsert triggers watches
|
|
|
|
time.AfterFunc(100*time.Millisecond, func() {
|
|
|
|
if err := state.UpsertNode(2, node); err != nil {
|
2015-10-28 18:21:39 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-10-28 19:29:06 +00:00
|
|
|
})
|
2015-10-28 18:21:39 +00:00
|
|
|
|
2015-10-28 19:29:06 +00:00
|
|
|
req := &structs.NodeListRequest{
|
2015-10-28 18:21:39 +00:00
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
MinQueryIndex: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
start := time.Now()
|
|
|
|
var resp structs.NodeListResponse
|
2015-10-28 19:29:06 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.List", req, &resp); err != nil {
|
2015-10-28 18:21:39 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-30 15:27:47 +00:00
|
|
|
if elapsed := time.Since(start); elapsed < 100*time.Millisecond {
|
2015-10-28 18:21: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)
|
|
|
|
}
|
2015-10-28 19:29:06 +00:00
|
|
|
if len(resp.Nodes) != 1 || resp.Nodes[0].ID != node.ID {
|
2015-10-28 18:21:39 +00:00
|
|
|
t.Fatalf("bad: %#v", resp.Nodes)
|
|
|
|
}
|
2015-10-28 19:29:06 +00:00
|
|
|
|
|
|
|
// Node drain updates trigger watches.
|
|
|
|
time.AfterFunc(100*time.Millisecond, func() {
|
|
|
|
if err := state.UpdateNodeDrain(3, node.ID, true); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
req.MinQueryIndex = 2
|
|
|
|
var resp2 structs.NodeListResponse
|
|
|
|
start = time.Now()
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.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 01:35:48 +00:00
|
|
|
t.Fatalf("should block (returned in %s) %#v", elapsed, resp2)
|
2015-10-28 19:29:06 +00:00
|
|
|
}
|
|
|
|
if resp2.Index != 3 {
|
|
|
|
t.Fatalf("Bad index: %d %d", resp2.Index, 3)
|
|
|
|
}
|
|
|
|
if len(resp2.Nodes) != 1 || !resp2.Nodes[0].Drain {
|
|
|
|
t.Fatalf("bad: %#v", resp2.Nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Node status update triggers watches
|
|
|
|
time.AfterFunc(100*time.Millisecond, func() {
|
|
|
|
if err := state.UpdateNodeStatus(4, node.ID, structs.NodeStatusDown); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
req.MinQueryIndex = 3
|
|
|
|
var resp3 structs.NodeListResponse
|
|
|
|
start = time.Now()
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.List", req, &resp3); 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 01:35:48 +00:00
|
|
|
t.Fatalf("should block (returned in %s) %#v", elapsed, resp3)
|
2015-10-28 19:29:06 +00:00
|
|
|
}
|
|
|
|
if resp3.Index != 4 {
|
|
|
|
t.Fatalf("Bad index: %d %d", resp3.Index, 4)
|
|
|
|
}
|
|
|
|
if len(resp3.Nodes) != 1 || resp3.Nodes[0].Status != structs.NodeStatusDown {
|
|
|
|
t.Fatalf("bad: %#v", resp3.Nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Node delete triggers watches.
|
|
|
|
time.AfterFunc(100*time.Millisecond, func() {
|
|
|
|
if err := state.DeleteNode(5, node.ID); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
req.MinQueryIndex = 4
|
|
|
|
var resp4 structs.NodeListResponse
|
|
|
|
start = time.Now()
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.List", req, &resp4); 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 01:35:48 +00:00
|
|
|
t.Fatalf("should block (returned in %s) %#v", elapsed, resp4)
|
2015-10-28 19:29:06 +00:00
|
|
|
}
|
|
|
|
if resp4.Index != 5 {
|
|
|
|
t.Fatalf("Bad index: %d %d", resp4.Index, 5)
|
|
|
|
}
|
|
|
|
if len(resp4.Nodes) != 0 {
|
|
|
|
t.Fatalf("bad: %#v", resp4.Nodes)
|
2015-10-28 18:21:39 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-22 02:51:34 +00:00
|
|
|
|
|
|
|
func TestBatchFuture(t *testing.T) {
|
|
|
|
bf := NewBatchFuture()
|
|
|
|
|
|
|
|
// Async respond to the future
|
|
|
|
expect := fmt.Errorf("testing")
|
|
|
|
go func() {
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
bf.Respond(1000, expect)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Block for the result
|
|
|
|
start := time.Now()
|
|
|
|
err := bf.Wait()
|
|
|
|
diff := time.Since(start)
|
|
|
|
if diff < 5*time.Millisecond {
|
|
|
|
t.Fatalf("too fast")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the results
|
|
|
|
if err != expect {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
if bf.Index() != 1000 {
|
|
|
|
t.Fatalf("bad: %d", bf.Index())
|
|
|
|
}
|
|
|
|
}
|
2016-08-19 20:13:51 +00:00
|
|
|
|
|
|
|
func TestClientEndpoint_DeriveVaultToken_Bad(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
state := s1.fsm.State()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Create the node
|
|
|
|
node := mock.Node()
|
|
|
|
if err := state.UpsertNode(2, node); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an alloc
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
task := alloc.Job.TaskGroups[0].Tasks[0]
|
|
|
|
tasks := []string{task.Name}
|
|
|
|
if err := state.UpsertAllocs(3, []*structs.Allocation{alloc}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &structs.DeriveVaultTokenRequest{
|
|
|
|
NodeID: node.ID,
|
|
|
|
SecretID: structs.GenerateUUID(),
|
|
|
|
AllocID: alloc.ID,
|
|
|
|
Tasks: tasks,
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp structs.DeriveVaultTokenResponse
|
2016-10-23 01:08:30 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.DeriveVaultToken", req, &resp); err != nil {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Error == nil || !strings.Contains(resp.Error.Error(), "SecretID mismatch") {
|
|
|
|
t.Fatalf("Expected SecretID mismatch: %v", resp.Error)
|
2016-08-19 20:13:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put the correct SecretID
|
|
|
|
req.SecretID = node.SecretID
|
|
|
|
|
|
|
|
// Now we should get an error about the allocation not running on the node
|
2016-10-23 01:08:30 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.DeriveVaultToken", req, &resp); err != nil {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Error == nil || !strings.Contains(resp.Error.Error(), "not running on Node") {
|
|
|
|
t.Fatalf("Expected not running on node error: %v", resp.Error)
|
2016-08-19 20:13:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update to be running on the node
|
|
|
|
alloc.NodeID = node.ID
|
|
|
|
if err := state.UpsertAllocs(4, []*structs.Allocation{alloc}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we should get an error about the job not needing any Vault secrets
|
2016-10-23 01:08:30 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.DeriveVaultToken", req, &resp); err != nil {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Error == nil || !strings.Contains(resp.Error.Error(), "does not require") {
|
|
|
|
t.Fatalf("Expected no policies error: %v", resp.Error)
|
2016-08-19 20:13:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update to be terminal
|
|
|
|
alloc.DesiredStatus = structs.AllocDesiredStatusStop
|
|
|
|
if err := state.UpsertAllocs(5, []*structs.Allocation{alloc}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we should get an error about the job not needing any Vault secrets
|
2016-10-23 01:08:30 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.DeriveVaultToken", req, &resp); err != nil {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Error == nil || !strings.Contains(resp.Error.Error(), "terminal") {
|
|
|
|
t.Fatalf("Expected terminal allocation error: %v", resp.Error)
|
2016-08-19 20:13:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientEndpoint_DeriveVaultToken(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
state := s1.fsm.State()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Enable vault and allow authenticated
|
2016-10-11 20:28:18 +00:00
|
|
|
tr := true
|
|
|
|
s1.config.VaultConfig.Enabled = &tr
|
|
|
|
s1.config.VaultConfig.AllowUnauthenticated = &tr
|
2016-08-19 20:13:51 +00:00
|
|
|
|
|
|
|
// Replace the Vault Client on the server
|
|
|
|
tvc := &TestVaultClient{}
|
|
|
|
s1.vault = tvc
|
|
|
|
|
|
|
|
// Create the node
|
|
|
|
node := mock.Node()
|
|
|
|
if err := state.UpsertNode(2, node); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an alloc an allocation that has vault policies required
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.NodeID = node.ID
|
|
|
|
task := alloc.Job.TaskGroups[0].Tasks[0]
|
|
|
|
tasks := []string{task.Name}
|
|
|
|
task.Vault = &structs.Vault{Policies: []string{"a", "b"}}
|
|
|
|
if err := state.UpsertAllocs(3, []*structs.Allocation{alloc}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a secret for the task
|
|
|
|
token := structs.GenerateUUID()
|
|
|
|
accessor := structs.GenerateUUID()
|
|
|
|
ttl := 10
|
|
|
|
secret := &vapi.Secret{
|
|
|
|
WrapInfo: &vapi.SecretWrapInfo{
|
|
|
|
Token: token,
|
|
|
|
WrappedAccessor: accessor,
|
|
|
|
TTL: ttl,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tvc.SetCreateTokenSecret(alloc.ID, task.Name, secret)
|
|
|
|
|
|
|
|
req := &structs.DeriveVaultTokenRequest{
|
|
|
|
NodeID: node.ID,
|
|
|
|
SecretID: node.SecretID,
|
|
|
|
AllocID: alloc.ID,
|
|
|
|
Tasks: tasks,
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp structs.DeriveVaultTokenResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.DeriveVaultToken", req, &resp); err != nil {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
2016-10-23 01:08:30 +00:00
|
|
|
if resp.Error != nil {
|
|
|
|
t.Fatalf("bad: %v", resp.Error)
|
|
|
|
}
|
2016-08-19 20:13:51 +00:00
|
|
|
|
|
|
|
// Check the state store and ensure that we created a VaultAccessor
|
|
|
|
va, err := state.VaultAccessor(accessor)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
if va == nil {
|
|
|
|
t.Fatalf("bad: %v", va)
|
|
|
|
}
|
|
|
|
|
|
|
|
if va.CreateIndex == 0 {
|
|
|
|
t.Fatalf("bad: %v", va)
|
|
|
|
}
|
|
|
|
|
|
|
|
va.CreateIndex = 0
|
|
|
|
expected := &structs.VaultAccessor{
|
|
|
|
AllocID: alloc.ID,
|
|
|
|
Task: task.Name,
|
|
|
|
NodeID: alloc.NodeID,
|
|
|
|
Accessor: accessor,
|
|
|
|
CreationTTL: ttl,
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expected, va) {
|
|
|
|
t.Fatalf("Got %#v; want %#v", va, expected)
|
|
|
|
}
|
|
|
|
}
|
2016-10-23 01:08:30 +00:00
|
|
|
|
|
|
|
func TestClientEndpoint_DeriveVaultToken_VaultError(t *testing.T) {
|
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
state := s1.fsm.State()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Enable vault and allow authenticated
|
|
|
|
tr := true
|
|
|
|
s1.config.VaultConfig.Enabled = &tr
|
|
|
|
s1.config.VaultConfig.AllowUnauthenticated = &tr
|
|
|
|
|
|
|
|
// Replace the Vault Client on the server
|
|
|
|
tvc := &TestVaultClient{}
|
|
|
|
s1.vault = tvc
|
|
|
|
|
|
|
|
// Create the node
|
|
|
|
node := mock.Node()
|
|
|
|
if err := state.UpsertNode(2, node); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an alloc an allocation that has vault policies required
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.NodeID = node.ID
|
|
|
|
task := alloc.Job.TaskGroups[0].Tasks[0]
|
|
|
|
tasks := []string{task.Name}
|
|
|
|
task.Vault = &structs.Vault{Policies: []string{"a", "b"}}
|
|
|
|
if err := state.UpsertAllocs(3, []*structs.Allocation{alloc}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return an error when creating the token
|
|
|
|
tvc.SetCreateTokenError(alloc.ID, task.Name,
|
|
|
|
structs.NewRecoverableError(fmt.Errorf("recover"), true))
|
|
|
|
|
|
|
|
req := &structs.DeriveVaultTokenRequest{
|
|
|
|
NodeID: node.ID,
|
|
|
|
SecretID: node.SecretID,
|
|
|
|
AllocID: alloc.ID,
|
|
|
|
Tasks: tasks,
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp structs.DeriveVaultTokenResponse
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Node.DeriveVaultToken", req, &resp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Error == nil || !resp.Error.Recoverable {
|
|
|
|
t.Fatalf("bad: %+v", resp.Error)
|
|
|
|
}
|
|
|
|
}
|