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