open-nomad/scheduler/context_test.go

108 lines
2.3 KiB
Go
Raw Normal View History

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
)
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
}
func TestEvalContext_ProposedAlloc(t *testing.T) {
state, ctx := testContext(t)
nodes := []*RankedNode{
&RankedNode{
Node: &structs.Node{
// Perfect fit
ID: structs.GenerateUUID(),
Resources: &structs.Resources{
CPU: 2048,
MemoryMB: 2048,
},
},
},
&RankedNode{
Node: &structs.Node{
// Perfect fit
ID: structs.GenerateUUID(),
Resources: &structs.Resources{
CPU: 2048,
MemoryMB: 2048,
},
},
},
}
// Add existing allocations
alloc1 := &structs.Allocation{
ID: structs.GenerateUUID(),
EvalID: structs.GenerateUUID(),
NodeID: nodes[0].Node.ID,
JobID: structs.GenerateUUID(),
Resources: &structs.Resources{
CPU: 2048,
MemoryMB: 2048,
},
2015-08-26 00:06:06 +00:00
DesiredStatus: structs.AllocDesiredStatusRun,
}
alloc2 := &structs.Allocation{
ID: structs.GenerateUUID(),
EvalID: structs.GenerateUUID(),
NodeID: nodes[1].Node.ID,
JobID: structs.GenerateUUID(),
Resources: &structs.Resources{
CPU: 1024,
MemoryMB: 1024,
},
2015-08-26 00:06:06 +00:00
DesiredStatus: structs.AllocDesiredStatusRun,
}
2015-09-07 03:47:42 +00:00
noErr(t, state.UpsertAllocs(1000, []*structs.Allocation{alloc1, alloc2}))
// 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}
// 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)
}
}