Skip to content

Commit 1285e8e

Browse files
astefanuttiopenshift-merge-robot
authored andcommitted
test: Print MNIST batch job logs
1 parent 2361ef5 commit 1285e8e

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

test/e2e/mnist_pytorch_mcad_job_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ 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"
2829

2930
. "github.com/project-codeflare/codeflare-operator/test/support"
3031
mcadv1beta1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/apis/controller/v1beta1"
@@ -179,4 +180,16 @@ torchvision==0.12.0
179180

180181
test.Eventually(Job(test, namespace, job.Name), TestTimeoutLong).
181182
Should(WithTransform(ConditionStatus(batchv1.JobComplete), Equal(corev1.ConditionTrue)))
183+
184+
// Refresh the job to get the generated pod selector
185+
job = GetJob(test, namespace, job.Name)
186+
187+
// Get the job Pod
188+
pods := GetPods(test, namespace, metav1.ListOptions{
189+
LabelSelector: labels.FormatLabels(job.Spec.Selector.MatchLabels)},
190+
)
191+
test.Expect(pods).To(HaveLen(1))
192+
193+
// Print the job logs
194+
test.T().Log(GetPodLogs(test, &pods[0], corev1.PodLogOptions{}))
182195
}

test/support/batch.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,8 @@ func Job(t Test, namespace *corev1.Namespace, name string) func(g gomega.Gomega)
3131
return job
3232
}
3333
}
34+
35+
func GetJob(t Test, namespace *corev1.Namespace, name string) *batchv1.Job {
36+
t.T().Helper()
37+
return Job(t, namespace, name)(t)
38+
}

test/support/core.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ package support
1818

1919
import (
2020
"encoding/json"
21+
"io"
2122

2223
"github.com/onsi/gomega"
24+
25+
corev1 "k8s.io/api/core/v1"
26+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2327
"k8s.io/apimachinery/pkg/runtime"
2428
)
2529

@@ -31,3 +35,25 @@ func Raw(t Test, obj runtime.Object) runtime.RawExtension {
3135
Raw: data,
3236
}
3337
}
38+
39+
func GetPods(t Test, namespace *corev1.Namespace, options metav1.ListOptions) []corev1.Pod {
40+
t.T().Helper()
41+
pods, err := t.Client().Core().CoreV1().Pods(namespace.Name).List(t.Ctx(), options)
42+
t.Expect(err).NotTo(gomega.HaveOccurred())
43+
return pods.Items
44+
}
45+
46+
func GetPodLogs(t Test, pod *corev1.Pod, options corev1.PodLogOptions) string {
47+
t.T().Helper()
48+
stream, err := t.Client().Core().CoreV1().Pods(pod.GetNamespace()).GetLogs(pod.GetName(), &options).Stream(t.Ctx())
49+
t.Expect(err).NotTo(gomega.HaveOccurred())
50+
51+
defer func() {
52+
t.Expect(stream.Close()).To(gomega.Succeed())
53+
}()
54+
55+
bytes, err := io.ReadAll(stream)
56+
t.Expect(err).NotTo(gomega.HaveOccurred())
57+
58+
return string(bytes)
59+
}

0 commit comments

Comments
 (0)