Skip to content

Commit 674a05d

Browse files
fix(test): move the failed job test to the end.
Signed-off-by: Electronic-Waste <[email protected]>
1 parent 162a333 commit 674a05d

File tree

1 file changed

+72
-64
lines changed

1 file changed

+72
-64
lines changed

pkg/controller.v1beta1/trial/trial_controller_test.go

+72-64
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ const (
5656
)
5757

5858
var (
59+
startTime = time.Now()
5960
batchJobKey = types.NamespacedName{Name: batchJobName, Namespace: namespace}
6061
observationLogAvailable = &api_pb.GetObservationLogReply{
6162
ObservationLog: &api_pb.ObservationLog{
@@ -180,67 +181,6 @@ func TestReconcileBatchJob(t *testing.T) {
180181
g.Expect(mgr.Start(mgrCtx)).NotTo(gomega.HaveOccurred())
181182
}()
182183

183-
t.Run(`Trial run with "Failed" BatchJob.`, func(t *testing.T) {
184-
g := gomega.NewGomegaWithT(t)
185-
mockManagerClient.EXPECT().DeleteTrialObservationLog(gomock.Any()).Return(nil, nil)
186-
187-
trial := newFakeTrialBatchJob(commonv1beta1.StdOutCollector, "test-failed-batch-job")
188-
trialKey := types.NamespacedName{Name: "test-failed-batch-job", Namespace: namespace}
189-
batchJob := &batchv1.Job{}
190-
191-
// Create the Trial with StdOut MC
192-
g.Expect(c.Create(ctx, trial)).NotTo(gomega.HaveOccurred())
193-
194-
// Expect that BatchJob with appropriate name is created
195-
g.Eventually(func(g gomega.Gomega) {
196-
g.Expect(c.Get(ctx, batchJobKey, batchJob)).Should(gomega.Succeed())
197-
}, timeout).Should(gomega.Succeed())
198-
199-
// Expect that Trial status is running
200-
g.Eventually(func(g gomega.Gomega) {
201-
g.Expect(c.Get(ctx, trialKey, trial)).Should(gomega.Succeed())
202-
g.Expect(trial.IsRunning()).Should(gomega.BeTrue())
203-
}, timeout).Should(gomega.Succeed())
204-
205-
// Manually update BatchJob status to failed
206-
// Expect that Trial status is failed
207-
g.Eventually(func(g gomega.Gomega) {
208-
g.Expect(c.Get(ctx, batchJobKey, batchJob)).Should(gomega.Succeed())
209-
batchJob.Status = batchv1.JobStatus{
210-
Conditions: []batchv1.JobCondition{
211-
{
212-
Type: batchv1.JobFailureTarget,
213-
Status: corev1.ConditionTrue,
214-
Message: "BatchJob failed test message",
215-
Reason: "BatchJob failed test reason",
216-
},
217-
{
218-
Type: batchv1.JobFailed,
219-
Status: corev1.ConditionTrue,
220-
Message: "BatchJob failed test message",
221-
Reason: "BatchJob failed test reason",
222-
},
223-
},
224-
StartTime: &metav1.Time{Time: time.Now()},
225-
}
226-
g.Expect(c.Status().Update(ctx, batchJob)).Should(gomega.Succeed())
227-
g.Expect(c.Get(ctx, trialKey, trial)).Should(gomega.Succeed())
228-
g.Expect(trial.IsFailed()).Should(gomega.BeTrue())
229-
}, timeout).Should(gomega.Succeed())
230-
231-
// Delete the Trial and Job
232-
g.Expect(c.Delete(ctx, trial)).NotTo(gomega.HaveOccurred())
233-
g.Expect(c.Delete(ctx, batchJob)).NotTo(gomega.HaveOccurred())
234-
235-
// Expect that Trial is deleted
236-
// BatchJob can't be deleted because GC doesn't work in envtest and BatchJob stuck in termination phase.
237-
// Ref: https://book.kubebuilder.io/reference/testing/envtest.html#testing-considerations.
238-
g.Eventually(func(g gomega.Gomega) {
239-
g.Expect(errors.IsNotFound(c.Get(ctx, trialKey, &trialsv1beta1.Trial{}))).Should(gomega.BeTrue())
240-
g.Expect(errors.IsNotFound(c.Get(ctx, batchJobKey, &batchv1.Job{}))).Should(gomega.BeTrue())
241-
}, timeout).Should(gomega.Succeed())
242-
})
243-
244184
t.Run(`Trial with "Complete" BatchJob and Available metrics.`, func(t *testing.T) {
245185
g := gomega.NewGomegaWithT(t)
246186
gomock.InOrder(
@@ -258,7 +198,7 @@ func TestReconcileBatchJob(t *testing.T) {
258198
g.Eventually(func(g gomega.Gomega) {
259199
g.Expect(c.Get(ctx, batchJobKey, batchJob)).Should(gomega.Succeed())
260200
}, timeout).Should(gomega.Succeed())
261-
201+
262202
// Update BatchJob status to Complete.
263203
batchJobCompleteMessage := "BatchJob completed test message"
264204
batchJobCompleteReason := "BatchJob completed test reason"
@@ -269,7 +209,7 @@ func TestReconcileBatchJob(t *testing.T) {
269209
Type: batchv1.JobSuccessCriteriaMet,
270210
Status: corev1.ConditionTrue,
271211
Message: batchJobCompleteMessage,
272-
Reason: batchv1.JobReasonSuccessPolicy,
212+
Reason: batchJobCompleteReason,
273213
},
274214
{
275215
Type: batchv1.JobComplete,
@@ -278,7 +218,7 @@ func TestReconcileBatchJob(t *testing.T) {
278218
Reason: batchJobCompleteReason,
279219
},
280220
},
281-
StartTime: &metav1.Time{Time: time.Now()},
221+
StartTime: &metav1.Time{Time: startTime},
282222
}
283223
g.Expect(c.Status().Update(ctx, batchJob)).NotTo(gomega.HaveOccurred())
284224

@@ -380,10 +320,78 @@ func TestReconcileBatchJob(t *testing.T) {
380320
}, timeout).Should(gomega.Succeed())
381321
})
382322

323+
t.Run(`Trial run with "Failed" BatchJob.`, func(t *testing.T) {
324+
g := gomega.NewGomegaWithT(t)
325+
mockManagerClient.EXPECT().DeleteTrialObservationLog(gomock.Any()).Return(nil, nil)
326+
327+
trial := newFakeTrialBatchJob(commonv1beta1.StdOutCollector, "test-failed-batch-job")
328+
trialKey := types.NamespacedName{Name: "test-failed-batch-job", Namespace: namespace}
329+
batchJob := &batchv1.Job{}
330+
331+
// Create the Trial with StdOut MC
332+
g.Expect(c.Create(ctx, trial)).NotTo(gomega.HaveOccurred())
333+
334+
// Expect that BatchJob with appropriate name is created
335+
g.Eventually(func(g gomega.Gomega) {
336+
g.Expect(c.Get(ctx, batchJobKey, batchJob)).Should(gomega.Succeed())
337+
}, timeout).Should(gomega.Succeed())
338+
339+
// Expect that Trial status is running
340+
g.Eventually(func(g gomega.Gomega) {
341+
g.Expect(c.Get(ctx, trialKey, trial)).Should(gomega.Succeed())
342+
g.Expect(trial.IsRunning()).Should(gomega.BeTrue())
343+
}, timeout).Should(gomega.Succeed())
344+
345+
// Manually update BatchJob status to failed
346+
// Expect that Trial status is failed
347+
batchJobFailedMessage := "BatchJob completed test message"
348+
batchJobFailedReason := "BatchJob completed test reason"
349+
g.Eventually(func(g gomega.Gomega) {
350+
g.Expect(c.Get(ctx, batchJobKey, batchJob)).Should(gomega.Succeed())
351+
batchJob.Status = batchv1.JobStatus{
352+
Conditions: []batchv1.JobCondition{
353+
{
354+
Type: batchv1.JobFailureTarget,
355+
Status: corev1.ConditionTrue,
356+
Message: batchJobFailedMessage,
357+
Reason: batchJobFailedReason,
358+
},
359+
{
360+
Type: batchv1.JobFailed,
361+
Status: corev1.ConditionTrue,
362+
Message: batchJobFailedMessage,
363+
Reason: batchJobFailedReason,
364+
},
365+
{
366+
Type: batchv1.JobSuccessCriteriaMet,
367+
Status: corev1.ConditionFalse,
368+
Message: batchJobFailedMessage,
369+
Reason: batchJobFailedReason,
370+
},
371+
},
372+
StartTime: &metav1.Time{Time: startTime},
373+
}
374+
g.Expect(c.Status().Update(ctx, batchJob)).Should(gomega.Succeed())
375+
g.Expect(c.Get(ctx, trialKey, trial)).Should(gomega.Succeed())
376+
g.Expect(trial.IsFailed()).Should(gomega.BeTrue())
377+
}, timeout).Should(gomega.Succeed())
378+
379+
// Delete the Trial
380+
g.Expect(c.Delete(ctx, trial)).NotTo(gomega.HaveOccurred())
381+
382+
// Expect that Trial is deleted
383+
// BatchJob can't be deleted because GC doesn't work in envtest and BatchJob stuck in termination phase.
384+
// Ref: https://book.kubebuilder.io/reference/testing/envtest.html#testing-considerations.
385+
g.Eventually(func(g gomega.Gomega) {
386+
g.Expect(errors.IsNotFound(c.Get(ctx, trialKey, &trialsv1beta1.Trial{}))).Should(gomega.BeTrue())
387+
}, timeout).Should(gomega.Succeed())
388+
})
389+
383390
t.Run("Update status for empty Trial", func(t *testing.T) {
384391
g := gomega.NewGomegaWithT(t)
385392
g.Expect(r.updateStatus(&trialsv1beta1.Trial{})).To(gomega.HaveOccurred())
386393
})
394+
387395
}
388396

389397
func TestGetObjectiveMetricValue(t *testing.T) {

0 commit comments

Comments
 (0)