Skip to content

Commit

Permalink
refactor: utilize testcontainers.CleanupContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
hhsnopek committed Oct 22, 2022
1 parent 29d2f08 commit bbe4097
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 36 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestIntegrationNginxLatestReturn(t *testing.T) {
}

// Clean up the container after the test is complete
defer nginxC.Terminate(ctx)
CleanupContainer(t, ctx, nginxC)

resp, err := http.Get(nginxC.URI)
if resp.StatusCode != http.StatusOK {
Expand All @@ -79,6 +79,8 @@ func TestIntegrationNginxLatestReturn(t *testing.T) {

Cleaning up your environment after test completion should be accomplished by deferring the container termination, e.g
`defer nginxC.Terminate(ctx)`. Reaper (Ryuk) is also enabled by default to help clean up.
Optionally, you may use `testcontainers.CleanupContainer(t, ctx, nginxC)` when
running in a test environment.

## Documentation

Expand Down
8 changes: 3 additions & 5 deletions container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func Test_BuildImageWithContexts(t *testing.T) {
} else if err != nil {
t.Fatal(err)
} else {
_ = c.Terminate(ctx)
CleanupContainer(t, ctx, c)
}
})
}
Expand All @@ -301,7 +301,7 @@ func Test_GetLogsFromFailedContainer(t *testing.T) {
if err != nil && !errors.Is(err, context.DeadlineExceeded) {
t.Fatal(err)
} else if err == nil {
_ = c.Terminate(ctx)
CleanupContainer(t, ctx, c)
t.Fatal("was expecting error starting container")
}

Expand Down Expand Up @@ -351,9 +351,7 @@ func createTestContainer(t *testing.T, ctx context.Context) int {
t.Fatalf("could not get mapped port: %v", err)
}

t.Cleanup(func() {
_ = container.Terminate(context.Background())
})
CleanupContainer(t, ctx, container)

return port.Int()
}
Expand Down
42 changes: 31 additions & 11 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"errors"
"fmt"
"log"

// Import mysql into the scope of this package (required)
"io"
Expand Down Expand Up @@ -632,9 +633,12 @@ func TestContainerTerminationRemovesDockerImage(t *testing.T) {
if err != nil {
t.Fatal(err)
}

_, _, err = client.ImageInspectWithRaw(ctx, imageID)
if err == nil {
t.Fatal("custom built image should have been removed")
t.Fatal("custom built image should have been removed", err)
} else {
t.Fatalf("unexpected error: %s", err)
}
})
}
Expand Down Expand Up @@ -1588,7 +1592,11 @@ func ExampleDockerProvider_CreateContainer() {
ContainerRequest: req,
Started: true,
})
CleanupContainer(t, ctx, nginxC)
defer func() {
if err := nginxC.Terminate(ctx); err != nil {
log.Fatalf("failed to terminate container: %s", err)
}
}()
}

func ExampleContainer_Host() {
Expand All @@ -1602,7 +1610,11 @@ func ExampleContainer_Host() {
ContainerRequest: req,
Started: true,
})
CleanupContainer(t, ctx, nginxC)
defer func() {
if err := nginxC.Terminate(ctx); err != nil {
log.Fatalf("failed to terminate container: %s", err)
}
}()
ip, _ := nginxC.Host(ctx)
println(ip)
}
Expand All @@ -1617,7 +1629,11 @@ func ExampleContainer_Start() {
nginxC, _ := GenericContainer(ctx, GenericContainerRequest{
ContainerRequest: req,
})
CleanupContainer(t, ctx, nginxC)
defer func() {
if err := nginxC.Terminate(ctx); err != nil {
log.Fatalf("failed to terminate container: %s", err)
}
}()
_ = nginxC.Start(ctx)
}

Expand All @@ -1631,7 +1647,11 @@ func ExampleContainer_Stop() {
nginxC, _ := GenericContainer(ctx, GenericContainerRequest{
ContainerRequest: req,
})
CleanupContainer(t, ctx, nginxC)
defer func() {
if err := nginxC.Terminate(ctx); err != nil {
log.Fatalf("failed to terminate container: %s", err)
}
}()
timeout := 10 * time.Second
_ = nginxC.Stop(ctx, &timeout)
}
Expand All @@ -1647,7 +1667,11 @@ func ExampleContainer_MappedPort() {
ContainerRequest: req,
Started: true,
})
CleanupContainer(t, ctx, nginxC)
defer func() {
if err := nginxC.Terminate(ctx); err != nil {
log.Fatalf("failed to terminate container: %s", err)
}
}()
ip, _ := nginxC.Host(ctx)
port, _ := nginxC.MappedPort(ctx, "80")
_, _ = http.Get(fmt.Sprintf("http://%s:%s", ip, port.Port()))
Expand Down Expand Up @@ -1795,11 +1819,7 @@ func TestContainerCustomPlatformImage(t *testing.T) {
Started: false,
})

t.Cleanup(func() {
if c != nil {
_ = c.Terminate(ctx)
}
})
CleanupContainer(t, ctx, c)

assert.Error(t, err)
})
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/cockroachdb.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestIntegrationDBInsertSelect(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer cdbContainer.Terminate(ctx)
testcontainers.CleanupContainer(t, ctx, cdbContainer)

db, err := sql.Open("pgx", cdbContainer.URI+"/projectmanagement")
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/nginx.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestIntegrationNginxLatestReturn(t *testing.T) {
}

// Clean up the container after the test is complete
defer nginxC.Terminate(ctx)
testcontainers.Cleanup(t, ctx, nginxC)

resp, err := http.Get(nginxC.URI)
if resp.StatusCode != http.StatusOK {
Expand Down
10 changes: 7 additions & 3 deletions docs/features/creating_container.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestIntegrationNginxLatestReturn(t *testing.T) {
}

// Clean up the container after the test is complete
defer nginxC.Terminate(ctx)
testcontainers.CleanupContainer(t, ctx, nginxC)

resp, err := http.Get(nginxC.URI)
if resp.StatusCode != http.StatusOK {
Expand Down Expand Up @@ -193,7 +193,6 @@ func main() {
}

res, err := testcontainers.ParallelContainers(ctx, requests, testcontainers.ParallelContainersOptions{})

if err != nil {
e, ok := err.(testcontainers.ParallelContainersError)
if !ok {
Expand All @@ -207,7 +206,12 @@ func main() {
}

for _, c := range res {
defer c.Terminate(ctx)
c := c
defer func() {
if err := c.Terminate(ctx); err != nil {
log.Fatalf("failed to terminate container: %s", c)
}
}()
}
}
```
3 changes: 3 additions & 0 deletions docs/features/garbage_collector.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ on test completion.
is as soon as you call `testcontainers.GenericContainer` but remember to
check for the `err` first.

Optionally you may use `testcontainers.CleanupContainer(tb testing.TB, context.Context, ctr testcontainers.Container)`
helper to terminate your running Container in a test environment.

## Ryuk

[Ryuk](https://github.com/testcontainers/moby-ryuk) (also referred to as
Expand Down
5 changes: 3 additions & 2 deletions docs/quickstart/gotest.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestWithRedis(t *testing.T) {
if err != nil {
t.Error(err)
}
defer redisC.Terminate(ctx)
testcontainers.CleanupContainer(t, ctx, redisC)
}
```

Expand Down Expand Up @@ -66,7 +66,8 @@ start, leaving to you the decision about when to start it.

All the containers must be removed at some point, otherwise they will run until
the host is overloaded. One of the ways we have to clean up is by deferring the
terminated function: `defer redisC.Terminate(ctx)`.
terminated function: `defer redisC.Terminate(ctx)`. Optionally you may use
terminate a container with the helper `tescontainers.CleaupContainer(t, ctx, redisC)`.

!!!tip

Expand Down
7 changes: 6 additions & 1 deletion network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testcontainers
import (
"context"
"fmt"
"log"
"testing"
"time"

Expand Down Expand Up @@ -37,7 +38,11 @@ func ExampleNetworkProvider_CreateNetwork() {
},
},
})
CleanupContainer(t, ctx, nginxC)
defer func() {
if err := nginxC.Terminate(ctx); err != nil {
log.Fatalf("failed to terminate container: %s", err)
}
}()

nginxC.GetContainerID()
}
Expand Down
4 changes: 1 addition & 3 deletions parallel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ func TestParallelContainers(t *testing.T) {

for _, c := range res {
c := c
t.Cleanup(func() {
_ = c.Terminate(context.Background())
})
CleanupContainer(t, context.Background(), c)
}

if len(res) != tc.resLen {
Expand Down
8 changes: 7 additions & 1 deletion wait/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"io"
"log"
"testing"
"time"

Expand All @@ -29,7 +30,12 @@ func ExampleExecStrategy() {
panic(err)
}

defer localstack.Terminate(ctx) // nolint: errcheck
defer func() {
if err := localstack.Terminate(ctx); err != nil {
log.Fatalf("failed to terminate container: %s", err)
}
}()

// Here you have a running container
}

Expand Down
19 changes: 12 additions & 7 deletions wait/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"crypto/x509"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -34,9 +35,13 @@ func ExampleHTTPStrategy() {
panic(err)
}

defer gogs.Terminate(ctx) // nolint: errcheck
// Here you have a running container
defer func() {
if err := gogs.Terminate(ctx); err != nil {
log.Fatalf("failed to terminate container: %s", err)
}
}()

// Here you have a running container
}

func TestHTTPStrategyWaitUntilReady(t *testing.T) {
Expand Down Expand Up @@ -79,20 +84,20 @@ func TestHTTPStrategyWaitUntilReady(t *testing.T) {
WithMethod(http.MethodPost).WithBody(bytes.NewReader([]byte("ping"))),
}

container, err := testcontainers.GenericContainer(context.Background(),
testcontainers.GenericContainerRequest{ContainerRequest: dockerReq, Started: true})
ctx := context.Background()
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ContainerRequest: dockerReq, Started: true})
if err != nil {
t.Error(err)
return
}
defer container.Terminate(context.Background()) // nolint: errcheck
testcontainers.CleanupContainer(t, ctx, container)

host, err := container.Host(context.Background())
host, err := container.Host(ctx)
if err != nil {
t.Error(err)
return
}
port, err := container.MappedPort(context.Background(), "6443/tcp")
port, err := container.MappedPort(ctx, "6443/tcp")
if err != nil {
t.Error(err)
return
Expand Down

0 comments on commit bbe4097

Please sign in to comment.