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"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"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-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-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)
|
|
|
|
}
|
|
|
|
}
|