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)