github.com/jrxfive/nomad@v0.6.1-0.20170802162750-1fef470e89bf/nomad/periodic_endpoint_test.go (about)

     1  package nomad
     2  
     3  import (
     4  	"testing"
     5  
     6  	memdb "github.com/hashicorp/go-memdb"
     7  	"github.com/hashicorp/net-rpc-msgpackrpc"
     8  	"github.com/hashicorp/nomad/nomad/mock"
     9  	"github.com/hashicorp/nomad/nomad/structs"
    10  	"github.com/hashicorp/nomad/testutil"
    11  )
    12  
    13  func TestPeriodicEndpoint_Force(t *testing.T) {
    14  	t.Parallel()
    15  	s1 := testServer(t, func(c *Config) {
    16  		c.NumSchedulers = 0 // Prevent automatic dequeue
    17  	})
    18  	state := s1.fsm.State()
    19  	defer s1.Shutdown()
    20  	codec := rpcClient(t, s1)
    21  	testutil.WaitForLeader(t, s1.RPC)
    22  
    23  	// Create and insert a periodic job.
    24  	job := mock.PeriodicJob()
    25  	job.Periodic.ProhibitOverlap = true // Shouldn't affect anything.
    26  	if err := state.UpsertJob(100, job); err != nil {
    27  		t.Fatalf("err: %v", err)
    28  	}
    29  	s1.periodicDispatcher.Add(job)
    30  
    31  	// Force launch it.
    32  	req := &structs.PeriodicForceRequest{
    33  		JobID:        job.ID,
    34  		WriteRequest: structs.WriteRequest{Region: "global"},
    35  	}
    36  
    37  	// Fetch the response
    38  	var resp structs.PeriodicForceResponse
    39  	if err := msgpackrpc.CallWithCodec(codec, "Periodic.Force", req, &resp); err != nil {
    40  		t.Fatalf("err: %v", err)
    41  	}
    42  	if resp.Index == 0 {
    43  		t.Fatalf("bad index: %d", resp.Index)
    44  	}
    45  
    46  	// Lookup the evaluation
    47  	ws := memdb.NewWatchSet()
    48  	eval, err := state.EvalByID(ws, resp.EvalID)
    49  	if err != nil {
    50  		t.Fatalf("err: %v", err)
    51  	}
    52  	if eval == nil {
    53  		t.Fatalf("expected eval")
    54  	}
    55  	if eval.CreateIndex != resp.EvalCreateIndex {
    56  		t.Fatalf("index mis-match")
    57  	}
    58  }
    59  
    60  func TestPeriodicEndpoint_Force_NonPeriodic(t *testing.T) {
    61  	t.Parallel()
    62  	s1 := testServer(t, func(c *Config) {
    63  		c.NumSchedulers = 0 // Prevent automatic dequeue
    64  	})
    65  	state := s1.fsm.State()
    66  	defer s1.Shutdown()
    67  	codec := rpcClient(t, s1)
    68  	testutil.WaitForLeader(t, s1.RPC)
    69  
    70  	// Create and insert a non-periodic job.
    71  	job := mock.Job()
    72  	if err := state.UpsertJob(100, job); err != nil {
    73  		t.Fatalf("err: %v", err)
    74  	}
    75  
    76  	// Force launch it.
    77  	req := &structs.PeriodicForceRequest{
    78  		JobID:        job.ID,
    79  		WriteRequest: structs.WriteRequest{Region: "global"},
    80  	}
    81  
    82  	// Fetch the response
    83  	var resp structs.PeriodicForceResponse
    84  	if err := msgpackrpc.CallWithCodec(codec, "Periodic.Force", req, &resp); err == nil {
    85  		t.Fatalf("Force on non-perodic job should err")
    86  	}
    87  }