From defad3cdd7bec41373c7fd0fe037ce83dc1a92b7 Mon Sep 17 00:00:00 2001 From: Matt Moyer Date: Fri, 26 Mar 2021 16:43:02 -0500 Subject: [PATCH] Remove library.DumpLogs test helper. We had this code that printed out pod logs when certain tests failed, but it is a bit cumbersome. We're removing it because we added a CI task that exports all pod logs after every CI run, which accomplishes the same thing and provides us a bunch more data. Signed-off-by: Matt Moyer --- test/integration/e2e_test.go | 4 -- test/integration/supervisor_login_test.go | 4 -- test/library/assertions.go | 7 +-- test/library/dumplogs.go | 64 ----------------------- 4 files changed, 2 insertions(+), 77 deletions(-) delete mode 100644 test/library/dumplogs.go diff --git a/test/integration/e2e_test.go b/test/integration/e2e_test.go index c0137cab..2cb207e7 100644 --- a/test/integration/e2e_test.go +++ b/test/integration/e2e_test.go @@ -42,10 +42,6 @@ import ( func TestE2EFullIntegration(t *testing.T) { env := library.IntegrationEnv(t) - // If anything in this test crashes, dump out the supervisor and proxy pod logs. - defer library.DumpLogs(t, env.SupervisorNamespace, "") - defer library.DumpLogs(t, "dex", "app=proxy") - ctx, cancelFunc := context.WithTimeout(context.Background(), 5*time.Minute) defer cancelFunc() diff --git a/test/integration/supervisor_login_test.go b/test/integration/supervisor_login_test.go index efc0134a..962895da 100644 --- a/test/integration/supervisor_login_test.go +++ b/test/integration/supervisor_login_test.go @@ -39,10 +39,6 @@ import ( func TestSupervisorLogin(t *testing.T) { env := library.IntegrationEnv(t) - // If anything in this test crashes, dump out the supervisor and proxy pod logs. - defer library.DumpLogs(t, env.SupervisorNamespace, "") - defer library.DumpLogs(t, "dex", "app=proxy") - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) defer cancel() diff --git a/test/library/assertions.go b/test/library/assertions.go index 35c2278b..5fccbca3 100644 --- a/test/library/assertions.go +++ b/test/library/assertions.go @@ -82,7 +82,7 @@ func assertNoRestartsDuringTest(t *testing.T, namespace, labelSelector string) { } // Expect the restart count to be the same as it was before the test. - if !assert.Equal( + assert.Equal( t, previousRestartCount, currentRestartCount, @@ -90,10 +90,7 @@ func assertNoRestartsDuringTest(t *testing.T, namespace, labelSelector string) { key.String(), currentRestartCount, previousRestartCount, - ) { - // Attempt to dump the logs from the previous container that crashed. - dumpContainerLogs(ctx, t, kubeClient, key.namespace, key.pod, key.container, true) - } + ) } }) } diff --git a/test/library/dumplogs.go b/test/library/dumplogs.go deleted file mode 100644 index dc0bf89c..00000000 --- a/test/library/dumplogs.go +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright 2020-2021 the Pinniped contributors. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package library - -import ( - "bufio" - "context" - "fmt" - "testing" - "time" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - corev1 "k8s.io/api/core/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. -func DumpLogs(t *testing.T, namespace string, labelSelector string) { - // Only trigger on failed tests. - if !t.Failed() { - return - } - - kubeClient := NewKubernetesClientset(t) - ctx, cancel := context.WithTimeout(context.Background(), time.Minute) - defer cancel() - - pods, err := kubeClient.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{LabelSelector: labelSelector}) - require.NoError(t, err) - - for _, pod := range pods.Items { - for _, container := range pod.Status.ContainerStatuses { - if container.RestartCount > 0 { - dumpContainerLogs(ctx, t, kubeClient, pod.Namespace, pod.Name, container.Name, true) - } - 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) -}