Skip to content

Commit

Permalink
Enable race detector for top-level unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Büringer [email protected]
  • Loading branch information
sbueringer committed Sep 19, 2024
1 parent 2b451f1 commit 3181c75
Show file tree
Hide file tree
Showing 21 changed files with 93 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -908,17 +908,21 @@ setup-envtest: $(SETUP_ENVTEST) ## Set up envtest (download kubebuilder assets)

.PHONY: test
test: $(SETUP_ENVTEST) ## Run unit and integration tests
KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test ./... $(TEST_ARGS)
KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -race ./... $(TEST_ARGS)
KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -run "^TestFuzzyConversion$" ./... $(TEST_ARGS)

.PHONY: test-verbose
test-verbose: ## Run unit and integration tests with verbose flag
$(MAKE) test TEST_ARGS="$(TEST_ARGS) -v"

.PHONY: test-junit
test-junit: $(SETUP_ENVTEST) $(GOTESTSUM) ## Run unit and integration tests and generate a junit report
set +o errexit; (KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -json ./... $(TEST_ARGS); echo $$? > $(ARTIFACTS)/junit.exitcode) | tee $(ARTIFACTS)/junit.stdout
set +o errexit; (KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -race -json ./... $(TEST_ARGS); echo $$? > $(ARTIFACTS)/junit.exitcode) | tee $(ARTIFACTS)/junit.stdout
$(GOTESTSUM) --junitfile $(ARTIFACTS)/junit.xml --raw-command cat $(ARTIFACTS)/junit.stdout
exit $$(cat $(ARTIFACTS)/junit.exitcode)
set +o errexit; (KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -run "^TestFuzzyConversion$" -json ./... $(TEST_ARGS); echo $$? > $(ARTIFACTS)/junit-fuzz.exitcode) | tee $(ARTIFACTS)/junit-fuzz.stdout
$(GOTESTSUM) --junitfile $(ARTIFACTS)/junit-fuzz.xml --raw-command cat $(ARTIFACTS)/junit-fuzz.stdout
exit $$(cat $(ARTIFACTS)/junit-fuzz.exitcode)

.PHONY: test-cover
test-cover: ## Run unit and integration tests and generate a coverage report
Expand Down
4 changes: 4 additions & 0 deletions bootstrap/kubeadm/types/upstreamv1beta1/conversion_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !race

/*
Copyright 2021 The Kubernetes Authors.
Expand Down Expand Up @@ -27,6 +29,8 @@ import (
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
)

// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.

func TestFuzzyConversion(t *testing.T) {
t.Run("for ClusterConfiguration", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &bootstrapv1.ClusterConfiguration{},
Expand Down
4 changes: 4 additions & 0 deletions bootstrap/kubeadm/types/upstreamv1beta2/conversion_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !race

/*
Copyright 2021 The Kubernetes Authors.
Expand Down Expand Up @@ -27,6 +29,8 @@ import (
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
)

// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.

func TestFuzzyConversion(t *testing.T) {
t.Run("for ClusterConfiguration", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &bootstrapv1.ClusterConfiguration{},
Expand Down
4 changes: 4 additions & 0 deletions bootstrap/kubeadm/types/upstreamv1beta3/conversion_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !race

/*
Copyright 2021 The Kubernetes Authors.
Expand Down Expand Up @@ -29,6 +31,8 @@ import (
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
)

// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.

func TestFuzzyConversion(t *testing.T) {
g := NewWithT(t)
scheme := runtime.NewScheme()
Expand Down
4 changes: 4 additions & 0 deletions bootstrap/kubeadm/types/upstreamv1beta4/conversion_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !race

/*
Copyright 2021 The Kubernetes Authors.
Expand Down Expand Up @@ -31,6 +33,8 @@ import (
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
)

// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.

func TestFuzzyConversion(t *testing.T) {
g := NewWithT(t)
scheme := runtime.NewScheme()
Expand Down
4 changes: 4 additions & 0 deletions exp/ipam/api/v1alpha1/conversion_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !race

/*
Copyright 2021 The Kubernetes Authors.
Expand Down Expand Up @@ -25,6 +27,8 @@ import (
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
)

// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.

func TestFuzzyConversion(t *testing.T) {
t.Run("for IPAddress", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &ipamv1.IPAddress{},
Expand Down
4 changes: 4 additions & 0 deletions internal/apis/bootstrap/kubeadm/v1alpha3/conversion_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !race

/*
Copyright 2020 The Kubernetes Authors.
Expand Down Expand Up @@ -28,6 +30,8 @@ import (
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
)

// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.

func TestFuzzyConversion(t *testing.T) {
t.Run("for KubeadmConfig", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &bootstrapv1.KubeadmConfig{},
Expand Down
4 changes: 4 additions & 0 deletions internal/apis/bootstrap/kubeadm/v1alpha4/conversion_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !race

/*
Copyright 2021 The Kubernetes Authors.
Expand Down Expand Up @@ -32,6 +34,8 @@ const (
fakeSecret = "abcdef0123456789"
)

// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.

func TestFuzzyConversion(t *testing.T) {
t.Run("for KubeadmConfig", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &bootstrapv1.KubeadmConfig{},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !race

/*
Copyright 2020 The Kubernetes Authors.
Expand Down Expand Up @@ -29,6 +31,8 @@ import (
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
)

// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.

func TestFuzzyConversion(t *testing.T) {
t.Run("for KubeadmControlPlane", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &controlplanev1.KubeadmControlPlane{},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !race

/*
Copyright 2020 The Kubernetes Authors.
Expand Down Expand Up @@ -36,6 +38,8 @@ const (
fakeSecret = "abcdef0123456789"
)

// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.

func TestFuzzyConversion(t *testing.T) {
t.Run("for KubeadmControlPlane", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &controlplanev1.KubeadmControlPlane{},
Expand Down
4 changes: 4 additions & 0 deletions internal/apis/core/exp/addons/v1alpha3/conversion_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !race

/*
Copyright 2021 The Kubernetes Authors.
Expand All @@ -23,6 +25,8 @@ import (
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
)

// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.

func TestFuzzyConversion(t *testing.T) {
t.Run("for ClusterResourceSet", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &addonsv1.ClusterResourceSet{},
Expand Down
4 changes: 4 additions & 0 deletions internal/apis/core/exp/addons/v1alpha4/conversion_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !race

/*
Copyright 2021 The Kubernetes Authors.
Expand All @@ -23,6 +25,8 @@ import (
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
)

// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.

func TestFuzzyConversion(t *testing.T) {
t.Run("for ClusterResourceSet", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &addonsv1.ClusterResourceSet{},
Expand Down
4 changes: 4 additions & 0 deletions internal/apis/core/exp/v1alpha3/conversion_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !race

/*
Copyright 2021 The Kubernetes Authors.
Expand Down Expand Up @@ -28,6 +30,8 @@ import (
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
)

// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.

func TestFuzzyConversion(t *testing.T) {
t.Run("for MachinePool", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &expv1.MachinePool{},
Expand Down
4 changes: 4 additions & 0 deletions internal/apis/core/exp/v1alpha4/conversion_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !race

/*
Copyright 2021 The Kubernetes Authors.
Expand Down Expand Up @@ -25,6 +27,8 @@ import (
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
)

// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.

func TestFuzzyConversion(t *testing.T) {
t.Run("for MachinePool", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &expv1.MachinePool{},
Expand Down
4 changes: 4 additions & 0 deletions internal/apis/core/v1alpha3/conversion_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !race

/*
Copyright 2020 The Kubernetes Authors.
Expand Down Expand Up @@ -29,6 +31,8 @@ import (
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
)

// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.

func TestFuzzyConversion(t *testing.T) {
t.Run("for Cluster", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &clusterv1.Cluster{},
Expand Down
4 changes: 4 additions & 0 deletions internal/apis/core/v1alpha4/conversion_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !race

/*
Copyright 2021 The Kubernetes Authors.
Expand Down Expand Up @@ -30,6 +32,8 @@ import (
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
)

// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.

func TestFuzzyConversion(t *testing.T) {
t.Run("for Cluster", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &clusterv1.Cluster{},
Expand Down
11 changes: 11 additions & 0 deletions internal/test/envtest/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/component-base/logs"
logsv1 "k8s.io/component-base/logs/api/v1"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -71,6 +73,15 @@ import (
)

func init() {
// This has to be done so klog.Background() uses a proper logger.
// Otherwise it would fall back and log to os.Stderr.
// This would lead to race conditions because input.M.Run() writes os.Stderr
// while some go routines in controller-runtime use os.Stderr to write logs.
if err := logsv1.ValidateAndApply(logs.NewOptions(), nil); err != nil {
klog.ErrorS(err, "Unable to validate and apply log options")
os.Exit(1)
}

logger := klog.Background()
// Use klog as the internal logger for this envtest environment.
log.SetLogger(logger)
Expand Down
4 changes: 4 additions & 0 deletions test/infrastructure/docker/api/v1alpha3/conversion_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !race

/*
Copyright 2021 The Kubernetes Authors.
Expand All @@ -23,6 +25,8 @@ import (
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
)

// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.

func TestFuzzyConversion(t *testing.T) {
t.Run("for DockerCluster", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &infrav1.DockerCluster{},
Expand Down
4 changes: 4 additions & 0 deletions test/infrastructure/docker/api/v1alpha4/conversion_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !race

/*
Copyright 2021 The Kubernetes Authors.
Expand All @@ -23,6 +25,8 @@ import (
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
)

// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.

func TestFuzzyConversion(t *testing.T) {
t.Run("for DockerCluster", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &infrav1.DockerCluster{},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !race

/*
Copyright 2021 The Kubernetes Authors.
Expand All @@ -23,6 +25,8 @@ import (
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
)

// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.

func TestFuzzyConversion(t *testing.T) {
t.Run("for DockerMachinePool", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &infraexpv1.DockerMachinePool{},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !race

/*
Copyright 2021 The Kubernetes Authors.
Expand All @@ -23,6 +25,8 @@ import (
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
)

// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.

func TestFuzzyConversion(t *testing.T) {
t.Run("for DockerMachinePool", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &infraexpv1.DockerMachinePool{},
Expand Down

0 comments on commit 3181c75

Please sign in to comment.