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 buringerst@vmware.com
  • Loading branch information
sbueringer committed Sep 20, 2024
1 parent 2b451f1 commit 696315e
Show file tree
Hide file tree
Showing 22 changed files with 161 additions and 42 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
68 changes: 68 additions & 0 deletions bootstrap/kubeadm/types/upstreamv1beta4/conversion_no_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package upstreamv1beta4

import (
"testing"
"time"

. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
)

// This test cas been moved out of conversion_test.go because it should be run with the race detector.
// The tests in conversion_test.go are disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.

func TestTimeoutForControlPlaneMigration(t *testing.T) {
timeout := metav1.Duration{Duration: 10 * time.Second}
t.Run("from ClusterConfiguration to InitConfiguration and back", func(t *testing.T) {
g := NewWithT(t)

clusterConfiguration := &bootstrapv1.ClusterConfiguration{
APIServer: bootstrapv1.APIServer{TimeoutForControlPlane: &timeout},
}

initConfiguration := &InitConfiguration{}
err := initConfiguration.ConvertFromClusterConfiguration(clusterConfiguration)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(initConfiguration.Timeouts.ControlPlaneComponentHealthCheck).To(Equal(&timeout))

clusterConfiguration = &bootstrapv1.ClusterConfiguration{}
err = initConfiguration.ConvertToClusterConfiguration(clusterConfiguration)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(clusterConfiguration.APIServer.TimeoutForControlPlane).To(Equal(&timeout))
})
t.Run("from ClusterConfiguration to JoinConfiguration and back", func(t *testing.T) {
g := NewWithT(t)

clusterConfiguration := &bootstrapv1.ClusterConfiguration{
APIServer: bootstrapv1.APIServer{TimeoutForControlPlane: &timeout},
}

joinConfiguration := &JoinConfiguration{}
err := joinConfiguration.ConvertFromClusterConfiguration(clusterConfiguration)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(joinConfiguration.Timeouts.ControlPlaneComponentHealthCheck).To(Equal(&timeout))

clusterConfiguration = &bootstrapv1.ClusterConfiguration{}
err = joinConfiguration.ConvertToClusterConfiguration(clusterConfiguration)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(clusterConfiguration.APIServer.TimeoutForControlPlane).To(Equal(&timeout))
})
}
44 changes: 4 additions & 40 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 All @@ -18,19 +20,19 @@ package upstreamv1beta4

import (
"testing"
"time"

fuzz "github.com/google/gofuzz"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"

bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
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 Expand Up @@ -144,41 +146,3 @@ func bootstrapv1JoinConfigurationFuzzer(obj *bootstrapv1.JoinConfiguration, c fu
obj.Discovery.File.KubeConfig = nil
}
}

func TestTimeoutForControlPlaneMigration(t *testing.T) {
timeout := metav1.Duration{Duration: 10 * time.Second}
t.Run("from ClusterConfiguration to InitConfiguration and back", func(t *testing.T) {
g := NewWithT(t)

clusterConfiguration := &bootstrapv1.ClusterConfiguration{
APIServer: bootstrapv1.APIServer{TimeoutForControlPlane: &timeout},
}

initConfiguration := &InitConfiguration{}
err := initConfiguration.ConvertFromClusterConfiguration(clusterConfiguration)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(initConfiguration.Timeouts.ControlPlaneComponentHealthCheck).To(Equal(&timeout))

clusterConfiguration = &bootstrapv1.ClusterConfiguration{}
err = initConfiguration.ConvertToClusterConfiguration(clusterConfiguration)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(clusterConfiguration.APIServer.TimeoutForControlPlane).To(Equal(&timeout))
})
t.Run("from ClusterConfiguration to JoinConfiguration and back", func(t *testing.T) {
g := NewWithT(t)

clusterConfiguration := &bootstrapv1.ClusterConfiguration{
APIServer: bootstrapv1.APIServer{TimeoutForControlPlane: &timeout},
}

joinConfiguration := &JoinConfiguration{}
err := joinConfiguration.ConvertFromClusterConfiguration(clusterConfiguration)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(joinConfiguration.Timeouts.ControlPlaneComponentHealthCheck).To(Equal(&timeout))

clusterConfiguration = &bootstrapv1.ClusterConfiguration{}
err = joinConfiguration.ConvertToClusterConfiguration(clusterConfiguration)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(clusterConfiguration.APIServer.TimeoutForControlPlane).To(Equal(&timeout))
})
}
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
Loading

0 comments on commit 696315e

Please sign in to comment.