Skip to content

Commit

Permalink
fix(tests): no more "log.SetLogger(...) was never called..." log entr…
Browse files Browse the repository at this point in the history
…y during e2e tests (#20)
  • Loading branch information
programmer04 authored Mar 18, 2024
1 parent 3d0d995 commit 8427c0c
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion test/e2e/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package e2e
import (
"context"
"fmt"
"io"
"os"
"strings"
"sync"
"testing"
"time"

Expand All @@ -25,6 +27,8 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayclient "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned"

Expand Down Expand Up @@ -97,6 +101,8 @@ func WithOperatorImage(image string) TestEnvOption {
}
}

var loggerOnce sync.Once

// CreateEnvironment creates a new independent testing environment for running isolated e2e test.
func CreateEnvironment(t *testing.T, ctx context.Context, opts ...TestEnvOption) TestEnvironment {
t.Helper()
Expand Down Expand Up @@ -169,7 +175,17 @@ func CreateEnvironment(t *testing.T, ctx context.Context, opts ...TestEnvOption)
clients.GatewayClient, err = gatewayclient.NewForConfig(env.Cluster().Config())
require.NoError(t, err)

t.Log("intializing manager client")
t.Log("initializing manager client")
loggerOnce.Do(func() {
// A new client from package "sigs.k8s.io/controller-runtime/pkg/client" is created per execution
// of this function (see the line below this block). It requires a logger to be set, otherwise it logs
// "[controller-runtime] log.SetLogger(...) was never called; logs will not be displayed" with a stack trace.
// Since setting logger is a package level operation not safe for concurrent use, ensure it is set
// only once.
ctrllog.SetLogger(zap.New(func(o *zap.Options) {
o.DestWriter = io.Discard
}))
})
clients.MgrClient, err = client.New(env.Cluster().Config(), client.Options{})
require.NoError(t, err)

Expand Down

0 comments on commit 8427c0c

Please sign in to comment.