github.com/maier/nomad@v0.4.1-0.20161110003312-a9e3d0b8549d/nomad/system_endpoint_test.go (about)

     1  package nomad
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/net-rpc-msgpackrpc"
     9  	"github.com/hashicorp/nomad/nomad/mock"
    10  	"github.com/hashicorp/nomad/nomad/structs"
    11  	"github.com/hashicorp/nomad/testutil"
    12  )
    13  
    14  func TestSystemEndpoint_GarbageCollect(t *testing.T) {
    15  	s1 := testServer(t, nil)
    16  	defer s1.Shutdown()
    17  	codec := rpcClient(t, s1)
    18  	testutil.WaitForLeader(t, s1.RPC)
    19  
    20  	// Insert a job that can be GC'd
    21  	state := s1.fsm.State()
    22  	job := mock.Job()
    23  	job.Type = structs.JobTypeBatch
    24  	if err := state.UpsertJob(1000, job); err != nil {
    25  		t.Fatalf("UpsertAllocs() failed: %v", err)
    26  	}
    27  
    28  	// Make the GC request
    29  	req := &structs.GenericRequest{
    30  		QueryOptions: structs.QueryOptions{
    31  			Region: "global",
    32  		},
    33  	}
    34  	var resp structs.GenericResponse
    35  	if err := msgpackrpc.CallWithCodec(codec, "System.GarbageCollect", req, &resp); err != nil {
    36  		t.Fatalf("expect err")
    37  	}
    38  
    39  	testutil.WaitForResult(func() (bool, error) {
    40  		// Check if the job has been GC'd
    41  		exist, err := state.JobByID(job.ID)
    42  		if err != nil {
    43  			return false, err
    44  		}
    45  		if exist != nil {
    46  			return false, fmt.Errorf("job %q wasn't garbage collected", job.ID)
    47  		}
    48  		return true, nil
    49  	}, func(err error) {
    50  		t.Fatalf("err: %s", err)
    51  	})
    52  }
    53  
    54  func TestSystemEndpoint_ReconcileSummaries(t *testing.T) {
    55  	s1 := testServer(t, nil)
    56  	defer s1.Shutdown()
    57  	codec := rpcClient(t, s1)
    58  	testutil.WaitForLeader(t, s1.RPC)
    59  
    60  	// Insert a job that can be GC'd
    61  	state := s1.fsm.State()
    62  	s1.fsm.State()
    63  	job := mock.Job()
    64  	if err := state.UpsertJob(1000, job); err != nil {
    65  		t.Fatalf("UpsertJob() failed: %v", err)
    66  	}
    67  
    68  	// Delete the job summary
    69  	state.DeleteJobSummary(1001, job.ID)
    70  
    71  	// Make the GC request
    72  	req := &structs.GenericRequest{
    73  		QueryOptions: structs.QueryOptions{
    74  			Region: "global",
    75  		},
    76  	}
    77  	var resp structs.GenericResponse
    78  	if err := msgpackrpc.CallWithCodec(codec, "System.ReconcileJobSummaries", req, &resp); err != nil {
    79  		t.Fatalf("expect err: %v", err)
    80  	}
    81  
    82  	testutil.WaitForResult(func() (bool, error) {
    83  		// Check if Nomad has reconciled the summary for the job
    84  		summary, err := state.JobSummaryByID(job.ID)
    85  		if err != nil {
    86  			return false, err
    87  		}
    88  		if summary.CreateIndex == 0 || summary.ModifyIndex == 0 {
    89  			t.Fatalf("create index: %v, modify index: %v", summary.CreateIndex, summary.ModifyIndex)
    90  		}
    91  
    92  		// setting the modifyindex and createindex of the expected summary to
    93  		// the output so that we can do deep equal
    94  		expectedSummary := structs.JobSummary{
    95  			JobID: job.ID,
    96  			Summary: map[string]structs.TaskGroupSummary{
    97  				"web": structs.TaskGroupSummary{
    98  					Queued: 10,
    99  				},
   100  			},
   101  			ModifyIndex: summary.ModifyIndex,
   102  			CreateIndex: summary.CreateIndex,
   103  		}
   104  		if !reflect.DeepEqual(&expectedSummary, summary) {
   105  			return false, fmt.Errorf("expected: %v, actual: %v", expectedSummary, summary)
   106  		}
   107  		return true, nil
   108  	}, func(err error) {
   109  		t.Fatalf("err: %s", err)
   110  	})
   111  }