@@ -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
8385func 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
103104func 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
252250func 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
284284func 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
315317func 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
328331func 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 }
0 commit comments