diff --git a/network_test.go b/network_test.go index 02e55f9b3a..41a5b33f9a 100644 --- a/network_test.go +++ b/network_test.go @@ -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() { @@ -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()) +} diff --git a/reaper.go b/reaper.go index bee6b25395..ff56be9d09 100644 --- a/reaper.go +++ b/reaper.go @@ -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 { @@ -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)}, @@ -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 {