Skip to content

Commit 483276e

Browse files
authored
Enable race detector for top-level unit tests (#11207)
Signed-off-by: Stefan Büringer buringerst@vmware.com Signed-off-by: Stefan Büringer buringerst@vmware.com
1 parent fef53c1 commit 483276e

File tree

23 files changed

+182
-49
lines changed

23 files changed

+182
-49
lines changed

Makefile

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -906,19 +906,30 @@ KUBEBUILDER_ASSETS ?= $(shell $(SETUP_ENVTEST) use --use-env -p path $(KUBEBUILD
906906
setup-envtest: $(SETUP_ENVTEST) ## Set up envtest (download kubebuilder assets)
907907
@echo KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS)
908908

909-
.PHONY: test
910-
test: $(SETUP_ENVTEST) ## Run unit and integration tests
909+
.PHONY: test-no-race
910+
test-no-race: $(SETUP_ENVTEST) ## Run unit and integration tests
911911
KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test ./... $(TEST_ARGS)
912912

913+
.PHONY: test
914+
test: $(SETUP_ENVTEST) ## Run unit and integration tests with race detector
915+
# Note: Fuzz tests are not executed with race detector because they would just time out.
916+
# To achieve that, all files with fuzz tests have the "!race" build tag, to still run fuzz tests
917+
# we have an additional `go test` run that focuses on "TestFuzzyConversion".
918+
KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -race ./... $(TEST_ARGS)
919+
KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -run "^TestFuzzyConversion$$" ./... $(TEST_ARGS)
920+
913921
.PHONY: test-verbose
914-
test-verbose: ## Run unit and integration tests with verbose flag
922+
test-verbose: ## Run unit and integration tests with race detector and with verbose flag
915923
$(MAKE) test TEST_ARGS="$(TEST_ARGS) -v"
916924

917925
.PHONY: test-junit
918-
test-junit: $(SETUP_ENVTEST) $(GOTESTSUM) ## Run unit and integration tests and generate a junit report
919-
set +o errexit; (KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -json ./... $(TEST_ARGS); echo $$? > $(ARTIFACTS)/junit.exitcode) | tee $(ARTIFACTS)/junit.stdout
926+
test-junit: $(SETUP_ENVTEST) $(GOTESTSUM) ## Run unit and integration tests with race detector and generate a junit report
927+
set +o errexit; (KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -race -json ./... $(TEST_ARGS); echo $$? > $(ARTIFACTS)/junit.exitcode) | tee $(ARTIFACTS)/junit.stdout
920928
$(GOTESTSUM) --junitfile $(ARTIFACTS)/junit.xml --raw-command cat $(ARTIFACTS)/junit.stdout
921929
exit $$(cat $(ARTIFACTS)/junit.exitcode)
930+
set +o errexit; (KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -run "^TestFuzzyConversion$$" -json ./... $(TEST_ARGS); echo $$? > $(ARTIFACTS)/junit-fuzz.exitcode) | tee $(ARTIFACTS)/junit-fuzz.stdout
931+
$(GOTESTSUM) --junitfile $(ARTIFACTS)/junit-fuzz.xml --raw-command cat $(ARTIFACTS)/junit-fuzz.stdout
932+
exit $$(cat $(ARTIFACTS)/junit-fuzz.exitcode)
922933

923934
.PHONY: test-cover
924935
test-cover: ## Run unit and integration tests and generate a coverage report

bootstrap/kubeadm/types/upstreamv1beta1/conversion_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build !race
2+
13
/*
24
Copyright 2021 The Kubernetes Authors.
35
@@ -27,6 +29,8 @@ import (
2729
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
2830
)
2931

32+
// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.
33+
3034
func TestFuzzyConversion(t *testing.T) {
3135
t.Run("for ClusterConfiguration", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
3236
Hub: &bootstrapv1.ClusterConfiguration{},

bootstrap/kubeadm/types/upstreamv1beta2/conversion_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build !race
2+
13
/*
24
Copyright 2021 The Kubernetes Authors.
35
@@ -27,6 +29,8 @@ import (
2729
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
2830
)
2931

32+
// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.
33+
3034
func TestFuzzyConversion(t *testing.T) {
3135
t.Run("for ClusterConfiguration", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
3236
Hub: &bootstrapv1.ClusterConfiguration{},

bootstrap/kubeadm/types/upstreamv1beta3/conversion_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build !race
2+
13
/*
24
Copyright 2021 The Kubernetes Authors.
35
@@ -29,6 +31,8 @@ import (
2931
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
3032
)
3133

34+
// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.
35+
3236
func TestFuzzyConversion(t *testing.T) {
3337
g := NewWithT(t)
3438
scheme := runtime.NewScheme()
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package upstreamv1beta4
18+
19+
import (
20+
"testing"
21+
"time"
22+
23+
. "github.com/onsi/gomega"
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
26+
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
27+
)
28+
29+
// This test case has been moved out of conversion_test.go because it should be run with the race detector.
30+
// Note: The tests in conversion_test.go are disabled when the race detector is enabled (via "//go:build !race") because otherwise the fuzz tests would just time out.
31+
32+
func TestTimeoutForControlPlaneMigration(t *testing.T) {
33+
timeout := metav1.Duration{Duration: 10 * time.Second}
34+
t.Run("from ClusterConfiguration to InitConfiguration and back", func(t *testing.T) {
35+
g := NewWithT(t)
36+
37+
clusterConfiguration := &bootstrapv1.ClusterConfiguration{
38+
APIServer: bootstrapv1.APIServer{TimeoutForControlPlane: &timeout},
39+
}
40+
41+
initConfiguration := &InitConfiguration{}
42+
err := initConfiguration.ConvertFromClusterConfiguration(clusterConfiguration)
43+
g.Expect(err).ToNot(HaveOccurred())
44+
g.Expect(initConfiguration.Timeouts.ControlPlaneComponentHealthCheck).To(Equal(&timeout))
45+
46+
clusterConfiguration = &bootstrapv1.ClusterConfiguration{}
47+
err = initConfiguration.ConvertToClusterConfiguration(clusterConfiguration)
48+
g.Expect(err).ToNot(HaveOccurred())
49+
g.Expect(clusterConfiguration.APIServer.TimeoutForControlPlane).To(Equal(&timeout))
50+
})
51+
t.Run("from ClusterConfiguration to JoinConfiguration and back", func(t *testing.T) {
52+
g := NewWithT(t)
53+
54+
clusterConfiguration := &bootstrapv1.ClusterConfiguration{
55+
APIServer: bootstrapv1.APIServer{TimeoutForControlPlane: &timeout},
56+
}
57+
58+
joinConfiguration := &JoinConfiguration{}
59+
err := joinConfiguration.ConvertFromClusterConfiguration(clusterConfiguration)
60+
g.Expect(err).ToNot(HaveOccurred())
61+
g.Expect(joinConfiguration.Timeouts.ControlPlaneComponentHealthCheck).To(Equal(&timeout))
62+
63+
clusterConfiguration = &bootstrapv1.ClusterConfiguration{}
64+
err = joinConfiguration.ConvertToClusterConfiguration(clusterConfiguration)
65+
g.Expect(err).ToNot(HaveOccurred())
66+
g.Expect(clusterConfiguration.APIServer.TimeoutForControlPlane).To(Equal(&timeout))
67+
})
68+
}

bootstrap/kubeadm/types/upstreamv1beta4/conversion_test.go

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build !race
2+
13
/*
24
Copyright 2021 The Kubernetes Authors.
35
@@ -18,19 +20,19 @@ package upstreamv1beta4
1820

1921
import (
2022
"testing"
21-
"time"
2223

2324
fuzz "github.com/google/gofuzz"
2425
. "github.com/onsi/gomega"
2526
"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
26-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727
"k8s.io/apimachinery/pkg/runtime"
2828
runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
2929

3030
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
3131
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
3232
)
3333

34+
// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.
35+
3436
func TestFuzzyConversion(t *testing.T) {
3537
g := NewWithT(t)
3638
scheme := runtime.NewScheme()
@@ -144,41 +146,3 @@ func bootstrapv1JoinConfigurationFuzzer(obj *bootstrapv1.JoinConfiguration, c fu
144146
obj.Discovery.File.KubeConfig = nil
145147
}
146148
}
147-
148-
func TestTimeoutForControlPlaneMigration(t *testing.T) {
149-
timeout := metav1.Duration{Duration: 10 * time.Second}
150-
t.Run("from ClusterConfiguration to InitConfiguration and back", func(t *testing.T) {
151-
g := NewWithT(t)
152-
153-
clusterConfiguration := &bootstrapv1.ClusterConfiguration{
154-
APIServer: bootstrapv1.APIServer{TimeoutForControlPlane: &timeout},
155-
}
156-
157-
initConfiguration := &InitConfiguration{}
158-
err := initConfiguration.ConvertFromClusterConfiguration(clusterConfiguration)
159-
g.Expect(err).ToNot(HaveOccurred())
160-
g.Expect(initConfiguration.Timeouts.ControlPlaneComponentHealthCheck).To(Equal(&timeout))
161-
162-
clusterConfiguration = &bootstrapv1.ClusterConfiguration{}
163-
err = initConfiguration.ConvertToClusterConfiguration(clusterConfiguration)
164-
g.Expect(err).ToNot(HaveOccurred())
165-
g.Expect(clusterConfiguration.APIServer.TimeoutForControlPlane).To(Equal(&timeout))
166-
})
167-
t.Run("from ClusterConfiguration to JoinConfiguration and back", func(t *testing.T) {
168-
g := NewWithT(t)
169-
170-
clusterConfiguration := &bootstrapv1.ClusterConfiguration{
171-
APIServer: bootstrapv1.APIServer{TimeoutForControlPlane: &timeout},
172-
}
173-
174-
joinConfiguration := &JoinConfiguration{}
175-
err := joinConfiguration.ConvertFromClusterConfiguration(clusterConfiguration)
176-
g.Expect(err).ToNot(HaveOccurred())
177-
g.Expect(joinConfiguration.Timeouts.ControlPlaneComponentHealthCheck).To(Equal(&timeout))
178-
179-
clusterConfiguration = &bootstrapv1.ClusterConfiguration{}
180-
err = joinConfiguration.ConvertToClusterConfiguration(clusterConfiguration)
181-
g.Expect(err).ToNot(HaveOccurred())
182-
g.Expect(clusterConfiguration.APIServer.TimeoutForControlPlane).To(Equal(&timeout))
183-
})
184-
}

exp/internal/controllers/machinepool_controller_test.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ import (
2424

2525
. "github.com/onsi/gomega"
2626
corev1 "k8s.io/api/core/v1"
27+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2728
apierrors "k8s.io/apimachinery/pkg/api/errors"
2829
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2930
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
30-
"k8s.io/client-go/kubernetes/scheme"
31+
"k8s.io/apimachinery/pkg/runtime"
32+
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3133
"k8s.io/utils/ptr"
3234
ctrl "sigs.k8s.io/controller-runtime"
3335
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -834,6 +836,13 @@ func TestRemoveMachinePoolFinalizerAfterDeleteReconcile(t *testing.T) {
834836
}
835837

836838
func TestMachinePoolConditions(t *testing.T) {
839+
g := NewWithT(t)
840+
scheme := runtime.NewScheme()
841+
g.Expect(apiextensionsv1.AddToScheme(scheme)).To(Succeed())
842+
g.Expect(clientgoscheme.AddToScheme(scheme)).To(Succeed())
843+
g.Expect(clusterv1.AddToScheme(scheme)).To(Succeed())
844+
g.Expect(expv1.AddToScheme(scheme)).To(Succeed())
845+
837846
testCluster := &clusterv1.Cluster{
838847
ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault, Name: "test-cluster"},
839848
}
@@ -1079,9 +1088,7 @@ func TestMachinePoolConditions(t *testing.T) {
10791088
tt.beforeFunc(bootstrap, infra, mp, nodes)
10801089
}
10811090

1082-
g.Expect(clusterv1.AddToScheme(scheme.Scheme)).To(Succeed())
1083-
1084-
clientFake := fake.NewClientBuilder().WithObjects(
1091+
clientFake := fake.NewClientBuilder().WithScheme(scheme).WithObjects(
10851092
testCluster,
10861093
mp,
10871094
infra,

exp/ipam/api/v1alpha1/conversion_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build !race
2+
13
/*
24
Copyright 2021 The Kubernetes Authors.
35
@@ -25,6 +27,8 @@ import (
2527
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
2628
)
2729

30+
// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.
31+
2832
func TestFuzzyConversion(t *testing.T) {
2933
t.Run("for IPAddress", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
3034
Hub: &ipamv1.IPAddress{},

internal/apis/bootstrap/kubeadm/v1alpha3/conversion_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build !race
2+
13
/*
24
Copyright 2020 The Kubernetes Authors.
35
@@ -28,6 +30,8 @@ import (
2830
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
2931
)
3032

33+
// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.
34+
3135
func TestFuzzyConversion(t *testing.T) {
3236
t.Run("for KubeadmConfig", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
3337
Hub: &bootstrapv1.KubeadmConfig{},

internal/apis/bootstrap/kubeadm/v1alpha4/conversion_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build !race
2+
13
/*
24
Copyright 2021 The Kubernetes Authors.
35
@@ -32,6 +34,8 @@ const (
3234
fakeSecret = "abcdef0123456789"
3335
)
3436

37+
// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.
38+
3539
func TestFuzzyConversion(t *testing.T) {
3640
t.Run("for KubeadmConfig", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
3741
Hub: &bootstrapv1.KubeadmConfig{},

0 commit comments

Comments
 (0)