2021-01-13 01:27:41 +00:00
|
|
|
// Copyright 2020-2021 the Pinniped contributors. All Rights Reserved.
|
2020-12-03 15:34:46 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package library
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
2021-03-17 16:24:59 +00:00
|
|
|
"fmt"
|
2020-12-03 15:34:46 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-03-17 16:24:59 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-12-03 15:34:46 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2021-03-17 16:24:59 +00:00
|
|
|
"k8s.io/client-go/kubernetes"
|
2020-12-03 15:34:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DumpLogs is meant to be called in a `defer` to dump the logs of components in the cluster on a test failure.
|
2020-12-03 17:28:48 +00:00
|
|
|
func DumpLogs(t *testing.T, namespace string, labelSelector string) {
|
2020-12-03 15:34:46 +00:00
|
|
|
// Only trigger on failed tests.
|
|
|
|
if !t.Failed() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-13 01:27:41 +00:00
|
|
|
kubeClient := NewKubernetesClientset(t)
|
2020-12-03 15:34:46 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-12-03 17:28:48 +00:00
|
|
|
pods, err := kubeClient.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{LabelSelector: labelSelector})
|
2020-12-03 15:34:46 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
for _, pod := range pods.Items {
|
|
|
|
for _, container := range pod.Status.ContainerStatuses {
|
2021-03-17 16:24:59 +00:00
|
|
|
if container.RestartCount > 0 {
|
|
|
|
dumpContainerLogs(ctx, t, kubeClient, pod.Namespace, pod.Name, container.Name, true)
|
2020-12-03 15:34:46 +00:00
|
|
|
}
|
2021-03-17 16:24:59 +00:00
|
|
|
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)"
|
2020-12-03 15:34:46 +00:00
|
|
|
}
|
2021-03-17 16:24:59 +00:00
|
|
|
t.Logf("%s > %s", prefix, scanner.Text())
|
2020-12-03 15:34:46 +00:00
|
|
|
}
|
2021-03-17 16:24:59 +00:00
|
|
|
assert.NoError(t, scanner.Err(), "failed to read logs from container %s", shortName)
|
2020-12-03 15:34:46 +00:00
|
|
|
}
|