Skip to content

Commit

Permalink
fix envtest timeout waiting for process kube-apiserver to stop
Browse files Browse the repository at this point in the history
Signed-off-by: Tommy Hughes <tohughes@redhat.com>
  • Loading branch information
tchughesiv committed Oct 31, 2022
1 parent 8832cef commit decd7f8
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/onsi/gomega/gexec"
operatorframework "github.com/operator-framework/api/pkg/operators/v1alpha1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
Expand All @@ -48,6 +47,7 @@ import (

var testEnv *envtest.Environment
var ctx context.Context
var cancel context.CancelFunc
var dRec *DBaaSReconciler
var iCtrl *spyctrl
var cCtrl *spyctrl
Expand Down Expand Up @@ -94,7 +94,7 @@ var _ = BeforeSuite(func() {

//+kubebuilder:scaffold:scheme

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

err = os.Setenv(InstallNamespaceEnvVar, testNamespace)
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -155,13 +155,21 @@ var _ = BeforeSuite(func() {
go func() {
defer GinkgoRecover()
err = k8sManager.Start(ctrl.SetupSignalHandler())
Expect(err).ToNot(HaveOccurred(), "failed to run manager")
gexec.KillAndWait(4 * time.Second)

// Teardown the test environment once controller is fnished.
// Otherwise from Kubernetes 1.21+, teardon timeouts waiting on
// kube-apiserver to return
err := testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
Expect(err).NotTo(HaveOccurred())
}()
}, 60)

var _ = AfterSuite(func() {
// https://github.com/kubernetes-sigs/controller-runtime/issues/1571
cancel()
By("tearing down the test environment,but I do nothing here.")
err := testEnv.Stop()
// Set 4 with random
if err != nil {
time.Sleep(4 * time.Second)
}
err = testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
err = os.Unsetenv(InstallNamespaceEnvVar)
Expect(err).NotTo(HaveOccurred())
})

0 comments on commit decd7f8

Please sign in to comment.