Skip to content

Commit 1440e56

Browse files
committed
♻️ Refactor linting
1 parent b9f3e10 commit 1440e56

File tree

6 files changed

+68
-55
lines changed

6 files changed

+68
-55
lines changed

.golangci.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
version: "2"
2+
run:
3+
allow-parallel-runners: true
4+
linters:
5+
default: standard
6+
enable:
7+
- bidichk
8+
- bodyclose
9+
- containedctx
10+
- durationcheck
11+
- errchkjson
12+
- errorlint
13+
- gocritic
14+
- godox
15+
- gosec
16+
- noctx
17+
exclusions:
18+
generated: lax
19+
presets:
20+
- comments
21+
- common-false-positives
22+
- legacy
23+
- std-error-handling
24+
paths:
25+
- third_party$
26+
- builtin$
27+
- examples$
28+
formatters:
29+
exclusions:
30+
generated: lax
31+
paths:
32+
- third_party$
33+
- builtin$
34+
- examples$

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,5 @@ require (
8787
gopkg.in/yaml.v3 v3.0.1 // indirect
8888
gotest.tools/v3 v3.0.3 // indirect
8989
)
90+
91+
tool github.com/maxbrunsfeld/counterfeiter/v6

golangci.yml

Lines changed: 0 additions & 17 deletions
This file was deleted.

localstack.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ import (
2121
_ "embed"
2222
"errors"
2323
"fmt"
24+
"io"
25+
"log"
26+
"sync"
27+
"time"
28+
2429
"github.com/Masterminds/semver/v3"
2530
"github.com/aws/aws-sdk-go-v2/aws"
2631
"github.com/aws/aws-sdk-go-v2/config"
@@ -32,10 +37,6 @@ import (
3237
"github.com/docker/docker/client"
3338
"github.com/docker/go-connections/nat"
3439
"github.com/sirupsen/logrus"
35-
"io"
36-
"log"
37-
"sync"
38-
"time"
3940

4041
"github.com/elgohr/go-localstack/internal"
4142
)
@@ -159,6 +160,7 @@ func (i *Instance) Start(services ...Service) error {
159160
}
160161

161162
// StartWithContext starts the localstack and ends it when the context is done.
163+
//
162164
// Deprecated: Use Start/Stop instead, as shutdown is not reliable
163165
func (i *Instance) StartWithContext(ctx context.Context, services ...Service) error {
164166
go func() {

localstack_test.go

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,22 @@ import (
1818
"bytes"
1919
"context"
2020
"fmt"
21-
"github.com/docker/docker/api/types/container"
22-
"github.com/docker/docker/api/types/filters"
23-
"github.com/docker/docker/client"
24-
log "github.com/sirupsen/logrus"
25-
"github.com/stretchr/testify/assert"
26-
"github.com/stretchr/testify/require"
2721
"net"
2822
"net/http"
23+
"net/http/httptest"
2924
"os"
3025
"strings"
3126
"sync"
3227
"testing"
3328
"time"
3429

30+
"github.com/docker/docker/api/types/container"
31+
"github.com/docker/docker/api/types/filters"
32+
"github.com/docker/docker/client"
33+
log "github.com/sirupsen/logrus"
34+
"github.com/stretchr/testify/assert"
35+
"github.com/stretchr/testify/require"
36+
3537
"github.com/elgohr/go-localstack"
3638
)
3739

@@ -81,8 +83,7 @@ func TestWithLogger(t *testing.T) {
8183
}
8284

8385
func TestWithTimeoutOnStartup(t *testing.T) {
84-
ctx, cancel := context.WithCancel(context.Background())
85-
defer cancel()
86+
ctx := t.Context()
8687
l, err := localstack.NewInstance(localstack.WithTimeout(time.Second))
8788
require.NoError(t, err)
8889
require.EqualError(t, l.StartWithContext(ctx), "localstack container has been stopped")
@@ -101,8 +102,7 @@ func TestWithTimeoutOnStartup(t *testing.T) {
101102
}
102103

103104
func TestWithTimeoutAfterStartup(t *testing.T) {
104-
ctx, cancel := context.WithCancel(context.Background())
105-
defer cancel()
105+
ctx := t.Context()
106106
l, err := localstack.NewInstance(localstack.WithTimeout(20 * time.Second))
107107
require.NoError(t, err)
108108

@@ -144,8 +144,7 @@ func TestWithLabels(t *testing.T) {
144144
l, err := localstack.NewInstance(localstack.WithLabels(s.labels))
145145
require.NoError(t, err)
146146

147-
ctx, cancel := context.WithCancel(context.Background())
148-
defer cancel()
147+
ctx := t.Context()
149148

150149
require.NoError(t, l.StartWithContext(ctx))
151150
t.Cleanup(func() { require.NoError(t, l.Stop()) })
@@ -239,8 +238,7 @@ func TestLocalStackWithContext(t *testing.T) {
239238
},
240239
} {
241240
t.Run(s.name, func(t *testing.T) {
242-
ctx, cancel := context.WithCancel(context.Background())
243-
defer cancel()
241+
ctx := t.Context()
244242
l, err := localstack.NewInstance(s.input...)
245243
require.NoError(t, err)
246244
require.NoError(t, l.StartWithContext(ctx))
@@ -251,14 +249,15 @@ func TestLocalStackWithContext(t *testing.T) {
251249

252250
func TestLocalStackWithIndividualServicesOnContext(t *testing.T) {
253251
cl := &http.Client{Timeout: time.Second}
252+
dialer := &net.Dialer{Timeout: time.Second}
254253
for service := range localstack.AvailableServices {
255254
t.Run(service.Name, func(t *testing.T) {
256255
ctx, cancel := context.WithCancel(context.Background())
257256
l, err := localstack.NewInstance()
258257
require.NoError(t, err)
259258
require.NoError(t, l.StartWithContext(ctx, service))
260259
for testService := range localstack.AvailableServices {
261-
conn, err := net.DialTimeout("tcp", strings.TrimPrefix(l.EndpointV2(testService), "http://"), time.Second)
260+
conn, err := dialer.DialContext(t.Context(), "tcp", strings.TrimPrefix(l.EndpointV2(testService), "http://"))
262261
if testService == service || testService == localstack.DynamoDB {
263262
require.NoError(t, err, testService)
264263
require.NoError(t, conn.Close())
@@ -268,7 +267,8 @@ func TestLocalStackWithIndividualServicesOnContext(t *testing.T) {
268267

269268
// wait until service was shutdown
270269
require.Eventually(t, func() bool {
271-
res, err := cl.Get(l.EndpointV2(service))
270+
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, l.EndpointV2(service), nil)
271+
res, err := cl.Do(req)
272272
defer func() {
273273
if res == nil || res.Body == nil {
274274
return
@@ -283,13 +283,14 @@ func TestLocalStackWithIndividualServicesOnContext(t *testing.T) {
283283

284284
func TestLocalStackWithIndividualServices(t *testing.T) {
285285
cl := &http.Client{Timeout: time.Second}
286+
dialer := &net.Dialer{Timeout: time.Second}
286287
for service := range localstack.AvailableServices {
287288
t.Run(service.Name, func(t *testing.T) {
288289
l, err := localstack.NewInstance()
289290
require.NoError(t, err)
290291
require.NoError(t, l.Start(service))
291292
for testService := range localstack.AvailableServices {
292-
conn, err := net.DialTimeout("tcp", strings.TrimPrefix(l.EndpointV2(testService), "http://"), time.Second)
293+
conn, err := dialer.DialContext(t.Context(), "tcp", strings.TrimPrefix(l.EndpointV2(testService), "http://"))
293294
if testService == service || testService == localstack.DynamoDB {
294295
require.NoError(t, err, testService)
295296
require.NoError(t, conn.Close())
@@ -299,7 +300,8 @@ func TestLocalStackWithIndividualServices(t *testing.T) {
299300

300301
// wait until service was shutdown
301302
require.Eventually(t, func() bool {
302-
res, err := cl.Get(l.EndpointV2(service))
303+
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, l.EndpointV2(service), nil)
304+
res, err := cl.Do(req)
303305
defer func() {
304306
if res == nil || res.Body == nil {
305307
return
@@ -313,6 +315,7 @@ func TestLocalStackWithIndividualServices(t *testing.T) {
313315
}
314316

315317
func TestInstanceStartedTwiceWithoutLeaking(t *testing.T) {
318+
dialer := &net.Dialer{Timeout: time.Second}
316319
l, err := localstack.NewInstance()
317320
require.NoError(t, err)
318321
t.Cleanup(func() {
@@ -321,19 +324,18 @@ func TestInstanceStartedTwiceWithoutLeaking(t *testing.T) {
321324
require.NoError(t, l.Start())
322325
firstInstance := l.Endpoint(localstack.S3)
323326
require.NoError(t, l.Start())
324-
_, err = net.Dial("tcp", firstInstance)
327+
_, err = dialer.DialContext(t.Context(), "tcp", firstInstance)
325328
require.Error(t, err, "should be teared down")
326329
}
327330

328331
func TestContextInstanceStartedTwiceWithoutLeaking(t *testing.T) {
329-
ctx, cancel := context.WithCancel(context.Background())
330-
defer cancel()
332+
dialer := &net.Dialer{Timeout: time.Second}
331333
l, err := localstack.NewInstance()
332334
require.NoError(t, err)
333335
require.NoError(t, l.Start())
334336
firstInstance := l.Endpoint(localstack.S3)
335-
require.NoError(t, l.StartWithContext(ctx))
336-
_, err = net.Dial("tcp", firstInstance)
337+
require.NoError(t, l.StartWithContext(t.Context()))
338+
_, err = dialer.DialContext(t.Context(), "tcp", firstInstance)
337339
require.Error(t, err, "should be teared down")
338340
}
339341

@@ -424,10 +426,7 @@ func TestWithClientFromEnv(t *testing.T) {
424426
if s.expectStart != nil {
425427
i, err := localstack.NewInstance(opt)
426428
require.NoError(t, err)
427-
428-
ctx, cancel := context.WithCancel(context.Background())
429-
defer cancel()
430-
s.expectStart(t, i.StartWithContext(ctx))
429+
s.expectStart(t, i.StartWithContext(t.Context()))
431430
}
432431
})
433432
}

tools.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)