Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testenv: Remove klog init flags #148

Merged
merged 1 commit into from
Sep 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
darkowlzz marked this conversation as resolved.
Show resolved Hide resolved
}

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())
Copy link
Member

@squaremo squaremo Sep 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given this is equivalent to ctrl.SetLogger, it'd be better to use the same in both the example given and the code, otherwise people might wonder why they differ. Also worth explaining that subsequent calls to ctrl.SetLogger have no effect (the comment implies this, but pays to be explicit)


opts := options{}
for _, apply := range o {
apply(&opts)
Expand Down