From 4f82123c1b2a482c06be8a00668bb80fefde2bf0 Mon Sep 17 00:00:00 2001 From: Terin Stock Date: Sat, 28 Sep 2024 21:06:42 +0200 Subject: [PATCH] chore(suite): remove build tags The integration suite was guarded behind the "suite" build tag, which prevented Go tooling from working with the integration tests by default. This made the upcoming refactoring to support API tokens more difficult, and has historically discouraged the addition of more integration tests. As modern versions of Go support `testing.Cleanup` we can move the envtest setup into a helper function that runs, and automatically cleans up, in every test, instead of wrapping the main function. The tests are guarded by checking for the presence of the KUBEBUILDER_ASSETS environment variable, which is set by `setup-env` in the CI environment. --- .github/workflows/tests.yaml | 12 ++-- pkgs/controllers/originissuer_suite_test.go | 69 ++++++++++----------- 2 files changed, 40 insertions(+), 41 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 9ecbf2e..8a33cc5 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -14,7 +14,10 @@ jobs: - uses: actions/setup-go@v4 with: go-version: stable - - run: make test + - name: test + run: | + go install gotest.tools/gotestsum@latest + make test lint: runs-on: ubuntu-latest steps: @@ -24,7 +27,6 @@ jobs: go-version: "stable" - uses: dominikh/staticcheck-action@v1 with: - build-tags: suite install-go: false integration: needs: @@ -36,7 +38,9 @@ jobs: - uses: actions/setup-go@v4 with: go-version: "stable" - - run: | + - name: integration + run: | go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest + go install gotest.tools/gotestsum@latest source <(setup-envtest use -p env) - go test ./... -tags suite + make test diff --git a/pkgs/controllers/originissuer_suite_test.go b/pkgs/controllers/originissuer_suite_test.go index d1839dd..ae6df94 100644 --- a/pkgs/controllers/originissuer_suite_test.go +++ b/pkgs/controllers/originissuer_suite_test.go @@ -1,21 +1,14 @@ -//go:build suite -// +build suite - package controllers import ( "context" - "log" "os" "path/filepath" "testing" "time" cmapi "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1" - "github.com/cloudflare/origin-ca-issuer/internal/cfapi" v1 "github.com/cloudflare/origin-ca-issuer/pkgs/apis/v1" - "github.com/go-logr/zerologr" - "github.com/rs/zerolog" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" @@ -30,27 +23,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" ) -var cfg *rest.Config - -func TestMain(m *testing.M) { - zl := zerolog.Nop() - logf.SetLogger(zerologr.New(&zl)) - t := &envtest.Environment{ - CRDDirectoryPaths: []string{filepath.Join("..", "..", "deploy", "crds")}, - } - cmapi.AddToScheme(scheme.Scheme) - v1.AddToScheme(scheme.Scheme) - - var err error - if cfg, err = t.Start(); err != nil { - log.Fatal(err) - } - - code := m.Run() - t.Stop() - os.Exit(code) -} - func TestOriginIssuerReconcileSuite(t *testing.T) { issuer := &v1.OriginIssuer{ ObjectMeta: metav1.ObjectMeta{ @@ -77,6 +49,15 @@ func TestOriginIssuerReconcileSuite(t *testing.T) { }, } + if os.Getenv("KUBEBUILDER_ASSETS") == "" { + t.Skip("did not enable integration tests") + } + + cfg, err := envtestConfig(t) + if err != nil { + t.Fatalf("error starting envtest: %v", err) + } + mgr, err := manager.New(cfg, manager.Options{ Metrics: metricsserver.Options{ BindAddress: "0", @@ -88,16 +69,11 @@ func TestOriginIssuerReconcileSuite(t *testing.T) { } c := mgr.GetClient() - f := cfapi.FactoryFunc(func(serviceKey []byte) (cfapi.Interface, error) { - return nil, nil - }) - controller := &OriginIssuerController{ - Client: c, - Reader: c, - Clock: clock.RealClock{}, - Factory: f, - Log: logf.Log, + Client: c, + Reader: c, + Clock: clock.RealClock{}, + Log: logf.Log, } builder.ControllerManagedBy(mgr). @@ -138,6 +114,25 @@ func TestOriginIssuerReconcileSuite(t *testing.T) { }, 5*time.Second, 10*time.Millisecond, "OriginIssuer reconciler") } +func envtestConfig(t *testing.T) (*rest.Config, error) { + env := &envtest.Environment{ + CRDDirectoryPaths: []string{filepath.Join("..", "..", "deploy", "crds")}, + } + cmapi.AddToScheme(scheme.Scheme) + v1.AddToScheme(scheme.Scheme) + + cfg, err := env.Start() + if err != nil { + return nil, err + } + + t.Cleanup(func() { + env.Stop() + }) + + return cfg, nil +} + func StartTestManager(mgr manager.Manager, t *testing.T) (context.CancelFunc, chan error) { t.Helper()