test: fix ginkgolinter issues

All of these issues were reported by https://github.com/nunnatsa/ginkgolinter.
Fixing these issues is useful (several expressions get simpler, using
framework.ExpectNoError is better because it has additional support for
failures) and a necessary step for enabling that linter in our golangci-lint
invocation.
This commit is contained in:
Patrick Ohly 2023-02-22 10:35:09 +01:00
parent 443b1c6c23
commit 41f23f52d0
17 changed files with 47 additions and 47 deletions

View File

@ -48,7 +48,7 @@ var _ = Describe("Getting the Resources", func() {
instance = openapi.NewOpenAPIParser(openapi.NewOpenAPIGetter(&client))
var err error
expectedData, err = openapi.NewOpenAPIData(nil)
Expect(err).To(BeNil())
Expect(err).ToNot(HaveOccurred())
})
Context("when the server returns a successful result", func() {
@ -56,12 +56,12 @@ var _ = Describe("Getting the Resources", func() {
Expect(client.Calls).To(Equal(0))
result, err := instance.Parse()
Expect(err).To(BeNil())
Expect(err).ToNot(HaveOccurred())
Expect(result).To(Equal(expectedData))
Expect(client.Calls).To(Equal(1))
result, err = instance.Parse()
Expect(err).To(BeNil())
Expect(err).ToNot(HaveOccurred())
Expect(result).To(Equal(expectedData))
// No additional client calls expected
Expect(client.Calls).To(Equal(1))

View File

@ -34,9 +34,9 @@ var _ = Describe("Reading apps/v1/Deployment from openAPIData", func() {
var resources openapi.Resources
BeforeEach(func() {
s, err := fakeSchema.OpenAPISchema()
Expect(err).To(BeNil())
Expect(err).ToNot(HaveOccurred())
resources, err = openapi.NewOpenAPIData(s)
Expect(err).To(BeNil())
Expect(err).ToNot(HaveOccurred())
})
gvk := schema.GroupVersionKind{
@ -52,7 +52,7 @@ var _ = Describe("Reading apps/v1/Deployment from openAPIData", func() {
Expect(schema.(*proto.Kind)).ToNot(BeNil())
consumes := resources.GetConsumes(gvk, "PATCH")
Expect(consumes).ToNot(BeNil())
Expect(len(consumes)).To(Equal(4))
Expect(consumes).To(HaveLen(4))
})
})
@ -60,9 +60,9 @@ var _ = Describe("Reading authorization.k8s.io/v1/SubjectAccessReview from openA
var resources openapi.Resources
BeforeEach(func() {
s, err := fakeSchema.OpenAPISchema()
Expect(err).To(BeNil())
Expect(err).ToNot(HaveOccurred())
resources, err = openapi.NewOpenAPIData(s)
Expect(err).To(BeNil())
Expect(err).ToNot(HaveOccurred())
})
gvk := schema.GroupVersionKind{

View File

@ -36,9 +36,9 @@ var _ = Describe("resource validation using OpenAPI Schema", func() {
var validator Schema
BeforeEach(func() {
s, err := fakeSchema.OpenAPISchema()
Expect(err).To(BeNil())
Expect(err).ToNot(HaveOccurred())
resources, err := openapi.NewOpenAPIData(s)
Expect(err).To(BeNil())
Expect(err).ToNot(HaveOccurred())
validator = NewSchemaValidation(resources)
Expect(validator).ToNot(BeNil())
})
@ -65,7 +65,7 @@ spec:
- image: redis
name: redis
`))
Expect(err).To(BeNil())
Expect(err).ToNot(HaveOccurred())
})
It("validates a valid pod", func() {
@ -87,7 +87,7 @@ spec:
image: gcr.io/fake_project/fake_image:fake_tag
name: master
`))
Expect(err).To(BeNil())
Expect(err).ToNot(HaveOccurred())
})
It("finds invalid command (string instead of []string) in Json Pod", func() {
@ -317,7 +317,7 @@ spec:
command:
`))
Expect(err).To(BeNil())
Expect(err).ToNot(HaveOccurred())
})
It("can validate lists", func() {
@ -336,7 +336,7 @@ items:
- name: name
`))
Expect(err).To(BeNil())
Expect(err).ToNot(HaveOccurred())
})
It("fails because apiVersion is not provided", func() {
@ -407,6 +407,6 @@ metadata:
spec:
foo: bar
`))
Expect(err).To(BeNil())
Expect(err).ToNot(HaveOccurred())
})
})

View File

@ -122,7 +122,7 @@ var _ = SIGDescribe("Servers with support for Table transformation", func() {
SetHeader("Accept", "application/json;as=Table;v=v1beta1;g=meta.k8s.io").
Do(ctx).Into(pagedTable)
framework.ExpectNoError(err, "failed to get pod templates in Table form in namespace: %s", ns)
gomega.Expect(len(pagedTable.Rows)).To(gomega.BeNumerically(">", 0))
gomega.Expect(pagedTable.Rows).ToNot(gomega.BeEmpty())
framework.ExpectEqual(pagedTable.Rows[0].Cells[0], "template-0002")
})
@ -135,7 +135,7 @@ var _ = SIGDescribe("Servers with support for Table transformation", func() {
framework.Logf("Table: %#v", table)
gomega.Expect(len(table.ColumnDefinitions)).To(gomega.BeNumerically(">=", 2))
gomega.Expect(len(table.Rows)).To(gomega.BeNumerically(">=", 1))
gomega.Expect(table.Rows).ToNot(gomega.BeEmpty())
framework.ExpectEqual(len(table.Rows[0].Cells), len(table.ColumnDefinitions))
framework.ExpectEqual(table.ColumnDefinitions[0].Name, "Name")
framework.ExpectNotEqual(table.ResourceVersion, "")

View File

@ -208,7 +208,7 @@ var _ = SIGDescribe("CronJob", func() {
jobs, err := f.ClientSet.BatchV1().Jobs(f.Namespace.Name).List(ctx, metav1.ListOptions{})
framework.ExpectNoError(err, "Failed to list the CronJobs in namespace %s", f.Namespace.Name)
activeJobs, _ := filterActiveJobs(jobs)
gomega.Expect(len(activeJobs)).To(gomega.BeNumerically(">=", 1))
gomega.Expect(activeJobs).ToNot(gomega.BeEmpty())
ginkgo.By("Removing cronjob")
err = deleteCronJob(ctx, f.ClientSet, f.Namespace.Name, cronJob.Name)
@ -459,8 +459,7 @@ var _ = SIGDescribe("CronJob", func() {
// CronJob resource delete operations
expectFinalizer := func(cj *batchv1.CronJob, msg string) {
framework.ExpectNotEqual(cj.DeletionTimestamp, nil, fmt.Sprintf("expected deletionTimestamp, got nil on step: %q, cronjob: %+v", msg, cj))
gomega.Expect(len(cj.Finalizers)).To(gomega.BeNumerically(">", 0), "expected finalizers on cronjob, got none on step: %q, cronjob: %+v", msg, cj)
gomega.Expect(cj.Finalizers).ToNot(gomega.BeEmpty(), "expected finalizers on cronjob, got none on step: %q, cronjob: %+v", msg, cj)
}
ginkgo.By("deleting")

View File

@ -1028,7 +1028,7 @@ func listDaemonPods(ctx context.Context, c clientset.Interface, ns string, label
options := metav1.ListOptions{LabelSelector: selector.String()}
podList, err := c.CoreV1().Pods(ns).List(ctx, options)
framework.ExpectNoError(err)
gomega.Expect(len(podList.Items)).To(gomega.BeNumerically(">", 0))
gomega.Expect(podList.Items).ToNot(gomega.BeEmpty())
return podList
}
@ -1191,7 +1191,7 @@ func checkDaemonSetPodsLabels(podList *v1.PodList, hash string) {
continue
}
podHash := pod.Labels[appsv1.DefaultDaemonSetUniqueLabelKey]
gomega.Expect(len(podHash)).To(gomega.BeNumerically(">", 0))
gomega.Expect(podHash).ToNot(gomega.BeEmpty())
if len(hash) > 0 {
framework.ExpectEqual(podHash, hash, "unexpected hash for pod %s", pod.Name)
}
@ -1221,7 +1221,7 @@ func listDaemonHistories(ctx context.Context, c clientset.Interface, ns string,
options := metav1.ListOptions{LabelSelector: selector.String()}
historyList, err := c.AppsV1().ControllerRevisions(ns).List(ctx, options)
framework.ExpectNoError(err)
gomega.Expect(len(historyList.Items)).To(gomega.BeNumerically(">", 0))
gomega.Expect(historyList.Items).ToNot(gomega.BeEmpty())
return historyList
}
@ -1231,7 +1231,7 @@ func curHistory(historyList *appsv1.ControllerRevisionList, ds *appsv1.DaemonSet
for i := range historyList.Items {
history := &historyList.Items[i]
// Every history should have the hash label
gomega.Expect(len(history.Labels[appsv1.DefaultDaemonSetUniqueLabelKey])).To(gomega.BeNumerically(">", 0))
gomega.Expect(history.Labels[appsv1.DefaultDaemonSetUniqueLabelKey]).ToNot(gomega.BeEmpty())
match, err := daemon.Match(ds, history)
framework.ExpectNoError(err)
if match {

View File

@ -1338,7 +1338,7 @@ func listDeploymentReplicaSets(ctx context.Context, c clientset.Interface, ns st
options := metav1.ListOptions{LabelSelector: selector.String()}
rsList, err := c.AppsV1().ReplicaSets(ns).List(ctx, options)
framework.ExpectNoError(err)
gomega.Expect(len(rsList.Items)).To(gomega.BeNumerically(">", 0))
gomega.Expect(rsList.Items).ToNot(gomega.BeEmpty())
return rsList
}

View File

@ -369,7 +369,7 @@ var _ = SIGDescribe("ReplicationController", func() {
ginkgo.By("listing all ReplicationControllers")
rcs, err := f.ClientSet.CoreV1().ReplicationControllers("").List(ctx, metav1.ListOptions{LabelSelector: "test-rc-static=true"})
framework.ExpectNoError(err, "failed to list ReplicationController")
gomega.Expect(len(rcs.Items)).To(gomega.BeNumerically(">", 0), "Expected to find a ReplicationController but none was found")
gomega.Expect(rcs.Items).ToNot(gomega.BeEmpty(), "Expected to find a ReplicationController but none was found")
ginkgo.By("checking that ReplicationController has expected values")
foundRc := false

View File

@ -134,7 +134,7 @@ var _ = SIGDescribe("Kubelet", func() {
*/
framework.ConformanceIt("should be possible to delete [NodeConformance]", func(ctx context.Context) {
err := podClient.Delete(ctx, podName, metav1.DeleteOptions{})
gomega.Expect(err).To(gomega.BeNil(), fmt.Sprintf("Error deleting Pod %v", err))
framework.ExpectNoError(err, "deleting Pod")
})
})
ginkgo.Context("when scheduling an agnhost Pod with hostAliases", func() {

View File

@ -64,7 +64,7 @@ var _ = SIGDescribe("NodeLease", func() {
return nil
}, 5*time.Minute, 5*time.Second).Should(gomega.BeNil())
// check basic expectations for the lease
gomega.Expect(expectLease(lease, nodeName)).To(gomega.BeNil())
framework.ExpectNoError(expectLease(lease, nodeName))
ginkgo.By("check that node lease is updated at least once within the lease duration")
gomega.Eventually(ctx, func() error {
@ -126,7 +126,7 @@ var _ = SIGDescribe("NodeLease", func() {
return nil
}, 5*time.Minute, 5*time.Second).Should(gomega.BeNil())
// check basic expectations for the lease
gomega.Expect(expectLease(lease, nodeName)).To(gomega.BeNil())
framework.ExpectNoError(expectLease(lease, nodeName))
leaseDuration := time.Duration(*lease.Spec.LeaseDurationSeconds) * time.Second
ginkgo.By("verify NodeStatus report period is longer than lease duration")

View File

@ -148,11 +148,12 @@ var _ = SIGDescribe("Sysctls [LinuxOnly] [NodeConformance]", func() {
client := f.ClientSet.CoreV1().Pods(f.Namespace.Name)
_, err := client.Create(ctx, pod, metav1.CreateOptions{})
gomega.Expect(err).NotTo(gomega.BeNil())
gomega.Expect(err.Error()).To(gomega.ContainSubstring(`Invalid value: "foo-"`))
gomega.Expect(err.Error()).To(gomega.ContainSubstring(`Invalid value: "bar.."`))
gomega.Expect(err.Error()).NotTo(gomega.ContainSubstring(`safe-and-unsafe`))
gomega.Expect(err.Error()).NotTo(gomega.ContainSubstring("kernel.shmmax"))
gomega.Expect(err).To(gomega.MatchError(gomega.SatisfyAll(
gomega.ContainSubstring(`Invalid value: "foo-"`),
gomega.ContainSubstring(`Invalid value: "bar.."`),
gomega.Not(gomega.ContainSubstring(`safe-and-unsafe`)),
gomega.Not(gomega.ContainSubstring("kernel.shmmax")),
)))
})
// Pod is created with kernel.msgmax, an unsafe sysctl.

View File

@ -174,7 +174,7 @@ func CreateStorageServer(ctx context.Context, cs clientset.Interface, config Tes
pod = startVolumeServer(ctx, cs, config)
gomega.Expect(pod).NotTo(gomega.BeNil(), "storage server pod should not be nil")
ip = pod.Status.PodIP
gomega.Expect(len(ip)).NotTo(gomega.BeZero(), fmt.Sprintf("pod %s's IP should not be empty", pod.Name))
gomega.Expect(ip).NotTo(gomega.BeEmpty(), fmt.Sprintf("pod %s's IP should not be empty", pod.Name))
framework.Logf("%s server pod IP address: %s", config.Prefix, ip)
return pod, ip
}
@ -371,7 +371,7 @@ func TestServerCleanup(ctx context.Context, f *framework.Framework, config TestC
}
err := e2epod.DeletePodWithWaitByName(ctx, f.ClientSet, config.Prefix+"-server", config.Namespace)
gomega.Expect(err).To(gomega.BeNil(), "Failed to delete pod %v in namespace %v", config.Prefix+"-server", config.Namespace)
framework.ExpectNoError(err, "delete pod %v in namespace %v", config.Prefix+"-server", config.Namespace)
}
func runVolumeTesterPod(ctx context.Context, client clientset.Interface, timeouts *framework.TimeoutContext, config TestConfig, podSuffix string, privileged bool, fsGroup *int64, tests []Test, slow bool) (*v1.Pod, error) {

View File

@ -753,7 +753,7 @@ metadata:
return strings.Contains(logOutput, content), nil
})
gomega.Expect(err).To(gomega.BeNil(), fmt.Sprintf("unexpected error waiting for '%v' output", content))
framework.ExpectNoError(err, "waiting for '%v' output", content)
return logOutput
}
@ -768,7 +768,7 @@ metadata:
gomega.Expect(runOutput).To(gomega.ContainSubstring("abcd1234"))
gomega.Expect(runOutput).To(gomega.ContainSubstring("stdin closed"))
gomega.Expect(c.CoreV1().Pods(ns).Delete(ctx, "run-test", metav1.DeleteOptions{})).To(gomega.BeNil())
framework.ExpectNoError(c.CoreV1().Pods(ns).Delete(ctx, "run-test", metav1.DeleteOptions{}))
ginkgo.By("executing a command with run and attach without stdin")
// There is a race on this scenario described in #73099
@ -784,7 +784,7 @@ metadata:
gomega.Expect(runOutput).ToNot(gomega.ContainSubstring("abcd1234"))
gomega.Expect(runOutput).To(gomega.ContainSubstring("stdin closed"))
gomega.Expect(c.CoreV1().Pods(ns).Delete(ctx, "run-test-2", metav1.DeleteOptions{})).To(gomega.BeNil())
framework.ExpectNoError(c.CoreV1().Pods(ns).Delete(ctx, "run-test-2", metav1.DeleteOptions{}))
ginkgo.By("executing a command with run and attach with stdin with open stdin should remain running")
e2ekubectl.NewKubectlCommand(ns, "run", "run-test-3", "--image="+busyboxImage, "--restart=OnFailure", podRunningTimeoutArg, "--attach=true", "--leave-stdin-open=true", "--stdin", "--", "sh", "-c", "cat && echo 'stdin closed'").
@ -800,7 +800,7 @@ metadata:
framework.ExpectNoError(err)
framework.ExpectNoError(e2epod.WaitTimeoutForPodReadyInNamespace(ctx, c, runTestPod.Name, ns, time.Minute))
gomega.Expect(c.CoreV1().Pods(ns).Delete(ctx, "run-test-3", metav1.DeleteOptions{})).To(gomega.BeNil())
framework.ExpectNoError(c.CoreV1().Pods(ns).Delete(ctx, "run-test-3", metav1.DeleteOptions{}))
})
ginkgo.It("should contain last line of the log", func(ctx context.Context) {

View File

@ -125,7 +125,7 @@ var _ = SIGDescribe("Kubectl logs", func() {
ginkgo.By("limiting log lines")
out := e2ekubectl.RunKubectlOrDie(ns, "logs", podName, containerName, "--tail=1")
framework.Logf("got output %q", out)
gomega.Expect(len(out)).NotTo(gomega.BeZero())
gomega.Expect(out).NotTo(gomega.BeEmpty())
framework.ExpectEqual(len(lines(out)), 1)
ginkgo.By("limiting log bytes")
@ -191,13 +191,13 @@ var _ = SIGDescribe("Kubectl logs", func() {
ginkgo.By("specified container log lines")
out := e2ekubectl.RunKubectlOrDie(ns, "logs", podName, "-c", "container-1")
framework.Logf("got output %q", out)
gomega.Expect(len(out)).NotTo(gomega.BeZero())
gomega.Expect(out).NotTo(gomega.BeEmpty())
framework.ExpectEqual(len(lines(out)), 10)
ginkgo.By("log all containers log lines")
out = e2ekubectl.RunKubectlOrDie(ns, "logs", podName, "--all-containers")
framework.Logf("got output %q", out)
gomega.Expect(len(out)).NotTo(gomega.BeZero())
gomega.Expect(out).NotTo(gomega.BeEmpty())
framework.ExpectEqual(len(lines(out)), 30)
ginkgo.By("default container logs")

View File

@ -77,7 +77,7 @@ func (t *dnsTestCommon) init(ctx context.Context) {
namespace := "kube-system"
pods, err := t.f.ClientSet.CoreV1().Pods(namespace).List(ctx, options)
framework.ExpectNoError(err, "failed to list pods in namespace: %s", namespace)
gomega.Expect(len(pods.Items)).Should(gomega.BeNumerically(">=", 1))
gomega.Expect(pods.Items).ToNot(gomega.BeEmpty())
t.dnsPod = &pods.Items[0]
framework.Logf("Using DNS pod: %v", t.dnsPod.Name)

View File

@ -86,7 +86,7 @@ var _ = utils.SIGDescribe("vcp-performance [Feature:vsphere]", func() {
nodes, err := e2enode.GetReadySchedulableNodes(ctx, client)
framework.ExpectNoError(err)
gomega.Expect(len(nodes.Items)).To(gomega.BeNumerically(">=", 1), "Requires at least %d nodes (not %d)", 2, len(nodes.Items))
gomega.Expect(nodes.Items).ToNot(gomega.BeEmpty(), "Requires at least one ready node")
msg := fmt.Sprintf("Cannot attach %d volumes to %d nodes. Maximum volumes that can be attached on %d nodes is %d", volumeCount, len(nodes.Items), len(nodes.Items), SCSIUnitsAvailablePerNode*len(nodes.Items))
gomega.Expect(volumeCount).To(gomega.BeNumerically("<=", SCSIUnitsAvailablePerNode*len(nodes.Items)), msg)

View File

@ -778,7 +778,7 @@ func verifyEvictionEvents(ctx context.Context, f *framework.Framework, testSpecs
"reason": eviction.Reason,
}.AsSelector().String()
podEvictEvents, err := f.ClientSet.CoreV1().Events(f.Namespace.Name).List(ctx, metav1.ListOptions{FieldSelector: selector})
gomega.Expect(err).To(gomega.BeNil(), "Unexpected error getting events during eviction test: %v", err)
framework.ExpectNoError(err, "getting events")
framework.ExpectEqual(len(podEvictEvents.Items), 1, "Expected to find 1 eviction event for pod %s, got %d", pod.Name, len(podEvictEvents.Items))
event := podEvictEvents.Items[0]
@ -811,7 +811,7 @@ func verifyEvictionEvents(ctx context.Context, f *framework.Framework, testSpecs
framework.ExpectEqual(len(offendingContainersUsage), 1, "Expected to find the offending container's usage in the %s annotation, but found %+v",
eviction.OffendingContainersUsageKey, offendingContainersUsage)
usageQuantity, err := resource.ParseQuantity(offendingContainersUsage[0])
gomega.Expect(err).To(gomega.BeNil(), "Expected to be able to parse pod %s's %s annotation as a quantity, but got err: %v", pod.Name, eviction.OffendingContainersUsageKey, err)
framework.ExpectNoError(err, "parsing pod %s's %s annotation as a quantity", pod.Name, eviction.OffendingContainersUsageKey)
request := pod.Spec.Containers[0].Resources.Requests[starvedResource]
framework.ExpectEqual(usageQuantity.Cmp(request), 1, "Expected usage of offending container: %s in pod %s to exceed its request %s",
usageQuantity.String(), pod.Name, request.String())