Skip to content

Commit 4c2f96b

Browse files
Make Go linter happy (#47106) (#47207)
* Use calls with context * Replace io/ioutil uses * Mark type conversion as safe * Remove unnecessary loops * Added missing import * Pass parent context * Simplify logic * Bumping up Node.js command timeout to 30s * Use calls with context * Pass parent context (cherry picked from commit d319703) Co-authored-by: Shaunak Kashyap <[email protected]>
1 parent f6effae commit 4c2f96b

File tree

14 files changed

+36
-37
lines changed

14 files changed

+36
-37
lines changed

dev-tools/mage/gotest.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,18 @@ func DefaultGoWindowsTestIntegrationArgs() GoTestArgs {
164164

165165
// DefaultGoTestIntegrationArgs returns a default set of arguments for running
166166
// all integration tests. We tag integration test files with 'integration'.
167-
func DefaultGoTestIntegrationArgs() GoTestArgs {
167+
func DefaultGoTestIntegrationArgs(ctx context.Context) GoTestArgs {
168168
args := makeGoTestArgs("Integration")
169169
args.Tags = append(args.Tags, "integration")
170170

171-
synth := exec.Command("npx", "@elastic/synthetics", "-h")
171+
cmdCtx, cmdCancel := context.WithTimeout(ctx, 5*time.Second)
172+
defer cmdCancel()
173+
174+
synth := exec.CommandContext(cmdCtx, "npx", "@elastic/synthetics", "-h")
172175
if synth.Run() == nil {
173176
// Run an empty journey to ensure playwright can be loaded
174177
// catches situations like missing playwright deps
175-
cmd := exec.Command("sh", "-c", "echo 'step(\"t\", () => { })' | elastic-synthetics --inline")
178+
cmd := exec.CommandContext(cmdCtx, "sh", "-c", "echo 'step(\"t\", () => { })' | elastic-synthetics --inline")
176179
var out strings.Builder
177180
cmd.Stdout = &out
178181
cmd.Stderr = &out
@@ -195,17 +198,17 @@ func DefaultGoTestIntegrationArgs() GoTestArgs {
195198

196199
// DefaultGoTestIntegrationFromHostArgs returns a default set of arguments for running
197200
// all integration tests from the host system (outside the docker network).
198-
func DefaultGoTestIntegrationFromHostArgs() GoTestArgs {
199-
args := DefaultGoTestIntegrationArgs()
201+
func DefaultGoTestIntegrationFromHostArgs(ctx context.Context) GoTestArgs {
202+
args := DefaultGoTestIntegrationArgs(ctx)
200203
args.Env = WithGoIntegTestHostEnv(args.Env)
201204
return args
202205
}
203206

204207
// FIPSOnlyGoTestIngrationFromHostArgs returns a default set of arguments for running
205208
// all integration tests from the host system (outside the docker network) along
206209
// with the GODEBUG=fips140=only arg set.
207-
func FIPSOnlyGoTestIntegrationFromHostArgs() GoTestArgs {
208-
args := DefaultGoTestIntegrationArgs()
210+
func FIPSOnlyGoTestIntegrationFromHostArgs(ctx context.Context) GoTestArgs {
211+
args := DefaultGoTestIntegrationArgs(ctx)
209212
args.Tags = append(args.Tags, "requirefips")
210213
args.Env = WithGoIntegTestHostEnv(args.Env)
211214
args.Env["GODEBUG"] = "fips140=only"

dev-tools/mage/target/integtest/integtest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func GoIntegTest(ctx context.Context) error {
7070
return err
7171
}
7272
return runner.Test("goIntegTest", func() error {
73-
return devtools.GoTest(ctx, devtools.DefaultGoTestIntegrationArgs())
73+
return devtools.GoTest(ctx, devtools.DefaultGoTestIntegrationArgs(ctx))
7474
})
7575
}
7676

filebeat/magefile.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,13 @@ func IntegTest() {
196196
// GoIntegTest starts the docker containers and executes the Go integration tests.
197197
func GoIntegTest(ctx context.Context) error {
198198
mg.Deps(BuildSystemTestBinary)
199-
return devtools.GoIntegTestFromHost(ctx, devtools.DefaultGoTestIntegrationFromHostArgs())
199+
return devtools.GoIntegTestFromHost(ctx, devtools.DefaultGoTestIntegrationFromHostArgs(ctx))
200200
}
201201

202202
// GoFIPSOnlyIntegTest starts the docker containers and executes the Go integration tests with GODEBUG=fips140=only set.
203203
func GoFIPSOnlyIntegTest(ctx context.Context) error {
204204
mg.Deps(BuildSystemTestBinary)
205-
return devtools.GoIntegTestFromHost(ctx, devtools.FIPSOnlyGoTestIntegrationFromHostArgs())
205+
return devtools.GoIntegTestFromHost(ctx, devtools.FIPSOnlyGoTestIntegrationFromHostArgs(ctx))
206206
}
207207

208208
// PythonIntegTest starts the docker containers and executes the Python integration tests.

heartbeat/hbtest/hbtestutil.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"crypto/x509"
2323
"fmt"
2424
"io"
25-
"io/ioutil"
2625
"math/bits"
2726
"net"
2827
"net/http"
@@ -128,7 +127,7 @@ func ServerPort(server *httptest.Server) (uint16, error) {
128127
if err != nil {
129128
return 0, err
130129
}
131-
return uint16(p), nil
130+
return uint16(p), nil //nolint:gosec // G115 Conversion from int to unit16 is safe here.
132131
}
133132

134133
// TLSChecks validates the given x509 cert at the given position.
@@ -277,7 +276,7 @@ func RespondingTCPChecks() validator.Validator {
277276
func CertToTempFile(t *testing.T, cert *x509.Certificate) *os.File {
278277
// Write the certificate to a tempFile. Heartbeat would normally read certs from
279278
// disk, not memory, so this little bit of extra work is worthwhile
280-
certFile, err := ioutil.TempFile("", "sslcert")
279+
certFile, err := os.CreateTemp("", "sslcert")
281280
require.NoError(t, err)
282281
_, _ = certFile.WriteString(x509util.CertToPEMString(cert))
283282
return certFile

heartbeat/magefile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func IntegTest() {
9898

9999
func GoIntegTest(ctx context.Context) error {
100100
if runtime.GOOS != "windows" {
101-
return devtools.GoIntegTestFromHost(ctx, devtools.DefaultGoTestIntegrationFromHostArgs())
101+
return devtools.GoIntegTestFromHost(ctx, devtools.DefaultGoTestIntegrationFromHostArgs(ctx))
102102
}
103103
return nil
104104
}

heartbeat/monitors/active/http/http_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ package http
2020
import (
2121
"bytes"
2222
"compress/gzip"
23+
"context"
2324
"crypto/tls"
2425
"crypto/x509"
2526
"fmt"
2627
"io"
27-
"io/ioutil"
2828
"net"
2929
"net/http"
3030
"net/http/httptest"
@@ -579,7 +579,7 @@ func TestHTTPSx509Auth(t *testing.T) {
579579
certReader, err := file.ReadOpen(clientCertPath)
580580
require.NoError(t, err)
581581

582-
clientCertBytes, err := ioutil.ReadAll(certReader)
582+
clientCertBytes, err := io.ReadAll(certReader)
583583
require.NoError(t, err)
584584

585585
clientCerts := x509.NewCertPool()
@@ -762,7 +762,12 @@ func httpConnectTunnel(writer http.ResponseWriter, request *http.Request) {
762762
http.Error(writer, "Only CONNECT method is supported", http.StatusMethodNotAllowed)
763763
return
764764
}
765-
destConn, err := net.DialTimeout("tcp", request.Host, 10*time.Second)
765+
766+
dialCtx, dialCancel := context.WithTimeout(request.Context(), 10*time.Second)
767+
defer dialCancel()
768+
769+
dialer := &net.Dialer{}
770+
destConn, err := dialer.DialContext(dialCtx, "tcp", request.Host)
766771
if err != nil {
767772
http.Error(writer, err.Error(), http.StatusServiceUnavailable)
768773
return

libbeat/magefile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func IntegTest() {
7676
// GoIntegTest starts the docker containers and executes the Go integration tests.
7777
func GoIntegTest(ctx context.Context) error {
7878
mg.Deps(Fields, devtools.BuildSystemTestBinary)
79-
args := devtools.DefaultGoTestIntegrationFromHostArgs()
79+
args := devtools.DefaultGoTestIntegrationFromHostArgs(ctx)
8080
// ES_USER must be admin in order for the Go Integration tests to function because they require
8181
// indices:data/read/search
8282
args.Env["ES_USER"] = args.Env["ES_SUPERUSER_USER"]

libbeat/processors/add_kubernetes_metadata/indexers.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ func NewIndexers(configs PluginConfig, metaGen metadata.MetaGen) *Indexers {
9090
func (i *Indexers) GetIndexes(pod *kubernetes.Pod) []string {
9191
var indexes []string
9292
for _, indexer := range i.indexers {
93-
for _, i := range indexer.GetIndexes(pod) {
94-
indexes = append(indexes, i)
95-
}
93+
indexes = append(indexes, indexer.GetIndexes(pod)...)
9694
}
9795
return indexes
9896
}
@@ -101,20 +99,14 @@ func (i *Indexers) GetIndexes(pod *kubernetes.Pod) []string {
10199
func (i *Indexers) GetMetadata(pod *kubernetes.Pod) []MetadataIndex {
102100
var metadata []MetadataIndex
103101
for _, indexer := range i.indexers {
104-
for _, m := range indexer.GetMetadata(pod) {
105-
metadata = append(metadata, m)
106-
}
102+
metadata = append(metadata, indexer.GetMetadata(pod)...)
107103
}
108104
return metadata
109105
}
110106

111107
// Empty returns true if indexers list is empty
112108
func (i *Indexers) Empty() bool {
113-
if len(i.indexers) == 0 {
114-
return true
115-
}
116-
117-
return false
109+
return len(i.indexers) == 0
118110
}
119111

120112
// PodNameIndexer implements default indexer based on pod name

x-pack/agentbeat/magefile.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func IntegTest() {
215215
// GoIntegTest starts the docker containers and executes the Go integration tests.
216216
func GoIntegTest(ctx context.Context) error {
217217
mg.Deps(BuildSystemTestBinary)
218-
args := devtools.DefaultGoTestIntegrationFromHostArgs()
218+
args := devtools.DefaultGoTestIntegrationFromHostArgs(ctx)
219219
args.Tags = append(args.Tags, "agentbeat")
220220
for _, beat := range getIncludedBeats() {
221221
// matricbeat integration test TestIndexTotalFieldsLimitNotReached fails with
@@ -237,7 +237,7 @@ func SystemTest(ctx context.Context) error {
237237
if slices.Contains(getIncludedBeats(), "packetbeat") {
238238
mg.SerialDeps(xpacketbeat.GetNpcapInstallerFn("../packetbeat"), Update, devtools.BuildSystemTestBinary)
239239

240-
args := devtools.DefaultGoTestIntegrationArgs()
240+
args := devtools.DefaultGoTestIntegrationArgs(ctx)
241241
args.Packages = []string{"../packetbeat/tests/system/..."}
242242
args.Tags = append(args.Tags, "agentbeat")
243243

x-pack/filebeat/magefile.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func IntegTest() {
175175
func GoIntegTest(ctx context.Context) error {
176176
// build integration test binary with otel sub command
177177
devtools.BuildSystemTestOTelBinary()
178-
args := devtools.DefaultGoTestIntegrationFromHostArgs()
178+
args := devtools.DefaultGoTestIntegrationFromHostArgs(ctx)
179179
// ES_USER must be admin in order for the Go Integration tests to function because they require
180180
// indices:data/read/search
181181
args.Env["ES_USER"] = args.Env["ES_SUPERUSER_USER"]
@@ -187,7 +187,7 @@ func GoIntegTest(ctx context.Context) error {
187187
func GoFIPSOnlyIntegTest(ctx context.Context) error {
188188
// build integration test binary with otel sub command
189189
devtools.BuildSystemTestOTelBinary()
190-
args := devtools.DefaultGoTestIntegrationFromHostArgs()
190+
args := devtools.DefaultGoTestIntegrationFromHostArgs(ctx)
191191
// ES_USER must be admin in order for the Go Integration tests to function because they require
192192
// indices:data/read/search
193193
args.Env["ES_USER"] = args.Env["ES_SUPERUSER_USER"]

0 commit comments

Comments
 (0)