Skip to content

Commit f7ae500

Browse files
committed
fix: avoid shadowing of 'err' and related variables in E2E and external plugin tests
Replaced short variable declarations with assignments in test/e2e and pkg/plugins/external to avoid shadowing the outer 'err' variable and other previously declared variables. This includes explicit declarations for variables like 'output', 'status', and 'fileInfo' to ensure consistent error handling and improved readability in test logic.
1 parent e3658a6 commit f7ae500

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

pkg/model/resource/resource_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ var _ = Describe("Resource", func() {
199199
webhookVersion = "v1"
200200
)
201201

202-
res := Resource{
202+
res = Resource{
203203
GVK: gvk,
204204
Plural: plural,
205205
Path: path,

pkg/plugins/external/external_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ var _ = Describe("Run external plugin using Scaffold", func() {
142142

143143
AfterEach(func() {
144144
filename := filepath.Join("tmp", "externalPlugin", "LICENSE")
145-
fileInfo, err := fs.FS.Stat(filename)
145+
var fileInfo os.FileInfo
146+
fileInfo, err = fs.FS.Stat(filename)
146147
Expect(err).ToNot(HaveOccurred())
147148
Expect(fileInfo).NotTo(BeNil())
148149
})

test/e2e/deployimage/plugin_cluster_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ func Run(kbc *utils.TestContext) {
100100
By("validating that the controller-manager pod is running as expected")
101101
verifyControllerUp := func(g Gomega) {
102102
// Get pod name
103-
podOutput, err := kbc.Kubectl.Get(
103+
var podOutput string
104+
podOutput, err = kbc.Kubectl.Get(
104105
true,
105106
"pods", "-l", "control-plane=controller-manager",
106107
"-o", "go-template={{ range .items }}{{ if not .metadata.deletionTimestamp }}{{ .metadata.name }}"+
@@ -116,8 +117,8 @@ func Run(kbc *utils.TestContext) {
116117
To(Equal("Running"), "incorrect controller pod status")
117118
}
118119
defer func() {
119-
out, err := kbc.Kubectl.CommandInNamespace("describe", "all")
120-
Expect(err).NotTo(HaveOccurred())
120+
out, errDescribe := kbc.Kubectl.CommandInNamespace("describe", "all")
121+
Expect(errDescribe).NotTo(HaveOccurred())
121122
_, _ = fmt.Fprintln(GinkgoWriter, out)
122123
}()
123124
Eventually(verifyControllerUp).Should(Succeed())

test/e2e/v4/plugin_cluster_test.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller, isToUseHelmChart,
193193

194194
By("validating that the Prometheus manager has provisioned the Service")
195195
Eventually(func(g Gomega) {
196-
_, err := kbc.Kubectl.Get(
196+
_, err = kbc.Kubectl.Get(
197197
false,
198198
"Service", "prometheus-operator")
199199
g.Expect(err).NotTo(HaveOccurred())
@@ -207,7 +207,8 @@ func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller, isToUseHelmChart,
207207

208208
if hasNetworkPolicies {
209209
By("Checking for Calico pods")
210-
outputGet, err := kbc.Kubectl.Get(
210+
var outputGet string
211+
outputGet, err = kbc.Kubectl.Get(
211212
false,
212213
"pods",
213214
"-n", "kube-system",
@@ -223,7 +224,8 @@ func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller, isToUseHelmChart,
223224
"metrics=enabled")).Error().NotTo(HaveOccurred())
224225

225226
By("Ensuring the Allow Metrics Traffic NetworkPolicy exists", func() {
226-
output, err := kbc.Kubectl.Get(
227+
var output string
228+
output, err = kbc.Kubectl.Get(
227229
true,
228230
"networkpolicy", fmt.Sprintf("e2e-%s-allow-metrics-traffic", kbc.TestSuffix),
229231
)
@@ -240,7 +242,8 @@ func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller, isToUseHelmChart,
240242
Expect(err).NotTo(HaveOccurred())
241243

242244
By("Ensuring the allow-webhook-traffic NetworkPolicy exists", func() {
243-
output, err := kbc.Kubectl.Get(
245+
var output string
246+
output, err = kbc.Kubectl.Get(
244247
true,
245248
"networkpolicy", fmt.Sprintf("e2e-%s-allow-webhook-traffic", kbc.TestSuffix),
246249
)
@@ -255,7 +258,8 @@ func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller, isToUseHelmChart,
255258
By("validating that cert-manager has provisioned the certificate Secret")
256259

257260
verifyWebhookCert := func(g Gomega) {
258-
output, err := kbc.Kubectl.Get(
261+
var output string
262+
output, err = kbc.Kubectl.Get(
259263
true,
260264
"secrets", "webhook-server-cert")
261265
g.Expect(err).ToNot(HaveOccurred(), "webhook-server-cert should exist in the namespace")
@@ -266,7 +270,8 @@ func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller, isToUseHelmChart,
266270

267271
By("validating that the mutating|validating webhooks have the CA injected")
268272
verifyCAInjection := func(g Gomega) {
269-
mwhOutput, err := kbc.Kubectl.Get(
273+
var mwhOutput, vwhOutput string
274+
mwhOutput, err = kbc.Kubectl.Get(
270275
false,
271276
"mutatingwebhookconfigurations.admissionregistration.k8s.io",
272277
fmt.Sprintf("e2e-%s-mutating-webhook-configuration", kbc.TestSuffix),
@@ -275,7 +280,7 @@ func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller, isToUseHelmChart,
275280
// check that ca should be long enough, because there may be a place holder "\n"
276281
g.Expect(len(mwhOutput)).To(BeNumerically(">", 10))
277282

278-
vwhOutput, err := kbc.Kubectl.Get(
283+
vwhOutput, err = kbc.Kubectl.Get(
279284
false,
280285
"validatingwebhookconfigurations.admissionregistration.k8s.io",
281286
fmt.Sprintf("e2e-%s-validating-webhook-configuration", kbc.TestSuffix),
@@ -290,7 +295,8 @@ func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller, isToUseHelmChart,
290295
By("validating that the CA injection is applied for CRD conversion")
291296
crdKind := "ConversionTest"
292297
verifyCAInjection = func(g Gomega) {
293-
crdOutput, err := kbc.Kubectl.Get(
298+
var crdOutput string
299+
crdOutput, err = kbc.Kubectl.Get(
294300
false,
295301
"customresourcedefinition.apiextensions.k8s.io",
296302
"-o", fmt.Sprintf(
@@ -366,7 +372,7 @@ func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller, isToUseHelmChart,
366372
By("applying the CR in the created namespace")
367373

368374
applySampleNamespaced := func(g Gomega) {
369-
_, err := kbc.Kubectl.Apply(false, "-n", namespace, "-f", sampleFile)
375+
_, err = kbc.Kubectl.Apply(false, "-n", namespace, "-f", sampleFile)
370376
g.Expect(err).To(Not(HaveOccurred()))
371377
}
372378
Eventually(applySampleNamespaced, 2*time.Minute, time.Second).Should(Succeed())
@@ -493,7 +499,8 @@ func getMetricsOutput(kbc *utils.TestContext) string {
493499

494500
By("ensuring the service endpoint is ready")
495501
checkServiceEndpoint := func(g Gomega) {
496-
output, err := kbc.Kubectl.Get(
502+
var output string
503+
output, err = kbc.Kubectl.Get(
497504
true,
498505
"endpoints", fmt.Sprintf("e2e-%s-controller-manager-metrics-service", kbc.TestSuffix),
499506
"-o", "jsonpath={.subsets[*].addresses[*].ip}",
@@ -511,7 +518,8 @@ func getMetricsOutput(kbc *utils.TestContext) string {
511518

512519
By("validating that the curl pod is running as expected")
513520
verifyCurlUp := func(g Gomega) {
514-
status, err := kbc.Kubectl.Get(
521+
var status string
522+
status, err = kbc.Kubectl.Get(
515523
true,
516524
"pods", "curl", "-o", "jsonpath={.status.phase}")
517525
g.Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)