Skip to content

Commit

Permalink
Enable ginkgolinter len assertion check and fix findings
Browse files Browse the repository at this point in the history
  • Loading branch information
oscr committed Feb 9, 2023
1 parent 327aae2 commit 7ab078e
Show file tree
Hide file tree
Showing 29 changed files with 65 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ linters-settings:
local-prefixes: "sigs.k8s.io/cluster-api"
ginkgolinter:
# Suppress the wrong length assertion warning.
suppress-len-assertion: true
suppress-len-assertion: false
# Suppress the wrong nil assertion warning.
suppress-nil-assertion: false
# Suppress the wrong error assertion warning.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ func TestKubeadmConfigReconciler_Reconcile_GenerateCloudConfigData(t *testing.T)

// Expect the Secret to exist, and for it to contain some data under the "value" key.
g.Expect(myclient.Get(ctx, client.ObjectKey{Namespace: metav1.NamespaceDefault, Name: configName}, s)).To(Succeed())
g.Expect(len(s.Data["value"])).To(BeNumerically(">", 0))
g.Expect(s.Data["value"]).ToNot(BeEmpty())
// Ensure that we don't fail trying to refresh any bootstrap tokens
_, err = k.Reconcile(ctx, request)
g.Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -703,7 +703,7 @@ func TestReconcileIfJoinCertificatesAvailableConditioninNodesAndControlPlaneIsRe
l := &corev1.SecretList{}
err = myclient.List(ctx, l, client.ListOption(client.InNamespace(metav1.NamespaceSystem)))
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(l.Items)).To(Equal(1))
g.Expect(l.Items).To(HaveLen(1))
})
}
}
Expand Down Expand Up @@ -779,7 +779,7 @@ func TestReconcileIfJoinNodePoolsAndControlPlaneIsReady(t *testing.T) {
l := &corev1.SecretList{}
err = myclient.List(ctx, l, client.ListOption(client.InNamespace(metav1.NamespaceSystem)))
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(l.Items)).To(Equal(1))
g.Expect(l.Items).To(HaveLen(1))
})
}
}
Expand Down Expand Up @@ -1053,7 +1053,7 @@ func TestBootstrapTokenTTLExtension(t *testing.T) {
l := &corev1.SecretList{}
err = myclient.List(ctx, l, client.ListOption(client.InNamespace(metav1.NamespaceSystem)))
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(l.Items)).To(Equal(2))
g.Expect(l.Items).To(HaveLen(2))

// ensure that the token is refreshed...
tokenExpires := make([][]byte, len(l.Items))
Expand Down Expand Up @@ -1086,7 +1086,7 @@ func TestBootstrapTokenTTLExtension(t *testing.T) {
l = &corev1.SecretList{}
err = myclient.List(ctx, l, client.ListOption(client.InNamespace(metav1.NamespaceSystem)))
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(l.Items)).To(Equal(2))
g.Expect(l.Items).To(HaveLen(2))

for i, item := range l.Items {
g.Expect(bytes.Equal(tokenExpires[i], item.Data[bootstrapapi.BootstrapTokenExpirationKey])).To(BeFalse())
Expand Down Expand Up @@ -1128,7 +1128,7 @@ func TestBootstrapTokenTTLExtension(t *testing.T) {
l = &corev1.SecretList{}
err = myclient.List(ctx, l, client.ListOption(client.InNamespace(metav1.NamespaceSystem)))
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(l.Items)).To(Equal(2))
g.Expect(l.Items).To(HaveLen(2))

for i, item := range l.Items {
g.Expect(bytes.Equal(tokenExpires[i], item.Data[bootstrapapi.BootstrapTokenExpirationKey])).To(BeFalse())
Expand Down Expand Up @@ -1179,7 +1179,7 @@ func TestBootstrapTokenTTLExtension(t *testing.T) {
l = &corev1.SecretList{}
err = myclient.List(ctx, l, client.ListOption(client.InNamespace(metav1.NamespaceSystem)))
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(l.Items)).To(Equal(2))
g.Expect(l.Items).To(HaveLen(2))

for i, item := range l.Items {
g.Expect(bytes.Equal(tokenExpires[i], item.Data[bootstrapapi.BootstrapTokenExpirationKey])).To(BeTrue())
Expand Down Expand Up @@ -1237,7 +1237,7 @@ func TestBootstrapTokenRotationMachinePool(t *testing.T) {
l := &corev1.SecretList{}
err = myclient.List(ctx, l, client.ListOption(client.InNamespace(metav1.NamespaceSystem)))
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(l.Items)).To(Equal(1))
g.Expect(l.Items).To(HaveLen(1))

// ensure that the token is refreshed...
tokenExpires := make([][]byte, len(l.Items))
Expand All @@ -1264,7 +1264,7 @@ func TestBootstrapTokenRotationMachinePool(t *testing.T) {
l = &corev1.SecretList{}
err = myclient.List(ctx, l, client.ListOption(client.InNamespace(metav1.NamespaceSystem)))
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(l.Items)).To(Equal(1))
g.Expect(l.Items).To(HaveLen(1))

for i, item := range l.Items {
g.Expect(bytes.Equal(tokenExpires[i], item.Data[bootstrapapi.BootstrapTokenExpirationKey])).To(BeFalse())
Expand Down Expand Up @@ -1292,7 +1292,7 @@ func TestBootstrapTokenRotationMachinePool(t *testing.T) {
l = &corev1.SecretList{}
err = myclient.List(ctx, l, client.ListOption(client.InNamespace(metav1.NamespaceSystem)))
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(l.Items)).To(Equal(1))
g.Expect(l.Items).To(HaveLen(1))

for i, item := range l.Items {
g.Expect(bytes.Equal(tokenExpires[i], item.Data[bootstrapapi.BootstrapTokenExpirationKey])).To(BeFalse())
Expand Down Expand Up @@ -1324,7 +1324,7 @@ func TestBootstrapTokenRotationMachinePool(t *testing.T) {
l = &corev1.SecretList{}
err = myclient.List(ctx, l, client.ListOption(client.InNamespace(metav1.NamespaceSystem)))
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(l.Items)).To(Equal(1))
g.Expect(l.Items).To(HaveLen(1))

for i, item := range l.Items {
g.Expect(bytes.Equal(tokenExpires[i], item.Data[bootstrapapi.BootstrapTokenExpirationKey])).To(BeTrue())
Expand All @@ -1349,7 +1349,7 @@ func TestBootstrapTokenRotationMachinePool(t *testing.T) {
l = &corev1.SecretList{}
err = myclient.List(ctx, l, client.ListOption(client.InNamespace(metav1.NamespaceSystem)))
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(l.Items)).To(Equal(2))
g.Expect(l.Items).To(HaveLen(2))
foundOld := false
foundNew := true
for _, item := range l.Items {
Expand Down
4 changes: 2 additions & 2 deletions cmd/clusterctl/client/cluster/components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,15 +326,15 @@ func Test_providerComponents_DeleteCoreProviderWebhookNamespace(t *testing.T) {

// assert length before deleting
_ = proxyClient.List(ctx, &nsList)
g.Expect(len(nsList.Items)).Should(Equal(1))
g.Expect(nsList.Items).Should(HaveLen(1))

c := newComponentsClient(proxy)
err := c.DeleteWebhookNamespace()
g.Expect(err).To(Not(HaveOccurred()))

// assert length after deleting
_ = proxyClient.List(ctx, &nsList)
g.Expect(len(nsList.Items)).Should(Equal(0))
g.Expect(nsList.Items).Should(BeEmpty())
})
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/clusterctl/client/cluster/objectgraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func assertGraph(t *testing.T, got *objectGraph, want wantGraph) {

g := NewWithT(t)

g.Expect(len(got.uidToNode)).To(Equal(len(want.nodes)), "the number of nodes in the objectGraph doesn't match the number of expected nodes")
g.Expect(got.uidToNode).To(HaveLen(len(want.nodes)), "the number of nodes in the objectGraph doesn't match the number of expected nodes")

for uid, wantNode := range want.nodes {
gotNode, ok := got.uidToNode[types.UID(uid)]
Expand Down
4 changes: 2 additions & 2 deletions cmd/clusterctl/client/cluster/topology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,13 @@ func Test_topologyClient_Plan(t *testing.T) {
g.Expect(err).NotTo(HaveOccurred())

// Check affected ClusterClasses.
g.Expect(len(res.ClusterClasses)).To(Equal(len(tt.want.affectedClusterClasses)))
g.Expect(res.ClusterClasses).To(HaveLen(len(tt.want.affectedClusterClasses)))
for _, cc := range tt.want.affectedClusterClasses {
g.Expect(res.ClusterClasses).To(ContainElement(cc))
}

// Check affected Clusters.
g.Expect(len(res.Clusters)).To(Equal(len(tt.want.affectedClusters)))
g.Expect(res.Clusters).To(HaveLen(len(tt.want.affectedClusters)))
for _, cluster := range tt.want.affectedClusters {
g.Expect(res.Clusters).To(ContainElement(cluster))
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/clusterctl/client/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func Test_getComponentsByName_withEmptyVariables(t *testing.T) {
}
components, err := client.GetProviderComponents(repository1Config.Name(), repository1Config.Type(), options)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(components.Variables())).To(Equal(1))
g.Expect(components.Variables()).To(HaveLen(1))
g.Expect(components.Name()).To(Equal("p1"))
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/clusterctl/client/repository/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ metadata:

merged, err := MergeTemplates(template1, template2)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(merged.Objs())).To(Equal(2))
g.Expect(len(merged.VariableMap())).To(Equal(3))
g.Expect(merged.Objs()).To(HaveLen(2))
g.Expect(merged.VariableMap()).To(HaveLen(3))

// Make sure that the SAME_VARIABLE default value comes from the first template
// that defines it
Expand Down
2 changes: 1 addition & 1 deletion cmd/clusterctl/internal/util/obj_refs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestGetObjectReferences(t *testing.T) {
return
}
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(got)).To(Equal(len(tt.want)))
g.Expect(got).To(HaveLen(len(tt.want)))
for i := range got {
g.Expect(got[i].Kind).To(Equal(tt.want[i].Kind))
g.Expect(got[i].Name).To(Equal(tt.want[i].Name))
Expand Down
4 changes: 2 additions & 2 deletions controllers/remote/keyedmutex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestKeyedMutex(t *testing.T) {
km.Unlock(cluster1)

// Ensure that the lock was cleaned up from the internal map.
g.Expect(km.locks).To(HaveLen(0))
g.Expect(km.locks).To(BeEmpty())
})

t.Run("Can lock different Clusters in parallel but each one only once", func(t *testing.T) {
Expand Down Expand Up @@ -77,6 +77,6 @@ func TestKeyedMutex(t *testing.T) {
}

// Ensure that the lock was cleaned up from the internal map.
g.Expect(km.locks).To(HaveLen(0))
g.Expect(km.locks).To(BeEmpty())
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -1167,9 +1167,9 @@ func TestValidateVersion(t *testing.T) {

allErrs := kcp.validateVersion(tt.oldVersion)
if tt.expectErr {
g.Expect(allErrs).ToNot(HaveLen(0))
g.Expect(allErrs).ToNot(BeEmpty())
} else {
g.Expect(allErrs).To(HaveLen(0))
g.Expect(allErrs).To(BeEmpty())
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion controlplane/kubeadm/internal/controllers/scale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func TestKubeadmControlPlaneReconciler_scaleDownControlPlane_NoError(t *testing.

controlPlaneMachines := clusterv1.MachineList{}
g.Expect(fakeClient.List(context.Background(), &controlPlaneMachines)).To(Succeed())
g.Expect(controlPlaneMachines.Items).To(HaveLen(0))
g.Expect(controlPlaneMachines.Items).To(BeEmpty())
})
t.Run("deletes the oldest control plane Machine even if preflight checks fails", func(t *testing.T) {
g := NewWithT(t)
Expand Down
6 changes: 3 additions & 3 deletions controlplane/kubeadm/internal/etcd/etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestEtcdMembers_WithErrors(t *testing.T) {

members, err := client.Members(ctx)
g.Expect(err).To(HaveOccurred())
g.Expect(len(members)).To(Equal(0))
g.Expect(members).To(BeEmpty())

err = client.MoveLeader(ctx, 1)
g.Expect(err).To(HaveOccurred())
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestEtcdMembers_WithSuccess(t *testing.T) {

members, err := client.Members(ctx)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(members)).To(Equal(1))
g.Expect(members).To(HaveLen(1))

err = client.MoveLeader(ctx, 1)
g.Expect(err).NotTo(HaveOccurred())
Expand All @@ -101,6 +101,6 @@ func TestEtcdMembers_WithSuccess(t *testing.T) {

updatedMembers, err := client.UpdateMemberPeerURLs(ctx, 1234, []string{"https://4.5.6.7:2000"})
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(updatedMembers[0].PeerURLs)).To(Equal(2))
g.Expect(updatedMembers[0].PeerURLs).To(HaveLen(2))
g.Expect(updatedMembers[0].PeerURLs).To(Equal([]string{"https://1.2.3.4:2000", "https://4.5.6.7:2000"}))
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestGetorCreateClusterResourceSetBinding(t *testing.T) {
clusterResourceSetBinding, err := r.getOrCreateClusterResourceSetBinding(context.TODO(), tt.cluster, &addonsv1.ClusterResourceSet{})
gs.Expect(err).NotTo(HaveOccurred())

gs.Expect(len(clusterResourceSetBinding.Spec.Bindings)).To(Equal(tt.numOfClusterResourceSets))
gs.Expect(clusterResourceSetBinding.Spec.Bindings).To(HaveLen(tt.numOfClusterResourceSets))
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func TestMachinePoolGetNodeReference(t *testing.T) {
return
}

g.Expect(len(result.references)).To(Equal(len(test.expected.references)), "Expected NodeRef count to be %v, got %v", len(result.references), len(test.expected.references))
g.Expect(result.references).To(HaveLen(len(test.expected.references)), "Expected NodeRef count to be %v, got %v", len(result.references), len(test.expected.references))

for n := range test.expected.references {
g.Expect(result.references[n].Name).To(Equal(test.expected.references[n].Name), "Expected NodeRef's name to be %v, got %v", result.references[n].Name, test.expected.references[n].Name)
Expand Down
2 changes: 1 addition & 1 deletion exp/internal/controllers/machinepool_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ func TestMachinePoolConditions(t *testing.T) {
t.Helper()
g := NewWithT(t)

g.Expect(getter.GetConditions()).NotTo(HaveLen(0))
g.Expect(getter.GetConditions()).NotTo(BeEmpty())
for _, c := range getter.GetConditions() {
g.Expect(c.Status).To(Equal(corev1.ConditionTrue))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ func TestExtensionReconciler_Reconcile(t *testing.T) {

// Expect three handlers for the extension and expect the name to be the handler name plus the extension name.
handlers := config.Status.Handlers
g.Expect(len(handlers)).To(Equal(3))
g.Expect(handlers).To(HaveLen(3))
g.Expect(handlers[0].Name).To(Equal("first.ext1"))
g.Expect(handlers[1].Name).To(Equal("second.ext1"))
g.Expect(handlers[2].Name).To(Equal("third.ext1"))

conditions := config.GetConditions()
g.Expect(len(conditions)).To(Equal(1))
g.Expect(conditions).To(HaveLen(1))
g.Expect(conditions[0].Status).To(Equal(corev1.ConditionTrue))
g.Expect(conditions[0].Type).To(Equal(runtimev1.RuntimeExtensionDiscoveredCondition))
_, err = registry.Get("first.ext1")
Expand Down Expand Up @@ -168,11 +168,11 @@ func TestExtensionReconciler_Reconcile(t *testing.T) {

// Expect two handlers for the extension and expect the name to be the handler name plus the extension name.
handlers := config.Status.Handlers
g.Expect(len(handlers)).To(Equal(2))
g.Expect(handlers).To(HaveLen(2))
g.Expect(handlers[0].Name).To(Equal("first.ext1"))
g.Expect(handlers[1].Name).To(Equal("third.ext1"))
conditions := config.GetConditions()
g.Expect(len(conditions)).To(Equal(1))
g.Expect(conditions).To(HaveLen(1))
g.Expect(conditions[0].Status).To(Equal(corev1.ConditionTrue))
g.Expect(conditions[0].Type).To(Equal(runtimev1.RuntimeExtensionDiscoveredCondition))

Expand Down Expand Up @@ -227,13 +227,13 @@ func TestExtensionReconciler_discoverExtensionConfig(t *testing.T) {

// Expect exactly one handler and expect the name to be the handler name plus the extension name.
handlers := discoveredExtensionConfig.Status.Handlers
g.Expect(len(handlers)).To(Equal(1))
g.Expect(handlers).To(HaveLen(1))
g.Expect(handlers[0].Name).To(Equal("first.ext1"))

// Expect exactly one condition and expect the condition to have type RuntimeExtensionDiscoveredCondition and
// Status true.
conditions := discoveredExtensionConfig.GetConditions()
g.Expect(len(conditions)).To(Equal(1))
g.Expect(conditions).To(HaveLen(1))
g.Expect(conditions[0].Status).To(Equal(corev1.ConditionTrue))
g.Expect(conditions[0].Type).To(Equal(runtimev1.RuntimeExtensionDiscoveredCondition))
})
Expand Down Expand Up @@ -261,12 +261,12 @@ func TestExtensionReconciler_discoverExtensionConfig(t *testing.T) {

// Expect exactly one handler and expect the name to be the handler name plus the extension name.
handlers := discoveredExtensionConfig.Status.Handlers
g.Expect(len(handlers)).To(Equal(0))
g.Expect(handlers).To(BeEmpty())

// Expect exactly one condition and expect the condition to have type RuntimeExtensionDiscoveredCondition and
// Status false.
conditions := discoveredExtensionConfig.GetConditions()
g.Expect(len(conditions)).To(Equal(1))
g.Expect(conditions).To(HaveLen(1))
g.Expect(conditions[0].Status).To(Equal(corev1.ConditionFalse))
g.Expect(conditions[0].Type).To(Equal(runtimev1.RuntimeExtensionDiscoveredCondition))
})
Expand Down
16 changes: 8 additions & 8 deletions exp/runtime/internal/controllers/warmup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,17 @@ func Test_warmupRunnable_Start(t *testing.T) {
}
list := &runtimev1.ExtensionConfigList{}
g.Expect(env.GetAPIReader().List(ctx, list)).To(Succeed())
g.Expect(len(list.Items)).To(Equal(3))
g.Expect(list.Items).To(HaveLen(3))
for i, config := range list.Items {
// Expect three handlers for each extension and expect the name to be the handler name plus the extension name.
handlers := config.Status.Handlers
g.Expect(len(handlers)).To(Equal(3))
g.Expect(handlers).To(HaveLen(3))
g.Expect(handlers[0].Name).To(Equal(fmt.Sprintf("first.ext%d", i+1)))
g.Expect(handlers[1].Name).To(Equal(fmt.Sprintf("second.ext%d", i+1)))
g.Expect(handlers[2].Name).To(Equal(fmt.Sprintf("third.ext%d", i+1)))

conditions := config.GetConditions()
g.Expect(len(conditions)).To(Equal(1))
g.Expect(conditions).To(HaveLen(1))
g.Expect(conditions[0].Status).To(Equal(corev1.ConditionTrue))
g.Expect(conditions[0].Type).To(Equal(runtimev1.RuntimeExtensionDiscoveredCondition))
}
Expand Down Expand Up @@ -153,29 +153,29 @@ func Test_warmupRunnable_Start(t *testing.T) {
}
list := &runtimev1.ExtensionConfigList{}
g.Expect(env.GetAPIReader().List(ctx, list)).To(Succeed())
g.Expect(len(list.Items)).To(Equal(3))
g.Expect(list.Items).To(HaveLen(3))

for i, config := range list.Items {
handlers := config.Status.Handlers
conditions := config.GetConditions()

// Expect no handlers and a failed condition for the broken extension.
if config.Name == brokenExtension {
g.Expect(len(conditions)).To(Equal(1))
g.Expect(conditions).To(HaveLen(1))
g.Expect(conditions[0].Status).To(Equal(corev1.ConditionFalse))
g.Expect(conditions[0].Type).To(Equal(runtimev1.RuntimeExtensionDiscoveredCondition))
g.Expect(len(handlers)).To(Equal(0))
g.Expect(handlers).To(BeEmpty())

continue
}

// For other extensions expect handler name plus the extension name, and expect the condition to be True.
g.Expect(len(handlers)).To(Equal(3))
g.Expect(handlers).To(HaveLen(3))
g.Expect(handlers[0].Name).To(Equal(fmt.Sprintf("first.ext%d", i+1)))
g.Expect(handlers[1].Name).To(Equal(fmt.Sprintf("second.ext%d", i+1)))
g.Expect(handlers[2].Name).To(Equal(fmt.Sprintf("third.ext%d", i+1)))

g.Expect(len(conditions)).To(Equal(1))
g.Expect(conditions).To(HaveLen(1))
g.Expect(conditions[0].Status).To(Equal(corev1.ConditionTrue))
g.Expect(conditions[0].Type).To(Equal(runtimev1.RuntimeExtensionDiscoveredCondition))
}
Expand Down
Loading

0 comments on commit 7ab078e

Please sign in to comment.