Skip to content

Commit

Permalink
Fix envtest tearing down
Browse files Browse the repository at this point in the history
PR openshift#307 was too optimistic. The original issue worked around
by 053f866 still exists actually ; `make test` is now
failing with :

  [FAILED] Unexpected error:
      <errors.aggregate | len:1, cap:1>: [
          <*errors.errorString | 0xc0005ebf80>{
              s: "timeout waiting for process kube-apiserver to stop",
          },
      ]
      timeout waiting for process kube-apiserver to stop
  occurred

This is caused by a missing cancellation of the certwatcher in the
controller-runtime. The fix, as documented in [1], is to create
a cancellable context, pass it to envtest and cancel it just before
tearing down the test.

[1] kubernetes-sigs/kubebuilder#2379

Signed-off-by: Greg Kurz <groug@kaod.org>
  • Loading branch information
gkurz committed May 17, 2023
1 parent 6eb6385 commit 3b105a3
Showing 1 changed file with 15 additions and 5 deletions.
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())
})

0 comments on commit 3b105a3

Please sign in to comment.