Skip to content

Commit eb8af4d

Browse files
fix(trial): use propagated gomega to improve debuggability. (#2432)
Signed-off-by: Electronic-Waste <2690692950@qq.com>
1 parent 9889b33 commit eb8af4d

File tree

1 file changed

+35
-45
lines changed

1 file changed

+35
-45
lines changed

pkg/controller.v1beta1/trial/trial_controller_test.go

+35-45
Original file line numberDiff line numberDiff line change
@@ -187,24 +187,20 @@ func TestReconcileBatchJob(t *testing.T) {
187187
g.Expect(c.Create(ctx, trial)).NotTo(gomega.HaveOccurred())
188188

189189
// Expect that BatchJob with appropriate name is created
190-
g.Eventually(func() error {
191-
return c.Get(ctx, batchJobKey, batchJob)
190+
g.Eventually(func(g gomega.Gomega) {
191+
g.Expect(c.Get(ctx, batchJobKey, batchJob)).Should(gomega.Succeed())
192192
}, timeout).Should(gomega.Succeed())
193193

194194
// Expect that Trial status is running
195-
g.Eventually(func() bool {
196-
if err = c.Get(ctx, trialKey, trial); err != nil {
197-
return false
198-
}
199-
return trial.IsRunning()
200-
}, timeout).Should(gomega.BeTrue())
195+
g.Eventually(func(g gomega.Gomega) {
196+
g.Expect(c.Get(ctx, trialKey, trial)).Should(gomega.Succeed())
197+
g.Expect(trial.IsRunning()).Should(gomega.BeTrue())
198+
}, timeout).Should(gomega.Succeed())
201199

202200
// Manually update BatchJob status to failed
203201
// Expect that Trial status is failed
204-
g.Eventually(func() bool {
205-
if err = c.Get(ctx, batchJobKey, batchJob); err != nil {
206-
return false
207-
}
202+
g.Eventually(func(g gomega.Gomega) {
203+
g.Expect(c.Get(ctx, batchJobKey, batchJob)).Should(gomega.Succeed())
208204
batchJob.Status = batchv1.JobStatus{
209205
Conditions: []batchv1.JobCondition{
210206
{
@@ -215,25 +211,20 @@ func TestReconcileBatchJob(t *testing.T) {
215211
},
216212
},
217213
}
218-
if err = c.Status().Update(ctx, batchJob); err != nil {
219-
return false
220-
}
221-
222-
if err = c.Get(ctx, trialKey, trial); err != nil {
223-
return false
224-
}
225-
return trial.IsFailed()
226-
}, timeout).Should(gomega.BeTrue())
214+
g.Expect(c.Status().Update(ctx, batchJob)).Should(gomega.Succeed())
215+
g.Expect(c.Get(ctx, trialKey, trial)).Should(gomega.Succeed())
216+
g.Expect(trial.IsFailed()).Should(gomega.BeTrue())
217+
}, timeout).Should(gomega.Succeed())
227218

228219
// Delete the Trial
229220
g.Expect(c.Delete(ctx, trial)).NotTo(gomega.HaveOccurred())
230221

231222
// Expect that Trial is deleted
232223
// BatchJob can't be deleted because GC doesn't work in envtest and BatchJob stuck in termination phase.
233224
// Ref: https://book.kubebuilder.io/reference/testing/envtest.html#testing-considerations.
234-
g.Eventually(func() bool {
235-
return errors.IsNotFound(c.Get(ctx, trialKey, &trialsv1beta1.Trial{}))
236-
}, timeout).Should(gomega.BeTrue())
225+
g.Eventually(func(g gomega.Gomega) {
226+
g.Expect(errors.IsNotFound(c.Get(ctx, trialKey, &trialsv1beta1.Trial{}))).Should(gomega.BeTrue())
227+
}, timeout).Should(gomega.Succeed())
237228
})
238229

239230
t.Run(`Trial with "Complete" BatchJob and Available metrics.`, func(t *testing.T) {
@@ -266,26 +257,25 @@ func TestReconcileBatchJob(t *testing.T) {
266257

267258
// Expect that Trial status is succeeded and metrics are properly populated
268259
// Metrics available because GetTrialObservationLog returns values
269-
start := time.Now()
270-
g.Eventually(func() bool {
271-
if err = c.Get(ctx, trialKey, trial); err != nil {
272-
t.Log(time.Since(start), err)
273-
return false
274-
}
275-
return trial.IsSucceeded() &&
276-
len(trial.Status.Observation.Metrics) > 0 &&
277-
trial.Status.Observation.Metrics[0].Min == "0.11" &&
278-
trial.Status.Observation.Metrics[0].Max == "0.99" &&
279-
trial.Status.Observation.Metrics[0].Latest == "0.11"
280-
}, timeout).Should(gomega.BeTrue())
260+
g.Eventually(func(g gomega.Gomega) {
261+
g.Expect(c.Get(ctx, trialKey, trial)).Should(gomega.Succeed())
262+
g.Expect(trial.IsSucceeded()).Should(gomega.BeTrue())
263+
g.Expect(trial.Status.Observation.Metrics).ShouldNot(gomega.HaveLen(0))
264+
g.Expect(trial.Status.Observation.Metrics[0]).Should(gomega.BeComparableTo(commonv1beta1.Metric{
265+
Name: objectiveMetric,
266+
Min: "0.11",
267+
Max: "0.99",
268+
Latest: "0.11",
269+
}))
270+
}, timeout).Should(gomega.Succeed())
281271

282272
// Delete the Trial
283273
g.Expect(c.Delete(ctx, trial)).NotTo(gomega.HaveOccurred())
284274

285275
// Expect that Trial is deleted
286-
g.Eventually(func() bool {
287-
return errors.IsNotFound(c.Get(ctx, trialKey, &trialsv1beta1.Trial{}))
288-
}, timeout).Should(gomega.BeTrue())
276+
g.Eventually(func(g gomega.Gomega) {
277+
g.Expect(errors.IsNotFound(c.Get(ctx, trialKey, &trialsv1beta1.Trial{}))).Should(gomega.BeTrue())
278+
}, timeout).Should(gomega.Succeed())
289279
})
290280

291281
t.Run(`Trial with "Complete" BatchJob and Unavailable metrics(StdOut MC).`, func(t *testing.T) {
@@ -317,9 +307,9 @@ func TestReconcileBatchJob(t *testing.T) {
317307
g.Expect(c.Delete(ctx, trial)).NotTo(gomega.HaveOccurred())
318308

319309
// Expect that Trial is deleted
320-
g.Eventually(func() bool {
321-
return errors.IsNotFound(c.Get(ctx, trialKey, &trialsv1beta1.Trial{}))
322-
}, timeout).Should(gomega.BeTrue())
310+
g.Eventually(func(g gomega.Gomega) {
311+
g.Expect(errors.IsNotFound(c.Get(ctx, trialKey, &trialsv1beta1.Trial{}))).Should(gomega.BeTrue())
312+
}, timeout).Should(gomega.Succeed())
323313
})
324314

325315
t.Run(`Trial with "Complete" BatchJob and Unavailable metrics(Push MC, failed once).`, func(t *testing.T) {
@@ -358,9 +348,9 @@ func TestReconcileBatchJob(t *testing.T) {
358348
g.Expect(c.Delete(ctx, trial)).NotTo(gomega.HaveOccurred())
359349

360350
// Expect that Trial is deleted
361-
g.Eventually(func() bool {
362-
return errors.IsNotFound(c.Get(ctx, trialKey, &trialsv1beta1.Trial{}))
363-
}, timeout).Should(gomega.BeTrue())
351+
g.Eventually(func(g gomega.Gomega) {
352+
g.Expect(errors.IsNotFound(c.Get(ctx, trialKey, &trialsv1beta1.Trial{}))).Should(gomega.BeTrue())
353+
}, timeout).Should(gomega.Succeed())
364354
})
365355

366356
t.Run("Update status for empty Trial", func(t *testing.T) {

0 commit comments

Comments
 (0)