Skip to content

Commit

Permalink
testenv: Remove klog init flags
Browse files Browse the repository at this point in the history
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 <darkowlzz@protonmail.com>
  • Loading branch information
darkowlzz committed Sep 23, 2021
1 parent f2b114a commit c5a17be
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions runtime/testenv/testenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 <package>_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)
Expand Down

0 comments on commit c5a17be

Please sign in to comment.