2015-08-13 18:33:58 +00:00
|
|
|
package scheduler
|
|
|
|
|
|
|
|
import (
|
2015-08-13 18:54:59 +00:00
|
|
|
"log"
|
2015-08-13 18:33:58 +00:00
|
|
|
"os"
|
2016-01-28 21:43:48 +00:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
2015-08-13 18:33:58 +00:00
|
|
|
"testing"
|
|
|
|
|
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"
|
2015-08-13 18:33:58 +00:00
|
|
|
)
|
|
|
|
|
2015-10-13 03:15:07 +00:00
|
|
|
func testContext(t testing.TB) (*state.StateStore, *EvalContext) {
|
2015-08-13 18:33:58 +00:00
|
|
|
state, err := state.NewStateStore(os.Stderr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-08-13 20:08:15 +00:00
|
|
|
plan := &structs.Plan{
|
2015-08-26 00:06:06 +00:00
|
|
|
NodeUpdate: make(map[string][]*structs.Allocation),
|
2015-08-13 20:08:15 +00:00
|
|
|
NodeAllocation: make(map[string][]*structs.Allocation),
|
|
|
|
}
|
2015-08-13 18:33:58 +00:00
|
|
|
|
2015-08-13 18:54:59 +00:00
|
|
|
logger := log.New(os.Stderr, "", log.LstdFlags)
|
|
|
|
|
|
|
|
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{
|
|
|
|
&RankedNode{
|
|
|
|
Node: &structs.Node{
|
|
|
|
// Perfect fit
|
2015-09-07 22:23:03 +00:00
|
|
|
ID: structs.GenerateUUID(),
|
2015-08-16 17:28:58 +00:00
|
|
|
Resources: &structs.Resources{
|
|
|
|
CPU: 2048,
|
|
|
|
MemoryMB: 2048,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&RankedNode{
|
|
|
|
Node: &structs.Node{
|
|
|
|
// Perfect fit
|
2015-09-07 22:23:03 +00:00
|
|
|
ID: structs.GenerateUUID(),
|
2015-08-16 17:28:58 +00:00
|
|
|
Resources: &structs.Resources{
|
|
|
|
CPU: 2048,
|
|
|
|
MemoryMB: 2048,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add existing allocations
|
|
|
|
alloc1 := &structs.Allocation{
|
2015-09-07 22:23:03 +00:00
|
|
|
ID: structs.GenerateUUID(),
|
|
|
|
EvalID: structs.GenerateUUID(),
|
2015-08-16 17:28:58 +00:00
|
|
|
NodeID: nodes[0].Node.ID,
|
2015-09-07 22:23:03 +00:00
|
|
|
JobID: structs.GenerateUUID(),
|
2015-08-16 17:28:58 +00:00
|
|
|
Resources: &structs.Resources{
|
|
|
|
CPU: 2048,
|
|
|
|
MemoryMB: 2048,
|
|
|
|
},
|
2015-08-26 00:06:06 +00:00
|
|
|
DesiredStatus: structs.AllocDesiredStatusRun,
|
2015-12-16 23:01:15 +00:00
|
|
|
ClientStatus: structs.AllocClientStatusPending,
|
2015-08-16 17:28:58 +00:00
|
|
|
}
|
|
|
|
alloc2 := &structs.Allocation{
|
2015-09-07 22:23:03 +00:00
|
|
|
ID: structs.GenerateUUID(),
|
|
|
|
EvalID: structs.GenerateUUID(),
|
2015-08-16 17:28:58 +00:00
|
|
|
NodeID: nodes[1].Node.ID,
|
2015-09-07 22:23:03 +00:00
|
|
|
JobID: structs.GenerateUUID(),
|
2015-08-16 17:28:58 +00:00
|
|
|
Resources: &structs.Resources{
|
|
|
|
CPU: 1024,
|
|
|
|
MemoryMB: 1024,
|
|
|
|
},
|
2015-08-26 00:06:06 +00:00
|
|
|
DesiredStatus: structs.AllocDesiredStatusRun,
|
2015-12-16 23:01:15 +00:00
|
|
|
ClientStatus: structs.AllocClientStatusPending,
|
2015-08-16 17:28:58 +00:00
|
|
|
}
|
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{
|
|
|
|
&structs.Allocation{
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
CPU: 1024,
|
|
|
|
MemoryMB: 1024,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
cc := uint64(100)
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that if I pass class zero it returns escaped
|
|
|
|
if status := e.JobStatus(0); status != EvalComputedClassEscaped {
|
|
|
|
t.Fatalf("JobStatus() returned %v; want %v", status, EvalComputedClassEscaped)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEvalEligibility_TaskGroupStatus(t *testing.T) {
|
|
|
|
e := NewEvalEligibility()
|
|
|
|
cc := uint64(100)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that if I pass class zero it returns escaped
|
|
|
|
if status := e.TaskGroupStatus(tg, 0); status != EvalComputedClassEscaped {
|
|
|
|
t.Fatalf("TaskGroupStatus() returned %v; want %v", status, EvalComputedClassEscaped)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEvalEligibility_SetJob(t *testing.T) {
|
|
|
|
e := NewEvalEligibility()
|
|
|
|
ne1 := &structs.Constraint{
|
|
|
|
LTarget: "$attr.kernel.name",
|
|
|
|
RTarget: "linux",
|
|
|
|
Operand: "=",
|
|
|
|
}
|
|
|
|
e1 := &structs.Constraint{
|
|
|
|
LTarget: "$attr.unique.kernel.name",
|
|
|
|
RTarget: "linux",
|
|
|
|
Operand: "=",
|
|
|
|
}
|
|
|
|
e2 := &structs.Constraint{
|
|
|
|
LTarget: "$meta.unique.key_foo",
|
|
|
|
RTarget: "linux",
|
|
|
|
Operand: "<",
|
|
|
|
}
|
|
|
|
e3 := &structs.Constraint{
|
|
|
|
LTarget: "$meta.unique.key_foo",
|
|
|
|
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
|
|
|
|
|
|
|
type uint64Array []uint64
|
|
|
|
|
|
|
|
func (u uint64Array) Len() int { return len(u) }
|
|
|
|
func (u uint64Array) Less(i, j int) bool { return u[i] <= u[j] }
|
|
|
|
func (u uint64Array) Swap(i, j int) { u[i], u[j] = u[j], u[i] }
|
|
|
|
|
|
|
|
func TestEvalEligibility_GetClasses(t *testing.T) {
|
|
|
|
e := NewEvalEligibility()
|
|
|
|
e.SetJobEligibility(true, 1)
|
|
|
|
e.SetJobEligibility(false, 2)
|
|
|
|
e.SetTaskGroupEligibility(true, "foo", 3)
|
|
|
|
e.SetTaskGroupEligibility(false, "bar", 4)
|
|
|
|
e.SetTaskGroupEligibility(true, "bar", 5)
|
|
|
|
expElig := []uint64{1, 3, 5}
|
|
|
|
expInelig := []uint64{2, 4}
|
|
|
|
|
|
|
|
actElig, actInelig := e.GetClasses()
|
|
|
|
sort.Sort(uint64Array(actElig))
|
|
|
|
sort.Sort(uint64Array(actInelig))
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actElig, expElig) {
|
|
|
|
t.Fatalf("GetClasses() returned %#v; want %#v", actElig, expElig)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actInelig, expInelig) {
|
|
|
|
t.Fatalf("GetClasses() returned %#v; want %#v", actInelig, expInelig)
|
|
|
|
}
|
|
|
|
}
|