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

fix(tests): no more "log.SetLogger(...) was never called..." log entry during e2e tests #20

Merged
merged 1 commit into from
Mar 18, 2024
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
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
Loading