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 4f82123
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 41 deletions.
12 changes: 8 additions & 4 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -24,7 +27,6 @@ jobs:
go-version: "stable"
- uses: dominikh/staticcheck-action@v1
with:
build-tags: suite
install-go: false
integration:
needs:
Expand All @@ -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
69 changes: 32 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("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",
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,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()

Expand Down

0 comments on commit 4f82123

Please sign in to comment.