From 2e61dfbf1d1cc66a5abfd8d138a1aa9117b7c2d5 Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Mon, 12 Sep 2022 12:02:37 +0200 Subject: [PATCH] e2e: avoid random control plane output through GinkoWriter If the control plane emits anything at the time when the test runs, for example "unable to sync kubernetes service", the test breaks because that additional output is unexpected. --- .../unittests/cleanup/cleanup_test.go | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/test/e2e/framework/internal/unittests/cleanup/cleanup_test.go b/test/e2e/framework/internal/unittests/cleanup/cleanup_test.go index 9af63ab9fad..902f93f71c6 100644 --- a/test/e2e/framework/internal/unittests/cleanup/cleanup_test.go +++ b/test/e2e/framework/internal/unittests/cleanup/cleanup_test.go @@ -23,6 +23,8 @@ import ( "github.com/onsi/ginkgo/v2" + "k8s.io/klog/v2" + "k8s.io/klog/v2/ktesting" "k8s.io/kubernetes/test/e2e/framework" "k8s.io/kubernetes/test/e2e/framework/internal/output" testapiserver "k8s.io/kubernetes/test/utils/apiserver" @@ -45,8 +47,6 @@ import ( // // // -// -// // This must be line #50. var _ = ginkgo.Describe("framework", func() { @@ -104,6 +104,24 @@ STEP: Destroying namespace "test-namespace-zzz" for this suite. ) func TestCleanup(t *testing.T) { + // The control plane is noisy and randomly logs through klog, for example: + // E0912 07:08:46.100164 75466 controller.go:254] unable to sync kubernetes service: Endpoints "kubernetes" is invalid: subsets[0].addresses[0].ip: Invalid value: "127.0.0.1": may not be in the loopback range (127.0.0.0/8, ::1/128) + // + // By creating a ktesting logger and registering that as global + // default logger we get the control plane output into the + // "go test" output in case of a failure (useful for debugging!) + // while keeping it out of the captured Ginkgo output that + // the test is comparing below. + // + // There are some small drawbacks: + // - The source code location for control plane log messages + // is shown as klog.go because klog does not properly + // skip its own helper functions. That's okay, normally + // ktesting should not be installed as logging backend like this. + // - klog.Infof messages are printed with an extra newline. + logger, _ := ktesting.NewTestContext(t) + klog.SetLogger(logger) + apiServer := testapiserver.StartAPITestServer(t) // This simulates how test/e2e uses the framework and how users