2015-07-27 22:31:49 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2022-06-23 16:06:27 +00:00
|
|
|
"sync"
|
2015-07-27 22:31:49 +00:00
|
|
|
"testing"
|
2015-08-12 22:44:36 +00:00
|
|
|
"time"
|
2015-07-27 22:31:49 +00:00
|
|
|
|
2019-01-15 19:46:12 +00:00
|
|
|
msgpackrpc "github.com/hashicorp/net-rpc-msgpackrpc"
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2015-08-11 21:27:14 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2015-07-27 22:31:49 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2022-06-23 16:06:27 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-01-27 01:14:08 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-07-27 22:31:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPlanEndpoint_Submit(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-12-04 00:15:11 +00:00
|
|
|
|
|
|
|
s1, cleanupS1 := TestServer(t, func(c *Config) {
|
2015-08-12 22:44:36 +00:00
|
|
|
c.NumSchedulers = 0
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS1()
|
2015-07-27 22:31:49 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
2015-08-12 22:44:36 +00:00
|
|
|
// Create the register request
|
|
|
|
eval1 := mock.Eval()
|
2016-05-18 18:35:15 +00:00
|
|
|
s1.evalBroker.Enqueue(eval1)
|
|
|
|
|
2015-08-12 22:44:36 +00:00
|
|
|
evalOut, token, err := s1.evalBroker.Dequeue([]string{eval1.Type}, time.Second)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if evalOut != eval1 {
|
|
|
|
t.Fatalf("Bad eval")
|
|
|
|
}
|
|
|
|
|
2015-07-27 22:31:49 +00:00
|
|
|
// Submit a plan
|
2015-08-11 21:27:14 +00:00
|
|
|
plan := mock.Plan()
|
2015-08-12 22:44:36 +00:00
|
|
|
plan.EvalID = eval1.ID
|
|
|
|
plan.EvalToken = token
|
2017-10-13 21:36:02 +00:00
|
|
|
plan.Job = mock.Job()
|
2015-07-27 22:31:49 +00:00
|
|
|
req := &structs.PlanRequest{
|
|
|
|
Plan: plan,
|
2015-09-14 01:18:40 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
2015-07-27 22:31:49 +00:00
|
|
|
}
|
|
|
|
var resp structs.PlanResponse
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Plan.Submit", req, &resp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Result == nil {
|
|
|
|
t.Fatalf("missing result")
|
|
|
|
}
|
|
|
|
}
|
2022-01-27 01:14:08 +00:00
|
|
|
|
|
|
|
// TestPlanEndpoint_Submit_Bad asserts that the Plan.Submit endpoint rejects
|
|
|
|
// bad data with an error instead of panicking.
|
|
|
|
func TestPlanEndpoint_Submit_Bad(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2022-01-27 01:14:08 +00:00
|
|
|
|
|
|
|
s1, cleanupS1 := TestServer(t, func(c *Config) {
|
|
|
|
c.NumSchedulers = 0
|
|
|
|
})
|
|
|
|
defer cleanupS1()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// Mock a valid eval being dequeued by a worker
|
|
|
|
eval := mock.Eval()
|
|
|
|
s1.evalBroker.Enqueue(eval)
|
|
|
|
|
|
|
|
evalOut, _, err := s1.evalBroker.Dequeue([]string{eval.Type}, time.Second)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, eval, evalOut)
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Plan *structs.Plan
|
|
|
|
Err string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "Nil",
|
|
|
|
Plan: nil,
|
|
|
|
Err: "cannot submit nil plan",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Empty",
|
|
|
|
Plan: &structs.Plan{},
|
|
|
|
Err: "evaluation is not outstanding",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "BadEvalID",
|
|
|
|
Plan: &structs.Plan{
|
|
|
|
EvalID: "1234", // does not exist
|
|
|
|
},
|
|
|
|
Err: "evaluation is not outstanding",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "MissingToken",
|
|
|
|
Plan: &structs.Plan{
|
|
|
|
EvalID: eval.ID,
|
|
|
|
},
|
|
|
|
Err: "evaluation token does not match",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "InvalidToken",
|
|
|
|
Plan: &structs.Plan{
|
|
|
|
EvalID: eval.ID,
|
|
|
|
EvalToken: "1234", // invalid
|
|
|
|
},
|
|
|
|
Err: "evaluation token does not match",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range cases {
|
|
|
|
tc := cases[i]
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
req := &structs.PlanRequest{
|
|
|
|
Plan: tc.Plan,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
var resp structs.PlanResponse
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Plan.Submit", req, &resp)
|
|
|
|
require.EqualError(t, err, tc.Err)
|
|
|
|
require.Nil(t, resp.Result)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure no plans were enqueued
|
|
|
|
require.Zero(t, s1.planner.planQueue.Stats().Depth)
|
|
|
|
}
|
2022-06-23 16:06:27 +00:00
|
|
|
|
|
|
|
func TestPlanEndpoint_ApplyConcurrent(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
s1, cleanupS1 := TestServer(t, func(c *Config) {
|
|
|
|
c.NumSchedulers = 0
|
|
|
|
})
|
|
|
|
defer cleanupS1()
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
plans := []*structs.Plan{}
|
|
|
|
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
|
|
|
|
// Create a node to place on
|
|
|
|
node := mock.Node()
|
|
|
|
store := s1.fsm.State()
|
|
|
|
require.NoError(t, store.UpsertNode(structs.MsgTypeTestSetup, 100, node))
|
|
|
|
|
|
|
|
// Create the eval
|
|
|
|
eval1 := mock.Eval()
|
|
|
|
s1.evalBroker.Enqueue(eval1)
|
|
|
|
require.NoError(t, store.UpsertEvals(
|
|
|
|
structs.MsgTypeTestSetup, 150, []*structs.Evaluation{eval1}))
|
|
|
|
|
|
|
|
evalOut, token, err := s1.evalBroker.Dequeue([]string{eval1.Type}, time.Second)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, eval1, evalOut)
|
|
|
|
|
|
|
|
// Submit a plan
|
|
|
|
plan := mock.Plan()
|
|
|
|
plan.EvalID = eval1.ID
|
|
|
|
plan.EvalToken = token
|
|
|
|
plan.Job = mock.Job()
|
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.JobID = plan.Job.ID
|
|
|
|
alloc.Job = plan.Job
|
|
|
|
|
|
|
|
plan.NodeAllocation = map[string][]*structs.Allocation{
|
|
|
|
node.ID: []*structs.Allocation{alloc}}
|
|
|
|
|
|
|
|
plans = append(plans, plan)
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
for _, plan := range plans {
|
|
|
|
plan := plan
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
|
|
|
|
req := &structs.PlanRequest{
|
|
|
|
Plan: plan,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
var resp structs.PlanResponse
|
|
|
|
err := s1.RPC("Plan.Submit", req, &resp)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, resp.Result, "missing result")
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|