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