Skip to content

Commit

Permalink
Merge pull request #3 from gianarb/feature/defer-accept-t
Browse files Browse the repository at this point in the history
Terminate accept testing.T to better handle failure.
  • Loading branch information
gianarb authored Jul 23, 2018
2 parents a07ed57 + 5651d89 commit 589c694
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 32 deletions.
62 changes: 33 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestNginxLatestReturn(t *testing.T) {
if err != nil {
t.Error(err)
}
defer nginxC.Terminate(ctx)
defer nginxC.Terminate(ctx, t)
ip, err := nginxC.GetIPAddress(ctx)
if err != nil {
t.Error(err)
Expand All @@ -46,39 +46,43 @@ This is a simple example, you can create one container in my case using the
`nginx` image. You can get its IP `ip, err := nginxC.GetIPAddress(ctx)` and you
can use it to make a GET: `resp, err := http.Get(fmt.Sprintf("http://%s", ip))`

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

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

```go
ctx := context.Background()
redisC, err := testcontainer.RunContainer(ctx, "redis", testcontainer.RequestContainer{
ExportedPort: []string{
"6379/tpc",
},
})
if err != nil {
t.Error(err)
}
defer redisC.Terminate(ctx)
redisIP, err := redisC.GetIPAddress(ctx)
if err != nil {
t.Error(err)
}
func TestRedisPing(t testing.T) {
ctx := context.Background()
redisC, err := testcontainer.RunContainer(ctx, "redis", testcontainer.RequestContainer{
ExportedPort: []string{
"6379/tpc",
},
})
if err != nil {
t.Error(err)
}
defer redisC.Terminate(ctx, t)
redisIP, err := redisC.GetIPAddress(ctx)
if err != nil {
t.Error(err)
}

appC, err := testcontainer.RunContainer(ctx, "your/app", testcontainer.RequestContainer{
ExportedPort: []string{
"8081/tpc",
},
Env: map[string]string{
"REDIS_HOST": fmt.Sprintf("http://%s:6379", redisIP),
},
})
if err != nil {
t.Error(err)
}
defer appC.Terminate(ctx)
appC, err := testcontainer.RunContainer(ctx, "your/app", testcontainer.RequestContainer{
ExportedPort: []string{
"8081/tpc",
},
Env: map[string]string{
"REDIS_HOST": fmt.Sprintf("http://%s:6379", redisIP),
},
})
if err != nil {
t.Error(err)
}
defer appC.Terminate(ctx, t)

// your assertions
// your assertions
}
```
5 changes: 3 additions & 2 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testcontainer
import (
"context"
"strings"
"testing"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
Expand All @@ -26,10 +27,10 @@ type Container struct {
}

// Terminate is used to kill the container. It is usally triggered by as defer function.
func (c *Container) Terminate(ctx context.Context) error {
var err error
func (c *Container) Terminate(ctx context.Context, t *testing.T) error {
cli, err := client.NewEnvClient()
if err != nil {
t.Error(err)
return err
}
return cli.ContainerRemove(ctx, c.ID, types.ContainerRemoveOptions{
Expand Down
2 changes: 1 addition & 1 deletion docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestContainerCreation(t *testing.T) {
if err != nil {
t.Error(err)
}
defer nginxC.Terminate(ctx)
defer nginxC.Terminate(ctx, t)
ip, err := nginxC.GetIPAddress(ctx)
if err != nil {
t.Error(err)
Expand Down

0 comments on commit 589c694

Please sign in to comment.