Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
test: fix data race between multiple unit test that use envtest
Browse files Browse the repository at this point in the history
  • Loading branch information
supershal committed Apr 2, 2024
1 parent aa467be commit 66c2656
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions test/helpers/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,26 @@ var TestEnv *TestEnvironment

// Initialize the test environment. BeforeSuite will be only executed if this package is loaded by the test.
var _ = BeforeSuite(func(ctx SpecContext) {
By("Initialize loggers for testing")
// Uninitialized logger spits stacktrace as warning during test execution
logger := textlogger.NewLogger(textlogger.NewConfig())
// use klog as the internal logger for this envtest environment.
log.SetLogger(logger)
// additionally force all of the controllers to use the Ginkgo logger.
ctrl.SetLogger(logger)
klog.InitFlags(nil)
// add logger for ginkgo
klog.SetOutput(GinkgoWriter)

By("Starting test environment")
testEnvConfig := NewTestEnvironmentConfiguration()
var err error
TestEnv, err = testEnvConfig.Build()
if err != nil {
panic(err)
}
By("Starting the manager")
go func() {
By("Starting the manager")
defer GinkgoRecover()
Expect(TestEnv.StartManager(ctx)).To(Succeed())
}()
Expand All @@ -36,15 +47,3 @@ var _ = BeforeSuite(func(ctx SpecContext) {
var _ = AfterSuite(func(ctx context.Context) {
Expect(TestEnv.Stop()).To(Succeed())
})

//nolint:gochecknoinits //needed for test initialization
func init() {
klog.InitFlags(nil)
logger := textlogger.NewLogger(textlogger.NewConfig())
// use klog as the internal logger for this envtest environment.
log.SetLogger(logger)
// additionally force all of the controllers to use the Ginkgo logger.
ctrl.SetLogger(logger)
// add logger for ginkgo
klog.SetOutput(GinkgoWriter)
}

0 comments on commit 66c2656

Please sign in to comment.