@@ -3,14 +3,15 @@ package testcontainers
3
3
import (
4
4
"context"
5
5
"errors"
6
- "net/http"
7
6
"os"
8
7
"os/exec"
9
8
"strings"
10
9
"sync"
11
10
"testing"
12
11
"time"
13
12
13
+ "github.com/docker/docker/api/types/container"
14
+ "github.com/docker/docker/api/types/filters"
14
15
"github.com/stretchr/testify/require"
15
16
16
17
"github.com/testcontainers/testcontainers-go/wait"
@@ -23,6 +24,8 @@ const (
23
24
func TestGenericReusableContainer (t * testing.T ) {
24
25
ctx := context .Background ()
25
26
27
+ reusableContainerName := reusableContainerName + "_" + time .Now ().Format ("20060102150405" )
28
+
26
29
n1 , err := GenericContainer (ctx , GenericContainerRequest {
27
30
ProviderType : providerType ,
28
31
ContainerRequest : ContainerRequest {
@@ -133,18 +136,37 @@ func TestGenericReusableContainerInSubprocess(t *testing.T) {
133
136
// create containers in subprocesses, as "go test ./..." does.
134
137
output := createReuseContainerInSubprocess (t )
135
138
139
+ t .Log (output )
136
140
// check is reuse container with WaitingFor work correctly.
137
141
require .True (t , strings .Contains (output , "⏳ Waiting for container id" ))
138
142
require .True (t , strings .Contains (output , "🔔 Container is ready" ))
139
143
}()
140
144
}
141
145
142
146
wg .Wait ()
147
+
148
+ cli , err := NewDockerClientWithOpts (context .Background ())
149
+ require .NoError (t , err )
150
+
151
+ f := filters .NewArgs (filters.KeyValuePair {Key : "name" , Value : reusableContainerName })
152
+
153
+ ctrs , err := cli .ContainerList (context .Background (), container.ListOptions {
154
+ All : true ,
155
+ Filters : f ,
156
+ })
157
+ require .NoError (t , err )
158
+ require .Len (t , ctrs , 1 )
159
+
160
+ nginxC , err := containerFromDockerResponse (context .Background (), ctrs [0 ])
161
+ require .NoError (t , err )
162
+
163
+ terminateContainerOnEnd (t , context .Background (), nginxC )
143
164
}
144
165
145
166
func createReuseContainerInSubprocess (t * testing.T ) string {
146
- cmd := exec .Command (os .Args [0 ], "-test.run=TestHelperContainerStarterProcess" )
147
- cmd .Env = []string {"GO_WANT_HELPER_PROCESS=1" }
167
+ // force verbosity in subprocesses, so that the output is printed
168
+ cmd := exec .Command (os .Args [0 ], "-test.run=TestHelperContainerStarterProcess" , "-test.v=true" )
169
+ cmd .Env = append (os .Environ (), "GO_WANT_HELPER_PROCESS=1" )
148
170
149
171
output , err := cmd .CombinedOutput ()
150
172
require .NoError (t , err , string (output ))
@@ -174,18 +196,4 @@ func TestHelperContainerStarterProcess(t *testing.T) {
174
196
})
175
197
require .NoError (t , err )
176
198
require .True (t , nginxC .IsRunning ())
177
-
178
- origin , err := nginxC .PortEndpoint (ctx , nginxDefaultPort , "http" )
179
- require .NoError (t , err )
180
-
181
- // check is reuse container with WaitingFor work correctly.
182
- req , err := http .NewRequestWithContext (ctx , http .MethodGet , origin , nil )
183
- require .NoError (t , err )
184
- req .Close = true
185
-
186
- resp , err := http .DefaultClient .Do (req )
187
- require .NoError (t , err )
188
- defer resp .Body .Close ()
189
-
190
- require .Equal (t , http .StatusOK , resp .StatusCode )
191
199
}
0 commit comments