Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
324 changes: 158 additions & 166 deletions docs/features/creating_container.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,72 +62,72 @@ and `Network.Remove` which can be seen in the examples.
package main

import (
"context"
"fmt"
"net/http"
"testing"
"context"
"fmt"
"net/http"
"testing"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)

type nginxContainer struct {
testcontainers.Container
URI string
testcontainers.Container
URI string
}


func setupNginx(ctx context.Context, networkName string) (*nginxContainer, error) {
req := testcontainers.ContainerRequest{
Image: "nginx",
ExposedPorts: []string{"80/tcp"},
Networks: []string{"bridge", networkName},
WaitingFor: wait.ForHTTP("/"),
}
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
var nginxC *nginxContainer
if container != nil {
nginxC = &nginxContainer{Container: container}
}
if err != nil {
return nginxC, err
}

ip, err := container.Host(ctx)
if err != nil {
return nginxC, err
}

mappedPort, err := container.MappedPort(ctx, "80")
if err != nil {
return nginxC, err
}

nginxC.URI = fmt.Sprintf("http://%s:%s", ip, mappedPort.Port())

return nginxC, nil
req := testcontainers.ContainerRequest{
Image: "nginx",
ExposedPorts: []string{"80/tcp"},
Networks: []string{"bridge", networkName},
WaitingFor: wait.ForHTTP("/"),
}
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
var nginxC *nginxContainer
if container != nil {
nginxC = &nginxContainer{Container: container}
}
if err != nil {
return nginxC, err
}

ip, err := container.Host(ctx)
if err != nil {
return nginxC, err
}

mappedPort, err := container.MappedPort(ctx, "80")
if err != nil {
return nginxC, err
}

nginxC.URI = fmt.Sprintf("http://%s:%s", ip, mappedPort.Port())

return nginxC, nil
}

func TestIntegrationNginxLatestReturn(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
if testing.Short() {
t.Skip("skipping integration test")
}

ctx := context.Background()
ctx := context.Background()

nw, err := network.New(ctx)
require.NoError(t, err)
testcontainers.CleanupNetwork(t, nw)
nw, err := network.New(ctx)
require.NoError(t, err)
testcontainers.CleanupNetwork(t, nw)

nginxC, err := setupNginx(ctx, nw.Name)
testcontainers.CleanupContainer(t, nginxC)
require.NoError(t, err)
nginxC, err := setupNginx(ctx, nw.Name)
testcontainers.CleanupContainer(t, nginxC)
require.NoError(t, err)

resp, err := http.Get(nginxC.URI)
require.Equal(t, http.StatusOK, resp.StatusCode)
resp, err := http.Get(nginxC.URI)
require.Equal(t, http.StatusOK, resp.StatusCode)
}
```

Expand Down Expand Up @@ -196,83 +196,75 @@ The aforementioned `Run` function represents a straightforward way to configure

## Reusable container

With `Reuse` option you can reuse an existing container. Reusing will work only if you pass an
existing container name via 'req.Name' field. If the name is not in a list of existing containers,
the function will create a new generic container. If `Reuse` is true and `Name` is empty, you will get error.
Using the `WithReuseByName` option you can reuse an existing container. Reuse works only when you provide an
existing container name to this option. If the name is not found among existing containers,
the function will create a new container. If the name is empty, an error is returned.

The following test creates an NGINX container, adds a file into it and then reuses the container again for checking the file:

```go
package main

import (
"context"
"fmt"
"log"
"context"
"fmt"
"log"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)

const (
reusableContainerName = "my_test_reusable_container"
reusableContainerName = "my_test_reusable_container"
)

func main() {
ctx := context.Background()

n1, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "nginx:1.17.6",
ExposedPorts: []string{"80/tcp"},
WaitingFor: wait.ForListeningPort("80/tcp"),
Name: reusableContainerName,
},
Started: true,
})
defer func() {
if err := testcontainers.TerminateContainer(n1); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
if err != nil {
log.Print(err)
return
}

copiedFileName := "hello_copy.sh"
err = n1.CopyFileToContainer(ctx, "./testdata/hello.sh", "/"+copiedFileName, 700)

if err != nil {
log.Print(err)
return
}

n2, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "nginx:1.17.6",
ExposedPorts: []string{"80/tcp"},
WaitingFor: wait.ForListeningPort("80/tcp"),
Name: reusableContainerName,
},
Started: true,
Reuse: true,
})
defer func() {
if err := testcontainers.TerminateContainer(n2); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
if err != nil {
log.Print(err)
return
}

c, _, err := n2.Exec(ctx, []string{"bash", copiedFileName})
if err != nil {
log.Print(err)
return
}
fmt.Println(c)
ctx := context.Background()

n1, err := testcontainers.Run(ctx, "nginx:1.17.6",
testcontainers.WithExposedPorts("80/tcp"),
testcontainers.WithWaitStrategy(wait.ForListeningPort("80/tcp")),
testcontainers.WithReuseByName(reusableContainerName),
)
defer func() {
if err := testcontainers.TerminateContainer(n1); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
if err != nil {
log.Print(err)
return
}

copiedFileName := "hello_copy.sh"
err = n1.CopyFileToContainer(ctx, "./testdata/hello.sh", "/"+copiedFileName, 700)

if err != nil {
log.Print(err)
return
}

n2, err := testcontainers.Run(ctx, "nginx:1.17.6",
testcontainers.WithExposedPorts("80/tcp"),
testcontainers.WithWaitStrategy(wait.ForListeningPort("80/tcp")),
testcontainers.WithReuseByName(reusableContainerName),
)
defer func() {
if err := testcontainers.TerminateContainer(n2); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
if err != nil {
log.Print(err)
return
}

c, _, err := n2.Exec(ctx, []string{"bash", copiedFileName})
if err != nil {
log.Print(err)
return
}
fmt.Println(c)
}
```

Expand All @@ -286,60 +278,60 @@ The following test creates two NGINX containers in parallel:
package main

import (
"context"
"fmt"
"log"
"context"
"fmt"
"log"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go"
)

func main() {
ctx := context.Background()

requests := testcontainers.ParallelContainerRequest{
{
ContainerRequest: testcontainers.ContainerRequest{

Image: "nginx",
ExposedPorts: []string{
"10080/tcp",
},
},
Started: true,
},
{
ContainerRequest: testcontainers.ContainerRequest{

Image: "nginx",
ExposedPorts: []string{
"10081/tcp",
},
},
Started: true,
},
}

res, err := testcontainers.ParallelContainers(ctx, requests, testcontainers.ParallelContainersOptions{})
for _, c := range res {
c := c
defer func() {
if err := testcontainers.TerminateContainer(c); err != nil {
log.Printf("failed to terminate container: %s", c)
}
}()
}

if err != nil {
e, ok := err.(testcontainers.ParallelContainersError)
if !ok {
log.Printf("unknown error: %v", err)
return
}

for _, pe := range e.Errors {
fmt.Println(pe.Request, pe.Error)
}
return
}
ctx := context.Background()

requests := testcontainers.ParallelContainerRequest{
{
ContainerRequest: testcontainers.ContainerRequest{

Image: "nginx",
ExposedPorts: []string{
"10080/tcp",
},
},
Started: true,
},
{
ContainerRequest: testcontainers.ContainerRequest{

Image: "nginx",
ExposedPorts: []string{
"10081/tcp",
},
},
Started: true,
},
}

res, err := testcontainers.ParallelContainers(ctx, requests, testcontainers.ParallelContainersOptions{})
for _, c := range res {
c := c
defer func() {
if err := testcontainers.TerminateContainer(c); err != nil {
log.Printf("failed to terminate container: %s", c)
}
}()
}

if err != nil {
e, ok := err.(testcontainers.ParallelContainersError)
if !ok {
log.Printf("unknown error: %v", err)
return
}

for _, pe := range e.Errors {
fmt.Println(pe.Request, pe.Error)
}
return
}
}
```
4 changes: 2 additions & 2 deletions docs/features/follow_logs.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ You can associate `LogConsumer`s in two manners:

## Passing the LogConsumers in the ContainerRequest

This will represent the current way for associating `LogConsumer`s. You simply define your consumers, and attach them as a slice to the `ContainerRequest` in the
`LogConsumerCfg` field. See the following example, where `g` is an instance of a given `LogConsumer` struct.
This will represent the current way for associating `LogConsumer`s. You simply define your consumers, and attach them as a slice using the `WithLogConsumerConfig` functional option.
See the following example, where `g` is an instance of a given `LogConsumer` struct.

<!--codeinclude-->
[Passing LogConsumers](../../logconsumer_test.go) inside_block:logConsumersAtRequest
Expand Down
Loading
Loading