2015-07-23 21:41:18 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/net-rpc-msgpackrpc"
|
2015-08-11 21:27:14 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2015-07-23 21:41:18 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestJobEndpoint_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
|
|
|
job := mock.Job()
|
2015-07-23 21:41:18 +00:00
|
|
|
req := &structs.JobRegisterRequest{
|
|
|
|
Job: job,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "region1"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
2015-08-06 18:48:44 +00:00
|
|
|
var resp structs.JobRegisterResponse
|
2015-07-23 21:41:18 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Job.Register", req, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Index == 0 {
|
|
|
|
t.Fatalf("bad index: %d", resp.Index)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the node in the FSM
|
|
|
|
state := s1.fsm.State()
|
2015-07-23 22:15:48 +00:00
|
|
|
out, err := state.GetJobByID(job.ID)
|
2015-07-23 21:41:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("expected job")
|
|
|
|
}
|
2015-08-06 18:48:44 +00:00
|
|
|
if out.CreateIndex != resp.JobModifyIndex {
|
2015-07-23 21:41:18 +00:00
|
|
|
t.Fatalf("index mis-match")
|
|
|
|
}
|
2015-08-06 18:48:44 +00:00
|
|
|
|
|
|
|
// Lookup the evaluation
|
|
|
|
eval, err := state.GetEvalByID(resp.EvalID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if eval == nil {
|
|
|
|
t.Fatalf("expected eval")
|
|
|
|
}
|
|
|
|
if eval.CreateIndex != resp.EvalCreateIndex {
|
|
|
|
t.Fatalf("index mis-match")
|
|
|
|
}
|
|
|
|
|
|
|
|
if eval.Priority != job.Priority {
|
|
|
|
t.Fatalf("bad: %#v", eval)
|
|
|
|
}
|
|
|
|
if eval.Type != job.Type {
|
|
|
|
t.Fatalf("bad: %#v", eval)
|
|
|
|
}
|
|
|
|
if eval.TriggeredBy != structs.EvalTriggerJobRegister {
|
|
|
|
t.Fatalf("bad: %#v", eval)
|
|
|
|
}
|
|
|
|
if eval.JobID != job.ID {
|
|
|
|
t.Fatalf("bad: %#v", eval)
|
|
|
|
}
|
|
|
|
if eval.JobModifyIndex != resp.JobModifyIndex {
|
|
|
|
t.Fatalf("bad: %#v", eval)
|
|
|
|
}
|
2015-08-06 23:39:20 +00:00
|
|
|
if eval.Status != structs.EvalStatusPending {
|
|
|
|
t.Fatalf("bad: %#v", eval)
|
|
|
|
}
|
2015-07-23 21:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestJobEndpoint_Register_Existing(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
|
|
|
job := mock.Job()
|
2015-07-23 21:41:18 +00:00
|
|
|
req := &structs.JobRegisterRequest{
|
|
|
|
Job: job,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "region1"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
2015-08-06 18:48:44 +00:00
|
|
|
var resp structs.JobRegisterResponse
|
2015-07-23 21:41:18 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Job.Register", req, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Index == 0 {
|
|
|
|
t.Fatalf("bad index: %d", resp.Index)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the job definition
|
2015-08-11 21:27:14 +00:00
|
|
|
job2 := mock.Job()
|
2015-07-23 21:41:18 +00:00
|
|
|
job2.Priority = 100
|
2015-07-23 22:15:48 +00:00
|
|
|
job2.ID = job.ID
|
2015-07-23 21:41:18 +00:00
|
|
|
req.Job = job2
|
|
|
|
|
|
|
|
// Attempt update
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Job.Register", req, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Index == 0 {
|
|
|
|
t.Fatalf("bad index: %d", resp.Index)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the node in the FSM
|
|
|
|
state := s1.fsm.State()
|
2015-07-23 22:15:48 +00:00
|
|
|
out, err := state.GetJobByID(job.ID)
|
2015-07-23 21:41:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("expected job")
|
|
|
|
}
|
2015-08-06 18:48:44 +00:00
|
|
|
if out.ModifyIndex != resp.JobModifyIndex {
|
2015-07-23 21:41:18 +00:00
|
|
|
t.Fatalf("index mis-match")
|
|
|
|
}
|
|
|
|
if out.Priority != 100 {
|
|
|
|
t.Fatalf("expected update")
|
|
|
|
}
|
2015-08-06 18:48:44 +00:00
|
|
|
|
|
|
|
// Lookup the evaluation
|
|
|
|
eval, err := state.GetEvalByID(resp.EvalID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if eval == nil {
|
|
|
|
t.Fatalf("expected eval")
|
|
|
|
}
|
|
|
|
if eval.CreateIndex != resp.EvalCreateIndex {
|
|
|
|
t.Fatalf("index mis-match")
|
|
|
|
}
|
|
|
|
|
|
|
|
if eval.Priority != job2.Priority {
|
|
|
|
t.Fatalf("bad: %#v", eval)
|
|
|
|
}
|
|
|
|
if eval.Type != job2.Type {
|
|
|
|
t.Fatalf("bad: %#v", eval)
|
|
|
|
}
|
|
|
|
if eval.TriggeredBy != structs.EvalTriggerJobRegister {
|
|
|
|
t.Fatalf("bad: %#v", eval)
|
|
|
|
}
|
|
|
|
if eval.JobID != job2.ID {
|
|
|
|
t.Fatalf("bad: %#v", eval)
|
|
|
|
}
|
|
|
|
if eval.JobModifyIndex != resp.JobModifyIndex {
|
|
|
|
t.Fatalf("bad: %#v", eval)
|
|
|
|
}
|
2015-08-06 23:39:20 +00:00
|
|
|
if eval.Status != structs.EvalStatusPending {
|
|
|
|
t.Fatalf("bad: %#v", eval)
|
|
|
|
}
|
2015-07-23 21:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestJobEndpoint_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
|
|
|
job := mock.Job()
|
2015-07-23 21:41:18 +00:00
|
|
|
reg := &structs.JobRegisterRequest{
|
|
|
|
Job: job,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "region1"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
2015-08-06 18:48:44 +00:00
|
|
|
var resp structs.JobRegisterResponse
|
2015-07-23 21:41:18 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Job.Register", reg, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deregister
|
|
|
|
dereg := &structs.JobDeregisterRequest{
|
2015-07-23 22:15:48 +00:00
|
|
|
JobID: job.ID,
|
2015-07-23 21:41:18 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Region: "region1"},
|
|
|
|
}
|
2015-08-06 21:17:18 +00:00
|
|
|
var resp2 structs.JobDeregisterResponse
|
2015-07-23 21:41:18 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Job.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
|
|
|
|
state := s1.fsm.State()
|
2015-07-23 22:15:48 +00:00
|
|
|
out, err := state.GetJobByID(job.ID)
|
2015-07-23 21:41:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatalf("unexpected job")
|
|
|
|
}
|
2015-08-06 21:17:18 +00:00
|
|
|
|
|
|
|
// Lookup the evaluation
|
|
|
|
eval, err := state.GetEvalByID(resp2.EvalID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if eval == nil {
|
|
|
|
t.Fatalf("expected eval")
|
|
|
|
}
|
|
|
|
if eval.CreateIndex != resp2.EvalCreateIndex {
|
|
|
|
t.Fatalf("index mis-match")
|
|
|
|
}
|
|
|
|
|
|
|
|
if eval.Priority != structs.JobDefaultPriority {
|
|
|
|
t.Fatalf("bad: %#v", eval)
|
|
|
|
}
|
|
|
|
if eval.Type != structs.JobTypeService {
|
|
|
|
t.Fatalf("bad: %#v", eval)
|
|
|
|
}
|
|
|
|
if eval.TriggeredBy != structs.EvalTriggerJobDeregister {
|
|
|
|
t.Fatalf("bad: %#v", eval)
|
|
|
|
}
|
|
|
|
if eval.JobID != job.ID {
|
|
|
|
t.Fatalf("bad: %#v", eval)
|
|
|
|
}
|
|
|
|
if eval.JobModifyIndex != resp2.JobModifyIndex {
|
|
|
|
t.Fatalf("bad: %#v", eval)
|
|
|
|
}
|
2015-08-06 23:39:20 +00:00
|
|
|
if eval.Status != structs.EvalStatusPending {
|
|
|
|
t.Fatalf("bad: %#v", eval)
|
|
|
|
}
|
2015-07-23 21:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestJobEndpoint_GetJob(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
|
|
|
job := mock.Job()
|
2015-07-23 21:41:18 +00:00
|
|
|
reg := &structs.JobRegisterRequest{
|
|
|
|
Job: job,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "region1"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
2015-08-06 18:48:44 +00:00
|
|
|
var resp structs.JobRegisterResponse
|
2015-07-23 21:41:18 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Job.Register", reg, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-08-06 18:48:44 +00:00
|
|
|
job.CreateIndex = resp.JobModifyIndex
|
|
|
|
job.ModifyIndex = resp.JobModifyIndex
|
2015-07-23 21:41:18 +00:00
|
|
|
|
|
|
|
// Lookup the job
|
|
|
|
get := &structs.JobSpecificRequest{
|
2015-07-23 22:15:48 +00:00
|
|
|
JobID: job.ID,
|
2015-07-23 21:41:18 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Region: "region1"},
|
|
|
|
}
|
|
|
|
var resp2 structs.SingleJobResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Job.GetJob", get, &resp2); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-08-06 18:48:44 +00:00
|
|
|
if resp2.Index != resp.JobModifyIndex {
|
2015-07-23 21:41:18 +00:00
|
|
|
t.Fatalf("Bad index: %d %d", resp2.Index, resp.Index)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(job, resp2.Job) {
|
|
|
|
t.Fatalf("bad: %#v %#v", job, resp2.Job)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup non-existing job
|
2015-07-23 22:15:48 +00:00
|
|
|
get.JobID = "foobarbaz"
|
2015-07-23 21:41:18 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Job.GetJob", get, &resp2); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-08-06 18:48:44 +00:00
|
|
|
if resp2.Index != resp.JobModifyIndex {
|
2015-07-23 21:41:18 +00:00
|
|
|
t.Fatalf("Bad index: %d %d", resp2.Index, resp.Index)
|
|
|
|
}
|
|
|
|
if resp2.Job != nil {
|
|
|
|
t.Fatalf("unexpected job")
|
|
|
|
}
|
|
|
|
}
|