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