Skip to content

Commit

Permalink
Merge pull request #134 from spinkube/dani/webhooktests
Browse files Browse the repository at this point in the history
Tests: Fix shared-scheme in webhook tests and enable the race detector
  • Loading branch information
endocrimes authored Mar 6, 2024
2 parents 862c762 + 6b3223a commit 9e0051f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ vet: ## Run go vet against code.

.PHONY: test
test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test -race ./... -coverprofile cover.out

GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
GOLANGCI_LINT_VERSION ?= v1.54.2
Expand Down
19 changes: 12 additions & 7 deletions internal/webhook/admission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"net"
"path/filepath"
"runtime"
goruntime "runtime"
"testing"
"time"

Expand All @@ -15,7 +15,8 @@ import (
"github.com/stretchr/testify/require"
admissionv1 "k8s.io/api/admission/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/apimachinery/pkg/runtime"
clientscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -28,6 +29,7 @@ type envTestState struct {
cfg *rest.Config
k8sClient client.Client
testEnv *envtest.Environment
scheme *runtime.Scheme
}

// setupEnvTest will start a fake kubernetes and client for use when testing
Expand All @@ -45,7 +47,7 @@ func setupEnvTest(t *testing.T) *envTestState {
// Note that you must have the required binaries setup under the bin directory to perform
// the tests directly. When we run make test it will be setup and used automatically.
BinaryAssetsDirectory: filepath.Join("..", "..", "bin", "k8s",
fmt.Sprintf("1.28.3-%s-%s", runtime.GOOS, runtime.GOARCH)),
fmt.Sprintf("1.28.3-%s-%s", goruntime.GOOS, goruntime.GOARCH)),

WebhookInstallOptions: envtest.WebhookInstallOptions{
Paths: []string{filepath.Join("..", "..", "config", "webhook")},
Expand All @@ -60,20 +62,23 @@ func setupEnvTest(t *testing.T) *envTestState {
require.NoError(t, err)
require.NotNil(t, cfg)

err = spinv1.AddToScheme(scheme.Scheme)
scheme := runtime.NewScheme()
require.NoError(t, clientscheme.AddToScheme(scheme))
err = spinv1.AddToScheme(scheme)
require.NoError(t, err)

err = admissionv1.AddToScheme(scheme.Scheme)
err = admissionv1.AddToScheme(scheme)
require.NoError(t, err)

k8sClient, err := client.New(cfg, client.Options{Scheme: scheme.Scheme})
k8sClient, err := client.New(cfg, client.Options{Scheme: scheme})
require.NoError(t, err)
require.NotNil(t, k8sClient)

return &envTestState{
cfg: cfg,
k8sClient: k8sClient,
testEnv: testEnv,
scheme: scheme,
}
}

Expand All @@ -83,7 +88,7 @@ func startWebhookServer(t *testing.T, envtest *envTestState) {
// start webhook server using Manager
webhookInstallOptions := &envtest.testEnv.WebhookInstallOptions
mgr, err := ctrl.NewManager(envtest.cfg, ctrl.Options{
Scheme: scheme.Scheme,
Scheme: envtest.scheme,
WebhookServer: webhook.NewServer(webhook.Options{
Host: webhookInstallOptions.LocalServingHost,
Port: webhookInstallOptions.LocalServingPort,
Expand Down

0 comments on commit 9e0051f

Please sign in to comment.