Skip to content

Commit

Permalink
#173: Reusing existing reaper for subsequent container/network create…
Browse files Browse the repository at this point in the history
… calls. This ensures that any networks, created as part of the test case will be removed.
  • Loading branch information
ikolomiyets committed Apr 5, 2020
1 parent 90c3f43 commit 533329b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 5 deletions.
82 changes: 81 additions & 1 deletion network_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package testcontainers

import "context"
import (
"context"
"fmt"
"github.com/testcontainers/testcontainers-go/wait"
"testing"
"time"
)

// Create a network using a provider. By default it is Docker.
func ExampleNetworkProvider_CreateNetwork() {
Expand Down Expand Up @@ -28,3 +34,77 @@ func ExampleNetworkProvider_CreateNetwork() {
defer nginxC.Terminate(ctx)
nginxC.GetContainerID()
}

func Test_MultipleContainersInTheNewNetwork(t *testing.T) {
ctx := context.Background()

networkName := "test-network"

networkRequest := NetworkRequest{
Driver: "bridge",
Name: networkName,
Attachable: true,
}

env := make(map[string]string)
env["POSTGRES_PASSWORD"] = "Password1"
dbContainerRequest := ContainerRequest{
Image: "postgres:12.2",
ExposedPorts: []string{"5432/tcp"},
AutoRemove: true,
Env: env,
WaitingFor: wait.ForListeningPort("5432/tcp"),
Networks: []string{networkName},
}

gcr := GenericContainerRequest{
ContainerRequest: dbContainerRequest,
Started: true,
}

provider, err := gcr.ProviderType.GetProvider()
if err != nil {
t.Fatal("cannot get provider")
}

net, err := provider.CreateNetwork(ctx, networkRequest)
if err != nil {
t.Fatal("cannot create network")
}

defer net.Remove(ctx)

postgres, err := GenericContainer(ctx, gcr)
if err != nil {
t.Fatal(err)
}

defer postgres.Terminate(ctx)

env = make(map[string]string)
env["RABBITMQ_ERLANG_COOKIE"] = "f2a2d3d27c75"
env["RABBITMQ_DEFAULT_USER"] = "admin"
env["RABBITMQ_DEFAULT_PASS"] = "Password1"
hp := wait.ForListeningPort("5672/tcp")
hp.WithStartupTimeout(3 * time.Minute)
amqpRequest := ContainerRequest{
Image: "rabbitmq:management-alpine",
ExposedPorts: []string{"15672/tcp", "5672/tcp"},
Env: env,
AutoRemove: true,
WaitingFor: hp,
Networks: []string{networkName},
}
rabbitmq, err := GenericContainer(ctx, GenericContainerRequest{
ContainerRequest: amqpRequest,
Started: true,
})
if err != nil {
t.Fatal(err)
return
}

defer rabbitmq.Terminate(ctx)
fmt.Println(postgres.GetContainerID())
fmt.Println(rabbitmq.GetContainerID())
}
15 changes: 11 additions & 4 deletions reaper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const (
ReaperDefaultImage = "quay.io/testcontainers/ryuk:0.2.3"
)

var reaper *Reaper // We would like to create reaper only once

// ReaperProvider represents a provider for the reaper to run itself with
// The ContainerProvider interface should usually satisfy this as well, so it is pluggable
type ReaperProvider interface {
Expand All @@ -36,14 +38,19 @@ type Reaper struct {

// NewReaper creates a Reaper with a sessionID to identify containers and a provider to use
func NewReaper(ctx context.Context, sessionID string, provider ReaperProvider, reaperImageName string) (*Reaper, error) {
r := &Reaper{
// If reaper already exists re-use it
if reaper != nil {
return reaper, nil
}

// Otherwise create a new one
reaper = &Reaper{
Provider: provider,
SessionID: sessionID,
}

listeningPort := nat.Port("8080/tcp")

// TODO: reuse reaper if there already is one
req := ContainerRequest{
Image: reaperImage(reaperImageName),
ExposedPorts: []string{string(listeningPort)},
Expand All @@ -68,9 +75,9 @@ func NewReaper(ctx context.Context, sessionID string, provider ReaperProvider, r
if err != nil {
return nil, err
}
r.Endpoint = endpoint
reaper.Endpoint = endpoint

return r, nil
return reaper, nil
}

func reaperImage(reaperImageName string) string {
Expand Down

0 comments on commit 533329b

Please sign in to comment.