Skip to content

Commit 69230b5

Browse files
fix(test): seperate job name between failed job and succeeded job.
Signed-off-by: Electronic-Waste <[email protected]>
1 parent 1bc7d44 commit 69230b5

File tree

1 file changed

+75
-79
lines changed

1 file changed

+75
-79
lines changed

pkg/controller.v1beta1/trial/trial_controller_test.go

+75-79
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,18 @@ import (
4949
)
5050

5151
const (
52-
namespace = "default"
53-
batchJobName = "test-job"
54-
objectiveMetric = "accuracy"
55-
timeout = time.Second * 10
52+
namespace = "default"
53+
succeededBatchJobName = "test-job-succeeded"
54+
failedBatchJobName = "test-job-failed"
55+
objectiveMetric = "accuracy"
56+
timeout = time.Second * 10
5657
)
5758

5859
var (
5960
startTime = time.Now()
6061
completionTime = time.Now().Add(time.Second)
61-
batchJobKey = types.NamespacedName{Name: batchJobName, Namespace: namespace}
62+
succeededBatchJobKey = types.NamespacedName{Name: succeededBatchJobName, Namespace: namespace}
63+
failedBatchJobKey = types.NamespacedName{Name: failedBatchJobName, Namespace: namespace}
6264
observationLogAvailable = &api_pb.GetObservationLogReply{
6365
ObservationLog: &api_pb.ObservationLog{
6466
MetricLogs: []*api_pb.MetricLog{
@@ -182,6 +184,67 @@ func TestReconcileBatchJob(t *testing.T) {
182184
g.Expect(mgr.Start(mgrCtx)).NotTo(gomega.HaveOccurred())
183185
}()
184186

187+
t.Run(`Trial run with "Failed" BatchJob.`, func(t *testing.T) {
188+
g := gomega.NewGomegaWithT(t)
189+
mockManagerClient.EXPECT().DeleteTrialObservationLog(gomock.Any()).Return(nil, nil)
190+
191+
trial := newFakeTrialBatchJob(commonv1beta1.StdOutCollector, "test-failed-batch-job", failedBatchJobName)
192+
trialKey := types.NamespacedName{Name: "test-failed-batch-job", Namespace: namespace}
193+
batchJob := &batchv1.Job{}
194+
195+
// Create the Trial with StdOut MC
196+
g.Expect(c.Create(ctx, trial)).NotTo(gomega.HaveOccurred())
197+
198+
// Expect that BatchJob with appropriate name is created
199+
g.Eventually(func(g gomega.Gomega) {
200+
g.Expect(c.Get(ctx, failedBatchJobKey, batchJob)).Should(gomega.Succeed())
201+
}, timeout).Should(gomega.Succeed())
202+
203+
// Expect that Trial status is running
204+
g.Eventually(func(g gomega.Gomega) {
205+
g.Expect(c.Get(ctx, trialKey, trial)).Should(gomega.Succeed())
206+
g.Expect(trial.IsRunning()).Should(gomega.BeTrue())
207+
}, timeout).Should(gomega.Succeed())
208+
209+
// Manually update BatchJob status to failed
210+
// Expect that Trial status is failed
211+
batchJobFailedMessage := "BatchJob completed test message"
212+
batchJobFailedReason := "BatchJob completed test reason"
213+
g.Eventually(func(g gomega.Gomega) {
214+
g.Expect(c.Get(ctx, failedBatchJobKey, batchJob)).Should(gomega.Succeed())
215+
batchJob.Status = batchv1.JobStatus{
216+
Conditions: []batchv1.JobCondition{
217+
{
218+
Type: batchv1.JobFailureTarget,
219+
Status: corev1.ConditionTrue,
220+
Message: batchJobFailedMessage,
221+
Reason: batchJobFailedReason,
222+
},
223+
{
224+
Type: batchv1.JobFailed,
225+
Status: corev1.ConditionTrue,
226+
Message: batchJobFailedMessage,
227+
Reason: batchJobFailedReason,
228+
},
229+
},
230+
StartTime: &metav1.Time{Time: startTime},
231+
}
232+
g.Expect(c.Status().Update(ctx, batchJob)).Should(gomega.Succeed())
233+
g.Expect(c.Get(ctx, trialKey, trial)).Should(gomega.Succeed())
234+
g.Expect(trial.IsFailed()).Should(gomega.BeTrue())
235+
}, timeout).Should(gomega.Succeed())
236+
237+
// Delete the Trial
238+
g.Expect(c.Delete(ctx, trial)).NotTo(gomega.HaveOccurred())
239+
240+
// Expect that Trial is deleted
241+
// BatchJob can't be deleted because GC doesn't work in envtest and BatchJob stuck in termination phase.
242+
// Ref: https://book.kubebuilder.io/reference/testing/envtest.html#testing-considerations.
243+
g.Eventually(func(g gomega.Gomega) {
244+
g.Expect(errors.IsNotFound(c.Get(ctx, trialKey, &trialsv1beta1.Trial{}))).Should(gomega.BeTrue())
245+
}, timeout).Should(gomega.Succeed())
246+
})
247+
185248
t.Run(`Trial with "Complete" BatchJob and Available metrics.`, func(t *testing.T) {
186249
g := gomega.NewGomegaWithT(t)
187250
gomock.InOrder(
@@ -190,20 +253,20 @@ func TestReconcileBatchJob(t *testing.T) {
190253
)
191254

192255
// Create the Trial with StdOut MC
193-
trial := newFakeTrialBatchJob(commonv1beta1.StdOutCollector, "test-available-stdout")
256+
trial := newFakeTrialBatchJob(commonv1beta1.StdOutCollector, "test-available-stdout", succeededBatchJobName)
194257
trialKey := types.NamespacedName{Name: "test-available-stdout", Namespace: namespace}
195258
batchJob := &batchv1.Job{}
196259
g.Expect(c.Create(ctx, trial)).NotTo(gomega.HaveOccurred())
197260

198261
// Expect that BatchJob with appropriate name is created
199262
g.Eventually(func(g gomega.Gomega) {
200-
g.Expect(c.Get(ctx, batchJobKey, batchJob)).Should(gomega.Succeed())
263+
g.Expect(c.Get(ctx, succeededBatchJobKey, batchJob)).Should(gomega.Succeed())
201264
}, timeout).Should(gomega.Succeed())
202265

203266
// Update BatchJob status to Complete.
204267
batchJobCompleteMessage := "BatchJob completed test message"
205268
batchJobCompleteReason := "BatchJob completed test reason"
206-
g.Expect(c.Get(ctx, batchJobKey, batchJob)).NotTo(gomega.HaveOccurred())
269+
g.Expect(c.Get(ctx, succeededBatchJobKey, batchJob)).NotTo(gomega.HaveOccurred())
207270
batchJob.Status = batchv1.JobStatus{
208271
Conditions: []batchv1.JobCondition{
209272
{
@@ -254,7 +317,7 @@ func TestReconcileBatchJob(t *testing.T) {
254317
mockManagerClient.EXPECT().DeleteTrialObservationLog(gomock.Any()).Return(nil, nil),
255318
)
256319
// Create the Trial with StdOut MC
257-
trial := newFakeTrialBatchJob(commonv1beta1.StdOutCollector, "test-unavailable-stdout")
320+
trial := newFakeTrialBatchJob(commonv1beta1.StdOutCollector, "test-unavailable-stdout", succeededBatchJobName)
258321
trialKey := types.NamespacedName{Name: "test-unavailable-stdout", Namespace: namespace}
259322
g.Expect(c.Create(ctx, trial)).NotTo(gomega.HaveOccurred())
260323

@@ -295,7 +358,7 @@ func TestReconcileBatchJob(t *testing.T) {
295358
mockManagerClient.EXPECT().ReportTrialObservationLog(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
296359

297360
// Create the Trial with Push MC
298-
trial := newFakeTrialBatchJob(commonv1beta1.PushCollector, "test-unavailable-push-failed-once")
361+
trial := newFakeTrialBatchJob(commonv1beta1.PushCollector, "test-unavailable-push-failed-once", succeededBatchJobName)
299362
trialKey := types.NamespacedName{Name: "test-unavailable-push-failed-once", Namespace: namespace}
300363
g.Expect(c.Create(ctx, trial)).NotTo(gomega.HaveOccurred())
301364

@@ -322,73 +385,6 @@ func TestReconcileBatchJob(t *testing.T) {
322385
}, timeout).Should(gomega.Succeed())
323386
})
324387

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

457-
func newFakeTrialBatchJob(mcType commonv1beta1.CollectorKind, trialName string) *trialsv1beta1.Trial {
453+
func newFakeTrialBatchJob(mcType commonv1beta1.CollectorKind, trialName, jobName string) *trialsv1beta1.Trial {
458454
primaryContainer := "training-container"
459455

460456
job := &batchv1.Job{
@@ -463,7 +459,7 @@ func newFakeTrialBatchJob(mcType commonv1beta1.CollectorKind, trialName string)
463459
Kind: "Job",
464460
},
465461
ObjectMeta: metav1.ObjectMeta{
466-
Name: batchJobName,
462+
Name: jobName,
467463
Namespace: namespace,
468464
},
469465
Spec: batchv1.JobSpec{

0 commit comments

Comments
 (0)