github.com/ryanslade/nomad@v0.2.4-0.20160128061903-fc95782f2089/nomad/periodic_endpoint_test.go (about)

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