Skip to content

Commit 91ba1c5

Browse files
authored
Allows per test directory for generated files on failure (#10579)
1 parent 4f8afe2 commit 91ba1c5

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
changelog:
2+
- type: NON_USER_FACING
3+
issueLink: https://github.com/solo-io/solo-projects/issues/7634
4+
resolvesIssue: false
5+
description: >-
6+
Added optional option for PreFailHandler for dumping into per test directory

test/kubernetes/e2e/example/info_logging_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ func TestInstallationWithInfoLogLevel(t *testing.T) {
4646
if !nsEnvPredefined {
4747
os.Unsetenv(testutils.InstallNamespace)
4848
}
49+
50+
// This clean up function is called only once after all the tests in all the registered suites are done.
51+
// Moreover, PreFailHandler() will wipe out the output directory before dumping out the stats and
52+
// logs of the cluster. If PreFailHandler() is called in the suite's AfterTest() function already,
53+
// The following should be removed.
4954
if t.Failed() {
5055
testInstallation.PreFailHandler(ctx)
5156
}

test/kubernetes/e2e/features/example/suite.go

+17
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ func (s *testingSuite) BeforeTest(suiteName, testName string) {
4444

4545
func (s *testingSuite) AfterTest(suiteName, testName string) {
4646
// This is code that will be executed after each test is run
47+
48+
// PreFailHandler() logs the states of the clusters and dump out logs from various pods for debugging
49+
// when the test fails. If the test create and remove resources that will spin up the pod and delete
50+
// it afterward, PreFailHandler() should be called here (before the resources are deleted) so we can
51+
// capture the log before the pod is destroyed.
52+
53+
// WARNING: In this example test, another place calling PreFailHandler() is in the main _test.go file
54+
// (test/kubernetes/e2e/example/info_logging_test.go) where it sets up the go test cleanup function with t.Cleanup().
55+
// That clean up function is only called once/ after all the tests in all registered suites finished.
56+
// If PreFailHandler() is called here, the one in the cleanup function should be removed as it would wipe out the
57+
// entire output directory (including these per-test logs) before dumping out the logs at the very end.
58+
// If you only need to dump the logs once at the very end, the following should be removed.
59+
if s.T().Failed() {
60+
// Calling PreFailHandler() with optional TestName so that the logs would go into
61+
// a per test directory
62+
s.testInstallation.PreFailHandler(s.ctx, e2e.PreFailHandlerOption{TestName: testName})
63+
}
4764
}
4865

4966
func (s *testingSuite) TestExampleAssertion() {

test/kubernetes/e2e/test.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io/fs"
88
"os"
9+
"path"
910
"path/filepath"
1011
"runtime"
1112
"testing"
@@ -235,12 +236,19 @@ func (i *TestInstallation) UninstallGlooGateway(ctx context.Context, uninstallFn
235236
i.Assertions.EventuallyUninstallationSucceeded(ctx)
236237
}
237238

239+
type PreFailHandlerOption struct {
240+
TestName string
241+
}
242+
238243
// PreFailHandler is the function that is invoked if a test in the given TestInstallation fails
239-
func (i *TestInstallation) PreFailHandler(ctx context.Context) {
244+
func (i *TestInstallation) PreFailHandler(ctx context.Context, options ...PreFailHandlerOption) {
240245
// The idea here is we want to accumulate ALL information about this TestInstallation into a single directory
241246
// That way we can upload it in CI, or inspect it locally
242247

243248
failureDir := i.GeneratedFiles.FailureDir
249+
if len(options) > 0 && len(options[0].TestName) > 0 {
250+
failureDir = path.Join(failureDir, options[0].TestName)
251+
}
244252
err := os.Mkdir(failureDir, os.ModePerm)
245253
// We don't want to fail on the output directory already existing. This could occur
246254
// if multiple tests running in the same cluster from the same installation namespace

0 commit comments

Comments
 (0)