Skip to content

Commit

Permalink
tests: move NewWithT(t) to inside the t.Run where applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
cpanato committed Mar 27, 2020
1 parent 1c86f1c commit 59036db
Show file tree
Hide file tree
Showing 46 changed files with 278 additions and 231 deletions.
30 changes: 22 additions & 8 deletions api/v1alpha2/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ func TestFuzzyConversion(t *testing.T) {
}

func TestConvertCluster(t *testing.T) {
g := NewWithT(t)

t.Run("to hub", func(t *testing.T) {
t.Run("should convert the first value in Status.APIEndpoints to Spec.ControlPlaneEndpoint", func(t *testing.T) {
g := NewWithT(t)

src := &Cluster{
Status: ClusterStatus{
APIEndpoints: []APIEndpoint{
Expand All @@ -66,6 +66,8 @@ func TestConvertCluster(t *testing.T) {

t.Run("from hub", func(t *testing.T) {
t.Run("preserves fields from hub version", func(t *testing.T) {
g := NewWithT(t)

src := &v1alpha3.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "hub",
Expand All @@ -92,6 +94,8 @@ func TestConvertCluster(t *testing.T) {
})

t.Run("should convert Spec.ControlPlaneEndpoint to Status.APIEndpoints[0]", func(t *testing.T) {
g := NewWithT(t)

src := &v1alpha3.Cluster{
Spec: v1alpha3.ClusterSpec{
ControlPlaneEndpoint: v1alpha3.APIEndpoint{
Expand All @@ -110,10 +114,10 @@ func TestConvertCluster(t *testing.T) {
}

func TestConvertMachine(t *testing.T) {
g := NewWithT(t)

t.Run("to hub", func(t *testing.T) {
t.Run("should convert the Spec.ClusterName from label", func(t *testing.T) {
g := NewWithT(t)

src := &Machine{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand All @@ -130,6 +134,8 @@ func TestConvertMachine(t *testing.T) {

t.Run("from hub", func(t *testing.T) {
t.Run("preserves fields from hub version", func(t *testing.T) {
g := NewWithT(t)

failureDomain := "my failure domain"
src := &v1alpha3.Machine{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -159,10 +165,10 @@ func TestConvertMachine(t *testing.T) {
}

func TestConvertMachineSet(t *testing.T) {
g := NewWithT(t)

t.Run("to hub", func(t *testing.T) {
t.Run("should convert the Spec.ClusterName from label", func(t *testing.T) {
g := NewWithT(t)

src := &MachineSet{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand All @@ -180,6 +186,8 @@ func TestConvertMachineSet(t *testing.T) {

t.Run("from hub", func(t *testing.T) {
t.Run("preserves field from hub version", func(t *testing.T) {
g := NewWithT(t)

src := &v1alpha3.MachineSet{
ObjectMeta: metav1.ObjectMeta{
Name: "hub",
Expand Down Expand Up @@ -208,10 +216,10 @@ func TestConvertMachineSet(t *testing.T) {
}

func TestConvertMachineDeployment(t *testing.T) {
g := NewWithT(t)

t.Run("to hub", func(t *testing.T) {
t.Run("should convert the Spec.ClusterName from label", func(t *testing.T) {
g := NewWithT(t)

src := &MachineDeployment{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand All @@ -227,6 +235,8 @@ func TestConvertMachineDeployment(t *testing.T) {
})

t.Run("should convert the annotations", func(t *testing.T) {
g := NewWithT(t)

src := &MachineDeployment{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
Expand All @@ -249,6 +259,8 @@ func TestConvertMachineDeployment(t *testing.T) {

t.Run("from hub", func(t *testing.T) {
t.Run("preserves fields from hub version", func(t *testing.T) {
g := NewWithT(t)

src := &v1alpha3.MachineDeployment{
ObjectMeta: metav1.ObjectMeta{
Name: "hub",
Expand Down Expand Up @@ -278,6 +290,8 @@ func TestConvertMachineDeployment(t *testing.T) {
})

t.Run("should convert the annotations", func(t *testing.T) {
g := NewWithT(t)

src := &v1alpha3.MachineDeployment{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
Expand Down
5 changes: 2 additions & 3 deletions bootstrap/kubeadm/api/v1alpha2/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import (
)

func TestConvertKubeadmConfig(t *testing.T) {
g := NewWithT(t)

t.Run("from hub", func(t *testing.T) {
t.Run("preserves fields from hub version", func(t *testing.T) {
g := NewWithT(t)

src := &v1alpha3.KubeadmConfig{
ObjectMeta: metav1.ObjectMeta{
Name: "hub",
Expand All @@ -52,6 +52,5 @@ func TestConvertKubeadmConfig(t *testing.T) {
g.Expect(restored.Status.Ready).To(Equal(src.Status.Ready))
g.Expect(restored.Status.DataSecretName).To(Equal(src.Status.DataSecretName))
})

})
}
28 changes: 14 additions & 14 deletions bootstrap/kubeadm/controllers/kubeadmconfig_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,6 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnNilIfAssociatedClusterIsNotFoun

// If the control plane isn't initialized then there is no cluster for either a worker or control plane node to join.
func TestKubeadmConfigReconciler_Reconcile_RequeueJoiningNodesIfControlPlaneNotInitialized(t *testing.T) {
g := NewWithT(t)

cluster := newCluster("cluster")
cluster.Status.InfrastructureReady = true

Expand Down Expand Up @@ -409,6 +407,8 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueJoiningNodesIfControlPlaneNotI
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)

myclient := fake.NewFakeClientWithScheme(setupScheme(), tc.objects...)

k := &KubeadmConfigReconciler{
Expand Down Expand Up @@ -552,8 +552,6 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueIfControlPlaneIsMissingAPIEndp
}

func TestReconcileIfJoinNodesAndControlPlaneIsReady(t *testing.T) {
g := NewWithT(t)

cluster := newCluster("cluster")
cluster.Status.InfrastructureReady = true
cluster.Status.ControlPlaneInitialized = true
Expand Down Expand Up @@ -596,6 +594,8 @@ func TestReconcileIfJoinNodesAndControlPlaneIsReady(t *testing.T) {
for _, rt := range useCases {
rt := rt // pin!
t.Run(rt.name, func(t *testing.T) {
g := NewWithT(t)

config := rt.configBuilder(rt.machine, rt.configName)

objects := []runtime.Object{
Expand Down Expand Up @@ -638,8 +638,6 @@ func TestReconcileIfJoinNodesAndControlPlaneIsReady(t *testing.T) {
}

func TestReconcileIfJoinNodePoolsAndControlPlaneIsReady(t *testing.T) {
g := NewWithT(t)

_ = feature.MutableGates.Set("MachinePool=true")

cluster := newCluster("cluster")
Expand Down Expand Up @@ -672,6 +670,8 @@ func TestReconcileIfJoinNodePoolsAndControlPlaneIsReady(t *testing.T) {
for _, rt := range useCases {
rt := rt // pin!
t.Run(rt.name, func(t *testing.T) {
g := NewWithT(t)

config := rt.configBuilder(rt.machinePool, rt.configName)

objects := []runtime.Object{
Expand Down Expand Up @@ -863,8 +863,6 @@ func TestBootstrapTokenTTLExtension(t *testing.T) {

// Ensure the discovery portion of the JoinConfiguration gets generated correctly.
func TestKubeadmConfigReconciler_Reconcile_DisocveryReconcileBehaviors(t *testing.T) {
g := NewWithT(t)

k := &KubeadmConfigReconciler{
Log: log.Log,
Client: fake.NewFakeClientWithScheme(setupScheme()),
Expand Down Expand Up @@ -995,6 +993,8 @@ func TestKubeadmConfigReconciler_Reconcile_DisocveryReconcileBehaviors(t *testin

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)

err := k.reconcileDiscovery(context.Background(), tc.cluster, tc.config, secret.Certificates{})
g.Expect(err).NotTo(HaveOccurred())

Expand All @@ -1006,8 +1006,6 @@ func TestKubeadmConfigReconciler_Reconcile_DisocveryReconcileBehaviors(t *testin

// Test failure cases for the discovery reconcile function.
func TestKubeadmConfigReconciler_Reconcile_DisocveryReconcileFailureBehaviors(t *testing.T) {
g := NewWithT(t)

k := &KubeadmConfigReconciler{
Log: log.Log,
Client: nil,
Expand Down Expand Up @@ -1037,6 +1035,8 @@ func TestKubeadmConfigReconciler_Reconcile_DisocveryReconcileFailureBehaviors(t

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)

err := k.reconcileDiscovery(context.Background(), tc.cluster, tc.config, secret.Certificates{})
g.Expect(err).To(HaveOccurred())
})
Expand All @@ -1045,8 +1045,6 @@ func TestKubeadmConfigReconciler_Reconcile_DisocveryReconcileFailureBehaviors(t

// Set cluster configuration defaults based on dynamic values from the cluster object.
func TestKubeadmConfigReconciler_Reconcile_DynamicDefaultsForClusterConfiguration(t *testing.T) {
g := NewWithT(t)

k := &KubeadmConfigReconciler{
Log: log.Log,
Client: nil,
Expand Down Expand Up @@ -1123,6 +1121,8 @@ func TestKubeadmConfigReconciler_Reconcile_DynamicDefaultsForClusterConfiguratio

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)

k.reconcileTopLevelObjectSettings(tc.cluster, tc.machine, tc.config)

g.Expect(tc.config.Spec.ClusterConfiguration.ControlPlaneEndpoint).To(Equal("myControlPlaneEndpoint:6443"))
Expand All @@ -1137,8 +1137,6 @@ func TestKubeadmConfigReconciler_Reconcile_DynamicDefaultsForClusterConfiguratio

// Allow users to skip CA Verification if they *really* want to.
func TestKubeadmConfigReconciler_Reconcile_AlwaysCheckCAVerificationUnlessRequestedToSkip(t *testing.T) {
g := NewWithT(t)

// Setup work for an initialized cluster
clusterName := "my-cluster"
cluster := newCluster(clusterName)
Expand Down Expand Up @@ -1194,6 +1192,8 @@ func TestKubeadmConfigReconciler_Reconcile_AlwaysCheckCAVerificationUnlessReques
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)

myclient := fake.NewFakeClientWithScheme(setupScheme(), objects...)
reconciler := KubeadmConfigReconciler{
Client: myclient,
Expand Down
4 changes: 2 additions & 2 deletions bootstrap/kubeadm/internal/cloudinit/controlplane_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import (
)

func TestTemplateYAMLIndent(t *testing.T) {
g := NewWithT(t)

testcases := []struct {
name string
input string
Expand All @@ -48,6 +46,8 @@ func TestTemplateYAMLIndent(t *testing.T) {
for _, tc := range testcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)

g.Expect(templateYAMLIndent(tc.indent, tc.input)).To(Equal(tc.expected))
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ func TestControlPlaneInitMutex_Lock(t *testing.T) {
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)

l := &ControlPlaneInitMutex{
log: log.Log,
client: tc.client,
Expand Down Expand Up @@ -192,6 +194,8 @@ func TestControlPlaneInitMutex_UnLock(t *testing.T) {
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)

l := &ControlPlaneInitMutex{
log: log.Log,
client: tc.client,
Expand Down
Loading

0 comments on commit 59036db

Please sign in to comment.