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.
This commit is contained in:
Patrick Ohly 2022-05-11 10:47:19 +02:00
parent b74d023e70
commit 228640ffab

View File

@ -27,6 +27,7 @@ import (
"github.com/onsi/ginkgo/config" "github.com/onsi/ginkgo/config"
"github.com/onsi/ginkgo/reporters" "github.com/onsi/ginkgo/reporters"
"github.com/onsi/gomega" "github.com/onsi/gomega"
"github.com/stretchr/testify/assert"
"k8s.io/kubernetes/test/e2e/framework" "k8s.io/kubernetes/test/e2e/framework"
) )
@ -40,7 +41,6 @@ import (
// //
// //
// //
//
func runTests(t *testing.T, reporter ginkgo.Reporter) { func runTests(t *testing.T, reporter ginkgo.Reporter) {
// This source code line will be part of the stack dump comparison. // 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", 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. // assert.Equal prints a useful diff if the slices are not
framework.ExpectEqual(len(actual), len(expected), "%d entries in %v", len(expected), actual) // equal. However, the diff does not show changes inside the
for i, a := range actual { // strings. Therefore we also compare the individual fields.
b := expected[i] if !assert.Equal(t, expected, actual) {
framework.ExpectEqual(a.name, b.name, "name in %d", i) for i := 0; i < len(expected) && i < len(actual); i++ {
framework.ExpectEqual(a.output, b.output, "output in %d", i) assert.Equal(t, expected[i].output, actual[i].output, "output from test #%d: %s", i, expected[i].name)
framework.ExpectEqual(a.failure, b.failure, "failure in %d", i) assert.Equal(t, expected[i].stack, actual[i].stack, "stack from test #%d: %s", i, expected[i].name)
// 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)
} }
} }