Skip to content

Commit c18e431

Browse files
committed
Fixed more tests
1 parent ddf10e1 commit c18e431

10 files changed

+92
-20
lines changed

command/agent/alloc_endpoint_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ func TestHTTP_AllocsList(t *testing.T) {
1616
state := s.Agent.server.State()
1717
alloc1 := mock.Alloc()
1818
alloc2 := mock.Alloc()
19+
state.UpsertJobSummary(998, mock.JobSummary(alloc1.JobID))
20+
state.UpsertJobSummary(999, mock.JobSummary(alloc2.JobID))
1921
err := state.UpsertAllocs(1000,
2022
[]*structs.Allocation{alloc1, alloc2})
2123
if err != nil {

command/agent/eval_endpoint_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ func TestHTTP_EvalAllocations(t *testing.T) {
111111
alloc1 := mock.Alloc()
112112
alloc2 := mock.Alloc()
113113
alloc2.EvalID = alloc1.EvalID
114+
state.UpsertJobSummary(998, mock.JobSummary(alloc1.JobID))
115+
state.UpsertJobSummary(999, mock.JobSummary(alloc2.JobID))
114116
err := state.UpsertAllocs(1000,
115117
[]*structs.Allocation{alloc1, alloc2})
116118
if err != nil {

nomad/alloc_endpoint_test.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,13 @@ func TestAllocEndpoint_List_Blocking(t *testing.T) {
117117
alloc2.ID = alloc.ID
118118
alloc2.ClientStatus = structs.AllocClientStatusRunning
119119
time.AfterFunc(100*time.Millisecond, func() {
120-
if err := state.UpdateAllocsFromClient(3, []*structs.Allocation{alloc2}); err != nil {
120+
state.UpsertJobSummary(3, mock.JobSummary(alloc2.JobID))
121+
if err := state.UpdateAllocsFromClient(4, []*structs.Allocation{alloc2}); err != nil {
121122
t.Fatalf("err: %v", err)
122123
}
123124
})
124125

125-
req.MinQueryIndex = 2
126+
req.MinQueryIndex = 3
126127
start = time.Now()
127128
var resp2 structs.AllocListResponse
128129
if err := msgpackrpc.CallWithCodec(codec, "Alloc.List", req, &resp2); err != nil {
@@ -132,8 +133,8 @@ func TestAllocEndpoint_List_Blocking(t *testing.T) {
132133
if elapsed := time.Since(start); elapsed < 100*time.Millisecond {
133134
t.Fatalf("should block (returned in %s) %#v", elapsed, resp2)
134135
}
135-
if resp2.Index != 3 {
136-
t.Fatalf("Bad index: %d %d", resp2.Index, 3)
136+
if resp2.Index != 4 {
137+
t.Fatalf("Bad index: %d %d", resp2.Index, 4)
137138
}
138139
if len(resp2.Allocations) != 1 || resp.Allocations[0].ID != alloc.ID ||
139140
resp2.Allocations[0].ClientStatus != structs.AllocClientStatusRunning {
@@ -150,6 +151,7 @@ func TestAllocEndpoint_GetAlloc(t *testing.T) {
150151
// Create the register request
151152
alloc := mock.Alloc()
152153
state := s1.fsm.State()
154+
state.UpsertJobSummary(999, mock.JobSummary(alloc.JobID))
153155
err := state.UpsertAllocs(1000, []*structs.Allocation{alloc})
154156
if err != nil {
155157
t.Fatalf("err: %v", err)
@@ -186,6 +188,7 @@ func TestAllocEndpoint_GetAlloc_Blocking(t *testing.T) {
186188

187189
// First create an unrelated alloc
188190
time.AfterFunc(100*time.Millisecond, func() {
191+
state.UpsertJobSummary(99, mock.JobSummary(alloc1.JobID))
189192
err := state.UpsertAllocs(100, []*structs.Allocation{alloc1})
190193
if err != nil {
191194
t.Fatalf("err: %v", err)
@@ -194,6 +197,7 @@ func TestAllocEndpoint_GetAlloc_Blocking(t *testing.T) {
194197

195198
// Create the alloc we are watching later
196199
time.AfterFunc(200*time.Millisecond, func() {
200+
state.UpsertJobSummary(999, mock.JobSummary(alloc2.JobID))
197201
err := state.UpsertAllocs(200, []*structs.Allocation{alloc2})
198202
if err != nil {
199203
t.Fatalf("err: %v", err)
@@ -235,6 +239,8 @@ func TestAllocEndpoint_GetAllocs(t *testing.T) {
235239
alloc := mock.Alloc()
236240
alloc2 := mock.Alloc()
237241
state := s1.fsm.State()
242+
state.UpsertJobSummary(998, mock.JobSummary(alloc.JobID))
243+
state.UpsertJobSummary(999, mock.JobSummary(alloc2.JobID))
238244
err := state.UpsertAllocs(1000, []*structs.Allocation{alloc, alloc2})
239245
if err != nil {
240246
t.Fatalf("err: %v", err)

nomad/core_sched_test.go

+15-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func TestCoreScheduler_EvalGC(t *testing.T) {
1818
state := s1.fsm.State()
1919
eval := mock.Eval()
2020
eval.Status = structs.EvalStatusFailed
21+
state.UpsertJobSummary(999, mock.JobSummary(eval.JobID))
2122
err := state.UpsertEvals(1000, []*structs.Evaluation{eval})
2223
if err != nil {
2324
t.Fatalf("err: %v", err)
@@ -27,6 +28,7 @@ func TestCoreScheduler_EvalGC(t *testing.T) {
2728
alloc := mock.Alloc()
2829
alloc.EvalID = eval.ID
2930
alloc.DesiredStatus = structs.AllocDesiredStatusStop
31+
alloc.JobID = eval.JobID
3032
err = state.UpsertAllocs(1001, []*structs.Allocation{alloc})
3133
if err != nil {
3234
t.Fatalf("err: %v", err)
@@ -157,6 +159,7 @@ func TestCoreScheduler_EvalGC_Partial(t *testing.T) {
157159
state := s1.fsm.State()
158160
eval := mock.Eval()
159161
eval.Status = structs.EvalStatusComplete
162+
state.UpsertJobSummary(999, mock.JobSummary(eval.JobID))
160163
err := state.UpsertEvals(1000, []*structs.Evaluation{eval})
161164
if err != nil {
162165
t.Fatalf("err: %v", err)
@@ -166,15 +169,17 @@ func TestCoreScheduler_EvalGC_Partial(t *testing.T) {
166169
alloc := mock.Alloc()
167170
alloc.EvalID = eval.ID
168171
alloc.DesiredStatus = structs.AllocDesiredStatusStop
169-
err = state.UpsertAllocs(1001, []*structs.Allocation{alloc})
172+
state.UpsertJobSummary(1001, mock.JobSummary(alloc.JobID))
173+
err = state.UpsertAllocs(1002, []*structs.Allocation{alloc})
170174
if err != nil {
171175
t.Fatalf("err: %v", err)
172176
}
173177

174178
// Insert "running" alloc
175179
alloc2 := mock.Alloc()
176180
alloc2.EvalID = eval.ID
177-
err = state.UpsertAllocs(1002, []*structs.Allocation{alloc2})
181+
state.UpsertJobSummary(1003, mock.JobSummary(alloc2.JobID))
182+
err = state.UpsertAllocs(1004, []*structs.Allocation{alloc2})
178183
if err != nil {
179184
t.Fatalf("err: %v", err)
180185
}
@@ -233,6 +238,7 @@ func TestCoreScheduler_EvalGC_Force(t *testing.T) {
233238
state := s1.fsm.State()
234239
eval := mock.Eval()
235240
eval.Status = structs.EvalStatusFailed
241+
state.UpsertJobSummary(999, mock.JobSummary(eval.JobID))
236242
err := state.UpsertEvals(1000, []*structs.Evaluation{eval})
237243
if err != nil {
238244
t.Fatalf("err: %v", err)
@@ -242,7 +248,8 @@ func TestCoreScheduler_EvalGC_Force(t *testing.T) {
242248
alloc := mock.Alloc()
243249
alloc.EvalID = eval.ID
244250
alloc.DesiredStatus = structs.AllocDesiredStatusStop
245-
err = state.UpsertAllocs(1001, []*structs.Allocation{alloc})
251+
state.UpsertJobSummary(1001, mock.JobSummary(alloc.JobID))
252+
err = state.UpsertAllocs(1002, []*structs.Allocation{alloc})
246253
if err != nil {
247254
t.Fatalf("err: %v", err)
248255
}
@@ -255,7 +262,7 @@ func TestCoreScheduler_EvalGC_Force(t *testing.T) {
255262
core := NewCoreScheduler(s1, snap)
256263

257264
// Attempt the GC
258-
gc := s1.coreJobEval(structs.CoreJobForceGC, 1001)
265+
gc := s1.coreJobEval(structs.CoreJobForceGC, 1002)
259266
err = core.Process(gc)
260267
if err != nil {
261268
t.Fatalf("err: %v", err)
@@ -338,7 +345,8 @@ func TestCoreScheduler_NodeGC_TerminalAllocs(t *testing.T) {
338345
// Insert a terminal alloc on that node
339346
alloc := mock.Alloc()
340347
alloc.DesiredStatus = structs.AllocDesiredStatusStop
341-
if err := state.UpsertAllocs(1001, []*structs.Allocation{alloc}); err != nil {
348+
state.UpsertJobSummary(1001, mock.JobSummary(alloc.JobID))
349+
if err := state.UpsertAllocs(1002, []*structs.Allocation{alloc}); err != nil {
342350
t.Fatalf("err: %v", err)
343351
}
344352

@@ -389,7 +397,8 @@ func TestCoreScheduler_NodeGC_RunningAllocs(t *testing.T) {
389397
alloc.NodeID = node.ID
390398
alloc.DesiredStatus = structs.AllocDesiredStatusRun
391399
alloc.ClientStatus = structs.AllocClientStatusRunning
392-
if err := state.UpsertAllocs(1001, []*structs.Allocation{alloc}); err != nil {
400+
state.UpsertJobSummary(1001, mock.JobSummary(alloc.JobID))
401+
if err := state.UpsertAllocs(1002, []*structs.Allocation{alloc}); err != nil {
393402
t.Fatalf("err: %v", err)
394403
}
395404

nomad/eval_endpoint_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,8 @@ func TestEvalEndpoint_Allocations(t *testing.T) {
500500
alloc2 := mock.Alloc()
501501
alloc2.EvalID = alloc1.EvalID
502502
state := s1.fsm.State()
503+
state.UpsertJobSummary(998, mock.JobSummary(alloc1.JobID))
504+
state.UpsertJobSummary(999, mock.JobSummary(alloc2.JobID))
503505
err := state.UpsertAllocs(1000,
504506
[]*structs.Allocation{alloc1, alloc2})
505507
if err != nil {
@@ -537,6 +539,7 @@ func TestEvalEndpoint_Allocations_Blocking(t *testing.T) {
537539

538540
// Upsert an unrelated alloc first
539541
time.AfterFunc(100*time.Millisecond, func() {
542+
state.UpsertJobSummary(99, mock.JobSummary(alloc1.JobID))
540543
err := state.UpsertAllocs(100, []*structs.Allocation{alloc1})
541544
if err != nil {
542545
t.Fatalf("err: %v", err)
@@ -545,6 +548,7 @@ func TestEvalEndpoint_Allocations_Blocking(t *testing.T) {
545548

546549
// Upsert an alloc which will trigger the watch later
547550
time.AfterFunc(200*time.Millisecond, func() {
551+
state.UpsertJobSummary(199, mock.JobSummary(alloc2.JobID))
548552
err := state.UpsertAllocs(200, []*structs.Allocation{alloc2})
549553
if err != nil {
550554
t.Fatalf("err: %v", err)

nomad/fsm_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -966,9 +966,9 @@ func TestFSM_SnapshotRestore_JobSummary(t *testing.T) {
966966
out1, _ := state2.JobSummaryByID(job1.ID)
967967
out2, _ := state2.JobSummaryByID(job2.ID)
968968
if !reflect.DeepEqual(js1, out1) {
969-
t.Fatalf("bad: \n%#v\n%#v", js1, job1)
969+
t.Fatalf("bad: \n%#v\n%#v", js1, out1)
970970
}
971971
if !reflect.DeepEqual(js2, out2) {
972-
t.Fatalf("bad: \n%#v\n%#v", js2, job2)
972+
t.Fatalf("bad: \n%#v\n%#v", js2, out2)
973973
}
974974
}

nomad/node_endpoint_test.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,10 @@ func TestClientEndpoint_GetNode(t *testing.T) {
482482
t.Fatalf("bad ComputedClass: %#v", resp2.Node)
483483
}
484484

485+
// Update the status updated at value
486+
node.StatusUpdatedAt = resp2.Node.StatusUpdatedAt
485487
if !reflect.DeepEqual(node, resp2.Node) {
486-
t.Fatalf("bad: %#v %#v", node, resp2.Node)
488+
t.Fatalf("bad: %#v \n %#v", node, resp2.Node)
487489
}
488490

489491
// Lookup non-existing node
@@ -625,6 +627,7 @@ func TestClientEndpoint_GetAllocs(t *testing.T) {
625627
alloc := mock.Alloc()
626628
alloc.NodeID = node.ID
627629
state := s1.fsm.State()
630+
state.UpsertJobSummary(99, mock.JobSummary(alloc.JobID))
628631
err := state.UpsertAllocs(100, []*structs.Allocation{alloc})
629632
if err != nil {
630633
t.Fatalf("err: %v", err)
@@ -685,6 +688,7 @@ func TestClientEndpoint_GetClientAllocs(t *testing.T) {
685688
alloc := mock.Alloc()
686689
alloc.NodeID = node.ID
687690
state := s1.fsm.State()
691+
state.UpsertJobSummary(99, mock.JobSummary(alloc.JobID))
688692
err := state.UpsertAllocs(100, []*structs.Allocation{alloc})
689693
if err != nil {
690694
t.Fatalf("err: %v", err)
@@ -746,6 +750,7 @@ func TestClientEndpoint_GetClientAllocs_Blocking(t *testing.T) {
746750
alloc := mock.Alloc()
747751
alloc.NodeID = node.ID
748752
state := s1.fsm.State()
753+
state.UpsertJobSummary(99, mock.JobSummary(alloc.JobID))
749754
start := time.Now()
750755
time.AfterFunc(100*time.Millisecond, func() {
751756
err := state.UpsertAllocs(100, []*structs.Allocation{alloc})
@@ -787,6 +792,7 @@ func TestClientEndpoint_GetClientAllocs_Blocking(t *testing.T) {
787792
allocUpdate.NodeID = alloc.NodeID
788793
allocUpdate.ID = alloc.ID
789794
allocUpdate.ClientStatus = structs.AllocClientStatusRunning
795+
state.UpsertJobSummary(199, mock.JobSummary(allocUpdate.JobID))
790796
err := state.UpsertAllocs(200, []*structs.Allocation{allocUpdate})
791797
if err != nil {
792798
t.Fatalf("err: %v", err)
@@ -835,6 +841,7 @@ func TestClientEndpoint_GetAllocs_Blocking(t *testing.T) {
835841
alloc := mock.Alloc()
836842
alloc.NodeID = node.ID
837843
state := s1.fsm.State()
844+
state.UpsertJobSummary(99, mock.JobSummary(alloc.JobID))
838845
start := time.Now()
839846
time.AfterFunc(100*time.Millisecond, func() {
840847
err := state.UpsertAllocs(100, []*structs.Allocation{alloc})
@@ -876,6 +883,7 @@ func TestClientEndpoint_GetAllocs_Blocking(t *testing.T) {
876883
allocUpdate.NodeID = alloc.NodeID
877884
allocUpdate.ID = alloc.ID
878885
allocUpdate.ClientStatus = structs.AllocClientStatusRunning
886+
state.UpsertJobSummary(199, mock.JobSummary(allocUpdate.JobID))
879887
err := state.UpdateAllocsFromClient(200, []*structs.Allocation{allocUpdate})
880888
if err != nil {
881889
t.Fatalf("err: %v", err)
@@ -922,6 +930,7 @@ func TestClientEndpoint_UpdateAlloc(t *testing.T) {
922930
alloc := mock.Alloc()
923931
alloc.NodeID = node.ID
924932
state := s1.fsm.State()
933+
state.UpsertJobSummary(99, mock.JobSummary(alloc.JobID))
925934
err := state.UpsertAllocs(100, []*structs.Allocation{alloc})
926935
if err != nil {
927936
t.Fatalf("err: %v", err)
@@ -982,6 +991,7 @@ func TestClientEndpoint_BatchUpdate(t *testing.T) {
982991
alloc := mock.Alloc()
983992
alloc.NodeID = node.ID
984993
state := s1.fsm.State()
994+
state.UpsertJobSummary(99, mock.JobSummary(alloc.JobID))
985995
err := state.UpsertAllocs(100, []*structs.Allocation{alloc})
986996
if err != nil {
987997
t.Fatalf("err: %v", err)
@@ -1021,13 +1031,14 @@ func TestClientEndpoint_CreateNodeEvals(t *testing.T) {
10211031
// Inject fake evaluations
10221032
alloc := mock.Alloc()
10231033
state := s1.fsm.State()
1024-
if err := state.UpsertAllocs(1, []*structs.Allocation{alloc}); err != nil {
1034+
state.UpsertJobSummary(1, mock.JobSummary(alloc.JobID))
1035+
if err := state.UpsertAllocs(2, []*structs.Allocation{alloc}); err != nil {
10251036
t.Fatalf("err: %v", err)
10261037
}
10271038

10281039
// Inject a fake system job.
10291040
job := mock.SystemJob()
1030-
if err := state.UpsertJob(1, job); err != nil {
1041+
if err := state.UpsertJob(3, job); err != nil {
10311042
t.Fatalf("err: %v", err)
10321043
}
10331044

@@ -1115,7 +1126,8 @@ func TestClientEndpoint_Evaluate(t *testing.T) {
11151126
if err != nil {
11161127
t.Fatalf("err: %v", err)
11171128
}
1118-
err = state.UpsertAllocs(2, []*structs.Allocation{alloc})
1129+
state.UpsertJobSummary(2, mock.JobSummary(alloc.JobID))
1130+
err = state.UpsertAllocs(3, []*structs.Allocation{alloc})
11191131
if err != nil {
11201132
t.Fatalf("err: %v", err)
11211133
}

nomad/plan_apply_test.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@ func testRegisterNode(t *testing.T, s *Server, n *structs.Node) {
4040
}
4141
}
4242

43+
func testRegisterJob(t *testing.T, s *Server, j *structs.Job) {
44+
// Create the register request
45+
req := &structs.JobRegisterRequest{
46+
Job: j,
47+
WriteRequest: structs.WriteRequest{Region: "global"},
48+
}
49+
50+
// Fetch the response
51+
var resp structs.JobRegisterResponse
52+
if err := s.RPC("Job.Register", req, &resp); err != nil {
53+
t.Fatalf("err: %v", err)
54+
}
55+
if resp.Index == 0 {
56+
t.Fatalf("bad index: %d", resp.Index)
57+
}
58+
}
59+
4360
func TestPlanApply_applyPlan(t *testing.T) {
4461
s1 := testServer(t, nil)
4562
defer s1.Shutdown()
@@ -51,6 +68,7 @@ func TestPlanApply_applyPlan(t *testing.T) {
5168

5269
// Register alloc
5370
alloc := mock.Alloc()
71+
s1.State().UpsertJobSummary(1000, mock.JobSummary(alloc.JobID))
5472
plan := &structs.PlanResult{
5573
NodeAllocation: map[string][]*structs.Allocation{
5674
node.ID: []*structs.Allocation{alloc},
@@ -362,12 +380,15 @@ func TestPlanApply_EvalNodePlan_NodeFull(t *testing.T) {
362380
alloc.NodeID = node.ID
363381
node.Resources = alloc.Resources
364382
node.Reserved = nil
383+
state.UpsertJobSummary(999, mock.JobSummary(alloc.JobID))
365384
state.UpsertNode(1000, node)
366385
state.UpsertAllocs(1001, []*structs.Allocation{alloc})
367-
snap, _ := state.Snapshot()
368386

369387
alloc2 := mock.Alloc()
370388
alloc2.NodeID = node.ID
389+
state.UpsertJobSummary(1200, mock.JobSummary(alloc2.JobID))
390+
391+
snap, _ := state.Snapshot()
371392
plan := &structs.Plan{
372393
NodeAllocation: map[string][]*structs.Allocation{
373394
node.ID: []*structs.Allocation{alloc2},

nomad/state/state_store.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,9 @@ func (s *StateStore) UpsertJob(index uint64, job *structs.Job) error {
349349
}
350350
}
351351

352-
s.updateSummaryWithJob(index, job, watcher, txn)
352+
if err := s.updateSummaryWithJob(index, job, watcher, txn); err != nil {
353+
return fmt.Errorf("unable to create job summary: %v", err)
354+
}
353355

354356
// Insert the job
355357
if err := txn.Insert("jobs", job); err != nil {
@@ -1512,7 +1514,18 @@ func (r *StateRestore) CreateJobSummaries() error {
15121514
if raw == nil {
15131515
break
15141516
}
1515-
jobs = append(jobs, raw.(*structs.Job))
1517+
1518+
// Filter the jobs which have summaries
1519+
job := raw.(*structs.Job)
1520+
jobSummary, err := r.txn.Get("job_summary", "id", job.ID)
1521+
if err != nil {
1522+
return fmt.Errorf("unable to get job summary: %v", err)
1523+
}
1524+
if jobSummary != nil {
1525+
continue
1526+
}
1527+
1528+
jobs = append(jobs, job)
15161529
}
15171530

15181531
for _, job := range jobs {

0 commit comments

Comments
 (0)