Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 28 additions & 18 deletions test/e2e/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -436,12 +437,12 @@ func (tc *testContext) collectDeploymentLogs(deployment *appsv1.Deployment) erro
keyValPairs = append(keyValPairs, key+"="+value)
}
labelSelector := strings.Join(keyValPairs, ",")
_, err := tc.gatherPodLogs(labelSelector)
_, err := tc.gatherPodLogs(labelSelector, false)
return err
}

// getLogs uses a label selector and returns the logs associated with each pod
func (tc *testContext) getLogs(podLabelSelector string) (string, error) {
func (tc *testContext) getLogs(podLabelSelector string, latestOnly bool) (string, error) {
if podLabelSelector == "" {
return "", fmt.Errorf("pod label selector is empty")
}
Expand All @@ -454,6 +455,12 @@ func (tc *testContext) getLogs(podLabelSelector string) (string, error) {
return "", fmt.Errorf("expected at least 1 pod and found 0")
}
var logs string
if latestOnly {
latestPod := slices.MaxFunc(pods.Items, func(a, b v1.Pod) int {
return a.Status.StartTime.Time.Compare(b.Status.StartTime.Time)
})
pods.Items = []v1.Pod{latestPod}
}
for _, pod := range pods.Items {
logStream, err := tc.client.K8s.CoreV1().Pods(tc.workloadNamespace).GetLogs(pod.Name,
&v1.PodLogOptions{}).Stream(context.TODO())
Expand Down Expand Up @@ -1120,25 +1127,28 @@ func (tc *testContext) waitUntilJobSucceeds(name string) (string, error) {
if err != nil {
return "", err
}
if !slices.ContainsFunc(job.Status.Conditions, func(condition batchv1.JobCondition) bool {
return condition.Type == batchv1.JobComplete && condition.Status == v1.ConditionTrue
}) {
// job not yet complete, keep waiting
time.Sleep(retryInterval)
continue
}
labelSelector = "job-name=" + job.Name
if job.Status.Succeeded > 0 {
logs, err := tc.gatherPodLogs(labelSelector)
if err != nil {
log.Printf("Unable to get logs associated with pod %s: %v", labelSelector, err)
}
return logs, nil
logs, err := tc.gatherPodLogs(labelSelector, true)
if err != nil {
log.Printf("Unable to get logs associated with pod %s: %v", labelSelector, err)
}
if job.Status.Failed > 0 {
_, err = tc.gatherPodLogs(labelSelector)
if err != nil {
log.Printf("Unable to get logs associated with pod %s: %v", labelSelector, err)
}
if !slices.ContainsFunc(job.Status.Conditions, func(condition batchv1.JobCondition) bool {
return condition.Type == batchv1.JobSuccessCriteriaMet && condition.Status == v1.ConditionTrue
}) {
// Job did not succeed, return error
events, _ := tc.getPodEvents(name)
return "", fmt.Errorf("job %v failed: %v", job, events)
return logs, fmt.Errorf("job %v failed: %v", job, events)
}
time.Sleep(retryInterval)
return logs, nil
}
_, err = tc.gatherPodLogs(labelSelector)
_, err = tc.gatherPodLogs(labelSelector, true)
if err != nil {
log.Printf("Unable to get logs associated with pod %s: %v", labelSelector, err)
}
Expand All @@ -1148,15 +1158,15 @@ func (tc *testContext) waitUntilJobSucceeds(name string) (string, error) {

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

logs, err := tc.getLogs(labelSelector)
logs, err := tc.getLogs(labelSelector, latestOnly)
if err != nil {
return "", fmt.Errorf("unable to get logs for pod %s: %w", labelSelector, err)
}
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (tc *testContext) testParallelUpgradesChecker(t *testing.T) {
failedPods, err := tc.client.K8s.CoreV1().Pods(tc.workloadNamespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: "job-name=" + parallelUpgradesCheckerJobName, FieldSelector: "status.phase=Failed"})
require.NoError(t, err)
_, err = tc.gatherPodLogs("job-name=" + parallelUpgradesCheckerJobName)
_, err = tc.gatherPodLogs("job-name="+parallelUpgradesCheckerJobName, false)
if err != nil {
log.Printf("unable to gather logs: %v", err)
}
Expand Down