Skip to content

Commit a943c46

Browse files
run go fix ./..., add as step in bk
1 parent b8a9404 commit a943c46

111 files changed

Lines changed: 772 additions & 882 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

AGENTS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ Guiding principles and expectations: [CONTRIBUTING.md](./CONTRIBUTING.md).
199199
- **Update docs and tests** when behavior, configuration, or operator-facing workflow changes.
200200
- **Make non-obvious intent clear** through naming, structure, or brief “why” comments when needed.
201201
- **Formatting:** All go files must be formatted with `go fmt`, and imports must be ordereed with `goimports` which can be done with the `mage check:imports` target.
202+
- **Go Fix:** All .go files must be updated with `go fix`, this can be done with the `mage check:fix` target.
202203
- **Changelog:** For notable changes, add a fragment using **[elastic-agent-changelog-tool](https://github.com/elastic/elastic-agent-changelog-tool)**. Typical usage: `elastic-agent-changelog-tool new "$TITLE"` (see the tool’s [usage docs](https://github.com/elastic/elastic-agent-changelog-tool/blob/main/docs/usage.md)). PRs may use the **`skip-changelog`** label when appropriate; see `changelog/` for examples.
203204
- **`go.mod` / NOTICE:** If you change **`go.mod`** or add/update Go dependencies, regenerate **`NOTICE.txt`** and **`NOTICE-fips.txt`** with `mage check:notice`
204205
- **Before opening a PR:** `mage check:all` and `mage test:unit` must pass at minimum; integration or E2E tests must also pass when behavior depends on Elasticsearch or full HTTP flows (see **Testing** above).

internal/pkg/api/apiVersion.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"net/http"
1111
"regexp"
12+
"slices"
1213
"strings"
1314
)
1415

@@ -47,12 +48,7 @@ func (a *apiVersion) validateVersionFormat(version string) error {
4748
}
4849

4950
func (a *apiVersion) isVersionSupported(version string) bool {
50-
for _, vers := range a.supportedVersions {
51-
if vers == version {
52-
return true
53-
}
54-
}
55-
return false
51+
return slices.Contains(a.supportedVersions, version)
5652
}
5753

5854
func (a *apiVersion) middleware(next http.Handler) http.Handler {

internal/pkg/api/error_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ func Test_ErrorResp(t *testing.T) {
2222
tests := []struct {
2323
name string
2424
err error
25-
expectedTags map[string]interface{}
25+
expectedTags map[string]any
2626
}{{
2727
name: "generic error",
2828
err: fmt.Errorf("generic error"),
2929
}, {
3030
name: "elastic error",
3131
err: &es.ErrElastic{},
32-
expectedTags: map[string]interface{}{
32+
expectedTags: map[string]any{
3333
"error_type": "ErrElastic",
3434
},
3535
}, {
3636
name: "wrapped elastic error",
3737
err: fmt.Errorf("wrapped error: %w", &es.ErrElastic{}),
38-
expectedTags: map[string]interface{}{
38+
expectedTags: map[string]any{
3939
"error_type": "ErrElastic",
4040
},
4141
}}
@@ -64,7 +64,7 @@ func Test_ErrorResp(t *testing.T) {
6464
require.Len(t, payloads.Transactions, 1)
6565
require.Len(t, payloads.Errors, 1)
6666

67-
tags := make(map[string]interface{})
67+
tags := make(map[string]any)
6868
for _, tag := range payloads.Transactions[0].Context.Tags {
6969
tags[tag.Key] = tag.Value
7070
}

internal/pkg/api/handleAck_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestMakeUpdatePolicyBody(t *testing.T) {
4848

4949
data := makeUpdatePolicyBody(policyID, newRev)
5050

51-
var i interface{}
51+
var i any
5252
err := json.Unmarshal(data, &i)
5353

5454
if err != nil {
@@ -276,7 +276,7 @@ func TestHandleAckEvents(t *testing.T) {
276276
}},
277277
res: newAckResponse(false, []AckResponseItem{{
278278
Status: http.StatusOK,
279-
Message: ptr(http.StatusText(http.StatusOK)),
279+
Message: new(http.StatusText(http.StatusOK)),
280280
}}),
281281
bulker: func(t *testing.T) *ftesting.MockBulk {
282282
return ftesting.NewMockBulk()
@@ -291,7 +291,7 @@ func TestHandleAckEvents(t *testing.T) {
291291
}},
292292
res: newAckResponse(false, []AckResponseItem{{
293293
Status: http.StatusOK,
294-
Message: ptr(http.StatusText(http.StatusOK)),
294+
Message: new(http.StatusText(http.StatusOK)),
295295
}}),
296296
bulker: func(t *testing.T) *ftesting.MockBulk {
297297
m := ftesting.NewMockBulk()
@@ -354,7 +354,7 @@ func TestHandleAckEvents(t *testing.T) {
354354
}},
355355
res: newAckResponse(true, []AckResponseItem{{
356356
Status: http.StatusServiceUnavailable,
357-
Message: ptr(http.StatusText(http.StatusServiceUnavailable)),
357+
Message: new(http.StatusText(http.StatusServiceUnavailable)),
358358
}}),
359359
bulker: func(t *testing.T) *ftesting.MockBulk {
360360
m := ftesting.NewMockBulk()
@@ -405,27 +405,27 @@ func TestHandleAckEvents(t *testing.T) {
405405
res: newAckResponse(true, []AckResponseItem{
406406
{
407407
Status: http.StatusOK,
408-
Message: ptr(http.StatusText(http.StatusOK)),
408+
Message: new(http.StatusText(http.StatusOK)),
409409
},
410410
{
411411
Status: http.StatusOK,
412-
Message: ptr(http.StatusText(http.StatusOK)),
412+
Message: new(http.StatusText(http.StatusOK)),
413413
},
414414
{
415415
Status: http.StatusNotFound,
416-
Message: ptr(http.StatusText(http.StatusNotFound)),
416+
Message: new(http.StatusText(http.StatusNotFound)),
417417
},
418418
{
419419
Status: http.StatusOK,
420-
Message: ptr(http.StatusText(http.StatusOK)),
420+
Message: new(http.StatusText(http.StatusOK)),
421421
},
422422
{
423423
Status: http.StatusOK,
424-
Message: ptr(http.StatusText(http.StatusOK)),
424+
Message: new(http.StatusText(http.StatusOK)),
425425
},
426426
{
427427
Status: http.StatusOK,
428-
Message: ptr(http.StatusText(http.StatusOK)),
428+
Message: new(http.StatusText(http.StatusOK)),
429429
},
430430
}),
431431
bulker: func(t *testing.T) *ftesting.MockBulk {
@@ -468,7 +468,7 @@ func TestHandleAckEvents(t *testing.T) {
468468
res: newAckResponse(false, []AckResponseItem{
469469
{
470470
Status: http.StatusOK,
471-
Message: ptr(http.StatusText(http.StatusOK)),
471+
Message: new(http.StatusText(http.StatusOK)),
472472
},
473473
}),
474474
bulker: func(t *testing.T) *ftesting.MockBulk {
@@ -495,7 +495,7 @@ func TestHandleAckEvents(t *testing.T) {
495495
res: newAckResponse(false, []AckResponseItem{
496496
{
497497
Status: http.StatusOK,
498-
Message: ptr(http.StatusText(http.StatusOK)),
498+
Message: new(http.StatusText(http.StatusOK)),
499499
},
500500
}),
501501
bulker: func(t *testing.T) *ftesting.MockBulk {
@@ -706,7 +706,7 @@ func TestAckHandleUpgrade(t *testing.T) {
706706
bulker: func(t *testing.T) *ftesting.MockBulk {
707707
m := ftesting.NewMockBulk()
708708
m.On("Update", mock.Anything, mock.Anything, mock.Anything, mock.MatchedBy(func(p []byte) bool {
709-
var body map[string]map[string]interface{}
709+
var body map[string]map[string]any
710710
if err := json.Unmarshal(p, &body); err != nil {
711711
t.Fatal(err)
712712
}
@@ -725,7 +725,7 @@ func TestAckHandleUpgrade(t *testing.T) {
725725
bulker: func(t *testing.T) *ftesting.MockBulk {
726726
m := ftesting.NewMockBulk()
727727
m.On("Update", mock.Anything, mock.Anything, mock.Anything, mock.MatchedBy(func(p []byte) bool {
728-
var body map[string]map[string]interface{}
728+
var body map[string]map[string]any
729729
if err := json.Unmarshal(p, &body); err != nil {
730730
t.Fatal(err)
731731
}

internal/pkg/api/handleCheckin.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,7 @@ func (ct *CheckinT) validateRequest(zlog zerolog.Logger, w http.ResponseWriter,
228228
// sets timeout is set to max(1m, min(pDur-2m, max poll time))
229229
// sets the response write timeout to max(2m, timeout+1m)
230230
if pDur != time.Duration(0) {
231-
pollDuration = pDur - (2 * time.Minute)
232-
if pollDuration > ct.cfg.Timeouts.CheckinMaxPoll {
233-
pollDuration = ct.cfg.Timeouts.CheckinMaxPoll
234-
}
235-
if pollDuration < time.Minute {
236-
pollDuration = time.Minute
237-
}
231+
pollDuration = max(min(pDur-(2*time.Minute), ct.cfg.Timeouts.CheckinMaxPoll), time.Minute)
238232

239233
wTime := pollDuration + time.Minute
240234
rc := http.NewResponseController(w)
@@ -691,12 +685,7 @@ func (ct *CheckinT) writeResponse(zlog zerolog.Logger, w http.ResponseWriter, r
691685
// acceptsEncoding reports whether the request includes the passed encoding.
692686
// Only an exact match is checked as that is all the agent will send.
693687
func acceptsEncoding(r *http.Request, encoding string) bool {
694-
for _, v := range r.Header.Values("Accept-Encoding") {
695-
if v == encoding {
696-
return true
697-
}
698-
}
699-
return false
688+
return slices.Contains(r.Header.Values("Accept-Encoding"), encoding)
700689
}
701690

702691
// Resolve AckToken from request, fallback on the agent record
@@ -1057,7 +1046,7 @@ func parseMeta(zlog zerolog.Logger, agent *model.Agent, req *CheckinRequest) ([]
10571046
}
10581047

10591048
// Deserialize the request metadata
1060-
var reqLocalMeta interface{}
1049+
var reqLocalMeta any
10611050
if err := json.Unmarshal(req.LocalMetadata, &reqLocalMeta); err != nil {
10621051
return nil, fmt.Errorf("parseMeta request: %w", err)
10631052
}
@@ -1069,7 +1058,7 @@ func parseMeta(zlog zerolog.Logger, agent *model.Agent, req *CheckinRequest) ([]
10691058

10701059
// Deserialize the agent's metadata copy. If it fails, it's ignored as it will just
10711060
// be replaced with the correct contents from the clients checkin.
1072-
var agentLocalMeta interface{}
1061+
var agentLocalMeta any
10731062
if err := json.Unmarshal(agent.LocalMetadata, &agentLocalMeta); err != nil {
10741063
zlog.Warn().Err(err).Msg("local_metadata in document invalid; ignoring it")
10751064
}

internal/pkg/api/handleCheckin_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,7 @@ func TestResolveSeqNo(t *testing.T) {
358358
t.Run(tc.name, func(t *testing.T) {
359359
// setup mock CheckinT
360360
logger := testlog.SetLogger(t)
361-
ctx, cancel := context.WithCancel(context.Background())
362-
defer cancel()
361+
ctx := t.Context()
363362
verCon := mustBuildConstraints("8.0.0")
364363
cfg := &config.Server{}
365364
c, _ := cache.New(config.Cache{NumCounters: 100, MaxCost: 100000})
@@ -415,7 +414,7 @@ func TestProcessUpgradeDetails(t *testing.T) {
415414
mBulk := ftesting.NewMockBulk()
416415
mBulk.On("Update", mock.Anything, dl.FleetAgents, "doc-ID", mock.MatchedBy(func(p []byte) bool {
417416
doc := struct {
418-
Doc map[string]interface{} `json:"doc"`
417+
Doc map[string]any `json:"doc"`
419418
}{}
420419
if err := json.Unmarshal(p, &doc); err != nil {
421420
t.Logf("bulk match unmarshal error: %v", err)
@@ -1092,7 +1091,7 @@ func TestParseComponents(t *testing.T) {
10921091
}
10931092
}
10941093

1095-
func requireMarshalJSON(t *testing.T, obj interface{}) json.RawMessage {
1094+
func requireMarshalJSON(t *testing.T, obj any) json.RawMessage {
10961095
data, err := json.Marshal(obj)
10971096
require.NoError(t, err)
10981097
return data

internal/pkg/api/handleEnroll.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -607,16 +607,16 @@ func updateLocalMetaAgentID(data []byte, agentID string) ([]byte, error) {
607607
return data, nil
608608
}
609609

610-
var m map[string]interface{}
610+
var m map[string]any
611611
err := json.Unmarshal(data, &m)
612612
if err != nil {
613613
return nil, err
614614
}
615615

616616
if v, ok := m["elastic"]; ok {
617-
if sm, ok := v.(map[string]interface{}); ok {
617+
if sm, ok := v.(map[string]any); ok {
618618
if v, ok = sm["agent"]; ok {
619-
if sm, ok = v.(map[string]interface{}); ok {
619+
if sm, ok = v.(map[string]any); ok {
620620
if _, ok = sm["id"]; ok {
621621
sm["id"] = agentID
622622
data, err = json.Marshal(m)

internal/pkg/api/handleEnroll_test.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ func TestRemoveDuplicateStr(t *testing.T) {
6363
}
6464

6565
func TestEnroll(t *testing.T) {
66-
ctx, cancel := context.WithCancel(context.Background())
67-
defer cancel()
66+
ctx := t.Context()
6867
rb := &rollback.Rollback{}
6968
zlog := zerolog.Logger{}
7069
enrollmentID := "1234"
@@ -102,8 +101,7 @@ func TestEnroll(t *testing.T) {
102101
}
103102

104103
func TestEnrollWithAgentID(t *testing.T) {
105-
ctx, cancel := context.WithCancel(context.Background())
106-
defer cancel()
104+
ctx := t.Context()
107105
rb := &rollback.Rollback{}
108106
zlog := zerolog.Logger{}
109107
agentID := "1234"
@@ -144,8 +142,7 @@ func TestEnrollWithAgentID(t *testing.T) {
144142
}
145143

146144
func TestEnrollWithAgentIDExistingNonActive(t *testing.T) {
147-
ctx, cancel := context.WithCancel(context.Background())
148-
defer cancel()
145+
ctx := t.Context()
149146
rb := &rollback.Rollback{}
150147
zlog := zerolog.Logger{}
151148
agentID := "1234"
@@ -191,8 +188,7 @@ func TestEnrollWithAgentIDExistingNonActive(t *testing.T) {
191188
}
192189

193190
func TestEnrollWithAgentIDExistingActive_NotReplaceable(t *testing.T) {
194-
ctx, cancel := context.WithCancel(context.Background())
195-
defer cancel()
191+
ctx := t.Context()
196192
rb := &rollback.Rollback{}
197193
zlog := zerolog.Logger{}
198194
agentID := "1234"
@@ -226,8 +222,7 @@ func TestEnrollWithAgentIDExistingActive_NotReplaceable(t *testing.T) {
226222
}
227223

228224
func TestEnrollWithAgentIDExistingActive_InvalidReplaceToken_Missing(t *testing.T) {
229-
ctx, cancel := context.WithCancel(context.Background())
230-
defer cancel()
225+
ctx := t.Context()
231226
rb := &rollback.Rollback{}
232227
zlog := zerolog.Logger{}
233228
agentID := "1234"
@@ -269,8 +264,7 @@ func TestEnrollWithAgentIDExistingActive_InvalidReplaceToken_Missing(t *testing.
269264
}
270265

271266
func TestEnrollWithAgentIDExistingActive_InvalidReplaceToken_Mismatch(t *testing.T) {
272-
ctx, cancel := context.WithCancel(context.Background())
273-
defer cancel()
267+
ctx := t.Context()
274268
rb := &rollback.Rollback{}
275269
zlog := zerolog.Logger{}
276270
agentID := "1234"
@@ -314,8 +308,7 @@ func TestEnrollWithAgentIDExistingActive_InvalidReplaceToken_Mismatch(t *testing
314308
}
315309

316310
func TestEnrollWithAgentIDExistingActive_WrongPolicy(t *testing.T) {
317-
ctx, cancel := context.WithCancel(context.Background())
318-
defer cancel()
311+
ctx := t.Context()
319312
rb := &rollback.Rollback{}
320313
zlog := zerolog.Logger{}
321314
agentID := "1234"
@@ -359,8 +352,7 @@ func TestEnrollWithAgentIDExistingActive_WrongPolicy(t *testing.T) {
359352
}
360353

361354
func TestEnrollWithAgentIDExistingActive(t *testing.T) {
362-
ctx, cancel := context.WithCancel(context.Background())
363-
defer cancel()
355+
ctx := t.Context()
364356
rb := &rollback.Rollback{}
365357
zlog := zerolog.Logger{}
366358
agentID := "1234"

0 commit comments

Comments
 (0)