@@ -62,72 +62,72 @@ and `Network.Remove` which can be seen in the examples.
6262package main
6363
6464import (
65- "context"
66- "fmt"
67- "net/http"
68- "testing"
65+ "context"
66+ "fmt"
67+ "net/http"
68+ "testing"
6969
70- "github.com/testcontainers/testcontainers-go"
71- "github.com/testcontainers/testcontainers-go/wait"
70+ "github.com/testcontainers/testcontainers-go"
71+ "github.com/testcontainers/testcontainers-go/wait"
7272)
7373
7474type nginxContainer struct {
75- testcontainers.Container
76- URI string
75+ testcontainers.Container
76+ URI string
7777}
7878
7979
8080func setupNginx(ctx context.Context, networkName string) (*nginxContainer, error) {
81- req := testcontainers.ContainerRequest{
82- Image: "nginx",
83- ExposedPorts: []string{"80/tcp"},
84- Networks: []string{"bridge", networkName},
85- WaitingFor: wait.ForHTTP("/"),
86- }
87- container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
88- ContainerRequest: req,
89- Started: true,
90- })
91- var nginxC *nginxContainer
92- if container != nil {
93- nginxC = &nginxContainer{Container: container}
94- }
95- if err != nil {
96- return nginxC, err
97- }
98-
99- ip, err := container.Host(ctx)
100- if err != nil {
101- return nginxC, err
102- }
103-
104- mappedPort, err := container.MappedPort(ctx, "80")
105- if err != nil {
106- return nginxC, err
107- }
108-
109- nginxC.URI = fmt.Sprintf("http://%s:%s", ip, mappedPort.Port())
110-
111- return nginxC, nil
81+ req := testcontainers.ContainerRequest{
82+ Image: "nginx",
83+ ExposedPorts: []string{"80/tcp"},
84+ Networks: []string{"bridge", networkName},
85+ WaitingFor: wait.ForHTTP("/"),
86+ }
87+ container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
88+ ContainerRequest: req,
89+ Started: true,
90+ })
91+ var nginxC *nginxContainer
92+ if container != nil {
93+ nginxC = &nginxContainer{Container: container}
94+ }
95+ if err != nil {
96+ return nginxC, err
97+ }
98+
99+ ip, err := container.Host(ctx)
100+ if err != nil {
101+ return nginxC, err
102+ }
103+
104+ mappedPort, err := container.MappedPort(ctx, "80")
105+ if err != nil {
106+ return nginxC, err
107+ }
108+
109+ nginxC.URI = fmt.Sprintf("http://%s:%s", ip, mappedPort.Port())
110+
111+ return nginxC, nil
112112}
113113
114114func TestIntegrationNginxLatestReturn(t *testing.T) {
115- if testing.Short() {
116- t.Skip("skipping integration test")
117- }
115+ if testing.Short() {
116+ t.Skip("skipping integration test")
117+ }
118118
119- ctx := context.Background()
119+ ctx := context.Background()
120120
121- nw, err := network.New(ctx)
122- require.NoError(t, err)
123- testcontainers.CleanupNetwork(t, nw)
121+ nw, err := network.New(ctx)
122+ require.NoError(t, err)
123+ testcontainers.CleanupNetwork(t, nw)
124124
125- nginxC, err := setupNginx(ctx, nw.Name)
126- testcontainers.CleanupContainer(t, nginxC)
127- require.NoError(t, err)
125+ nginxC, err := setupNginx(ctx, nw.Name)
126+ testcontainers.CleanupContainer(t, nginxC)
127+ require.NoError(t, err)
128128
129- resp, err := http.Get(nginxC.URI)
130- require.Equal(t, http.StatusOK, resp.StatusCode)
129+ resp, err := http.Get(nginxC.URI)
130+ require.Equal(t, http.StatusOK, resp.StatusCode)
131131}
132132` ` `
133133
@@ -196,83 +196,75 @@ The aforementioned `Run` function represents a straightforward way to configure
196196
197197## Reusable container
198198
199- With `Reuse ` option you can reuse an existing container. Reusing will work only if you pass an
200- existing container name via ' req. Name ' field . If the name is not in a list of existing containers,
201- the function will create a new generic container. If `Reuse` is true and `Name` is empty, you will get error .
199+ Using the `WithReuseByName ` option you can reuse an existing container. Reuse works only when you provide an
200+ existing container name to this option . If the name is not found among existing containers,
201+ the function will create a new container. If the name is empty, an error is returned .
202202
203203The following test creates an NGINX container, adds a file into it and then reuses the container again for checking the file:
204+
204205```go
205206package main
206207
207208import (
208- "context"
209- "fmt"
210- "log"
209+ "context"
210+ "fmt"
211+ "log"
211212
212- "github.com/testcontainers/testcontainers-go"
213- "github.com/testcontainers/testcontainers-go/wait"
213+ "github.com/testcontainers/testcontainers-go"
214+ "github.com/testcontainers/testcontainers-go/wait"
214215)
215216
216217const (
217- reusableContainerName = "my_test_reusable_container"
218+ reusableContainerName = "my_test_reusable_container"
218219)
219220
220221func main() {
221- ctx := context.Background()
222-
223- n1, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
224- ContainerRequest: testcontainers.ContainerRequest{
225- Image: "nginx:1.17.6",
226- ExposedPorts: []string{"80/tcp"},
227- WaitingFor: wait.ForListeningPort("80/tcp"),
228- Name: reusableContainerName,
229- },
230- Started: true,
231- })
232- defer func() {
233- if err := testcontainers.TerminateContainer(n1); err != nil {
234- log.Printf("failed to terminate container: %s", err)
235- }
236- }()
237- if err != nil {
238- log.Print(err)
239- return
240- }
241-
242- copiedFileName := "hello_copy.sh"
243- err = n1.CopyFileToContainer(ctx, "./testdata/hello.sh", "/"+copiedFileName, 700)
244-
245- if err != nil {
246- log.Print(err)
247- return
248- }
249-
250- n2, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
251- ContainerRequest: testcontainers.ContainerRequest{
252- Image: "nginx:1.17.6",
253- ExposedPorts: []string{"80/tcp"},
254- WaitingFor: wait.ForListeningPort("80/tcp"),
255- Name: reusableContainerName,
256- },
257- Started: true,
258- Reuse: true,
259- })
260- defer func() {
261- if err := testcontainers.TerminateContainer(n2); err != nil {
262- log.Printf("failed to terminate container: %s", err)
263- }
264- }()
265- if err != nil {
266- log.Print(err)
267- return
268- }
269-
270- c, _, err := n2.Exec(ctx, []string{"bash", copiedFileName})
271- if err != nil {
272- log.Print(err)
273- return
274- }
275- fmt.Println(c)
222+ ctx := context.Background()
223+
224+ n1, err := testcontainers.Run(ctx, "nginx:1.17.6",
225+ testcontainers.WithExposedPorts("80/tcp"),
226+ testcontainers.WithWaitStrategy(wait.ForListeningPort("80/tcp")),
227+ testcontainers.WithReuseByName(reusableContainerName),
228+ )
229+ defer func() {
230+ if err := testcontainers.TerminateContainer(n1); err != nil {
231+ log.Printf("failed to terminate container: %s", err)
232+ }
233+ }()
234+ if err != nil {
235+ log.Print(err)
236+ return
237+ }
238+
239+ copiedFileName := "hello_copy.sh"
240+ err = n1.CopyFileToContainer(ctx, "./testdata/hello.sh", "/"+copiedFileName, 700)
241+
242+ if err != nil {
243+ log.Print(err)
244+ return
245+ }
246+
247+ n2, err := testcontainers.Run(ctx, "nginx:1.17.6",
248+ testcontainers.WithExposedPorts("80/tcp"),
249+ testcontainers.WithWaitStrategy(wait.ForListeningPort("80/tcp")),
250+ testcontainers.WithReuseByName(reusableContainerName),
251+ )
252+ defer func() {
253+ if err := testcontainers.TerminateContainer(n2); err != nil {
254+ log.Printf("failed to terminate container: %s", err)
255+ }
256+ }()
257+ if err != nil {
258+ log.Print(err)
259+ return
260+ }
261+
262+ c, _, err := n2.Exec(ctx, []string{"bash", copiedFileName})
263+ if err != nil {
264+ log.Print(err)
265+ return
266+ }
267+ fmt.Println(c)
276268}
277269```
278270
@@ -286,60 +278,60 @@ The following test creates two NGINX containers in parallel:
286278package main
287279
288280import (
289- "context"
290- "fmt"
291- "log"
281+ "context"
282+ "fmt"
283+ "log"
292284
293- "github.com/testcontainers/testcontainers-go"
285+ "github.com/testcontainers/testcontainers-go"
294286)
295287
296288func main() {
297- ctx := context.Background()
298-
299- requests := testcontainers.ParallelContainerRequest{
300- {
301- ContainerRequest: testcontainers.ContainerRequest{
302-
303- Image: "nginx",
304- ExposedPorts: []string{
305- "10080/tcp",
306- },
307- },
308- Started: true,
309- },
310- {
311- ContainerRequest: testcontainers.ContainerRequest{
312-
313- Image: "nginx",
314- ExposedPorts: []string{
315- "10081/tcp",
316- },
317- },
318- Started: true,
319- },
320- }
321-
322- res, err := testcontainers.ParallelContainers(ctx, requests, testcontainers.ParallelContainersOptions{})
323- for _, c := range res {
324- c := c
325- defer func() {
326- if err := testcontainers.TerminateContainer(c); err != nil {
327- log.Printf("failed to terminate container: %s", c)
328- }
329- }()
330- }
331-
332- if err != nil {
333- e, ok := err.(testcontainers.ParallelContainersError)
334- if !ok {
335- log.Printf("unknown error: %v", err)
336- return
337- }
338-
339- for _, pe := range e.Errors {
340- fmt.Println(pe.Request, pe.Error)
341- }
342- return
343- }
289+ ctx := context.Background()
290+
291+ requests := testcontainers.ParallelContainerRequest{
292+ {
293+ ContainerRequest: testcontainers.ContainerRequest{
294+
295+ Image: "nginx",
296+ ExposedPorts: []string{
297+ "10080/tcp",
298+ },
299+ },
300+ Started: true,
301+ },
302+ {
303+ ContainerRequest: testcontainers.ContainerRequest{
304+
305+ Image: "nginx",
306+ ExposedPorts: []string{
307+ "10081/tcp",
308+ },
309+ },
310+ Started: true,
311+ },
312+ }
313+
314+ res, err := testcontainers.ParallelContainers(ctx, requests, testcontainers.ParallelContainersOptions{})
315+ for _, c := range res {
316+ c := c
317+ defer func() {
318+ if err := testcontainers.TerminateContainer(c); err != nil {
319+ log.Printf("failed to terminate container: %s", c)
320+ }
321+ }()
322+ }
323+
324+ if err != nil {
325+ e, ok := err.(testcontainers.ParallelContainersError)
326+ if !ok {
327+ log.Printf("unknown error: %v", err)
328+ return
329+ }
330+
331+ for _, pe := range e.Errors {
332+ fmt.Println(pe.Request, pe.Error)
333+ }
334+ return
335+ }
344336}
345337```
0 commit comments