Skip to content

Commit c1d0092

Browse files
authored
Merge pull request testcontainers#3 from gianarb/feature/defer-accept-t
Terminate accept testing.T to better handle failure.
2 parents cbb5628 + e45c91f commit c1d0092

File tree

3 files changed

+37
-32
lines changed

3 files changed

+37
-32
lines changed

README.md

+33-29
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestNginxLatestReturn(t *testing.T) {
3131
if err != nil {
3232
t.Error(err)
3333
}
34-
defer nginxC.Terminate(ctx)
34+
defer nginxC.Terminate(ctx, t)
3535
ip, err := nginxC.GetIPAddress(ctx)
3636
if err != nil {
3737
t.Error(err)
@@ -46,39 +46,43 @@ This is a simple example, you can create one container in my case using the
4646
`nginx` image. You can get its IP `ip, err := nginxC.GetIPAddress(ctx)` and you
4747
can use it to make a GET: `resp, err := http.Get(fmt.Sprintf("http://%s", ip))`
4848

49-
To clean your environment you can defer the container termination `defer nginxC.Terminate(ctx)`.
49+
To clean your environment you can defer the container termination `defer
50+
nginxC.Terminate(ctx, t)`. `t` is `*testing.T` and it is used to notify is the
51+
`defer` failed marking the test as failed.
5052

5153
You can build more complex flow using envvar to configure the containers. Let's
5254
suppose you are testing an application that requites redis:
5355

5456
```go
55-
ctx := context.Background()
56-
redisC, err := testcontainer.RunContainer(ctx, "redis", testcontainer.RequestContainer{
57-
ExportedPort: []string{
58-
"6379/tpc",
59-
},
60-
})
61-
if err != nil {
62-
t.Error(err)
63-
}
64-
defer redisC.Terminate(ctx)
65-
redisIP, err := redisC.GetIPAddress(ctx)
66-
if err != nil {
67-
t.Error(err)
68-
}
57+
func TestRedisPing(t testing.T) {
58+
ctx := context.Background()
59+
redisC, err := testcontainer.RunContainer(ctx, "redis", testcontainer.RequestContainer{
60+
ExportedPort: []string{
61+
"6379/tpc",
62+
},
63+
})
64+
if err != nil {
65+
t.Error(err)
66+
}
67+
defer redisC.Terminate(ctx, t)
68+
redisIP, err := redisC.GetIPAddress(ctx)
69+
if err != nil {
70+
t.Error(err)
71+
}
6972

70-
appC, err := testcontainer.RunContainer(ctx, "your/app", testcontainer.RequestContainer{
71-
ExportedPort: []string{
72-
"8081/tpc",
73-
},
74-
Env: map[string]string{
75-
"REDIS_HOST": fmt.Sprintf("http://%s:6379", redisIP),
76-
},
77-
})
78-
if err != nil {
79-
t.Error(err)
80-
}
81-
defer appC.Terminate(ctx)
73+
appC, err := testcontainer.RunContainer(ctx, "your/app", testcontainer.RequestContainer{
74+
ExportedPort: []string{
75+
"8081/tpc",
76+
},
77+
Env: map[string]string{
78+
"REDIS_HOST": fmt.Sprintf("http://%s:6379", redisIP),
79+
},
80+
})
81+
if err != nil {
82+
t.Error(err)
83+
}
84+
defer appC.Terminate(ctx, t)
8285

83-
// your assertions
86+
// your assertions
87+
}
8488
```

docker.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package testcontainer
33
import (
44
"context"
55
"strings"
6+
"testing"
67

78
"github.com/docker/docker/api/types"
89
"github.com/docker/docker/api/types/container"
@@ -26,10 +27,10 @@ type Container struct {
2627
}
2728

2829
// Terminate is used to kill the container. It is usally triggered by as defer function.
29-
func (c *Container) Terminate(ctx context.Context) error {
30-
var err error
30+
func (c *Container) Terminate(ctx context.Context, t *testing.T) error {
3131
cli, err := client.NewEnvClient()
3232
if err != nil {
33+
t.Error(err)
3334
return err
3435
}
3536
return cli.ContainerRemove(ctx, c.ID, types.ContainerRemoveOptions{

docker_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestContainerCreation(t *testing.T) {
1717
if err != nil {
1818
t.Error(err)
1919
}
20-
defer nginxC.Terminate(ctx)
20+
defer nginxC.Terminate(ctx, t)
2121
ip, err := nginxC.GetIPAddress(ctx)
2222
if err != nil {
2323
t.Error(err)

0 commit comments

Comments
 (0)