Extend library.DumpLogs() to dump logs from the previous container, if one exists.
This is important in case the container has crashed and has been restarted. Signed-off-by: Matt Moyer <moyerm@vmware.com>
This commit is contained in:
parent
5a43a5d53a
commit
6520c5a3a1
@ -6,12 +6,15 @@ package library
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/client-go/kubernetes"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DumpLogs is meant to be called in a `defer` to dump the logs of components in the cluster on a test failure.
|
// DumpLogs is meant to be called in a `defer` to dump the logs of components in the cluster on a test failure.
|
||||||
@ -25,25 +28,37 @@ func DumpLogs(t *testing.T, namespace string, labelSelector string) {
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
logTailLines := int64(40)
|
|
||||||
pods, err := kubeClient.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{LabelSelector: labelSelector})
|
pods, err := kubeClient.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{LabelSelector: labelSelector})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
for _, pod := range pods.Items {
|
for _, pod := range pods.Items {
|
||||||
for _, container := range pod.Status.ContainerStatuses {
|
for _, container := range pod.Status.ContainerStatuses {
|
||||||
t.Logf("pod %s/%s container %s restarted %d times:", pod.Namespace, pod.Name, container.Name, container.RestartCount)
|
if container.RestartCount > 0 {
|
||||||
req := kubeClient.CoreV1().Pods(namespace).GetLogs(pod.Name, &corev1.PodLogOptions{
|
dumpContainerLogs(ctx, t, kubeClient, pod.Namespace, pod.Name, container.Name, true)
|
||||||
Container: container.Name,
|
|
||||||
TailLines: &logTailLines,
|
|
||||||
})
|
|
||||||
logReader, err := req.Stream(ctx)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
scanner := bufio.NewScanner(logReader)
|
|
||||||
for scanner.Scan() {
|
|
||||||
t.Logf("%s/%s/%s > %s", pod.Namespace, pod.Name, container.Name, scanner.Text())
|
|
||||||
}
|
}
|
||||||
require.NoError(t, scanner.Err())
|
dumpContainerLogs(ctx, t, kubeClient, pod.Namespace, pod.Name, container.Name, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func dumpContainerLogs(ctx context.Context, t *testing.T, kubeClient kubernetes.Interface, namespace, pod, container string, prev bool) {
|
||||||
|
logTailLines := int64(40)
|
||||||
|
shortName := fmt.Sprintf("%s/%s/%s", namespace, pod, container)
|
||||||
|
logReader, err := kubeClient.CoreV1().Pods(namespace).GetLogs(pod, &corev1.PodLogOptions{
|
||||||
|
Container: container,
|
||||||
|
TailLines: &logTailLines,
|
||||||
|
Previous: prev,
|
||||||
|
}).Stream(ctx)
|
||||||
|
if !assert.NoErrorf(t, err, "failed to stream logs for container %s", shortName) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
scanner := bufio.NewScanner(logReader)
|
||||||
|
for scanner.Scan() {
|
||||||
|
prefix := shortName
|
||||||
|
if prev {
|
||||||
|
prefix += " (previous)"
|
||||||
|
}
|
||||||
|
t.Logf("%s > %s", prefix, scanner.Text())
|
||||||
|
}
|
||||||
|
assert.NoError(t, scanner.Err(), "failed to read logs from container %s", shortName)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user