2015-08-13 18:33:58 +00:00
|
|
|
package scheduler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-06-13 22:33:25 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2017-09-29 16:58:48 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2016-01-27 01:34:41 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2015-08-13 18:33:58 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/state"
|
2015-08-13 18:54:59 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2018-11-07 18:08:23 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-08-13 18:33:58 +00:00
|
|
|
)
|
|
|
|
|
2015-10-13 03:15:07 +00:00
|
|
|
func testContext(t testing.TB) (*state.StateStore, *EvalContext) {
|
2017-10-13 21:36:02 +00:00
|
|
|
state := state.TestStateStore(t)
|
2015-08-13 20:08:15 +00:00
|
|
|
plan := &structs.Plan{
|
2018-09-21 21:05:00 +00:00
|
|
|
NodeUpdate: make(map[string][]*structs.Allocation),
|
|
|
|
NodeAllocation: make(map[string][]*structs.Allocation),
|
|
|
|
NodePreemptions: make(map[string][]*structs.Allocation),
|
2015-08-13 20:08:15 +00:00
|
|
|
}
|
2015-08-13 18:33:58 +00:00
|
|
|
|
2018-09-15 23:23:13 +00:00
|
|
|
logger := testlog.HCLogger(t)
|
2015-08-13 18:54:59 +00:00
|
|
|
|
|
|
|
ctx := NewEvalContext(state, plan, logger)
|
2015-08-13 18:33:58 +00:00
|
|
|
return state, ctx
|
|
|
|
}
|
2015-08-16 17:28:58 +00:00
|
|
|
|
|
|
|
func TestEvalContext_ProposedAlloc(t *testing.T) {
|
|
|
|
state, ctx := testContext(t)
|
|
|
|
nodes := []*RankedNode{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2015-08-16 17:28:58 +00:00
|
|
|
Node: &structs.Node{
|
|
|
|
// Perfect fit
|
2017-09-29 16:58:48 +00:00
|
|
|
ID: uuid.Generate(),
|
2018-10-03 16:47:18 +00:00
|
|
|
NodeResources: &structs.NodeResources{
|
|
|
|
Cpu: structs.NodeCpuResources{
|
2018-10-04 21:33:09 +00:00
|
|
|
CpuShares: 2048,
|
2018-10-03 16:47:18 +00:00
|
|
|
},
|
|
|
|
Memory: structs.NodeMemoryResources{
|
|
|
|
MemoryMB: 2048,
|
|
|
|
},
|
2015-08-16 17:28:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2015-08-16 17:28:58 +00:00
|
|
|
Node: &structs.Node{
|
|
|
|
// Perfect fit
|
2017-09-29 16:58:48 +00:00
|
|
|
ID: uuid.Generate(),
|
2018-10-03 16:47:18 +00:00
|
|
|
NodeResources: &structs.NodeResources{
|
|
|
|
Cpu: structs.NodeCpuResources{
|
2018-10-04 21:33:09 +00:00
|
|
|
CpuShares: 2048,
|
2018-10-03 16:47:18 +00:00
|
|
|
},
|
|
|
|
Memory: structs.NodeMemoryResources{
|
|
|
|
MemoryMB: 2048,
|
|
|
|
},
|
2015-08-16 17:28:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add existing allocations
|
2017-05-01 20:54:26 +00:00
|
|
|
j1, j2 := mock.Job(), mock.Job()
|
2015-08-16 17:28:58 +00:00
|
|
|
alloc1 := &structs.Allocation{
|
2017-09-29 16:58:48 +00:00
|
|
|
ID: uuid.Generate(),
|
2017-09-07 23:56:15 +00:00
|
|
|
Namespace: structs.DefaultNamespace,
|
2017-09-29 16:58:48 +00:00
|
|
|
EvalID: uuid.Generate(),
|
2017-09-07 23:56:15 +00:00
|
|
|
NodeID: nodes[0].Node.ID,
|
|
|
|
JobID: j1.ID,
|
|
|
|
Job: j1,
|
2018-10-03 16:47:18 +00:00
|
|
|
AllocatedResources: &structs.AllocatedResources{
|
|
|
|
Tasks: map[string]*structs.AllocatedTaskResources{
|
|
|
|
"web": {
|
|
|
|
Cpu: structs.AllocatedCpuResources{
|
|
|
|
CpuShares: 2048,
|
|
|
|
},
|
|
|
|
Memory: structs.AllocatedMemoryResources{
|
|
|
|
MemoryMB: 2048,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-08-16 17:28:58 +00:00
|
|
|
},
|
2015-08-26 00:06:06 +00:00
|
|
|
DesiredStatus: structs.AllocDesiredStatusRun,
|
2015-12-16 23:01:15 +00:00
|
|
|
ClientStatus: structs.AllocClientStatusPending,
|
2016-07-22 21:53:49 +00:00
|
|
|
TaskGroup: "web",
|
2015-08-16 17:28:58 +00:00
|
|
|
}
|
|
|
|
alloc2 := &structs.Allocation{
|
2017-09-29 16:58:48 +00:00
|
|
|
ID: uuid.Generate(),
|
2017-09-07 23:56:15 +00:00
|
|
|
Namespace: structs.DefaultNamespace,
|
2017-09-29 16:58:48 +00:00
|
|
|
EvalID: uuid.Generate(),
|
2017-09-07 23:56:15 +00:00
|
|
|
NodeID: nodes[1].Node.ID,
|
|
|
|
JobID: j2.ID,
|
|
|
|
Job: j2,
|
2018-10-03 16:47:18 +00:00
|
|
|
AllocatedResources: &structs.AllocatedResources{
|
|
|
|
Tasks: map[string]*structs.AllocatedTaskResources{
|
|
|
|
"web": {
|
|
|
|
Cpu: structs.AllocatedCpuResources{
|
|
|
|
CpuShares: 1024,
|
|
|
|
},
|
|
|
|
Memory: structs.AllocatedMemoryResources{
|
|
|
|
MemoryMB: 1024,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-08-16 17:28:58 +00:00
|
|
|
},
|
2015-08-26 00:06:06 +00:00
|
|
|
DesiredStatus: structs.AllocDesiredStatusRun,
|
2015-12-16 23:01:15 +00:00
|
|
|
ClientStatus: structs.AllocClientStatusPending,
|
2016-07-22 21:53:49 +00:00
|
|
|
TaskGroup: "web",
|
2015-08-16 17:28:58 +00:00
|
|
|
}
|
2016-07-22 21:53:49 +00:00
|
|
|
noErr(t, state.UpsertJobSummary(998, mock.JobSummary(alloc1.JobID)))
|
|
|
|
noErr(t, state.UpsertJobSummary(999, mock.JobSummary(alloc2.JobID)))
|
2015-09-07 03:47:42 +00:00
|
|
|
noErr(t, state.UpsertAllocs(1000, []*structs.Allocation{alloc1, alloc2}))
|
2015-08-16 17:28:58 +00:00
|
|
|
|
|
|
|
// Add a planned eviction to alloc1
|
|
|
|
plan := ctx.Plan()
|
2015-08-26 00:06:06 +00:00
|
|
|
plan.NodeUpdate[nodes[0].Node.ID] = []*structs.Allocation{alloc1}
|
2015-08-16 17:28:58 +00:00
|
|
|
|
|
|
|
// Add a planned placement to node1
|
|
|
|
plan.NodeAllocation[nodes[1].Node.ID] = []*structs.Allocation{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2018-10-03 16:47:18 +00:00
|
|
|
AllocatedResources: &structs.AllocatedResources{
|
|
|
|
Tasks: map[string]*structs.AllocatedTaskResources{
|
|
|
|
"web": {
|
|
|
|
Cpu: structs.AllocatedCpuResources{
|
|
|
|
CpuShares: 1024,
|
|
|
|
},
|
|
|
|
Memory: structs.AllocatedMemoryResources{
|
|
|
|
MemoryMB: 1024,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-08-16 17:28:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
proposed, err := ctx.ProposedAllocs(nodes[0].Node.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(proposed) != 0 {
|
|
|
|
t.Fatalf("bad: %#v", proposed)
|
|
|
|
}
|
|
|
|
|
|
|
|
proposed, err = ctx.ProposedAllocs(nodes[1].Node.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(proposed) != 2 {
|
|
|
|
t.Fatalf("bad: %#v", proposed)
|
|
|
|
}
|
|
|
|
}
|
2016-01-27 01:34:41 +00:00
|
|
|
|
|
|
|
func TestEvalEligibility_JobStatus(t *testing.T) {
|
|
|
|
e := NewEvalEligibility()
|
2016-01-30 01:46:44 +00:00
|
|
|
cc := "v1:100"
|
2016-01-27 01:34:41 +00:00
|
|
|
|
|
|
|
// Get the job before its been set.
|
|
|
|
if status := e.JobStatus(cc); status != EvalComputedClassUnknown {
|
|
|
|
t.Fatalf("JobStatus() returned %v; want %v", status, EvalComputedClassUnknown)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the job and get its status.
|
|
|
|
e.SetJobEligibility(false, cc)
|
|
|
|
if status := e.JobStatus(cc); status != EvalComputedClassIneligible {
|
|
|
|
t.Fatalf("JobStatus() returned %v; want %v", status, EvalComputedClassIneligible)
|
|
|
|
}
|
|
|
|
|
|
|
|
e.SetJobEligibility(true, cc)
|
|
|
|
if status := e.JobStatus(cc); status != EvalComputedClassEligible {
|
|
|
|
t.Fatalf("JobStatus() returned %v; want %v", status, EvalComputedClassEligible)
|
|
|
|
}
|
|
|
|
|
2016-01-30 01:46:44 +00:00
|
|
|
// Check that if I pass an empty class it returns escaped
|
|
|
|
if status := e.JobStatus(""); status != EvalComputedClassEscaped {
|
2016-01-27 01:34:41 +00:00
|
|
|
t.Fatalf("JobStatus() returned %v; want %v", status, EvalComputedClassEscaped)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEvalEligibility_TaskGroupStatus(t *testing.T) {
|
|
|
|
e := NewEvalEligibility()
|
2016-01-30 01:46:44 +00:00
|
|
|
cc := "v1:100"
|
2016-01-27 01:34:41 +00:00
|
|
|
tg := "foo"
|
|
|
|
|
|
|
|
// Get the tg before its been set.
|
|
|
|
if status := e.TaskGroupStatus(tg, cc); status != EvalComputedClassUnknown {
|
|
|
|
t.Fatalf("TaskGroupStatus() returned %v; want %v", status, EvalComputedClassUnknown)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the tg and get its status.
|
|
|
|
e.SetTaskGroupEligibility(false, tg, cc)
|
|
|
|
if status := e.TaskGroupStatus(tg, cc); status != EvalComputedClassIneligible {
|
|
|
|
t.Fatalf("TaskGroupStatus() returned %v; want %v", status, EvalComputedClassIneligible)
|
|
|
|
}
|
|
|
|
|
|
|
|
e.SetTaskGroupEligibility(true, tg, cc)
|
|
|
|
if status := e.TaskGroupStatus(tg, cc); status != EvalComputedClassEligible {
|
|
|
|
t.Fatalf("TaskGroupStatus() returned %v; want %v", status, EvalComputedClassEligible)
|
|
|
|
}
|
|
|
|
|
2016-01-30 01:46:44 +00:00
|
|
|
// Check that if I pass an empty class it returns escaped
|
|
|
|
if status := e.TaskGroupStatus(tg, ""); status != EvalComputedClassEscaped {
|
2016-01-27 01:34:41 +00:00
|
|
|
t.Fatalf("TaskGroupStatus() returned %v; want %v", status, EvalComputedClassEscaped)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEvalEligibility_SetJob(t *testing.T) {
|
|
|
|
e := NewEvalEligibility()
|
|
|
|
ne1 := &structs.Constraint{
|
2016-02-05 00:50:20 +00:00
|
|
|
LTarget: "${attr.kernel.name}",
|
2016-01-27 01:34:41 +00:00
|
|
|
RTarget: "linux",
|
|
|
|
Operand: "=",
|
|
|
|
}
|
|
|
|
e1 := &structs.Constraint{
|
2016-02-05 00:50:20 +00:00
|
|
|
LTarget: "${attr.unique.kernel.name}",
|
2016-01-27 01:34:41 +00:00
|
|
|
RTarget: "linux",
|
|
|
|
Operand: "=",
|
|
|
|
}
|
|
|
|
e2 := &structs.Constraint{
|
2016-02-05 00:50:20 +00:00
|
|
|
LTarget: "${meta.unique.key_foo}",
|
2016-01-27 01:34:41 +00:00
|
|
|
RTarget: "linux",
|
|
|
|
Operand: "<",
|
|
|
|
}
|
|
|
|
e3 := &structs.Constraint{
|
2016-02-05 00:50:20 +00:00
|
|
|
LTarget: "${meta.unique.key_foo}",
|
2016-01-27 01:34:41 +00:00
|
|
|
RTarget: "Windows",
|
|
|
|
Operand: "<",
|
|
|
|
}
|
|
|
|
|
|
|
|
job := mock.Job()
|
|
|
|
jobCon := []*structs.Constraint{ne1, e1, e2}
|
|
|
|
job.Constraints = jobCon
|
|
|
|
|
|
|
|
// Set the task constraints
|
|
|
|
tg := job.TaskGroups[0]
|
|
|
|
tg.Constraints = []*structs.Constraint{e1}
|
|
|
|
tg.Tasks[0].Constraints = []*structs.Constraint{e3}
|
|
|
|
|
|
|
|
e.SetJob(job)
|
|
|
|
if !e.HasEscaped() {
|
|
|
|
t.Fatalf("HasEscaped() should be true")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !e.jobEscaped {
|
|
|
|
t.Fatalf("SetJob() should mark job as escaped")
|
|
|
|
}
|
|
|
|
if escaped, ok := e.tgEscapedConstraints[tg.Name]; !ok || !escaped {
|
|
|
|
t.Fatalf("SetJob() should mark task group as escaped")
|
|
|
|
}
|
|
|
|
}
|
2016-01-28 21:43:48 +00:00
|
|
|
|
|
|
|
func TestEvalEligibility_GetClasses(t *testing.T) {
|
|
|
|
e := NewEvalEligibility()
|
2016-01-30 01:46:44 +00:00
|
|
|
e.SetJobEligibility(true, "v1:1")
|
|
|
|
e.SetJobEligibility(false, "v1:2")
|
|
|
|
e.SetTaskGroupEligibility(true, "foo", "v1:3")
|
|
|
|
e.SetTaskGroupEligibility(false, "bar", "v1:4")
|
|
|
|
e.SetTaskGroupEligibility(true, "bar", "v1:5")
|
|
|
|
|
2016-02-01 04:56:52 +00:00
|
|
|
// Mark an existing eligible class as ineligible in the TG.
|
|
|
|
e.SetTaskGroupEligibility(false, "fizz", "v1:1")
|
|
|
|
e.SetTaskGroupEligibility(false, "fizz", "v1:3")
|
|
|
|
|
2016-01-30 01:46:44 +00:00
|
|
|
expClasses := map[string]bool{
|
2018-11-07 19:59:24 +00:00
|
|
|
"v1:1": false,
|
2016-01-30 01:46:44 +00:00
|
|
|
"v1:2": false,
|
|
|
|
"v1:3": true,
|
|
|
|
"v1:4": false,
|
|
|
|
"v1:5": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
actClasses := e.GetClasses()
|
2018-11-07 19:59:24 +00:00
|
|
|
require.Equal(t, expClasses, actClasses)
|
2016-01-28 21:43:48 +00:00
|
|
|
}
|
2018-11-07 18:08:23 +00:00
|
|
|
func TestEvalEligibility_GetClasses_JobEligible_TaskGroupIneligible(t *testing.T) {
|
|
|
|
e := NewEvalEligibility()
|
|
|
|
e.SetJobEligibility(true, "v1:1")
|
|
|
|
e.SetTaskGroupEligibility(false, "foo", "v1:1")
|
|
|
|
|
|
|
|
e.SetJobEligibility(true, "v1:2")
|
|
|
|
e.SetTaskGroupEligibility(false, "foo", "v1:2")
|
|
|
|
e.SetTaskGroupEligibility(true, "bar", "v1:2")
|
|
|
|
|
|
|
|
e.SetJobEligibility(true, "v1:3")
|
|
|
|
e.SetTaskGroupEligibility(false, "foo", "v1:3")
|
|
|
|
e.SetTaskGroupEligibility(false, "bar", "v1:3")
|
|
|
|
|
|
|
|
expClasses := map[string]bool{
|
|
|
|
"v1:1": false,
|
|
|
|
"v1:2": true,
|
|
|
|
"v1:3": false,
|
2016-01-28 21:43:48 +00:00
|
|
|
}
|
2018-11-07 18:08:23 +00:00
|
|
|
|
|
|
|
actClasses := e.GetClasses()
|
|
|
|
require.Equal(t, expClasses, actClasses)
|
2016-01-28 21:43:48 +00:00
|
|
|
}
|