Skip to content

Commit

Permalink
chore(suite): remove build tags
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
terinjokes committed Sep 28, 2024
1 parent 4222912 commit 73d255d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 40 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ jobs:
go-version: "stable"
- uses: dominikh/staticcheck-action@v1
with:
build-tags: suite
install-go: false
integration:
needs:
Expand All @@ -36,7 +35,8 @@ 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
source <(setup-envtest use -p env)
go test ./... -tags suite
make test
73 changes: 36 additions & 37 deletions pkgs/controllers/originissuer_suite_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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{
Expand All @@ -77,6 +49,15 @@ func TestOriginIssuerReconcileSuite(t *testing.T) {
},
}

if os.Getenv("KUBEBUILDER_ASSETS") == "" {
t.Skip("kubebuilder environment was not setup")
}

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",
Expand All @@ -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).
Expand Down Expand Up @@ -138,6 +114,29 @@ func TestOriginIssuerReconcileSuite(t *testing.T) {
}, 5*time.Second, 10*time.Millisecond, "OriginIssuer reconciler")
}

func envtestConfig(t *testing.T) (*rest.Config, error) {
t.Helper()

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() {
if err := env.Stop(); err != nil {
t.Fatal(err)
}
})

return cfg, nil
}

func StartTestManager(mgr manager.Manager, t *testing.T) (context.CancelFunc, chan error) {
t.Helper()

Expand Down

0 comments on commit 73d255d

Please sign in to comment.