Skip to content

Commit

Permalink
Enable race detector for top-level unit tests (#11207)
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Büringer [email protected]

Signed-off-by: Stefan Büringer [email protected]
  • Loading branch information
sbueringer authored Sep 20, 2024
1 parent fef53c1 commit 483276e
Show file tree
Hide file tree
Showing 23 changed files with 182 additions and 49 deletions.
21 changes: 16 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -906,19 +906,30 @@ KUBEBUILDER_ASSETS ?= $(shell $(SETUP_ENVTEST) use --use-env -p path $(KUBEBUILD
setup-envtest: $(SETUP_ENVTEST) ## Set up envtest (download kubebuilder assets)
@echo KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS)

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

.PHONY: test
test: $(SETUP_ENVTEST) ## Run unit and integration tests with race detector
# Note: Fuzz tests are not executed with race detector because they would just time out.
# To achieve that, all files with fuzz tests have the "!race" build tag, to still run fuzz tests
# we have an additional `go test` run that focuses on "TestFuzzyConversion".
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
test-verbose: ## Run unit and integration tests with race detector and 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
test-junit: $(SETUP_ENVTEST) $(GOTESTSUM) ## Run unit and integration tests with race detector and generate a junit report
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 case has been moved out of conversion_test.go because it should be run with the race detector.
// 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.

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))
})
}
15 changes: 11 additions & 4 deletions exp/internal/controllers/machinepool_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ import (

. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -834,6 +836,13 @@ func TestRemoveMachinePoolFinalizerAfterDeleteReconcile(t *testing.T) {
}

func TestMachinePoolConditions(t *testing.T) {
g := NewWithT(t)
scheme := runtime.NewScheme()
g.Expect(apiextensionsv1.AddToScheme(scheme)).To(Succeed())
g.Expect(clientgoscheme.AddToScheme(scheme)).To(Succeed())
g.Expect(clusterv1.AddToScheme(scheme)).To(Succeed())
g.Expect(expv1.AddToScheme(scheme)).To(Succeed())

testCluster := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault, Name: "test-cluster"},
}
Expand Down Expand Up @@ -1079,9 +1088,7 @@ func TestMachinePoolConditions(t *testing.T) {
tt.beforeFunc(bootstrap, infra, mp, nodes)
}

g.Expect(clusterv1.AddToScheme(scheme.Scheme)).To(Succeed())

clientFake := fake.NewClientBuilder().WithObjects(
clientFake := fake.NewClientBuilder().WithScheme(scheme).WithObjects(
testCluster,
mp,
infra,
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
Loading

0 comments on commit 483276e

Please sign in to comment.