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 envtest tearing down #316

Merged
merged 1 commit into from
May 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 15 additions & 5 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package controllers

import (
"context"
"os"
"path/filepath"
"testing"
Expand All @@ -42,10 +43,14 @@ import (
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment
var k8sManager ctrl.Manager
var (
cfg *rest.Config
k8sClient client.Client
testEnv *envtest.Environment
k8sManager ctrl.Manager
ctx context.Context
cancel context.CancelFunc
)

func TestAPIs(t *testing.T) {
RegisterFailHandler(Fail)
Expand All @@ -56,6 +61,8 @@ func TestAPIs(t *testing.T) {
var _ = BeforeSuite(func() {
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

ctx, cancel = context.WithCancel(context.TODO())

By("bootstrapping test environment")
webhookOptions := envtest.WebhookInstallOptions{
Paths: []string{filepath.Join("..", "config", "webhook")},
Expand Down Expand Up @@ -111,7 +118,7 @@ var _ = BeforeSuite(func() {
Expect(err).ToNot(HaveOccurred())

go func() {
err = k8sManager.Start(ctrl.SetupSignalHandler())
err = k8sManager.Start(ctx)
Expect(err).ToNot(HaveOccurred())
}()

Expand All @@ -121,6 +128,9 @@ var _ = BeforeSuite(func() {

var _ = AfterSuite(func() {
By("tearing down the test environment")
// Stop() will timeout if the context isn't cancelled beforehand.
// See https://github.com/kubernetes-sigs/kubebuilder/pull/2379 for details.
cancel()
err := testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
})