How to use ParallelContainers method of testcontainers Package

Best Testcontainers-go code snippet using testcontainers.ParallelContainers

parallel_test.go

Source:parallel_test.go Github

copy

Full Screen

...6 "time"7 "github.com/stretchr/testify/require"8 "github.com/testcontainers/testcontainers-go/wait"9)10func TestParallelContainers(t *testing.T) {11 tests := []struct {12 name string13 reqs ParallelContainerRequest14 resLen int15 expErrors int16 }{17 {18 name: "running two containers (one error)",19 reqs: ParallelContainerRequest{20 {21 ContainerRequest: ContainerRequest{22 Image: "nginx",23 ExposedPorts: []string{24 "10080/tcp",25 },26 },27 Started: true,28 },29 {30 ContainerRequest: ContainerRequest{31 Image: "bad bad bad",32 ExposedPorts: []string{33 "10081/tcp",34 },35 },36 Started: true,37 },38 },39 resLen: 1,40 expErrors: 1,41 },42 {43 name: "running two containers (all errors)",44 reqs: ParallelContainerRequest{45 {46 ContainerRequest: ContainerRequest{47 Image: "bad bad bad",48 ExposedPorts: []string{49 "10081/tcp",50 },51 },52 Started: true,53 },54 {55 ContainerRequest: ContainerRequest{56 Image: "bad bad bad",57 ExposedPorts: []string{58 "10081/tcp",59 },60 },61 Started: true,62 },63 },64 resLen: 0,65 expErrors: 2,66 },67 {68 name: "running two containers (success)",69 reqs: ParallelContainerRequest{70 {71 ContainerRequest: ContainerRequest{72 Image: "nginx",73 ExposedPorts: []string{74 "10080/tcp",75 },76 },77 Started: true,78 },79 {80 ContainerRequest: ContainerRequest{81 Image: "nginx",82 ExposedPorts: []string{83 "10081/tcp",84 },85 },86 Started: true,87 },88 },89 resLen: 2,90 expErrors: 0,91 },92 }93 for _, tc := range tests {94 t.Run(tc.name, func(t *testing.T) {95 res, err := ParallelContainers(context.Background(), tc.reqs, ParallelContainersOptions{})96 if err != nil {97 require.NotZero(t, tc.expErrors)98 e, _ := err.(ParallelContainersError)99 if len(e.Errors) != tc.expErrors {100 t.Fatalf("expected erorrs: %d, got: %d\n", tc.expErrors, len(e.Errors))101 }102 }103 for _, c := range res {104 defer c.Terminate(context.Background())105 }106 if len(res) != tc.resLen {107 t.Fatalf("expected containers: %d, got: %d\n", tc.resLen, len(res))108 }109 })110 }111}112func TestParallelContainersWithReuse(t *testing.T) {113 const (114 postgresPort = 5432115 postgresPassword = "test"116 postgresUser = "test"117 postgresDb = "test"118 )119 natPort := fmt.Sprintf("%d/tcp", postgresPort)120 req := GenericContainerRequest{121 ContainerRequest: ContainerRequest{122 Image: "postgis/postgis",123 Name: "test-postgres",124 ExposedPorts: []string{natPort},125 Env: map[string]string{126 "POSTGRES_PASSWORD": postgresPassword,127 "POSTGRES_USER": postgresUser,128 "POSTGRES_DATABASE": postgresDb,129 },130 WaitingFor: wait.ForLog("database system is ready to accept connections").131 WithPollInterval(100 * time.Millisecond).132 WithOccurrence(2),133 },134 Started: true,135 Reuse: true,136 }137 parallelRequest := ParallelContainerRequest{138 req,139 req,140 req,141 }142 ctx := context.Background()143 res, err := ParallelContainers(ctx, parallelRequest, ParallelContainersOptions{})144 if err != nil {145 e, _ := err.(ParallelContainersError)146 t.Fatalf("expected errors: %d, got: %d\n", 0, len(e.Errors))147 }148 for _, c := range res {149 defer c.Terminate(ctx)150 }151}...

Full Screen

Full Screen

runDockerByYml.go

Source:runDockerByYml.go Github

copy

Full Screen

...16 newService := getRequest(v)17 initRequests = append(initRequests, newService)18 }19 }20 _, err := testcontainers.ParallelContainers(ctx, initRequests, testcontainers.ParallelContainersOptions{})21 if err != nil {22 e, ok := err.(testcontainers.ParallelContainersError)23 if !ok {24 log.Fatalf("unknown error: %v", err)25 }26 for _, pe := range e.Errors {27 fmt.Println(pe.Request, pe.Error)28 }29 return err30 }31 blockChannel <- 66632 return nil33}34func StartDockerByYml(compose DockerCompose) error {35 blockChannel := make(chan int)36 ctx := context.Background()37 requests := testcontainers.ParallelContainerRequest{}38 for _, v := range compose.Services {39 if v.Init == false {40 newService := getRequest(v)41 requests = append(requests, newService)42 }43 }44 go StartInitContainer(compose, blockChannel)45 <-blockChannel46 _, err := testcontainers.ParallelContainers(ctx, requests, testcontainers.ParallelContainersOptions{})47 if err != nil {48 e, ok := err.(testcontainers.ParallelContainersError)49 if !ok {50 log.Fatalf("unknown error: %v", err)51 }52 for _, pe := range e.Errors {53 fmt.Println(pe.Request, pe.Error)54 }55 return err56 }57 //for _, c := range res {58 // defer c.Terminate(ctx)59 //}60 return nil61}62func getRequest(item ContainerItem) testcontainers.GenericContainerRequest {...

Full Screen

Full Screen

containers.go

Source:containers.go Github

copy

Full Screen

...4 "fmt"5 "github.com/testcontainers/testcontainers-go"6 "log"7)8func CreateParallelContainers() {9 ctx := context.Background()10 requests := testcontainers.ParallelContainerRequest{11 {12 ContainerRequest: testcontainers.ContainerRequest{13 Image: "nginx",14 ExposedPorts: []string{15 "10080/tcp",16 },17 },18 Started: true,19 },20 {21 ContainerRequest: testcontainers.ContainerRequest{22 Image: "nginx",23 ExposedPorts: []string{24 "10081/tcp",25 },26 },27 Started: true,28 },29 }30 res, err := testcontainers.ParallelContainers(ctx, requests, testcontainers.ParallelContainersOptions{})31 if err != nil {32 e, ok := err.(testcontainers.ParallelContainersError)33 if !ok {34 log.Fatalf("unknown error: %v", err)35 }36 for _, pe := range e.Errors {37 fmt.Println(pe.Request, pe.Error)38 }39 return40 }41 for _, c := range res {42 defer c.Terminate(ctx)43 }44}...

Full Screen

Full Screen

ParallelContainers

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 Cmd: []string{"sleep", "3600"},6 ExposedPorts: []string{"80/tcp"},7 WaitingFor: wait.ForLog("listening on port 80"),8 }9 containers, err := testcontainers.ParallelContainers(ctx, req, 3)10 if err != nil {11 log.Fatal(err)12 }13 for _, container := range containers {14 fmt.Println(container.ContainerID)15 }16}

Full Screen

Full Screen

ParallelContainers

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 ExposedPorts: []string{"6379/tcp"},6 WaitingFor: wait.ForListeningPort("6379/tcp"),7 }8 redisContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{9 })10 if err != nil {11 log.Fatalf("Could not start container: %s", err)12 }13 defer redisContainer.Terminate(ctx)14 mappedPort, err := redisContainer.MappedPort(ctx, "6379")15 if err != nil {16 log.Fatalf("Could not get mapped port: %s", err)17 }18 fmt.Printf("Mapped port: %s", mappedPort.Port())19 containers, err := testcontainers.Parallel([]testcontainers.ContainerRequest{req, req})20 if err != nil {21 log.Fatalf("Could not run containers: %s", err)22 }23 for _, container := range containers {24 defer container.Terminate(ctx)25 }26 mappedPorts, err := testcontainers.MappedPorts(containers, "6379/tcp")27 if err != nil {28 log.Fatalf("Could not get mapped ports: %s", err)29 }30 fmt.Printf("Mapped ports: %s", mappedPorts)31}32import (33func main() {34 ctx := context.Background()35 network, err := testcontainers.GenericNetwork(ctx, testcontainers.GenericNetworkRequest{36 NetworkRequest: testcontainers.NetworkRequest{

Full Screen

Full Screen

ParallelContainers

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 network, err := testcontainers.GenericNetwork(ctx, testcontainers.GenericNetworkRequest{5 NetworkRequest: testcontainers.NetworkRequest{6 },7 })8 if err != nil {9 log.Fatal(err)10 }11 req := testcontainers.ContainerRequest{12 ExposedPorts: []string{"80/tcp"},13 WaitingFor: wait.ForHTTP("/"),14 Networks: []string{network.NetworkName()},15 }16 nginxContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{17 })18 if err != nil {19 log.Fatal(err)20 }21 req2 := testcontainers.ContainerRequest{22 ExposedPorts: []string{"80/tcp"},23 WaitingFor: wait.ForHTTP("/"),24 Networks: []string{network.NetworkName()},25 }26 nginxContainer2, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{27 })28 if err != nil {29 log.Fatal(err)30 }31 ipAddress, err := nginxContainer.Host(ctx)32 if err != nil {33 log.Fatal(err)34 }35 ipAddress2, err := nginxContainer2.Host(ctx)36 if err != nil {37 log.Fatal(err)38 }39 port, err := nginxContainer.MappedPort(ctx, "80")40 if err != nil {41 log.Fatal(err)42 }43 port2, err := nginxContainer2.MappedPort(ctx, "80")44 if err != nil {45 log.Fatal(err)46 }

Full Screen

Full Screen

ParallelContainers

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 ExposedPorts: []string{"6379/tcp"},6 WaitingFor: wait.ForListeningPort("6379/tcp"),7 }8 req2 := testcontainers.ContainerRequest{9 ExposedPorts: []string{"6379/tcp"},10 WaitingFor: wait.ForListeningPort("6379/tcp"),11 }12 redis1, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{13 })14 if err != nil {15 log.Fatal(err)16 }17 redis2, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{18 })19 if err != nil {20 log.Fatal(err)21 }22 host1, _ := redis1.Host(ctx)23 host2, _ := redis2.Host(ctx)24 port1, _ := redis1.MappedPort(ctx, "6379")25 port2, _ := redis2.MappedPort(ctx, "6379")26 fmt.Println("Container 1: ", host1, ":", port1.Port())27 fmt.Println("Container 2: ", host2, ":", port2.Port())28 redis1.Terminate(ctx)29 redis2.Terminate(ctx)30}31import (32func main() {33 ctx := context.Background()34 req := testcontainers.ContainerRequest{35 ExposedPorts: []string{"6379/tcp"},36 WaitingFor: wait.ForListeningPort("6379/tcp"),37 }38 req2 := testcontainers.ContainerRequest{

Full Screen

Full Screen

ParallelContainers

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 network, err := testcontainers.GenericNetwork(ctx, testcontainers.GenericNetworkRequest{5 NetworkRequest: testcontainers.NetworkRequest{6 },7 })8 if err != nil {9 log.Fatalf("Could not create network: %v", err)10 }11 defer network.Remove(ctx)12 req := testcontainers.ContainerRequest{13 ExposedPorts: []string{"6379/tcp"},14 WaitingFor: wait.ForListeningPort("6379/tcp"),15 }16 redisContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{17 })18 if err != nil {19 log.Fatalf("Could not start container: %v", err)20 }21 defer redisContainer.Terminate(ctx)22 redisIP, err := redisContainer.Host(ctx)23 if err != nil {24 log.Fatalf("Could not get container IP: %v", err)25 }26 fmt.Printf("Redis IP: %s27 err = network.Connect(ctx, testcontainers.NetworkConnectionConfig{28 ContainerID: redisContainer.GetContainerID(),29 EndpointConfig: map[string]interface{}{30 "Aliases": []string{"redis"},31 },32 })33 if err != nil {34 log.Fatalf("Could not connect container to network: %v", err)35 }36 req = testcontainers.ContainerRequest{37 Cmd: []string{"sh", "-c", "while true; do echo 'Hello World'; sleep 1; done"},38 Networks: []string{network.Name()},39 NetworkAliases: map[string][]string{40 network.Name(): {"alpine"},41 },42 WaitingFor: wait.ForLog("Hello World"),43 }44 alpineContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{

Full Screen

Full Screen

ParallelContainers

Using AI Code Generation

copy

Full Screen

1func main() {2 ctx := context.Background()3 req := testcontainers.ContainerRequest{4 ExposedPorts: []string{"6379/tcp"},5 WaitingFor: wait.ForListeningPort("6379"),6 }7 redis, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{8 })9 if err != nil {10 log.Fatal(err)11 }12 defer redis.Terminate(ctx)13 ip, err := redis.Host(ctx)14 if err != nil {15 log.Fatal(err)16 }17 port, err := redis.MappedPort(ctx, "6379")18 if err != nil {19 log.Fatal(err)20 }21 fmt.Printf("Redis is available at %s:%s22", ip, port.Port())23}24func main() {25 ctx := context.Background()26 req := testcontainers.ContainerRequest{27 ExposedPorts: []string{"6379/tcp"},28 WaitingFor: wait.ForListeningPort("6379"),29 }30 redis, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{31 })32 if err != nil {33 log.Fatal(err)34 }35 defer redis.Terminate(ctx)36 ip, err := redis.Host(ctx)37 if err != nil {38 log.Fatal(err)39 }40 port, err := redis.MappedPort(ctx, "6379")41 if err != nil {42 log.Fatal(err)43 }44 fmt.Printf("Redis is available at %s:%s45", ip, port.Port())46}47func main() {48 ctx := context.Background()49 req := testcontainers.ContainerRequest{50 ExposedPorts: []string{"6379/tcp"},51 WaitingFor: wait.ForListeningPort("6379"),52 }53 redis, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{54 })55 if err != nil {56 log.Fatal(err)57 }58 defer redis.Terminate(ctx)59 ip, err := redis.Host(ctx)60 if err != nil {61 log.Fatal(err)62 }

Full Screen

Full Screen

ParallelContainers

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 Cmd: []string{"sh", "-c", "while true; do echo hello; sleep 1; done"},6 ExposedPorts: []string{"80/tcp"},7 WaitingFor: wait.ForLog("hello").WithStartupTimeout(1 * time.Second),8 }9 container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{10 })11 if err != nil {12 log.Fatalf("Could not start container: %v", err)13 }14 defer container.Terminate(ctx)15 ip, err := container.Host(ctx)16 if err != nil {17 log.Fatalf("Could not get container IP: %v", err)18 }19 port, err := container.MappedPort(ctx, "80")20 if err != nil {21 log.Fatalf("Could not get container port: %v", err)22 }23 if err != nil {24 log.Fatalf("Could not make request to container: %v", err)25 }26 fmt.Println(resp)27}28import (29func main() {30 ctx := context.Background()31 req := testcontainers.ContainerRequest{32 Cmd: []string{"sh", "-c", "while true; do echo hello; sleep 1; done"},33 ExposedPorts: []string{"80/tcp"},34 WaitingFor: wait.ForLog("hello").WithStartupTimeout(1 * time.Second),35 }

Full Screen

Full Screen

ParallelContainers

Using AI Code Generation

copy

Full Screen

1func TestParallelContainers(t *testing.T) {2 ctx := context.Background()3 req := testcontainers.GenericContainerRequest{4 ContainerRequest: testcontainers.ContainerRequest{5 ExposedPorts: []string{"80/tcp"},6 WaitingFor: wait.ForListeningPort("80/tcp"),7 },8 }9 parallelContainer, _ := testcontainers.ParallelContainer(ctx, req, req)10 parallelContainer.Start(ctx)11 parallelContainerID, _ := parallelContainer.ContainerID(ctx)12 fmt.Println("Parallel Container ID: ", parallelContainerID)13 parallelContainerIP, _ := parallelContainer.Host(ctx)14 fmt.Println("Parallel Container IP: ", parallelContainerIP)15 parallelContainer.Terminate(ctx)16}

Full Screen

Full Screen

ParallelContainers

Using AI Code Generation

copy

Full Screen

1import (2func TestParallelContainers(t *testing.T) {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 Cmd: []string{"sleep", "1000"},6 ExposedPorts: []string{"80/tcp"},7 WaitingFor: wait.ForListeningPort("80/tcp"),8 }9 container1, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{10 })11 if err != nil {12 log.Fatal(err)13 }14 container2, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{15 })16 if err != nil {17 log.Fatal(err)18 }19 container3, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{20 })21 if err != nil {22 log.Fatal(err)23 }24 container4, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{25 })26 if err != nil {27 log.Fatal(err)28 }29 container5, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{30 })31 if err != nil {32 log.Fatal(err)33 }34 container6, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{35 })36 if err != nil {37 log.Fatal(err)38 }39 container7, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{40 })41 if err != nil {42 log.Fatal(err)43 }44 container8, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{

Full Screen

Full Screen

ParallelContainers

Using AI Code Generation

copy

Full Screen

1func TestParallelContainers(t *testing.T) {2 ctx := context.Background()3 dockerClient, err := client.NewEnvClient()4 if err != nil {5 t.Fatal(err)6 }7 testContainer := testcontainers.New(dockerClient, testcontainers.Request{8 Cmd: []string{"echo", "hello world"},9 })10 container1, err := testContainer.Container(ctx)11 if err != nil {12 t.Fatal(err)13 }14 container2, err := testContainer.Container(ctx)15 if err != nil {16 t.Fatal(err)17 }18 res := make(chan string)19 ctx, cancel := context.WithTimeout(ctx, 5*time.Second)20 defer cancel()21 ctx, cancel = context.WithCancel(ctx)22 defer cancel()23 wg.Add(1)24 go func() {25 defer wg.Done()26 defer cancel()27 if err := container1.Start(ctx); err != nil {28 t.Error(err)29 }30 status, err := container1.Wait(ctx)31 if err != nil {32 t.Error(err)33 }34 logs, err := container1.Logs(ctx)35 if err != nil {36 t.Error(err)37 }38 res <- fmt.Sprintf("Container1 exited with status %d: %s", status, logs)39 }()40 wg.Add(1)41 go func() {42 defer wg.Done()43 defer cancel()44 if err := container2.Start(ctx); err != nil {45 t.Error(err)46 }47 status, err := container2.Wait(ctx)48 if err != nil {49 t.Error(err)

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testcontainers-go automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful