Skip to content

Commit bbe7752

Browse files
authored
update golangci-lint to 1.62 (#3332)
- ensure consistent pointer/value receivers - testify: json assertions with dedicated methods
1 parent 2ab93f7 commit bbe7752

File tree

11 files changed

+52
-42
lines changed

11 files changed

+52
-42
lines changed

.github/workflows/go-tests-windows.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ jobs:
6161
- name: golangci-lint
6262
uses: golangci/golangci-lint-action@v6
6363
with:
64-
version: v1.61
64+
version: v1.62
6565
args: --issues-exit-code=1 --timeout 10m
6666
only-new-issues: false

.github/workflows/go-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,6 @@ jobs:
190190
- name: golangci-lint
191191
uses: golangci/golangci-lint-action@v6
192192
with:
193-
version: v1.61
193+
version: v1.62
194194
args: --issues-exit-code=1 --timeout 10m
195195
only-new-issues: false

.golangci.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,7 @@ linters:
211211
#
212212
# DEPRECATED by golangi-lint
213213
#
214-
- execinquery
215214
- exportloopref
216-
- gomnd
217215

218216
#
219217
# Redundant
@@ -456,3 +454,14 @@ issues:
456454
- revive
457455
path: "cmd/crowdsec/win_service.go"
458456
text: "deep-exit: .*"
457+
458+
- linters:
459+
- recvcheck
460+
path: "pkg/csplugin/hclog_adapter.go"
461+
text: 'the methods of "HCLogAdapter" use pointer receiver and non-pointer receiver.'
462+
463+
# encoding to json/yaml requires value receivers
464+
- linters:
465+
- recvcheck
466+
path: "pkg/cwhub/item.go"
467+
text: 'the methods of "Item" use pointer receiver and non-pointer receiver.'

cmd/crowdsec-cli/clinotifications/notifications.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func (cli *cliNotifications) notificationConfigFilter(cmd *cobra.Command, args [
260260
return ret, cobra.ShellCompDirectiveNoFileComp
261261
}
262262

263-
func (cli cliNotifications) newTestCmd() *cobra.Command {
263+
func (cli *cliNotifications) newTestCmd() *cobra.Command {
264264
var (
265265
pluginBroker csplugin.PluginBroker
266266
pluginTomb tomb.Tomb

cmd/crowdsec/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,14 @@ func (l *labelsMap) String() string {
148148
return "labels"
149149
}
150150

151-
func (l labelsMap) Set(label string) error {
151+
func (l *labelsMap) Set(label string) error {
152152
for _, pair := range strings.Split(label, ",") {
153153
split := strings.Split(pair, ":")
154154
if len(split) != 2 {
155155
return fmt.Errorf("invalid format for label '%s', must be key:value", pair)
156156
}
157157

158-
l[split[0]] = split[1]
158+
(*l)[split[0]] = split[1]
159159
}
160160

161161
return nil

pkg/apiserver/alerts_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,14 @@ func TestCreateAlert(t *testing.T) {
121121

122122
w := lapi.RecordResponse(t, ctx, http.MethodPost, "/v1/alerts", strings.NewReader("test"), "password")
123123
assert.Equal(t, 400, w.Code)
124-
assert.Equal(t, `{"message":"invalid character 'e' in literal true (expecting 'r')"}`, w.Body.String())
124+
assert.JSONEq(t, `{"message":"invalid character 'e' in literal true (expecting 'r')"}`, w.Body.String())
125125

126126
// Create Alert with invalid input
127127
alertContent := GetAlertReaderFromFile(t, "./tests/invalidAlert_sample.json")
128128

129129
w = lapi.RecordResponse(t, ctx, http.MethodPost, "/v1/alerts", alertContent, "password")
130130
assert.Equal(t, 500, w.Code)
131-
assert.Equal(t,
131+
assert.JSONEq(t,
132132
`{"message":"validation failure list:\n0.scenario in body is required\n0.scenario_hash in body is required\n0.scenario_version in body is required\n0.simulated in body is required\n0.source in body is required"}`,
133133
w.Body.String())
134134

@@ -176,7 +176,7 @@ func TestAlertListFilters(t *testing.T) {
176176

177177
w := lapi.RecordResponse(t, ctx, "GET", "/v1/alerts?test=test", alertContent, "password")
178178
assert.Equal(t, 500, w.Code)
179-
assert.Equal(t, `{"message":"Filter parameter 'test' is unknown (=test): invalid filter"}`, w.Body.String())
179+
assert.JSONEq(t, `{"message":"Filter parameter 'test' is unknown (=test): invalid filter"}`, w.Body.String())
180180

181181
// get without filters
182182

@@ -242,7 +242,7 @@ func TestAlertListFilters(t *testing.T) {
242242

243243
w = lapi.RecordResponse(t, ctx, "GET", "/v1/alerts?ip=gruueq", emptyBody, "password")
244244
assert.Equal(t, 500, w.Code)
245-
assert.Equal(t, `{"message":"unable to convert 'gruueq' to int: invalid address: invalid ip address / range"}`, w.Body.String())
245+
assert.JSONEq(t, `{"message":"unable to convert 'gruueq' to int: invalid address: invalid ip address / range"}`, w.Body.String())
246246

247247
// test range (ok)
248248

@@ -261,7 +261,7 @@ func TestAlertListFilters(t *testing.T) {
261261

262262
w = lapi.RecordResponse(t, ctx, "GET", "/v1/alerts?range=ratata", emptyBody, "password")
263263
assert.Equal(t, 500, w.Code)
264-
assert.Equal(t, `{"message":"unable to convert 'ratata' to int: invalid address: invalid ip address / range"}`, w.Body.String())
264+
assert.JSONEq(t, `{"message":"unable to convert 'ratata' to int: invalid address: invalid ip address / range"}`, w.Body.String())
265265

266266
// test since (ok)
267267

@@ -332,7 +332,7 @@ func TestAlertListFilters(t *testing.T) {
332332

333333
w = lapi.RecordResponse(t, ctx, "GET", "/v1/alerts?has_active_decision=ratatqata", emptyBody, "password")
334334
assert.Equal(t, 500, w.Code)
335-
assert.Equal(t, `{"message":"'ratatqata' is not a boolean: strconv.ParseBool: parsing \"ratatqata\": invalid syntax: unable to parse type"}`, w.Body.String())
335+
assert.JSONEq(t, `{"message":"'ratatqata' is not a boolean: strconv.ParseBool: parsing \"ratatqata\": invalid syntax: unable to parse type"}`, w.Body.String())
336336
}
337337

338338
func TestAlertBulkInsert(t *testing.T) {
@@ -354,7 +354,7 @@ func TestListAlert(t *testing.T) {
354354

355355
w := lapi.RecordResponse(t, ctx, "GET", "/v1/alerts?test=test", emptyBody, "password")
356356
assert.Equal(t, 500, w.Code)
357-
assert.Equal(t, `{"message":"Filter parameter 'test' is unknown (=test): invalid filter"}`, w.Body.String())
357+
assert.JSONEq(t, `{"message":"Filter parameter 'test' is unknown (=test): invalid filter"}`, w.Body.String())
358358

359359
// List Alert
360360

@@ -397,7 +397,7 @@ func TestDeleteAlert(t *testing.T) {
397397
req.RemoteAddr = "127.0.0.2:4242"
398398
lapi.router.ServeHTTP(w, req)
399399
assert.Equal(t, 403, w.Code)
400-
assert.Equal(t, `{"message":"access forbidden from this IP (127.0.0.2)"}`, w.Body.String())
400+
assert.JSONEq(t, `{"message":"access forbidden from this IP (127.0.0.2)"}`, w.Body.String())
401401

402402
// Delete Alert
403403
w = httptest.NewRecorder()
@@ -406,7 +406,7 @@ func TestDeleteAlert(t *testing.T) {
406406
req.RemoteAddr = "127.0.0.1:4242"
407407
lapi.router.ServeHTTP(w, req)
408408
assert.Equal(t, 200, w.Code)
409-
assert.Equal(t, `{"nbDeleted":"1"}`, w.Body.String())
409+
assert.JSONEq(t, `{"nbDeleted":"1"}`, w.Body.String())
410410
}
411411

412412
func TestDeleteAlertByID(t *testing.T) {
@@ -421,7 +421,7 @@ func TestDeleteAlertByID(t *testing.T) {
421421
req.RemoteAddr = "127.0.0.2:4242"
422422
lapi.router.ServeHTTP(w, req)
423423
assert.Equal(t, 403, w.Code)
424-
assert.Equal(t, `{"message":"access forbidden from this IP (127.0.0.2)"}`, w.Body.String())
424+
assert.JSONEq(t, `{"message":"access forbidden from this IP (127.0.0.2)"}`, w.Body.String())
425425

426426
// Delete Alert
427427
w = httptest.NewRecorder()
@@ -430,7 +430,7 @@ func TestDeleteAlertByID(t *testing.T) {
430430
req.RemoteAddr = "127.0.0.1:4242"
431431
lapi.router.ServeHTTP(w, req)
432432
assert.Equal(t, 200, w.Code)
433-
assert.Equal(t, `{"nbDeleted":"1"}`, w.Body.String())
433+
assert.JSONEq(t, `{"nbDeleted":"1"}`, w.Body.String())
434434
}
435435

436436
func TestDeleteAlertTrustedIPS(t *testing.T) {
@@ -475,7 +475,7 @@ func TestDeleteAlertTrustedIPS(t *testing.T) {
475475

476476
router.ServeHTTP(w, req)
477477
assert.Equal(t, 200, w.Code)
478-
assert.Equal(t, `{"nbDeleted":"1"}`, w.Body.String())
478+
assert.JSONEq(t, `{"nbDeleted":"1"}`, w.Body.String())
479479
}
480480

481481
lapi.InsertAlertFromFile(t, ctx, "./tests/alert_sample.json")

pkg/apiserver/decisions_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ func TestDeleteDecisionRange(t *testing.T) {
2222
// delete by ip wrong
2323
w := lapi.RecordResponse(t, ctx, "DELETE", "/v1/decisions?range=1.2.3.0/24", emptyBody, PASSWORD)
2424
assert.Equal(t, 200, w.Code)
25-
assert.Equal(t, `{"nbDeleted":"0"}`, w.Body.String())
25+
assert.JSONEq(t, `{"nbDeleted":"0"}`, w.Body.String())
2626

2727
// delete by range
2828

2929
w = lapi.RecordResponse(t, ctx, "DELETE", "/v1/decisions?range=91.121.79.0/24&contains=false", emptyBody, PASSWORD)
3030
assert.Equal(t, 200, w.Code)
31-
assert.Equal(t, `{"nbDeleted":"2"}`, w.Body.String())
31+
assert.JSONEq(t, `{"nbDeleted":"2"}`, w.Body.String())
3232

3333
// delete by range : ensure it was already deleted
3434

3535
w = lapi.RecordResponse(t, ctx, "DELETE", "/v1/decisions?range=91.121.79.0/24", emptyBody, PASSWORD)
3636
assert.Equal(t, 200, w.Code)
37-
assert.Equal(t, `{"nbDeleted":"0"}`, w.Body.String())
37+
assert.JSONEq(t, `{"nbDeleted":"0"}`, w.Body.String())
3838
}
3939

4040
func TestDeleteDecisionFilter(t *testing.T) {
@@ -48,19 +48,19 @@ func TestDeleteDecisionFilter(t *testing.T) {
4848

4949
w := lapi.RecordResponse(t, ctx, "DELETE", "/v1/decisions?ip=1.2.3.4", emptyBody, PASSWORD)
5050
assert.Equal(t, 200, w.Code)
51-
assert.Equal(t, `{"nbDeleted":"0"}`, w.Body.String())
51+
assert.JSONEq(t, `{"nbDeleted":"0"}`, w.Body.String())
5252

5353
// delete by ip good
5454

5555
w = lapi.RecordResponse(t, ctx, "DELETE", "/v1/decisions?ip=91.121.79.179", emptyBody, PASSWORD)
5656
assert.Equal(t, 200, w.Code)
57-
assert.Equal(t, `{"nbDeleted":"1"}`, w.Body.String())
57+
assert.JSONEq(t, `{"nbDeleted":"1"}`, w.Body.String())
5858

5959
// delete by scope/value
6060

6161
w = lapi.RecordResponse(t, ctx, "DELETE", "/v1/decisions?scopes=Ip&value=91.121.79.178", emptyBody, PASSWORD)
6262
assert.Equal(t, 200, w.Code)
63-
assert.Equal(t, `{"nbDeleted":"1"}`, w.Body.String())
63+
assert.JSONEq(t, `{"nbDeleted":"1"}`, w.Body.String())
6464
}
6565

6666
func TestDeleteDecisionFilterByScenario(t *testing.T) {
@@ -74,13 +74,13 @@ func TestDeleteDecisionFilterByScenario(t *testing.T) {
7474

7575
w := lapi.RecordResponse(t, ctx, "DELETE", "/v1/decisions?scenario=crowdsecurity/ssh-bff", emptyBody, PASSWORD)
7676
assert.Equal(t, 200, w.Code)
77-
assert.Equal(t, `{"nbDeleted":"0"}`, w.Body.String())
77+
assert.JSONEq(t, `{"nbDeleted":"0"}`, w.Body.String())
7878

7979
// delete by scenario good
8080

8181
w = lapi.RecordResponse(t, ctx, "DELETE", "/v1/decisions?scenario=crowdsecurity/ssh-bf", emptyBody, PASSWORD)
8282
assert.Equal(t, 200, w.Code)
83-
assert.Equal(t, `{"nbDeleted":"2"}`, w.Body.String())
83+
assert.JSONEq(t, `{"nbDeleted":"2"}`, w.Body.String())
8484
}
8585

8686
func TestGetDecisionFilters(t *testing.T) {

pkg/apiserver/jwt_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestLogin(t *testing.T) {
2323
router.ServeHTTP(w, req)
2424

2525
assert.Equal(t, 401, w.Code)
26-
assert.Equal(t, `{"code":401,"message":"machine test not validated"}`, w.Body.String())
26+
assert.JSONEq(t, `{"code":401,"message":"machine test not validated"}`, w.Body.String())
2727

2828
// Login with machine not exist
2929
w = httptest.NewRecorder()
@@ -32,7 +32,7 @@ func TestLogin(t *testing.T) {
3232
router.ServeHTTP(w, req)
3333

3434
assert.Equal(t, 401, w.Code)
35-
assert.Equal(t, `{"code":401,"message":"ent: machine not found"}`, w.Body.String())
35+
assert.JSONEq(t, `{"code":401,"message":"ent: machine not found"}`, w.Body.String())
3636

3737
// Login with invalid body
3838
w = httptest.NewRecorder()
@@ -41,7 +41,7 @@ func TestLogin(t *testing.T) {
4141
router.ServeHTTP(w, req)
4242

4343
assert.Equal(t, 401, w.Code)
44-
assert.Equal(t, `{"code":401,"message":"missing: invalid character 'e' in literal true (expecting 'r')"}`, w.Body.String())
44+
assert.JSONEq(t, `{"code":401,"message":"missing: invalid character 'e' in literal true (expecting 'r')"}`, w.Body.String())
4545

4646
// Login with invalid format
4747
w = httptest.NewRecorder()
@@ -50,7 +50,7 @@ func TestLogin(t *testing.T) {
5050
router.ServeHTTP(w, req)
5151

5252
assert.Equal(t, 401, w.Code)
53-
assert.Equal(t, `{"code":401,"message":"validation failure list:\npassword in body is required"}`, w.Body.String())
53+
assert.JSONEq(t, `{"code":401,"message":"validation failure list:\npassword in body is required"}`, w.Body.String())
5454

5555
// Validate machine
5656
ValidateMachine(t, ctx, "test", config.API.Server.DbConfig)
@@ -62,7 +62,7 @@ func TestLogin(t *testing.T) {
6262
router.ServeHTTP(w, req)
6363

6464
assert.Equal(t, 401, w.Code)
65-
assert.Equal(t, `{"code":401,"message":"incorrect Username or Password"}`, w.Body.String())
65+
assert.JSONEq(t, `{"code":401,"message":"incorrect Username or Password"}`, w.Body.String())
6666

6767
// Login with valid machine
6868
w = httptest.NewRecorder()

pkg/apiserver/machines_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestCreateMachine(t *testing.T) {
2525
router.ServeHTTP(w, req)
2626

2727
assert.Equal(t, http.StatusBadRequest, w.Code)
28-
assert.Equal(t, `{"message":"invalid character 'e' in literal true (expecting 'r')"}`, w.Body.String())
28+
assert.JSONEq(t, `{"message":"invalid character 'e' in literal true (expecting 'r')"}`, w.Body.String())
2929

3030
// Create machine with invalid input
3131
w = httptest.NewRecorder()
@@ -34,7 +34,7 @@ func TestCreateMachine(t *testing.T) {
3434
router.ServeHTTP(w, req)
3535

3636
assert.Equal(t, http.StatusUnprocessableEntity, w.Code)
37-
assert.Equal(t, `{"message":"validation failure list:\nmachine_id in body is required\npassword in body is required"}`, w.Body.String())
37+
assert.JSONEq(t, `{"message":"validation failure list:\nmachine_id in body is required\npassword in body is required"}`, w.Body.String())
3838

3939
// Create machine
4040
b, err := json.Marshal(MachineTest)
@@ -144,7 +144,7 @@ func TestCreateMachineAlreadyExist(t *testing.T) {
144144
router.ServeHTTP(w, req)
145145

146146
assert.Equal(t, http.StatusForbidden, w.Code)
147-
assert.Equal(t, `{"message":"user 'test': user already exist"}`, w.Body.String())
147+
assert.JSONEq(t, `{"message":"user 'test': user already exist"}`, w.Body.String())
148148
}
149149

150150
func TestAutoRegistration(t *testing.T) {

pkg/appsec/coraza_logger.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func (e *crzLogEvent) Stringer(key string, val fmt.Stringer) dbg.Event {
124124
return e
125125
}
126126

127-
func (e crzLogEvent) IsEnabled() bool {
127+
func (e *crzLogEvent) IsEnabled() bool {
128128
return !e.muted
129129
}
130130

pkg/cache/cache_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,27 @@ import (
55
"time"
66

77
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
89
)
910

1011
func TestCreateSetGet(t *testing.T) {
1112
err := CacheInit(CacheCfg{Name: "test", Size: 100, TTL: 1 * time.Second})
12-
assert.Empty(t, err)
13+
require.NoError(t, err)
1314
//set & get
1415
err = SetKey("test", "testkey0", "testvalue1", nil)
15-
assert.Empty(t, err)
16+
require.NoError(t, err)
1617

1718
ret, err := GetKey("test", "testkey0")
1819
assert.Equal(t, "testvalue1", ret)
19-
assert.Empty(t, err)
20+
require.NoError(t, err)
2021
//re-set
2122
err = SetKey("test", "testkey0", "testvalue2", nil)
22-
assert.Empty(t, err)
23+
require.NoError(t, err)
2324
assert.Equal(t, "testvalue1", ret)
24-
assert.Empty(t, err)
25+
require.NoError(t, err)
2526
//expire
2627
time.Sleep(1500 * time.Millisecond)
2728
ret, err = GetKey("test", "testkey0")
2829
assert.Equal(t, "", ret)
29-
assert.Empty(t, err)
30+
require.NoError(t, err)
3031
}

0 commit comments

Comments
 (0)