Skip to content

Commit

Permalink
Merge pull request #148 from darkowlzz/testenv-klog-init-flags
Browse files Browse the repository at this point in the history
hiddeco authored Sep 23, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents f2b114a + c5a17be commit ed06eb8
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
@@ -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 <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)

0 comments on commit ed06eb8

Please sign in to comment.