Skip to content

Commit

Permalink
Setup separate context for test files to avoid flaky errors
Browse files Browse the repository at this point in the history
Signed-off-by: Gong Zhang <[email protected]>
  • Loading branch information
zhanggbj committed Oct 25, 2023
1 parent fda92b9 commit 25028d6
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 8 deletions.
3 changes: 3 additions & 0 deletions controllers/clustermodule_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package controllers

import (
"context"
"testing"
"time"

Expand All @@ -38,6 +39,7 @@ import (
)

func TestReconciler_Reconcile(t *testing.T) {
ctx := context.Background()
kcpUUID, mdUUID := uuid.New().String(), uuid.New().String()
kcp := controlPlane("kcp", metav1.NamespaceDefault, fake.Clusterv1a2Name)
md := machineDeployment("md", metav1.NamespaceDefault, fake.Clusterv1a2Name)
Expand Down Expand Up @@ -437,6 +439,7 @@ func TestReconciler_Reconcile(t *testing.T) {
}

func TestReconciler_fetchMachineOwnerObjects(t *testing.T) {
ctx := context.Background()
tests := []struct {
name string
numOfMDs int
Expand Down
7 changes: 6 additions & 1 deletion controllers/serviceaccount_controller_intg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package controllers

import (
"context"
"fmt"
"os"
"reflect"
Expand All @@ -40,9 +41,13 @@ import (
)

var _ = Describe("ProviderServiceAccount controller integration tests", func() {
var intCtx *helpers.IntegrationTestContext
var (
ctx context.Context
intCtx *helpers.IntegrationTestContext
)

BeforeEach(func() {
ctx = context.Background()
intCtx = helpers.NewIntegrationTestContextWithClusters(ctx, testEnv.Manager.GetClient())
testSystemSvcAcctCM := "test-system-svc-acct-cm"
cfgMap := getSystemServiceAccountsConfigMap(intCtx.VSphereCluster.Namespace, testSystemSvcAcctCM)
Expand Down
2 changes: 2 additions & 0 deletions controllers/serviceaccount_controller_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var _ = Describe("ServiceAccountReconciler ReconcileNormal", unitTestsReconcileN

func unitTestsReconcileNormal() {
var (
ctx context.Context
controllerCtx *helpers.UnitTestContextForController
vsphereCluster *vmwarev1.VSphereCluster
initObjects []client.Object
Expand All @@ -44,6 +45,7 @@ func unitTestsReconcileNormal() {
)

JustBeforeEach(func() {
ctx = context.Background()
controllerCtx = helpers.NewUnitTestContextForController(namespace, vsphereCluster, false, initObjects, nil)
// Note: The service account provider requires a reference to the vSphereCluster hence the need to create
// a fake vSphereCluster in the test and pass it to during context setup.
Expand Down
3 changes: 3 additions & 0 deletions controllers/servicediscovery_controller_intg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package controllers

import (
"context"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
Expand All @@ -27,10 +28,12 @@ import (

var _ = Describe("Service Discovery controller integration tests", func() {
var (
ctx context.Context
intCtx *helpers.IntegrationTestContext
initObjects []client.Object
)
BeforeEach(func() {
ctx = context.Background()
intCtx = helpers.NewIntegrationTestContextWithClusters(ctx, testEnv.Manager.GetClient())
})
AfterEach(func() {
Expand Down
3 changes: 3 additions & 0 deletions controllers/servicediscovery_controller_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package controllers

import (
"context"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
Expand All @@ -34,13 +35,15 @@ var _ = Describe("ServiceDiscoveryReconciler ReconcileNormal", serviceDiscoveryU

func serviceDiscoveryUnitTestsReconcileNormal() {
var (
ctx context.Context
controllerCtx *helpers.UnitTestContextForController
vsphereCluster vmwarev1.VSphereCluster
initObjects []client.Object
reconciler serviceDiscoveryReconciler
)
namespace := capiutil.RandomString(6)
JustBeforeEach(func() {
ctx = context.Background()
vsphereCluster = fake.NewVSphereCluster(namespace)
controllerCtx = helpers.NewUnitTestContextForController(namespace, &vsphereCluster, false, initObjects, nil)
reconciler = serviceDiscoveryReconciler{
Expand Down
13 changes: 6 additions & 7 deletions controllers/vspherecluster_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ const (
)

var _ = Describe("VIM based VSphere ClusterReconciler", func() {
BeforeEach(func() {})
var ctx context.Context

BeforeEach(func() {
ctx = context.Background()
})
AfterEach(func() {})

Context("Reconcile an VSphereCluster", func() {
Expand Down Expand Up @@ -161,8 +165,6 @@ var _ = Describe("VIM based VSphere ClusterReconciler", func() {
})

It("should error if secret is already owned by a different cluster", func() {
ctx := context.Background()

capiCluster := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test1-",
Expand Down Expand Up @@ -246,15 +248,13 @@ var _ = Describe("VIM based VSphere ClusterReconciler", func() {
})
Context("Reconcile delete", func() {
var (
ctx context.Context
secret *corev1.Secret
capiCluster *clusterv1.Cluster
instance *infrav1.VSphereCluster
legacyFinalizer = "identity/infrastructure.cluster.x-k8s.io"
key client.ObjectKey
)
It("should remove legacy finalizer if present during the cluster deletion", func() {
ctx = context.Background()
capiCluster = &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test1-",
Expand Down Expand Up @@ -357,8 +357,6 @@ var _ = Describe("VIM based VSphere ClusterReconciler", func() {
})

It("should remove vspherecluster finalizer if the secret does not exist", func() {
ctx := context.Background()

capiCluster := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test1-",
Expand Down Expand Up @@ -665,6 +663,7 @@ func createVsphereMachine(ctx context.Context, env *helpers.TestEnvironment, nam

func TestClusterReconciler_ReconcileDeploymentZones(t *testing.T) {
server := "vcenter123.foo.com"
ctx := context.Background()

t.Run("with nil selectors", func(t *testing.T) {
g := NewWithT(t)
Expand Down
8 changes: 8 additions & 0 deletions controllers/vsphereclusteridentity_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package controllers

import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
Expand All @@ -29,6 +31,12 @@ import (
)

var _ = Describe("VSphereClusterIdentity Reconciler", func() {
var ctx context.Context

BeforeEach(func() {
ctx = context.Background()
})

controllerNamespace := testEnv.Manager.GetContext().Namespace

Context("Reconcile Normal", func() {
Expand Down
4 changes: 4 additions & 0 deletions controllers/vspheredeploymentzone_controller_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package controllers

import (
"context"
"testing"

"github.com/go-logr/logr"
Expand All @@ -33,6 +34,7 @@ import (
)

func TestVsphereDeploymentZoneReconciler_Reconcile_VerifyFailureDomain_ComputeClusterZone(t *testing.T) {
ctx := context.Background()
g := NewWithT(t)

model := simulator.VPX()
Expand Down Expand Up @@ -114,6 +116,7 @@ func TestVsphereDeploymentZoneReconciler_Reconcile_VerifyFailureDomain_ComputeCl
}

func TestVsphereDeploymentZoneReconciler_Reconcile_VerifyFailureDomain_HostGroupZone(t *testing.T) {
ctx := context.Background()
g := NewWithT(t)

model := simulator.VPX()
Expand Down Expand Up @@ -195,6 +198,7 @@ func TestVsphereDeploymentZoneReconciler_Reconcile_VerifyFailureDomain_HostGroup
}

func TestVsphereDeploymentZoneReconciler_Reconcile_CreateAndAttachMetadata(t *testing.T) {
ctx := context.Background()
simr, err := vcsim.NewBuilder().
WithOperations("cluster.group.create -cluster DC0_C0 -name group-one -host DC0_C0_H0 DC0_C0_H1",
"cluster.group.create -cluster DC0_C0 -name group-two -host DC0_C0_H2").
Expand Down
6 changes: 6 additions & 0 deletions controllers/vspheredeploymentzone_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (

var _ = Describe("VSphereDeploymentZoneReconciler", func() {
var (
ctx context.Context
simr *vcsim.Simulator

failureDomainKey, deploymentZoneKey client.ObjectKey
Expand Down Expand Up @@ -76,6 +77,7 @@ var _ = Describe("VSphereDeploymentZoneReconciler", func() {
})

BeforeEach(func() {
ctx = context.Background()
vsphereFailureDomain = &infrav1.VSphereFailureDomain{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "blah-fd-",
Expand Down Expand Up @@ -302,6 +304,7 @@ var _ = Describe("VSphereDeploymentZoneReconciler", func() {
})

func TestVSphereDeploymentZone_Reconcile(t *testing.T) {
ctx := context.Background()
g := NewWithT(t)
model := simulator.VPX()
model.Pool = 1
Expand Down Expand Up @@ -593,6 +596,8 @@ func TestVsphereDeploymentZone_Failed_ReconcilePlacementConstraint(t *testing.T)
},
}

ctx := context.Background()

for _, tt := range tests {
// Looks odd, but need to reinitialize test variable
tt := tt
Expand Down Expand Up @@ -648,6 +653,7 @@ func TestVsphereDeploymentZone_Failed_ReconcilePlacementConstraint(t *testing.T)
}

func TestVSphereDeploymentZoneReconciler_ReconcileDelete(t *testing.T) {
ctx := context.Background()
vsphereDeploymentZone := &infrav1.VSphereDeploymentZone{
TypeMeta: metav1.TypeMeta{
Kind: "VSphereDeploymentZone",
Expand Down
4 changes: 4 additions & 0 deletions controllers/vspheremachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package controllers

import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
Expand All @@ -32,6 +34,7 @@ import (

var _ = Describe("VsphereMachineReconciler", func() {
var (
ctx context.Context
capiCluster *clusterv1.Cluster
capiMachine *clusterv1.Machine

Expand All @@ -54,6 +57,7 @@ var _ = Describe("VsphereMachineReconciler", func() {

BeforeEach(func() {
var err error
ctx = context.Background()
testNs, err = testEnv.CreateNamespace(ctx, "vsphere-machine-reconciler")
Expect(err).NotTo(HaveOccurred())

Expand Down
3 changes: 3 additions & 0 deletions controllers/vspherevm_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import (

func TestReconcileNormal_WaitingForIPAddrAllocation(t *testing.T) {
var (
ctx context.Context
machine *clusterv1.Machine
cluster *clusterv1.Cluster

Expand All @@ -64,6 +65,7 @@ func TestReconcileNormal_WaitingForIPAddrAllocation(t *testing.T) {
ipAddressClaim *ipamv1.IPAddressClaim
)

ctx = context.Background()
poolAPIGroup := "some.ipam.api.group"

// initializing a fake server to replace the vSphere endpoint
Expand Down Expand Up @@ -534,6 +536,7 @@ func TestRetrievingVCenterCredentialsFromCluster(t *testing.T) {
}

func Test_reconcile(t *testing.T) {
ctx := context.Background()
ns := "test"
vsphereCluster := &infrav1.VSphereCluster{
ObjectMeta: metav1.ObjectMeta{
Expand Down
1 change: 1 addition & 0 deletions controllers/vspherevm_ipaddress_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
)

func Test_vmReconciler_reconcileIPAddressClaims(t *testing.T) {
ctx := context.Background()
name, namespace := "test-vm", "my-namespace"
setup := func(vsphereVM *infrav1.VSphereVM, initObjects ...client.Object) *capvcontext.VMContext {
ctx := fake.NewControllerContext(fake.NewControllerManagerContext(initObjects...))
Expand Down

0 comments on commit 25028d6

Please sign in to comment.