Switch to using the harness

This commit is contained in:
Alex Dadgar
2016-05-16 12:49:18 -07:00
parent de09d3c70c
commit f56bdbe03a
7 changed files with 27 additions and 58 deletions

View File

@@ -282,10 +282,10 @@ type JobPlanRequest struct {
}
type JobPlanResponse struct {
Cas uint64
CreatedEvals []*Evaluation
Diff *JobDiff
Annotations *PlanAnnotations
JobModifyIndex uint64
CreatedEvals []*Evaluation
Diff *JobDiff
Annotations *PlanAnnotations
}
type JobDiff struct {

View File

@@ -380,8 +380,8 @@ func TestJobs_Plan(t *testing.T) {
t.Fatalf("nil response")
}
if planResp.Cas == 0 {
t.Fatalf("bad Cas value: %#v", planResp)
if planResp.JobModifyIndex == 0 {
t.Fatalf("bad JobModifyIndex value: %#v", planResp)
}
if planResp.Diff == nil {
t.Fatalf("got nil diff: %#v", planResp)
@@ -405,8 +405,8 @@ func TestJobs_Plan(t *testing.T) {
t.Fatalf("nil response")
}
if planResp.Cas == 0 {
t.Fatalf("bad Cas value: %d", planResp.Cas)
if planResp.JobModifyIndex == 0 {
t.Fatalf("bad JobModifyIndex value: %d", planResp.JobModifyIndex)
}
if planResp.Diff != nil {
t.Fatalf("got non-nil diff: %#v", planResp)

View File

@@ -510,7 +510,7 @@ func TestHTTP_JobPlan(t *testing.T) {
// Check the response
plan := obj.(structs.JobPlanResponse)
if plan.Plan == nil {
if plan.Annotations == nil {
t.Fatalf("bad: %v", plan)
}

View File

@@ -443,7 +443,9 @@ func (j *Job) Plan(args *structs.JobPlanRequest, reply *structs.JobPlanResponse)
// Create an in-memory Planner that returns no errors and stores the
// submitted plan and created evals.
planner := scheduler.NewInMemoryPlanner()
planner := &scheduler.Harness{
State: &snap.StateStore,
}
// Create the scheduler and run it
sched, err := scheduler.NewScheduler(eval.Type, j.srv.logger, snap, planner)
@@ -456,7 +458,10 @@ func (j *Job) Plan(args *structs.JobPlanRequest, reply *structs.JobPlanResponse)
}
// Annotate and store the diff
annotations := planner.Plan.Annotations
if plans := len(planner.Plans); plans != 1 {
return fmt.Errorf("scheduler resulted in an unexpected number of plans: %d", plans)
}
annotations := planner.Plans[0].Annotations
if args.Diff {
jobDiff, err := oldJob.Diff(args.Job, true)
if err != nil {
@@ -471,7 +476,7 @@ func (j *Job) Plan(args *structs.JobPlanRequest, reply *structs.JobPlanResponse)
reply.JobModifyIndex = index
reply.Annotations = annotations
reply.CreatedEvals = planner.CreatedEvals
reply.CreatedEvals = planner.CreateEvals
reply.Index = index
return nil
}

View File

@@ -1,36 +0,0 @@
package scheduler
import "github.com/hashicorp/nomad/nomad/structs"
// InMemPlanner is an in-memory Planner that can be used to invoke the scheduler
// without side-effects.
type InMemPlanner struct {
CreatedEvals []*structs.Evaluation
Plan *structs.Plan
}
// NewInMemoryPlanner returns a new in-memory planner.
func NewInMemoryPlanner() *InMemPlanner {
return &InMemPlanner{}
}
func (i *InMemPlanner) SubmitPlan(plan *structs.Plan) (*structs.PlanResult, State, error) {
i.Plan = plan
// Create a fully committed plan result.
result := &structs.PlanResult{
NodeUpdate: plan.NodeUpdate,
NodeAllocation: plan.NodeAllocation,
}
return result, nil, nil
}
func (i *InMemPlanner) UpdateEval(eval *structs.Evaluation) error {
return nil
}
func (i *InMemPlanner) CreateEval(eval *structs.Evaluation) error {
i.CreatedEvals = append(i.CreatedEvals, eval)
return nil
}

View File

@@ -29,9 +29,9 @@ func (r *RejectPlan) CreateEval(*structs.Evaluation) error {
return nil
}
// Harness is a lightweight testing harness for schedulers.
// It manages a state store copy and provides the planner
// interface. It can be extended for various testing uses.
// Harness is a lightweight testing harness for schedulers. It manages a state
// store copy and provides the planner interface. It can be extended for various
// testing uses or for invoking the scheduler without side effects.
type Harness struct {
State *state.StateStore
@@ -178,10 +178,3 @@ func (h *Harness) AssertEvalStatus(t *testing.T, state string) {
t.Fatalf("bad: %#v", update)
}
}
// noErr is used to assert there are no errors
func noErr(t *testing.T, err error) {
if err != nil {
t.Fatalf("err: %v", err)
}
}

View File

@@ -12,6 +12,13 @@ import (
"github.com/hashicorp/nomad/nomad/structs"
)
// noErr is used to assert there are no errors
func noErr(t *testing.T, err error) {
if err != nil {
t.Fatalf("err: %v", err)
}
}
func TestMaterializeTaskGroups(t *testing.T) {
job := mock.Job()
index := materializeTaskGroups(job)