Skip to content

Commit d3376c3

Browse files
Merge pull request #3202 from sebsoto/fixTestJobLogic
OCPBUGS-60771: Fix job success logic
2 parents f4500ad + b1082ea commit d3376c3

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

test/e2e/network_test.go

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os"
1111
"os/exec"
1212
"path/filepath"
13+
"slices"
1314
"strings"
1415
"testing"
1516
"time"
@@ -436,12 +437,12 @@ func (tc *testContext) collectDeploymentLogs(deployment *appsv1.Deployment) erro
436437
keyValPairs = append(keyValPairs, key+"="+value)
437438
}
438439
labelSelector := strings.Join(keyValPairs, ",")
439-
_, err := tc.gatherPodLogs(labelSelector)
440+
_, err := tc.gatherPodLogs(labelSelector, false)
440441
return err
441442
}
442443

443444
// getLogs uses a label selector and returns the logs associated with each pod
444-
func (tc *testContext) getLogs(podLabelSelector string) (string, error) {
445+
func (tc *testContext) getLogs(podLabelSelector string, latestOnly bool) (string, error) {
445446
if podLabelSelector == "" {
446447
return "", fmt.Errorf("pod label selector is empty")
447448
}
@@ -454,6 +455,12 @@ func (tc *testContext) getLogs(podLabelSelector string) (string, error) {
454455
return "", fmt.Errorf("expected at least 1 pod and found 0")
455456
}
456457
var logs string
458+
if latestOnly {
459+
latestPod := slices.MaxFunc(pods.Items, func(a, b v1.Pod) int {
460+
return a.Status.StartTime.Time.Compare(b.Status.StartTime.Time)
461+
})
462+
pods.Items = []v1.Pod{latestPod}
463+
}
457464
for _, pod := range pods.Items {
458465
logStream, err := tc.client.K8s.CoreV1().Pods(tc.workloadNamespace).GetLogs(pod.Name,
459466
&v1.PodLogOptions{}).Stream(context.TODO())
@@ -1120,25 +1127,28 @@ func (tc *testContext) waitUntilJobSucceeds(name string) (string, error) {
11201127
if err != nil {
11211128
return "", err
11221129
}
1130+
if !slices.ContainsFunc(job.Status.Conditions, func(condition batchv1.JobCondition) bool {
1131+
return condition.Type == batchv1.JobComplete && condition.Status == v1.ConditionTrue
1132+
}) {
1133+
// job not yet complete, keep waiting
1134+
time.Sleep(retryInterval)
1135+
continue
1136+
}
11231137
labelSelector = "job-name=" + job.Name
1124-
if job.Status.Succeeded > 0 {
1125-
logs, err := tc.gatherPodLogs(labelSelector)
1126-
if err != nil {
1127-
log.Printf("Unable to get logs associated with pod %s: %v", labelSelector, err)
1128-
}
1129-
return logs, nil
1138+
logs, err := tc.gatherPodLogs(labelSelector, true)
1139+
if err != nil {
1140+
log.Printf("Unable to get logs associated with pod %s: %v", labelSelector, err)
11301141
}
1131-
if job.Status.Failed > 0 {
1132-
_, err = tc.gatherPodLogs(labelSelector)
1133-
if err != nil {
1134-
log.Printf("Unable to get logs associated with pod %s: %v", labelSelector, err)
1135-
}
1142+
if !slices.ContainsFunc(job.Status.Conditions, func(condition batchv1.JobCondition) bool {
1143+
return condition.Type == batchv1.JobSuccessCriteriaMet && condition.Status == v1.ConditionTrue
1144+
}) {
1145+
// Job did not succeed, return error
11361146
events, _ := tc.getPodEvents(name)
1137-
return "", fmt.Errorf("job %v failed: %v", job, events)
1147+
return logs, fmt.Errorf("job %v failed: %v", job, events)
11381148
}
1139-
time.Sleep(retryInterval)
1149+
return logs, nil
11401150
}
1141-
_, err = tc.gatherPodLogs(labelSelector)
1151+
_, err = tc.gatherPodLogs(labelSelector, true)
11421152
if err != nil {
11431153
log.Printf("Unable to get logs associated with pod %s: %v", labelSelector, err)
11441154
}
@@ -1148,15 +1158,15 @@ func (tc *testContext) waitUntilJobSucceeds(name string) (string, error) {
11481158

11491159
// gatherPodLogs writes the logs associated with the label selector of a given pod job or deployment to the Artifacts
11501160
// dir. Returns the written logs.
1151-
func (tc *testContext) gatherPodLogs(labelSelector string) (string, error) {
1161+
func (tc *testContext) gatherPodLogs(labelSelector string, latestOnly bool) (string, error) {
11521162
podArtifacts := filepath.Join(os.Getenv("ARTIFACT_DIR"), "pods")
11531163
podDir := filepath.Join(podArtifacts, labelSelector)
11541164
err := os.MkdirAll(podDir, os.ModePerm)
11551165
if err != nil {
11561166
return "", fmt.Errorf("error creating pod log collection directory %s: %w", podDir, err)
11571167
}
11581168

1159-
logs, err := tc.getLogs(labelSelector)
1169+
logs, err := tc.getLogs(labelSelector, latestOnly)
11601170
if err != nil {
11611171
return "", fmt.Errorf("unable to get logs for pod %s: %w", labelSelector, err)
11621172
}

test/e2e/upgrade_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (tc *testContext) testParallelUpgradesChecker(t *testing.T) {
110110
failedPods, err := tc.client.K8s.CoreV1().Pods(tc.workloadNamespace).List(context.TODO(), metav1.ListOptions{
111111
LabelSelector: "job-name=" + parallelUpgradesCheckerJobName, FieldSelector: "status.phase=Failed"})
112112
require.NoError(t, err)
113-
_, err = tc.gatherPodLogs("job-name=" + parallelUpgradesCheckerJobName)
113+
_, err = tc.gatherPodLogs("job-name="+parallelUpgradesCheckerJobName, false)
114114
if err != nil {
115115
log.Printf("unable to gather logs: %v", err)
116116
}

0 commit comments

Comments
 (0)