Skip to content

Commit fe5e700

Browse files
committed
chore: enable noctx linter
Signed-off-by: Matthieu MOREL <[email protected]>
1 parent 7eb93bb commit fe5e700

Some content is hidden

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

45 files changed

+410
-202
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ linters:
1414
- gocritic
1515
- misspell
1616
- nakedret
17+
- noctx
1718
- nolintlint
1819
- perfsprint
1920
- revive

docker_auth_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,8 @@ func localAddress(t *testing.T) string {
339339
return "localhost"
340340
}
341341

342-
conn, err := net.Dial("udp", "golang.org:80")
342+
d := &net.Dialer{}
343+
conn, err := d.DialContext(t.Context(), "udp", "golang.org:80")
343344
require.NoError(t, err)
344345
defer conn.Close()
345346

docker_test.go

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ func TestContainerWithHostNetworkOptions(t *testing.T) {
8181
endpoint, err := nginxC.PortEndpoint(ctx, nginxHighPort, "http")
8282
require.NoErrorf(t, err, "Expected server endpoint")
8383

84-
_, err = http.Get(endpoint)
84+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, http.NoBody)
85+
require.NoError(t, err)
86+
87+
_, err = http.DefaultClient.Do(req)
8588
require.NoErrorf(t, err, "Expected OK response")
8689
}
8790

@@ -103,10 +106,13 @@ func TestContainerWithHostNetworkOptions_UseExposePortsFromImageConfigs(t *testi
103106
endpoint, err := nginxC.Endpoint(ctx, "http")
104107
require.NoErrorf(t, err, "Expected server endpoint")
105108

106-
resp, err := http.Get(endpoint)
109+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, http.NoBody)
110+
require.NoError(t, err)
111+
112+
resp, err := http.DefaultClient.Do(req)
107113
require.NoError(t, err)
108-
defer resp.Body.Close()
109114

115+
defer resp.Body.Close()
110116
require.Equalf(t, http.StatusOK, resp.StatusCode, "Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
111117
}
112118

@@ -140,13 +146,19 @@ func TestContainerWithHostNetwork(t *testing.T) {
140146
require.NoErrorf(t, err, "Expected port endpoint %s", portEndpoint)
141147
t.Log(portEndpoint)
142148

143-
_, err = http.Get(portEndpoint)
149+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, portEndpoint, http.NoBody)
150+
require.NoError(t, err)
151+
152+
_, err = http.DefaultClient.Do(req)
144153
require.NoErrorf(t, err, "Expected OK response")
145154

146155
host, err := nginxC.Host(ctx)
147156
require.NoErrorf(t, err, "Expected host %s", host)
148157

149-
_, err = http.Get("http://" + host + ":8080")
158+
req, err = http.NewRequestWithContext(ctx, http.MethodGet, "http://"+host+":8080", http.NoBody)
159+
require.NoError(t, err)
160+
161+
_, err = http.DefaultClient.Do(req)
150162
require.NoErrorf(t, err, "Expected OK response")
151163
}
152164

@@ -318,19 +330,25 @@ func TestTwoContainersExposingTheSamePort(t *testing.T) {
318330
endpointA, err := nginxA.PortEndpoint(ctx, nginxDefaultPort, "http")
319331
require.NoError(t, err)
320332

321-
resp, err := http.Get(endpointA)
333+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpointA, http.NoBody)
322334
require.NoError(t, err)
323-
defer resp.Body.Close()
324335

336+
resp, err := http.DefaultClient.Do(req)
337+
require.NoError(t, err)
338+
339+
defer resp.Body.Close()
325340
require.Equalf(t, http.StatusOK, resp.StatusCode, "Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
326341

327342
endpointB, err := nginxB.PortEndpoint(ctx, nginxDefaultPort, "http")
328343
require.NoError(t, err)
329344

330-
resp, err = http.Get(endpointB)
345+
req, err = http.NewRequestWithContext(ctx, http.MethodGet, endpointB, http.NoBody)
346+
require.NoError(t, err)
347+
348+
resp, err = http.DefaultClient.Do(req)
331349
require.NoError(t, err)
332-
defer resp.Body.Close()
333350

351+
defer resp.Body.Close()
334352
require.Equalf(t, http.StatusOK, resp.StatusCode, "Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
335353
}
336354

@@ -347,7 +365,10 @@ func TestContainerCreation(t *testing.T) {
347365
endpoint, err := nginxC.PortEndpoint(ctx, nginxDefaultPort, "http")
348366
require.NoError(t, err)
349367

350-
resp, err := http.Get(endpoint)
368+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, http.NoBody)
369+
require.NoError(t, err)
370+
371+
resp, err := http.DefaultClient.Do(req)
351372
require.NoError(t, err)
352373
defer resp.Body.Close()
353374

@@ -408,7 +429,10 @@ func TestContainerCreationWithName(t *testing.T) {
408429
endpoint, err := nginxC.PortEndpoint(ctx, nginxDefaultPort, "http")
409430
require.NoError(t, err)
410431

411-
resp, err := http.Get(endpoint)
432+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, http.NoBody)
433+
require.NoError(t, err)
434+
435+
resp, err := http.DefaultClient.Do(req)
412436
require.NoError(t, err)
413437
defer resp.Body.Close()
414438

@@ -428,10 +452,14 @@ func TestContainerCreationAndWaitForListeningPortLongEnough(t *testing.T) {
428452

429453
origin, err := nginxC.PortEndpoint(ctx, nginxDefaultPort, "http")
430454
require.NoError(t, err)
431-
resp, err := http.Get(origin)
455+
456+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, origin, http.NoBody)
457+
require.NoError(t, err)
458+
459+
resp, err := http.DefaultClient.Do(req)
432460
require.NoError(t, err)
433-
defer resp.Body.Close()
434461

462+
defer resp.Body.Close()
435463
require.Equalf(t, http.StatusOK, resp.StatusCode, "Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
436464
}
437465

@@ -459,10 +487,14 @@ func TestContainerRespondsWithHttp200ForIndex(t *testing.T) {
459487

460488
origin, err := nginxC.PortEndpoint(ctx, nginxDefaultPort, "http")
461489
require.NoError(t, err)
462-
resp, err := http.Get(origin)
490+
491+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, origin, http.NoBody)
492+
require.NoError(t, err)
493+
494+
resp, err := http.DefaultClient.Do(req)
463495
require.NoError(t, err)
464-
defer resp.Body.Close()
465496

497+
defer resp.Body.Close()
466498
require.Equalf(t, http.StatusOK, resp.StatusCode, "Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
467499
}
468500

@@ -529,8 +561,12 @@ func Test_BuildContainerFromDockerfileWithBuildArgs(t *testing.T) {
529561
ep, err := c.Endpoint(ctx, "http")
530562
require.NoError(t, err)
531563

532-
resp, err := http.Get(ep + "/env")
564+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ep+"/env", http.NoBody)
565+
require.NoError(t, err)
566+
567+
resp, err := http.DefaultClient.Do(req)
533568
require.NoError(t, err)
569+
534570
defer resp.Body.Close()
535571

536572
body, err := io.ReadAll(resp.Body)
@@ -844,7 +880,8 @@ func ExampleContainer_MappedPort() {
844880
// buildingAddresses {
845881
ip, _ := nginxC.Host(ctx)
846882
port, _ := nginxC.MappedPort(ctx, "80")
847-
_, _ = http.Get(fmt.Sprintf("http://%s:%s", ip, port.Port()))
883+
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("http://%s:%s", ip, port.Port()), http.NoBody)
884+
_, _ = http.DefaultClient.Do(req)
848885
// }
849886

850887
state, err := nginxC.State(ctx)

docs/features/creating_container.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,13 @@ func TestIntegrationNginxLatestReturn(t *testing.T) {
126126
testcontainers.CleanupContainer(t, nginxC)
127127
require.NoError(t, err)
128128
129-
resp, err := http.Get(nginxC.URI)
129+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, nginxC.URI, http.NoBody)
130+
require.NoError(t, err)
131+
132+
resp, err := http.DefaultClient.Do(req)
133+
require.NoError(t, err)
134+
135+
defer resp.Body.Close()
130136
require.Equal(t, http.StatusOK, resp.StatusCode)
131137
}
132138
```

examples/nginx/nginx_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ func TestIntegrationNginxLatestReturn(t *testing.T) {
2121
testcontainers.CleanupContainer(t, nginxC)
2222
require.NoError(t, err)
2323

24-
resp, err := http.Get(nginxC.URI)
24+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, nginxC.URI, http.NoBody)
2525
require.NoError(t, err)
26+
27+
resp, err := http.DefaultClient.Do(req)
28+
require.NoError(t, err)
29+
30+
defer resp.Body.Close()
2631
require.Equal(t, http.StatusOK, resp.StatusCode)
2732
}

generic_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func TestGenericReusableContainerInSubprocess(t *testing.T) {
152152
func createReuseContainerInSubprocess(t *testing.T) string {
153153
t.Helper()
154154
// force verbosity in subprocesses, so that the output is printed
155-
cmd := exec.Command(os.Args[0], "-test.run=TestHelperContainerStarterProcess", "-test.v=true")
155+
cmd := exec.CommandContext(t.Context(), os.Args[0], "-test.run=TestHelperContainerStarterProcess", "-test.v=true")
156156
cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1")
157157

158158
output, err := cmd.CombinedOutput()

internal/core/docker_host.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os/exec"
99
"strings"
1010
"sync"
11+
"time"
1112

1213
"github.com/docker/docker/client"
1314

@@ -43,8 +44,10 @@ var (
4344
// deprecated
4445
// see https://github.com/testcontainers/testcontainers-java/blob/main/core/src/main/java/org/testcontainers/dockerclient/DockerClientConfigUtils.java#L46
4546
func DefaultGatewayIP() (string, error) {
47+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
48+
defer cancel()
4649
// see https://github.com/testcontainers/testcontainers-java/blob/3ad8d80e2484864e554744a4800a81f6b7982168/core/src/main/java/org/testcontainers/dockerclient/DockerClientConfigUtils.java#L27
47-
cmd := exec.Command("sh", "-c", "ip route|awk '/default/ { print $3 }'")
50+
cmd := exec.CommandContext(ctx, "sh", "-c", "ip route|awk '/default/ { print $3 }'")
4851
stdout, err := cmd.Output()
4952
if err != nil {
5053
return "", errors.New("failed to detect docker host")

logconsumer_test.go

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,25 @@ func Test_LogConsumerGetsCalled(t *testing.T) {
9595
ep, err := c.Endpoint(ctx, "http")
9696
require.NoError(t, err)
9797

98-
_, err = http.Get(ep + "/stdout?echo=hello")
98+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ep+"/stdout?echo=hello", http.NoBody)
9999
require.NoError(t, err)
100100

101-
_, err = http.Get(ep + "/stdout?echo=there")
101+
_, err = http.DefaultClient.Do(req)
102102
require.NoError(t, err)
103103

104-
_, err = http.Get(ep + "/stdout?echo=" + lastMessage)
104+
req, err = http.NewRequestWithContext(ctx, http.MethodGet, ep+"/stdout?echo=there", http.NoBody)
105105
require.NoError(t, err)
106106

107+
_, err = http.DefaultClient.Do(req)
108+
require.NoError(t, err)
109+
110+
req, err = http.NewRequestWithContext(ctx, http.MethodGet, ep+"/stdout?echo="+lastMessage, http.NoBody)
111+
require.NoError(t, err)
112+
113+
resp, err := http.DefaultClient.Do(req)
114+
require.NoError(t, err)
115+
require.NoError(t, resp.Body.Close())
116+
107117
select {
108118
case <-g.Done:
109119
case <-time.After(5 * time.Second):
@@ -154,13 +164,22 @@ func Test_ShouldRecognizeLogTypes(t *testing.T) {
154164
ep, err := c.Endpoint(ctx, "http")
155165
require.NoError(t, err)
156166

157-
_, err = http.Get(ep + "/stdout?echo=this-is-stdout")
167+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ep+"/stdout?echo=this-is-stdout", http.NoBody)
168+
require.NoError(t, err)
169+
170+
_, err = http.DefaultClient.Do(req)
158171
require.NoError(t, err)
159172

160-
_, err = http.Get(ep + "/stderr?echo=this-is-stderr")
173+
req, err = http.NewRequestWithContext(ctx, http.MethodGet, ep+"/stderr?echo=this-is-stderr", http.NoBody)
161174
require.NoError(t, err)
162175

163-
_, err = http.Get(ep + "/stdout?echo=" + lastMessage)
176+
_, err = http.DefaultClient.Do(req)
177+
require.NoError(t, err)
178+
179+
req, err = http.NewRequestWithContext(ctx, http.MethodGet, ep+"/stdout?echo="+lastMessage, http.NoBody)
180+
require.NoError(t, err)
181+
182+
_, err = http.DefaultClient.Do(req)
164183
require.NoError(t, err)
165184

166185
<-g.Ack
@@ -204,10 +223,16 @@ func Test_MultipleLogConsumers(t *testing.T) {
204223
ep, err := c.Endpoint(ctx, "http")
205224
require.NoError(t, err)
206225

207-
_, err = http.Get(ep + "/stdout?echo=mlem")
226+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ep+"/stdout?echo=mlem", http.NoBody)
227+
require.NoError(t, err)
228+
229+
_, err = http.DefaultClient.Do(req)
230+
require.NoError(t, err)
231+
232+
req, err = http.NewRequestWithContext(ctx, http.MethodGet, ep+"/stdout?echo="+lastMessage, http.NoBody)
208233
require.NoError(t, err)
209234

210-
_, err = http.Get(ep + "/stdout?echo=" + lastMessage)
235+
_, err = http.DefaultClient.Do(req)
211236
require.NoError(t, err)
212237

213238
<-first.Done
@@ -412,13 +437,22 @@ func TestContainerLogsEnableAtStart(t *testing.T) {
412437
ep, err := c.Endpoint(ctx, "http")
413438
require.NoError(t, err)
414439

415-
_, err = http.Get(ep + "/stdout?echo=hello")
440+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ep+"/stdout?echo=hello", http.NoBody)
441+
require.NoError(t, err)
442+
443+
_, err = http.DefaultClient.Do(req)
444+
require.NoError(t, err)
445+
446+
req, err = http.NewRequestWithContext(ctx, http.MethodGet, ep+"/stdout?echo=there", http.NoBody)
447+
require.NoError(t, err)
448+
449+
_, err = http.DefaultClient.Do(req)
416450
require.NoError(t, err)
417451

418-
_, err = http.Get(ep + "/stdout?echo=there")
452+
req, err = http.NewRequestWithContext(ctx, http.MethodGet, ep+"/stdout?echo="+lastMessage, http.NoBody)
419453
require.NoError(t, err)
420454

421-
_, err = http.Get(ep + "/stdout?echo=" + lastMessage)
455+
_, err = http.DefaultClient.Do(req)
422456
require.NoError(t, err)
423457

424458
select {
@@ -554,10 +588,16 @@ func Test_MultiContainerLogConsumer_CancelledContext(t *testing.T) {
554588
ep1, err := c.Endpoint(ctx, "http")
555589
require.NoError(t, err)
556590

557-
_, err = http.Get(ep1 + "/stdout?echo=hello1")
591+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ep1+"/stdout?echo=hello1", http.NoBody)
558592
require.NoError(t, err)
559593

560-
_, err = http.Get(ep1 + "/stdout?echo=there1")
594+
_, err = http.DefaultClient.Do(req)
595+
require.NoError(t, err)
596+
597+
req, err = http.NewRequestWithContext(ctx, http.MethodGet, ep1+"/stdout?echo=there1", http.NoBody)
598+
require.NoError(t, err)
599+
600+
_, err = http.DefaultClient.Do(req)
561601
require.NoError(t, err)
562602

563603
second := TestLogConsumer{
@@ -585,10 +625,16 @@ func Test_MultiContainerLogConsumer_CancelledContext(t *testing.T) {
585625
ep2, err := c2.Endpoint(ctx, "http")
586626
require.NoError(t, err)
587627

588-
_, err = http.Get(ep2 + "/stdout?echo=hello2")
628+
req, err = http.NewRequestWithContext(ctx, http.MethodGet, ep2+"/stdout?echo=hello2", http.NoBody)
629+
require.NoError(t, err)
630+
631+
_, err = http.DefaultClient.Do(req)
632+
require.NoError(t, err)
633+
634+
req, err = http.NewRequestWithContext(ctx, http.MethodGet, ep2+"/stdout?echo=there2", http.NoBody)
589635
require.NoError(t, err)
590636

591-
_, err = http.Get(ep2 + "/stdout?echo=there2")
637+
_, err = http.DefaultClient.Do(req)
592638
require.NoError(t, err)
593639

594640
// Deliberately calling context cancel

0 commit comments

Comments
 (0)