Skip to content

Commit df33e2f

Browse files
Linter fixes and cleanup (#1424)
* Linter fixes and cleanup - all linter fixes - provide godoc strings to packages - minor cleanup of testing - get rid of pkg/testutil (used in single test file) - get rid of pkg/wait (replace use with pkg/sleep) - move pkg/rnd under pkg/testing - use testify/mock in plg/testing/bulk.go - assert on expected calls that use the mock - provide testing/log to stop log output from appearing when tests are ran for unit tests (integration test have a race failure)
1 parent f39e4aa commit df33e2f

File tree

145 files changed

+1270
-1180
lines changed

Some content is hidden

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

145 files changed

+1270
-1180
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,9 @@ fleet-server.dev.yml
1414

1515
dev-tools/integration/.env.bck
1616

17-
.service_token
17+
# editor swap files
18+
*.swp
19+
*.swo
20+
*.swn
21+
22+
.service_token

.golangci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
run:
33
# timeout for analysis, e.g. 30s, 5m, default is 1m
44
timeout: 1m
5+
build-tags: integration
56

67
issues:
78
# Maximum count of issues with the same text.
@@ -33,7 +34,7 @@ linters:
3334
- goconst # finds repeated strings that could be replaced by a constant
3435
- dupl # tool for code clone detection
3536
- forbidigo # forbids identifiers matched by reg exps
36-
- gomoddirectives # manage the use of 'replace', 'retract', and 'excludes' directives in go.mod.
37+
#- gomoddirectives # manage the use of 'replace', 'retract', and 'excludes' directives in go.mod.
3738
- gosimple # linter for Go source code that specializes in simplifying a code
3839
- misspell # finds commonly misspelled English words in comments
3940
- nakedret # finds naked returns in functions greater than a specified function length

NOTICE.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2344,11 +2344,11 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23442344

23452345
--------------------------------------------------------------------------------
23462346
Dependency : github.com/rs/xid
2347-
Version: v1.2.1
2347+
Version: v1.3.0
23482348
Licence type (autodetected): MIT
23492349
--------------------------------------------------------------------------------
23502350

2351-
Contents of probable licence file $GOMODCACHE/github.com/rs/xid@v1.2.1/LICENSE:
2351+
Contents of probable licence file $GOMODCACHE/github.com/rs/xid@v1.3.0/LICENSE:
23522352

23532353
Copyright (c) 2015 Olivier Poitrey <rs@dailymotion.com>
23542354

@@ -2373,11 +2373,11 @@ THE SOFTWARE.
23732373

23742374
--------------------------------------------------------------------------------
23752375
Dependency : github.com/rs/zerolog
2376-
Version: v1.22.0
2376+
Version: v1.26.1
23772377
Licence type (autodetected): MIT
23782378
--------------------------------------------------------------------------------
23792379

2380-
Contents of probable licence file $GOMODCACHE/github.com/rs/zerolog@v1.22.0/LICENSE:
2380+
Contents of probable licence file $GOMODCACHE/github.com/rs/zerolog@v1.26.1/LICENSE:
23812381

23822382
MIT License
23832383

@@ -41763,11 +41763,11 @@ THE SOFTWARE.
4176341763

4176441764
--------------------------------------------------------------------------------
4176541765
Dependency : golang.org/x/crypto
41766-
Version: v0.0.0-20211202192323-5770296d904e
41766+
Version: v0.0.0-20211215165025-cf75a172585e
4176741767
Licence type (autodetected): BSD-3-Clause
4176841768
--------------------------------------------------------------------------------
4176941769

41770-
Contents of probable licence file $GOMODCACHE/golang.org/x/crypto@v0.0.0-20211202192323-5770296d904e/LICENSE:
41770+
Contents of probable licence file $GOMODCACHE/golang.org/x/crypto@v0.0.0-20211215165025-cf75a172585e/LICENSE:
4177141771

4177241772
Copyright (c) 2009 The Go Authors. All rights reserved.
4177341773

cmd/fleet/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ func makeCacheConfig(cfg *config.Config) cache.Config {
8787
ActionTTL: ccfg.ActionTTL,
8888
EnrollKeyTTL: ccfg.EnrollKeyTTL,
8989
ArtifactTTL: ccfg.ArtifactTTL,
90-
ApiKeyTTL: ccfg.APIKeyTTL,
91-
ApiKeyJitter: ccfg.APIKeyJitter,
90+
APIKeyTTL: ccfg.APIKeyTTL,
91+
APIKeyJitter: ccfg.APIKeyJitter,
9292
}
9393
}
9494

cmd/fleet/main_integration_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//go:build integration
66
// +build integration
77

8+
//nolint:unused // some unused code may be added to more tests
89
package fleet
910

1011
import (
@@ -53,7 +54,7 @@ output:
5354
service_token: '${ELASTICSEARCH_SERVICE_TOKEN}'
5455
`
5556

56-
var agentIdCfgData = `
57+
var agentIDCfgData = `
5758
output:
5859
elasticsearch:
5960
hosts: '${ELASTICSEARCH_HOSTS:localhost:9200}'
@@ -84,9 +85,9 @@ func (s *agentSuite) TestAgentMode(t *testing.T) {
8485
bulker := ftesting.SetupBulk(ctx, t)
8586

8687
// add a real default fleet server policy
87-
policyId := uuid.Must(uuid.NewV4()).String()
88+
policyID := uuid.Must(uuid.NewV4()).String()
8889
_, err := dl.CreatePolicy(ctx, bulker, model.Policy{
89-
PolicyID: policyId,
90+
PolicyID: policyID,
9091
RevisionIdx: 1,
9192
DefaultFleetServer: true,
9293
Data: policyData,
@@ -98,7 +99,7 @@ func (s *agentSuite) TestAgentMode(t *testing.T) {
9899
Name: "Default",
99100
APIKey: "keyvalue",
100101
APIKeyID: "keyid",
101-
PolicyID: policyId,
102+
PolicyID: policyID,
102103
Active: true,
103104
})
104105
require.NoError(t, err)
@@ -134,7 +135,7 @@ func (s *agentSuite) TestAgentMode(t *testing.T) {
134135
}, ftesting.RetrySleep(100*time.Millisecond), ftesting.RetryCount(120))
135136

136137
// reconfigure with agent ID set
137-
err = appState.UpdateConfig(agentIdCfgData)
138+
err = appState.UpdateConfig(agentIDCfgData)
138139
require.NoError(t, err)
139140

140141
// wait for fleet-server to report as healthy
@@ -160,7 +161,7 @@ func (s *agentSuite) TestAgentMode(t *testing.T) {
160161
}, ftesting.RetrySleep(100*time.Millisecond), ftesting.RetryCount(120))
161162

162163
// reconfigure to good config
163-
err = appState.UpdateConfig(agentIdCfgData)
164+
err = appState.UpdateConfig(agentIDCfgData)
164165
require.NoError(t, err)
165166

166167
// wait for fleet-server to report as healthy

cmd/fleet/server_integration_test.go

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type tserver struct {
4848
srv *FleetServer
4949
}
5050

51-
func (s *tserver) baseUrl() string {
51+
func (s *tserver) baseURL() string {
5252
input, _ := s.cfg.GetFleetInput()
5353
tls := input.Server.TLS
5454
schema := "http"
@@ -65,14 +65,14 @@ func (s *tserver) waitExit() error {
6565
func startTestServer(ctx context.Context) (*tserver, error) {
6666
cfg, err := config.LoadFile("../../fleet-server.yml")
6767
if err != nil {
68-
return nil, err
68+
return nil, fmt.Errorf("config load error: %w", err)
6969
}
7070

71-
logger.Init(cfg, "fleet-server")
71+
logger.Init(cfg, "fleet-server") //nolint:errcheck // test logging setup
7272

7373
port, err := ftesting.FreePort()
7474
if err != nil {
75-
return nil, err
75+
return nil, fmt.Errorf("unable to find port: %w", err)
7676
}
7777

7878
srvcfg := &config.Server{}
@@ -84,7 +84,7 @@ func startTestServer(ctx context.Context) (*tserver, error) {
8484

8585
srv, err := NewFleetServer(cfg, build.Info{Version: serverVersion}, status.NewLog())
8686
if err != nil {
87-
return nil, err
87+
return nil, fmt.Errorf("unable to create server: %w", err)
8888
}
8989

9090
g, ctx := errgroup.WithContext(ctx)
@@ -96,7 +96,7 @@ func startTestServer(ctx context.Context) (*tserver, error) {
9696
tsrv := &tserver{cfg: cfg, g: g, srv: srv}
9797
err = tsrv.waitServerUp(ctx, testWaitServerUp)
9898
if err != nil {
99-
return nil, err
99+
return nil, fmt.Errorf("unable to start server: %w", err)
100100
}
101101
return tsrv, nil
102102
}
@@ -105,7 +105,7 @@ func (s *tserver) waitServerUp(ctx context.Context, dur time.Duration) error {
105105
start := time.Now()
106106
cli := cleanhttp.DefaultClient()
107107
for {
108-
res, err := cli.Get(s.baseUrl() + "/api/status")
108+
res, err := cli.Get(s.baseURL() + "/api/status") //nolint:noctx // test setup
109109
if err != nil {
110110
if time.Since(start) > dur {
111111
return err
@@ -123,7 +123,7 @@ func (s *tserver) waitServerUp(ctx context.Context, dur time.Duration) error {
123123

124124
}
125125

126-
func (s *tserver) buildUrl(id string, cmd string) string {
126+
func (s *tserver) buildURL(id string, cmd string) string {
127127
ur := "/api/fleet/agents"
128128
if id != "" {
129129
ur = path.Join(ur, id)
@@ -132,7 +132,7 @@ func (s *tserver) buildUrl(id string, cmd string) string {
132132
ur = path.Join(ur, cmd)
133133
}
134134

135-
return s.baseUrl() + ur
135+
return s.baseURL() + ur
136136
}
137137

138138
func TestServerUnauthorized(t *testing.T) {
@@ -143,16 +143,16 @@ func TestServerUnauthorized(t *testing.T) {
143143
srv, err := startTestServer(ctx)
144144
require.NoError(t, err)
145145

146-
agentId := uuid.Must(uuid.NewV4()).String()
146+
agentID := uuid.Must(uuid.NewV4()).String()
147147
cli := cleanhttp.DefaultClient()
148148

149149
agenturls := []string{
150-
srv.buildUrl(agentId, "checkin"),
151-
srv.buildUrl(agentId, "acks"),
150+
srv.buildURL(agentID, "checkin"),
151+
srv.buildURL(agentID, "acks"),
152152
}
153153

154154
allurls := []string{
155-
srv.buildUrl("", "enroll"),
155+
srv.buildURL("", "enroll"),
156156
}
157157
allurls = append(allurls, agenturls...)
158158

@@ -161,7 +161,7 @@ func TestServerUnauthorized(t *testing.T) {
161161
// TODO: revisit error response format
162162
t.Run("no auth header", func(t *testing.T) {
163163
for _, u := range allurls {
164-
res, err := cli.Post(u, "application/json", bytes.NewBuffer([]byte("{}")))
164+
res, err := cli.Post(u, "application/json", bytes.NewBuffer([]byte("{}"))) //nolint:noctx // test case
165165
if err != nil {
166166
t.Fatal(err)
167167
}
@@ -190,9 +190,8 @@ func TestServerUnauthorized(t *testing.T) {
190190

191191
// Unauthorized, expecting error from /_security/_authenticate
192192
t.Run("unauthorized", func(t *testing.T) {
193-
194193
for _, u := range agenturls {
195-
req, err := http.NewRequest("POST", u, bytes.NewBuffer([]byte("{}")))
194+
req, err := http.NewRequest("POST", u, bytes.NewBuffer([]byte("{}"))) //nolint:noctx // test case
196195
require.NoError(t, err)
197196
req.Header.Set("Content-Type", "application/json")
198197
req.Header.Set("Authorization", "ApiKey ZExqY1hYWUJJUVVxWDVia2JvVGM6M05XaUt5aHBRYk9YSTRQWDg4YWp0UQ==")
@@ -225,7 +224,7 @@ func TestServerUnauthorized(t *testing.T) {
225224

226225
// Stop test server
227226
cancel()
228-
srv.waitExit()
227+
srv.waitExit() //nolint:errcheck // test case
229228
}
230229

231230
func TestServerInstrumentation(t *testing.T) {
@@ -239,7 +238,7 @@ func TestServerInstrumentation(t *testing.T) {
239238
return
240239
}
241240
tracerConnected <- struct{}{}
242-
io.Copy(io.Discard, req.Body)
241+
io.Copy(io.Discard, req.Body) //nolint:errcheck // test case
243242
tracerDisconnected <- struct{}{}
244243
}))
245244
defer server.Close()
@@ -248,8 +247,7 @@ func TestServerInstrumentation(t *testing.T) {
248247
srv, err := startTestServer(ctx)
249248
require.NoError(t, err)
250249

251-
newInstrumentationCfg := func(cfg config.Config, instr config.Instrumentation) {
252-
cfg.Inputs[0] = cfg.Inputs[0]
250+
newInstrumentationCfg := func(cfg config.Config, instr config.Instrumentation) { //nolint:govet // mutex should not be copied in operation (hopefully)
253251
cfg.Inputs[0].Server.Instrumentation = instr
254252

255253
newCfg, err := srv.cfg.Merge(&cfg)
@@ -259,18 +257,23 @@ func TestServerInstrumentation(t *testing.T) {
259257
}
260258

261259
// Enable instrumentation
262-
newInstrumentationCfg(*srv.cfg, config.Instrumentation{
260+
newInstrumentationCfg(*srv.cfg, config.Instrumentation{ //nolint:govet // mutex should not be copied in operation (hopefully)
263261
Enabled: true,
264262
Hosts: []string{server.URL},
265263
})
266264

267265
stopClient := make(chan struct{})
268266
cli := cleanhttp.DefaultClient()
269267
callCheckinFunc := func() {
268+
var Err error
269+
defer require.NoError(t, Err)
270270
for {
271-
agentId := "1e4954ce-af37-4731-9f4a-407b08e69e42"
272-
cli.Post(srv.buildUrl(agentId, "checkin"), "application/json", bytes.NewBuffer([]byte("{}")))
273-
require.NoError(t, err)
271+
agentID := "1e4954ce-af37-4731-9f4a-407b08e69e42"
272+
res, err := cli.Post(srv.buildURL(agentID, "checkin"), "application/json", bytes.NewBuffer([]byte("{}"))) //nolint:noctx,staticcheck // test case
273+
if res != nil && res.Body != nil {
274+
res.Body.Close()
275+
}
276+
Err = err //nolint:ineffassign,staticcheck,wastedassign // ugly work around for error checking
274277
select {
275278
case <-ctx.Done():
276279
return
@@ -292,7 +295,7 @@ func TestServerInstrumentation(t *testing.T) {
292295
}
293296

294297
// Turn instrumentation off
295-
newInstrumentationCfg(*srv.cfg, config.Instrumentation{
298+
newInstrumentationCfg(*srv.cfg, config.Instrumentation{ //nolint:govet // mutex should not be copied in operation (hopefully)
296299
Enabled: false,
297300
Hosts: []string{server.URL},
298301
})

go.mod

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ require (
1919
github.com/miolini/datacounter v1.0.2
2020
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58
2121
github.com/pkg/errors v0.9.1
22-
github.com/rs/xid v1.2.1
23-
github.com/rs/zerolog v1.22.0
22+
github.com/rs/xid v1.3.0
23+
github.com/rs/zerolog v1.26.1
2424
github.com/spf13/cobra v1.3.0
2525
github.com/stretchr/testify v1.7.0
2626
go.elastic.co/apm v1.15.0
@@ -56,6 +56,7 @@ require (
5656
github.com/pmezard/go-difflib v1.0.0 // indirect
5757
github.com/santhosh-tekuri/jsonschema v1.2.4 // indirect
5858
github.com/spf13/pflag v1.0.5 // indirect
59+
github.com/stretchr/objx v0.2.0 // indirect
5960
go.elastic.co/apm/module/apmgrpc v1.15.0 // indirect
6061
go.elastic.co/apm/module/apmhttp v1.15.0 // indirect
6162
go.elastic.co/fastjson v1.1.0 // indirect
@@ -82,7 +83,7 @@ require (
8283
go.elastic.co/ecszap v1.0.0 // indirect
8384
go.uber.org/atomic v1.9.0 // indirect
8485
go.uber.org/multierr v1.7.0 // indirect
85-
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e // indirect
86+
golang.org/x/crypto v0.0.0-20211215165025-cf75a172585e // indirect
8687
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f // indirect
8788
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
8889
golang.org/x/tools v0.1.8 // indirect

go.sum

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,10 +1762,12 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
17621762
github.com/rogpeppe/go-internal v1.5.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
17631763
github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
17641764
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
1765-
github.com/rs/xid v1.2.1 h1:mhH9Nq+C1fY2l1XIpgxIiUOfNpRBYH1kKcr+qfKgjRc=
17661765
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
1767-
github.com/rs/zerolog v1.22.0 h1:XrVUjV4K+izZpKXZHlPrYQiDtmdGiCylnT4i43AAWxg=
1766+
github.com/rs/xid v1.3.0 h1:6NjYksEUlhurdVehpc7S7dk6DAmcKv8V9gG0FsVN2U4=
1767+
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
17681768
github.com/rs/zerolog v1.22.0/go.mod h1:ZPhntP/xmq1nnND05hhpAh2QMhSsA4UN3MGZ6O2J3hM=
1769+
github.com/rs/zerolog v1.26.1 h1:/ihwxqH+4z8UxyI70wM1z9yCvkWcfz/a3mj48k/Zngc=
1770+
github.com/rs/zerolog v1.26.1/go.mod h1:/wSSJWX7lVrsOwlbyTRSOJvqRlc+WjWlfes+CiJ+tmc=
17691771
github.com/rubenv/sql-migrate v0.0.0-20210614095031-55d5740dbbcc/go.mod h1:HFLT6i9iR4QBOF5rdCyjddC9t59ArqWJV2xx+jwcCMo=
17701772
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
17711773
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
@@ -2163,8 +2165,8 @@ golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm
21632165
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
21642166
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
21652167
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
2166-
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e h1:MUP6MR3rJ7Gk9LEia0LP2ytiH6MuCfs7qYz+47jGdD8=
2167-
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
2168+
golang.org/x/crypto v0.0.0-20211215165025-cf75a172585e h1:1SzTfNOXwIS2oWiMF+6qu0OUDKb0dauo6MoDUQyu+yU=
2169+
golang.org/x/crypto v0.0.0-20211215165025-cf75a172585e/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
21682170
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
21692171
golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
21702172
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
@@ -2290,7 +2292,6 @@ golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d/go.mod h1:9nx3DQGgdP8bBQD5qx
22902292
golang.org/x/net v0.0.0-20210825183410-e898025ed96a/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
22912293
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
22922294
golang.org/x/net v0.0.0-20211020060615-d418f374d309/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
2293-
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
22942295
golang.org/x/net v0.0.0-20211209124913-491a49abca63/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
22952296
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f h1:hEYJvxw1lSnWIl8X9ofsYMklzaDs90JI2az5YMd4fPM=
22962297
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=

0 commit comments

Comments
 (0)