From 228640ffabcdcf38e6a221589c6d1d9f0f765f6f Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Wed, 11 May 2022 10:47:19 +0200 Subject: [PATCH] e2e: fix logs unit test The test had two problems: - the expected line was off by one (probably modified import statements) - when Gomega failed in TestFailureOutput, Ginkgo panicked because its fail handler was called outside of a Ginkgo node Now github.com/stretchr/testify/assert is used for comparing the output because it works in a unit test without further customization and because the failure messages are more useful. --- test/e2e/framework/log_test.go | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/test/e2e/framework/log_test.go b/test/e2e/framework/log_test.go index 103c7748b87..d63c38bbcb0 100644 --- a/test/e2e/framework/log_test.go +++ b/test/e2e/framework/log_test.go @@ -27,6 +27,7 @@ import ( "github.com/onsi/ginkgo/config" "github.com/onsi/ginkgo/reporters" "github.com/onsi/gomega" + "github.com/stretchr/testify/assert" "k8s.io/kubernetes/test/e2e/framework" ) @@ -40,7 +41,6 @@ import ( // // // -// func runTests(t *testing.T, reporter ginkgo.Reporter) { // This source code line will be part of the stack dump comparison. @@ -115,17 +115,14 @@ func TestFailureOutput(t *testing.T) { stack: "k8s.io/kubernetes/test/e2e/framework_test.glob..func1.2()\n\tlog_test.go:57\nk8s.io/kubernetes/test/e2e/framework_test.runTests()\n\tlog_test.go:47\n", }, } - // Compare individual fields. Comparing the slices leads to unreadable error output when there is any mismatch. - framework.ExpectEqual(len(actual), len(expected), "%d entries in %v", len(expected), actual) - for i, a := range actual { - b := expected[i] - framework.ExpectEqual(a.name, b.name, "name in %d", i) - framework.ExpectEqual(a.output, b.output, "output in %d", i) - framework.ExpectEqual(a.failure, b.failure, "failure in %d", i) - // There may be additional stack entries from the "testing" package at the - // end. We ignore those in the comparison because the line number in them - // varies. - framework.ExpectEqual(a.stack, b.stack, "stack in %d: %s", i, a.stack) + // assert.Equal prints a useful diff if the slices are not + // equal. However, the diff does not show changes inside the + // strings. Therefore we also compare the individual fields. + if !assert.Equal(t, expected, actual) { + for i := 0; i < len(expected) && i < len(actual); i++ { + assert.Equal(t, expected[i].output, actual[i].output, "output from test #%d: %s", i, expected[i].name) + assert.Equal(t, expected[i].stack, actual[i].stack, "stack from test #%d: %s", i, expected[i].name) + } } }