Skip to content

Commit db5035d

Browse files
authoredJan 10, 2024
Enable ginkgolinter and fix findings (#126)
* Enable ginkgolinter and fix findings ginkgolinter finds bugs and enforces standards of using the ginkgo and gomega packages. See more details here: https://github.com/nunnatsa/ginkgolinter This PR enables the ginkgolinter in the .golangci.yml, and fixes all the new finding from running golangci-lint. Note: all the finding were auto fixed by running the ginkgolinter cli with the `-fix` flag. Signed-off-by: Nahshon Unna-Tsameret <nunnatsa@redhat.com> * Fix the assertMetrics test helper function The assertMetrics function in handler/instrumented_enqueue_object_test.go, wasn't really doing anything. The switch-case compared a pointer value to another pointer, so the no case was actually ever selected, because the addresses were not the same. Tested with a debugger - no value were ever selected. This commit fixes this test by selecting the value rather than the address. Signed-off-by: Nahshon Unna-Tsameret <nunnatsa@redhat.com> * Simplify error check in tests Replace this patter: ```golang err := someFuncRetOnlyErr() Expect(err).ToNot(HaveOccurred() ``` With ```golang Expect(someFuncRetOnlyErr()).To(Succeed()) ``` Also, use the `MatchError` gomega matcher when checking errors. Signed-off-by: Nahshon Unna-Tsameret <nunnatsa@redhat.com> --------- Signed-off-by: Nahshon Unna-Tsameret <nunnatsa@redhat.com>
1 parent 2bc11dc commit db5035d

File tree

9 files changed

+168
-238
lines changed

9 files changed

+168
-238
lines changed
 

‎.golangci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ linters:
77
- asciicheck
88
- bodyclose
99
- errorlint
10+
- ginkgolinter
1011
- gofmt
1112
- goimports
1213
- gosec

‎conditions/conditions_test.go

+12-23
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,21 @@ var _ = Describe("Condition", func() {
6464
}
6565

6666
// Create Operator Condition
67-
err := os.Setenv(operatorCondEnvVar, "operator-condition-test")
68-
Expect(err).NotTo(HaveOccurred())
67+
Expect(os.Setenv(operatorCondEnvVar, "operator-condition-test")).To(Succeed())
6968
readNamespace = func() (string, error) {
7069
return ns, nil
7170
}
7271

7372
// create a new client
7473
sch := runtime.NewScheme()
75-
err = apiv2.AddToScheme(sch)
76-
Expect(err).NotTo(HaveOccurred())
74+
Expect(apiv2.AddToScheme(sch)).To(Succeed())
7775
cl = fake.NewClientBuilder().WithScheme(sch).WithStatusSubresource(operatorCond).Build()
7876

7977
// create an operator Condition resource
80-
err = cl.Create(ctx, operatorCond)
81-
Expect(err).NotTo(HaveOccurred())
78+
Expect(cl.Create(ctx, operatorCond)).To(Succeed())
8279

8380
// Update its status
84-
err = cl.Status().Update(ctx, operatorCond)
85-
Expect(err).NotTo(HaveOccurred())
81+
Expect(cl.Status().Update(ctx, operatorCond)).To(Succeed())
8682
})
8783

8884
AfterEach(func() {
@@ -107,14 +103,12 @@ var _ = Describe("Condition", func() {
107103
Expect(err).NotTo(HaveOccurred())
108104

109105
con, err := c.Get(ctx)
110-
Expect(err).To(HaveOccurred())
106+
Expect(err).To(MatchError(ContainSubstring(fmt.Sprintf("conditionType %v not found", conditionBar))))
111107
Expect(con).To(BeNil())
112-
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf("conditionType %v not found", conditionBar)))
113108
})
114109

115110
It("should error when operator Condition is not present in cluster", func() {
116-
err := os.Setenv(operatorCondEnvVar, "NON_EXISTING_COND")
117-
Expect(err).NotTo(HaveOccurred())
111+
Expect(os.Setenv(operatorCondEnvVar, "NON_EXISTING_COND")).To(Succeed())
118112

119113
By("setting the status of a new condition")
120114
c, err := NewCondition(cl, conditionFoo)
@@ -131,13 +125,11 @@ var _ = Describe("Condition", func() {
131125
By("setting the status of an existing condition")
132126
c, err := NewCondition(cl, conditionFoo)
133127
Expect(err).NotTo(HaveOccurred())
134-
err = c.Set(ctx, metav1.ConditionFalse, WithReason("not_in_foo_state"), WithMessage("test"))
135-
Expect(err).NotTo(HaveOccurred())
128+
Expect(c.Set(ctx, metav1.ConditionFalse, WithReason("not_in_foo_state"), WithMessage("test"))).To(Succeed())
136129

137130
By("fetching the condition from cluster")
138131
op := &apiv2.OperatorCondition{}
139-
err = cl.Get(ctx, objKey, op)
140-
Expect(err).NotTo(HaveOccurred())
132+
Expect(cl.Get(ctx, objKey, op)).To(Succeed())
141133

142134
By("checking if the condition has been updated")
143135
res := op.Spec.Conditions[0]
@@ -150,22 +142,19 @@ var _ = Describe("Condition", func() {
150142
By("setting the status of a new condition")
151143
c, err := NewCondition(cl, conditionBar)
152144
Expect(err).NotTo(HaveOccurred())
153-
err = c.Set(ctx, metav1.ConditionTrue, WithReason("in_bar_state"), WithMessage("test"))
154-
Expect(err).NotTo(HaveOccurred())
145+
Expect(c.Set(ctx, metav1.ConditionTrue, WithReason("in_bar_state"), WithMessage("test"))).To(Succeed())
155146

156147
By("fetching the condition from cluster")
157148
op := &apiv2.OperatorCondition{}
158-
err = cl.Get(ctx, objKey, op)
159-
Expect(err).NotTo(HaveOccurred())
149+
Expect(cl.Get(ctx, objKey, op)).To(Succeed())
160150

161151
By("checking if the condition has been updated")
162152
res := op.Spec.Conditions
163-
Expect(len(res)).To(BeEquivalentTo(2))
153+
Expect(res).To(HaveLen(2))
164154
Expect(meta.IsStatusConditionTrue(res, string(conditionBar))).To(BeTrue())
165155
})
166156
It("should error when operator Condition is not present in cluster", func() {
167-
err := os.Setenv(operatorCondEnvVar, "NON_EXISTING_COND")
168-
Expect(err).NotTo(HaveOccurred())
157+
Expect(os.Setenv(operatorCondEnvVar, "NON_EXISTING_COND")).To(Succeed())
169158

170159
By("setting the status of a new condition")
171160
c, err := NewCondition(cl, conditionBar)

‎conditions/factory_test.go

+10-20
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ var _ = Describe("NewCondition", func() {
3636
var cl client.Client
3737
BeforeEach(func() {
3838
sch := runtime.NewScheme()
39-
err := apiv2.AddToScheme(sch)
40-
Expect(err).NotTo(HaveOccurred())
39+
Expect(apiv2.AddToScheme(sch)).To(Succeed())
4140
cl = fake.NewClientBuilder().WithScheme(sch).Build()
4241
})
4342

@@ -56,8 +55,7 @@ var _ = Describe("InClusterFactory", func() {
5655

5756
BeforeEach(func() {
5857
sch := runtime.NewScheme()
59-
err := apiv2.AddToScheme(sch)
60-
Expect(err).NotTo(HaveOccurred())
58+
Expect(apiv2.AddToScheme(sch)).To(Succeed())
6159
cl = fake.NewClientBuilder().WithScheme(sch).Build()
6260
f = InClusterFactory{cl}
6361
})
@@ -73,8 +71,7 @@ var _ = Describe("InClusterFactory", func() {
7371

7472
func testNewCondition(fn func(apiv2.ConditionType) (Condition, error)) {
7573
It("should create a new condition", func() {
76-
err := os.Setenv(operatorCondEnvVar, "test-operator-condition")
77-
Expect(err).NotTo(HaveOccurred())
74+
Expect(os.Setenv(operatorCondEnvVar, "test-operator-condition")).To(Succeed())
7875
readNamespace = func() (string, error) {
7976
return "default", nil
8077
}
@@ -85,8 +82,7 @@ func testNewCondition(fn func(apiv2.ConditionType) (Condition, error)) {
8582
})
8683

8784
It("should error when namespacedName cannot be found", func() {
88-
err := os.Unsetenv(operatorCondEnvVar)
89-
Expect(err).NotTo(HaveOccurred())
85+
Expect(os.Unsetenv(operatorCondEnvVar)).To(Succeed())
9086

9187
c, err := fn(conditionFoo)
9288
Expect(err).To(HaveOccurred())
@@ -96,32 +92,27 @@ func testNewCondition(fn func(apiv2.ConditionType) (Condition, error)) {
9692

9793
func testGetNamespacedName(fn func() (*types.NamespacedName, error)) {
9894
It("should error when name of the operator condition cannot be found", func() {
99-
err := os.Unsetenv(operatorCondEnvVar)
100-
Expect(err).NotTo(HaveOccurred())
95+
Expect(os.Unsetenv(operatorCondEnvVar)).To(Succeed())
10196

10297
objKey, err := fn()
103-
Expect(err).To(HaveOccurred())
98+
Expect(err).To(MatchError(ContainSubstring("could not determine operator condition name")))
10499
Expect(objKey).To(BeNil())
105-
Expect(err.Error()).To(ContainSubstring("could not determine operator condition name"))
106100
})
107101

108102
It("should error when object namespace cannot be found", func() {
109-
err := os.Setenv(operatorCondEnvVar, "test")
110-
Expect(err).NotTo(HaveOccurred())
103+
Expect(os.Setenv(operatorCondEnvVar, "test")).To(Succeed())
111104

112105
readNamespace = func() (string, error) {
113106
return "", os.ErrNotExist
114107
}
115108

116109
objKey, err := fn()
117-
Expect(err).To(HaveOccurred())
110+
Expect(err).To(MatchError(ContainSubstring("get operator condition namespace: file does not exist")))
118111
Expect(objKey).To(BeNil())
119-
Expect(err.Error()).To(ContainSubstring("get operator condition namespace: file does not exist"))
120112
})
121113

122114
It("should return the right namespaced name from SA namespace file", func() {
123-
err := os.Setenv(operatorCondEnvVar, "test")
124-
Expect(err).NotTo(HaveOccurred())
115+
Expect(os.Setenv(operatorCondEnvVar, "test")).To(Succeed())
125116

126117
readNamespace = func() (string, error) {
127118
return "testns", nil
@@ -135,6 +126,5 @@ func testGetNamespacedName(fn func() (*types.NamespacedName, error)) {
135126
}
136127

137128
func deleteCondition(ctx context.Context, client client.Client, obj client.Object) {
138-
err := client.Delete(ctx, obj)
139-
Expect(err).NotTo(HaveOccurred())
129+
Expect(client.Delete(ctx, obj)).To(Succeed())
140130
}

‎handler/enqueue_annotation_test.go

+9-17
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ var _ = Describe("EnqueueRequestForAnnotation", func() {
5656

5757
podOwner.SetGroupVersionKind(schema.GroupVersionKind{Group: "", Kind: "Pod"})
5858

59-
err := SetOwnerAnnotations(podOwner, pod)
60-
Expect(err).To(BeNil())
59+
Expect(SetOwnerAnnotations(podOwner, pod)).To(Succeed())
6160
instance = EnqueueRequestForAnnotation{
6261
Type: schema.GroupKind{
6362
Group: "",
@@ -92,8 +91,7 @@ var _ = Describe("EnqueueRequestForAnnotation", func() {
9291
},
9392
}
9493

95-
err := SetOwnerAnnotations(podOwner, repl)
96-
Expect(err).To(BeNil())
94+
Expect(SetOwnerAnnotations(podOwner, repl)).To(Succeed())
9795

9896
evt := event.CreateEvent{
9997
Object: repl,
@@ -248,8 +246,7 @@ var _ = Describe("EnqueueRequestForAnnotation", func() {
248246
newPod.Name = pod.Name + "2"
249247
newPod.Namespace = pod.Namespace + "2"
250248

251-
err := SetOwnerAnnotations(podOwner, pod)
252-
Expect(err).To(BeNil())
249+
Expect(SetOwnerAnnotations(podOwner, pod)).To(Succeed())
253250

254251
evt := event.UpdateEvent{
255252
ObjectOld: pod,
@@ -337,8 +334,7 @@ var _ = Describe("EnqueueRequestForAnnotation", func() {
337334
newPod.Name = pod.Name + "2"
338335
newPod.Namespace = pod.Namespace + "2"
339336

340-
err := SetOwnerAnnotations(podOwner, pod)
341-
Expect(err).To(BeNil())
337+
Expect(SetOwnerAnnotations(podOwner, pod)).To(Succeed())
342338

343339
var podOwner2 = &corev1.Pod{
344340
ObjectMeta: metav1.ObjectMeta{
@@ -348,8 +344,7 @@ var _ = Describe("EnqueueRequestForAnnotation", func() {
348344
}
349345
podOwner2.SetGroupVersionKind(schema.GroupVersionKind{Group: "", Kind: "Pod"})
350346

351-
err = SetOwnerAnnotations(podOwner2, newPod)
352-
Expect(err).To(BeNil())
347+
Expect(SetOwnerAnnotations(podOwner2, newPod)).To(Succeed())
353348

354349
evt := event.UpdateEvent{
355350
ObjectOld: pod,
@@ -389,16 +384,15 @@ var _ = Describe("EnqueueRequestForAnnotation", func() {
389384
},
390385
}
391386

392-
err := SetOwnerAnnotations(podOwner, nd)
393-
Expect(err).To(BeNil())
387+
Expect(SetOwnerAnnotations(podOwner, nd)).To(Succeed())
394388

395389
expected := map[string]string{
396390
"my-test-annotation": "should-keep",
397391
NamespacedNameAnnotation: "podOwnerNs/podOwnerName",
398392
TypeAnnotation: schema.GroupKind{Group: "", Kind: "Pod"}.String(),
399393
}
400394

401-
Expect(len(nd.GetAnnotations())).To(Equal(3))
395+
Expect(nd.GetAnnotations()).To(HaveLen(3))
402396
Expect(nd.GetAnnotations()).To(Equal(expected))
403397
})
404398
It("should return error when the owner Kind is not present", func() {
@@ -409,8 +403,7 @@ var _ = Describe("EnqueueRequestForAnnotation", func() {
409403
}
410404

411405
podOwner.SetGroupVersionKind(schema.GroupVersionKind{Group: "Pod", Kind: ""})
412-
err := SetOwnerAnnotations(podOwner, nd)
413-
Expect(err).NotTo(BeNil())
406+
Expect(SetOwnerAnnotations(podOwner, nd)).ToNot(Succeed())
414407
})
415408
It("should return an error when the owner Name is not set", func() {
416409
nd := &corev1.Node{
@@ -426,8 +419,7 @@ var _ = Describe("EnqueueRequestForAnnotation", func() {
426419
}
427420

428421
ownerNew.SetGroupVersionKind(schema.GroupVersionKind{Group: "Pod", Kind: ""})
429-
err := SetOwnerAnnotations(ownerNew, nd)
430-
Expect(err).NotTo(BeNil())
422+
Expect(SetOwnerAnnotations(ownerNew, nd)).ToNot(Succeed())
431423
})
432424
})
433425
})

‎handler/instrumented_enqueue_object_test.go

+25-23
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ var _ = Describe("InstrumentedEnqueueRequestForObject", func() {
7979
// verify metrics
8080
gauges, err := registry.Gather()
8181
Expect(err).NotTo(HaveOccurred())
82-
Expect(len(gauges)).To(Equal(1))
82+
Expect(gauges).To(HaveLen(1))
8383
assertMetrics(gauges[0], 1, []*corev1.Pod{pod})
8484
})
8585
})
@@ -114,7 +114,7 @@ var _ = Describe("InstrumentedEnqueueRequestForObject", func() {
114114
// verify metrics
115115
gauges, err := registry.Gather()
116116
Expect(err).NotTo(HaveOccurred())
117-
Expect(len(gauges)).To(Equal(0))
117+
Expect(gauges).To(BeEmpty())
118118
})
119119
})
120120
Context("when a gauge does not exist", func() {
@@ -139,7 +139,7 @@ var _ = Describe("InstrumentedEnqueueRequestForObject", func() {
139139
// verify metrics
140140
gauges, err := registry.Gather()
141141
Expect(err).NotTo(HaveOccurred())
142-
Expect(len(gauges)).To(Equal(0))
142+
Expect(gauges).To(BeEmpty())
143143
})
144144
})
145145

@@ -174,7 +174,7 @@ var _ = Describe("InstrumentedEnqueueRequestForObject", func() {
174174
// verify metrics
175175
gauges, err := registry.Gather()
176176
Expect(err).NotTo(HaveOccurred())
177-
Expect(len(gauges)).To(Equal(1))
177+
Expect(gauges).To(HaveLen(1))
178178
assertMetrics(gauges[0], 2, []*corev1.Pod{newpod, pod})
179179
})
180180
})
@@ -183,7 +183,7 @@ var _ = Describe("InstrumentedEnqueueRequestForObject", func() {
183183
It("should fill out map with values from given objects", func() {
184184
labelMap := getResourceLabels(pod)
185185
Expect(labelMap).ShouldNot(BeEmpty())
186-
Expect(len(labelMap)).To(Equal(5))
186+
Expect(labelMap).To(HaveLen(5))
187187
Expect(labelMap["name"]).To(Equal(pod.GetObjectMeta().GetName()))
188188
Expect(labelMap["namespace"]).To(Equal(pod.GetObjectMeta().GetNamespace()))
189189
Expect(labelMap["group"]).To(Equal(pod.GetObjectKind().GroupVersionKind().Group))
@@ -195,28 +195,30 @@ var _ = Describe("InstrumentedEnqueueRequestForObject", func() {
195195

196196
func assertMetrics(gauge *dto.MetricFamily, count int, pods []*corev1.Pod) {
197197
// need variables to compare the pointers
198-
name := "name"
199-
namespace := "namespace"
200-
g := "group"
201-
v := "version"
202-
k := "kind"
203-
204-
Expect(len(gauge.Metric)).To(Equal(count))
198+
const (
199+
name = "name"
200+
namespace = "namespace"
201+
g = "group"
202+
v = "version"
203+
k = "kind"
204+
)
205+
206+
Expect(gauge.Metric).To(HaveLen(count))
205207
for i := 0; i < count; i++ {
206208
Expect(*gauge.Metric[i].Gauge.Value).To(Equal(float64(pods[i].GetObjectMeta().GetCreationTimestamp().UTC().Unix())))
207209

208210
for _, l := range gauge.Metric[i].Label {
209-
switch l.Name {
210-
case &name:
211-
Expect(l.Value).To(Equal(pods[i].GetObjectMeta().GetName()))
212-
case &namespace:
213-
Expect(l.Value).To(Equal(pods[i].GetObjectMeta().GetNamespace()))
214-
case &g:
215-
Expect(l.Value).To(Equal(pods[i].GetObjectKind().GroupVersionKind().Group))
216-
case &v:
217-
Expect(l.Value).To(Equal(pods[i].GetObjectKind().GroupVersionKind().Version))
218-
case &k:
219-
Expect(l.Value).To(Equal(pods[i].GetObjectKind().GroupVersionKind().Kind))
211+
switch *l.Name {
212+
case name:
213+
Expect(l.Value).To(HaveValue(Equal(pods[i].GetObjectMeta().GetName())))
214+
case namespace:
215+
Expect(l.Value).To(HaveValue(Equal(pods[i].GetObjectMeta().GetNamespace())))
216+
case g:
217+
Expect(l.Value).To(HaveValue(Equal(pods[i].GetObjectKind().GroupVersionKind().Group)))
218+
case v:
219+
Expect(l.Value).To(HaveValue(Equal(pods[i].GetObjectKind().GroupVersionKind().Version)))
220+
case k:
221+
Expect(l.Value).To(HaveValue(Equal(pods[i].GetObjectKind().GroupVersionKind().Kind)))
220222
}
221223
}
222224
}

‎internal/utils/utils_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ var _ = Describe("Helpers test", func() {
3838

3939
// test
4040
namespace, err := GetOperatorNamespace()
41-
Expect(err).Should(BeNil())
41+
Expect(err).ShouldNot(HaveOccurred())
4242
Expect(namespace).To(Equal("testnamespace"))
4343
})
4444
It("should trim whitespace from namespace", func() {
@@ -48,7 +48,7 @@ var _ = Describe("Helpers test", func() {
4848

4949
// test
5050
namespace, err := GetOperatorNamespace()
51-
Expect(err).Should(BeNil())
51+
Expect(err).ShouldNot(HaveOccurred())
5252
Expect(namespace).To(Equal("testnamespace"))
5353
})
5454
})

0 commit comments

Comments
 (0)
Please sign in to comment.