From c5a17be357c6253e104f197c6ae9463b48df7c8c Mon Sep 17 00:00:00 2001 From: Sunny Date: Thu, 16 Sep 2021 20:15:17 +0530 Subject: [PATCH] testenv: Remove klog init flags Removes klog.InitFlags() because the klog flags aren't parsed before using klog. To avoid conflicting with other flags, it's better to not populate the global flagset. This results in default klogr to be set as the default controller-runtime logger if not set in the test already. The logger setup not being in init() allows users to set their own logger before creating testenv Environment. Signed-off-by: Sunny --- runtime/testenv/testenv.go | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/runtime/testenv/testenv.go b/runtime/testenv/testenv.go index ae149bc8..1fc4f265 100644 --- a/runtime/testenv/testenv.go +++ b/runtime/testenv/testenv.go @@ -52,13 +52,6 @@ var ( env *envtest.Environment ) -func init() { - klog.InitFlags(nil) - logger := klogr.New() - log.SetLogger(logger) - ctrl.SetLogger(logger) -} - var ( cacheSyncBackoff = wait.Backoff{ Duration: 100 * time.Millisecond, @@ -118,7 +111,32 @@ func WithCRDPath(path ...string) Option { // // NOTE: This function should be called only once for each package you are running tests within, usually the environment // is initialised in a suite_test.go or _test.go file within a `TestMain` function. +// +// When a testenv Environment is created, it initializes the +// controller-runtime's deferred logger with a default logger based on klog. In +// order to override this behavior, the controller-runtime logger can be +// initialized before creating testenv Environment. +// +// import ( +// "testing" +// +// "github.com/fluxcd/pkg/runtime/testenv" +// ctrl "sigs.k8s.io/controller-runtime" +// "sigs.k8s.io/controller-runtime/pkg/log/zap" +// } +// +// func TestMain(m *testing.M) { +// zlog := zap.New(zap.UseDevMode(true)) +// ctrl.SetLogger(zlog) +// +// testEnv = testenv.New() +// ... +// } +// func New(o ...Option) *Environment { + // Set a default logger if not set already. + log.SetLogger(klogr.New()) + opts := options{} for _, apply := range o { apply(&opts)