Skip to content

Commit 12d106d

Browse files
committed
test: Print Job logs after successfull or failed completion
1 parent 9a96e45 commit 12d106d

File tree

3 files changed

+21
-43
lines changed

3 files changed

+21
-43
lines changed

test/e2e/mnist_pytorch_mcad_job_test.go

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
corev1 "k8s.io/api/core/v1"
2626
"k8s.io/apimachinery/pkg/api/resource"
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28-
"k8s.io/apimachinery/pkg/labels"
2928

3029
. "github.com/project-codeflare/codeflare-operator/test/support"
3130
mcadv1beta1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/apis/controller/v1beta1"
@@ -179,22 +178,17 @@ torchvision==0.12.0
179178
test.Eventually(AppWrapper(test, namespace, aw.Name), TestTimeoutMedium).
180179
Should(WithTransform(AppWrapperState, Equal(mcadv1beta1.AppWrapperStateActive)))
181180

182-
defer JobTroubleshooting(test, job)
183-
184-
test.T().Logf("Waiting for Job %s/%s to complete successfully", job.Namespace, job.Name)
185-
test.Eventually(Job(test, job.Namespace, job.Name), TestTimeoutLong).
186-
Should(WithTransform(ConditionStatus(batchv1.JobComplete), Equal(corev1.ConditionTrue)))
187-
188-
// Refresh the job to get the generated pod selector
189-
job = GetJob(test, job.Namespace, job.Name)
190-
191-
// Get the job Pod
192-
pods := GetPods(test, job.Namespace, metav1.ListOptions{
193-
LabelSelector: labels.FormatLabels(job.Spec.Selector.MatchLabels)},
194-
)
195-
test.Expect(pods).To(HaveLen(1))
181+
test.T().Logf("Waiting for Job %s/%s to complete", job.Namespace, job.Name)
182+
test.Eventually(Job(test, job.Namespace, job.Name), TestTimeoutLong).Should(
183+
Or(
184+
WithTransform(ConditionStatus(batchv1.JobComplete), Equal(corev1.ConditionTrue)),
185+
WithTransform(ConditionStatus(batchv1.JobFailed), Equal(corev1.ConditionTrue)),
186+
))
196187

197188
// Print the job logs
198-
test.T().Logf("Printing Job %s/%s logs", job.Namespace, job.Name)
199-
test.T().Log(GetPodLogs(test, &pods[0], corev1.PodLogOptions{}))
189+
PrintJobLogs(test, job.Namespace, job.Name)
190+
191+
// Assert the job has completed successfully
192+
test.Expect(GetJob(test, job.Namespace, job.Name)).
193+
To(WithTransform(ConditionStatus(batchv1.JobComplete), Equal(corev1.ConditionTrue)))
200194
}

test/e2e/mnist_raycluster_sdk_test.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
corev1 "k8s.io/api/core/v1"
2626
rbacv1 "k8s.io/api/rbac/v1"
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28-
"k8s.io/apimachinery/pkg/labels"
2928

3029
rayv1alpha1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1alpha1"
3130

@@ -187,29 +186,17 @@ func TestMNISTRayClusterSDK(t *testing.T) {
187186
test.Expect(err).NotTo(HaveOccurred())
188187
test.T().Logf("Created Job %s/%s successfully", job.Namespace, job.Name)
189188

190-
defer JobTroubleshooting(test, job)
191-
192189
test.T().Logf("Waiting for Job %s/%s to complete", job.Namespace, job.Name)
193190
test.Eventually(Job(test, job.Namespace, job.Name), TestTimeoutLong).Should(
194191
Or(
195192
WithTransform(ConditionStatus(batchv1.JobComplete), Equal(corev1.ConditionTrue)),
196193
WithTransform(ConditionStatus(batchv1.JobFailed), Equal(corev1.ConditionTrue)),
197194
))
198195

199-
// Refresh the job to get the generated pod selector
200-
job = GetJob(test, job.Namespace, job.Name)
201-
202-
// Get the job Pod
203-
pods := GetPods(test, job.Namespace, metav1.ListOptions{
204-
LabelSelector: labels.FormatLabels(job.Spec.Selector.MatchLabels)},
205-
)
206-
test.Expect(pods).To(HaveLen(1))
207-
208196
// Print the job logs
209-
test.T().Logf("Printing Job %s/%s logs", job.Namespace, job.Name)
210-
test.T().Log(GetPodLogs(test, &pods[0], corev1.PodLogOptions{}))
197+
PrintJobLogs(test, job.Namespace, job.Name)
211198

212199
// Assert the job has completed successfully
213-
test.T().Logf("Checking the Job %s/%s has completed successfully", job.Namespace, job.Name)
214-
test.Expect(job).To(WithTransform(ConditionStatus(batchv1.JobComplete), Equal(corev1.ConditionTrue)))
200+
test.Expect(GetJob(test, job.Namespace, job.Name)).
201+
To(WithTransform(ConditionStatus(batchv1.JobComplete), Equal(corev1.ConditionTrue)))
215202
}

test/support/batch.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,21 @@ func GetJob(t Test, namespace, name string) *batchv1.Job {
3838
return Job(t, namespace, name)(t)
3939
}
4040

41-
func JobTroubleshooting(test Test, job *batchv1.Job) {
42-
if !test.T().Failed() {
43-
return
44-
}
45-
job = GetJob(test, job.Namespace, job.Name)
41+
func PrintJobLogs(t Test, namespace, name string) {
42+
t.T().Helper()
4643

47-
test.T().Errorf("Job %s/%s hasn't completed in time: %s", job.Namespace, job.Name, job)
44+
job := GetJob(t, namespace, name)
4845

49-
pods := GetPods(test, job.Namespace, metav1.ListOptions{
46+
pods := GetPods(t, job.Namespace, metav1.ListOptions{
5047
LabelSelector: labels.FormatLabels(job.Spec.Selector.MatchLabels)},
5148
)
5249

5350
if len(pods) == 0 {
54-
test.T().Errorf("Job %s/%s has no pods scheduled", job.Namespace, job.Name)
51+
t.T().Errorf("Job %s/%s has no pods scheduled", job.Namespace, job.Name)
5552
} else {
5653
for i, pod := range pods {
57-
test.T().Logf("Printing Pod %s/%s logs", pod.Namespace, pod.Name)
58-
test.T().Log(GetPodLogs(test, &pods[i], corev1.PodLogOptions{}))
54+
t.T().Logf("Printing Pod %s/%s logs", pod.Namespace, pod.Name)
55+
t.T().Log(GetPodLogs(t, &pods[i], corev1.PodLogOptions{}))
5956
}
6057
}
6158
}

0 commit comments

Comments
 (0)