How to use TestParallelContainers method of testcontainers Package

Best Testcontainers-go code snippet using testcontainers.TestParallelContainers

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,...

Full Screen

Full Screen

TestParallelContainers

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 ExposedPorts: []string{"80/tcp"},6 WaitingFor: wait.ForListeningPort("80/tcp"),7 }8 container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{9 })10 if err != nil {11 log.Fatal(err)12 }13 ip, err := container.Host(ctx)14 if err != nil {15 log.Fatal(err)16 }17 port, err := container.MappedPort(ctx, "80")18 if err != nil {19 log.Fatal(err)20 }21 fmt.Println(ip, port.Int())22}

Full Screen

Full Screen

TestParallelContainers

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 dockerEnv, err := testcontainers.DockerEnvironment(context.Background())4 if err != nil {5 log.Fatal(err)6 }7 req := testcontainers.ContainerRequest{8 ExposedPorts: []string{"80/tcp"},9 WaitingFor: wait.ForLog(".*ready for start up.*"),10 }11 nginxContainer, err := testcontainers.GenericContainer(context.Background(), testcontainers.GenericContainerRequest{12 })13 if err != nil {14 log.Fatal(err)15 }16 defer nginxContainer.Terminate(context.Background())17 ip, err := nginxContainer.Host(context.Background())18 if err != nil {19 log.Fatal(err)20 }21 port, err := nginxContainer.MappedPort(context.Background(), "80")22 if err != nil {23 log.Fatal(err)24 }25 fmt.Printf("Container IP: %s, Port: %s26", ip, port.Port())27 req = testcontainers.ContainerRequest{28 ExposedPorts: []string{"80/tcp"},29 WaitingFor: wait.ForLog(".*ready for start up.*"),30 }31 httpdContainer, err := testcontainers.GenericContainer(context.Background(), testcontainers.GenericContainerRequest{32 })33 if err != nil {34 log.Fatal(err)35 }36 defer httpdContainer.Terminate(context.Background())37 ip, err = httpdContainer.Host(context.Background())38 if err != nil {39 log.Fatal(err)40 }41 port, err = httpdContainer.MappedPort(context.Background(), "80")42 if err != nil {43 log.Fatal(err)44 }45 fmt.Printf("Container IP: %s, Port: %s46", ip, port.Port())

Full Screen

Full Screen

TestParallelContainers

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 ExposedPorts: []string{"3306/tcp"},6 WaitingFor: wait.ForLog("port: 3306 MySQL Community Server - GPL"),7 }8 mysqlContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{9 })10 if err != nil {11 log.Fatal(err)12 }13 defer mysqlContainer.Terminate(ctx)14 ip, err := mysqlContainer.Host(ctx)15 if err != nil {16 log.Fatal(err)17 }18 port, err := mysqlContainer.MappedPort(ctx, "3306")19 if err != nil {20 log.Fatal(err)21 }22 fmt.Printf("MySQL is available on %s:%s23", ip, port.Port())24 cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())25 if err != nil {26 panic(err)27 }28 ctx = context.Background()29 container, err := cli.ContainerCreate(ctx, &container.Config{30 }, &container.HostConfig{31 PortBindings: nat.PortMap{32 "3306/tcp": []nat.PortBinding{33 {

Full Screen

Full Screen

TestParallelContainers

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.ForLog("Ready to accept connections"),7 }8 redis, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{9 })10 if err != nil {11 log.Fatal(err)12 }13 defer redis.Terminate(ctx)14 redisHost, err := redis.Host(ctx)15 if err != nil {16 log.Fatal(err)17 }18 redisPort, err := redis.MappedPort(ctx, "6379")19 if err != nil {20 log.Fatal(err)21 }22 redisAddr := fmt.Sprintf("%s:%s", redisHost, redisPort.Port())23 fmt.Println("Redis address:", redisAddr)24 redisClient := redisContainerClient{addr: redisAddr}25 err = redisClient.set("hello", "world")26 if err != nil {27 log.Fatal(err)28 }29 value, err := redisClient.get("hello")30 if err != nil {31 log.Fatal(err)32 }33 fmt.Println("Value of 'hello' is", value)34 redis2, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{35 })36 if err != nil {37 log.Fatal(err)38 }39 defer redis2.Terminate(ctx)40 redisHost2, err := redis2.Host(ctx)41 if err != nil {42 log.Fatal(err)43 }44 redisPort2, err := redis2.MappedPort(ctx, "6379")45 if err != nil {46 log.Fatal(err)47 }48 redisAddr2 := fmt.Sprintf("%s:%s", redisHost2,

Full Screen

Full Screen

TestParallelContainers

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 Cmd: []string{"sleep", "1h"},6 ExposedPorts: []string{"80/tcp"},7 WaitingFor: wait.ForListeningPort("80/tcp"),8 }9 provider, err := testcontainers.NewDockerProvider()10 if err != nil {11 log.Fatalln(err)12 }13 container1, err := testcontainers.GenericContainer(ctx, provider, req)14 if err != nil {15 log.Fatalln(err)16 }17 defer container1.Terminate(ctx)18 ipAddress1, err := container1.Host(ctx)19 if err != nil {20 log.Fatalln(err)21 }22 port1, err := container1.MappedPort(ctx, "80")23 if err != nil {24 log.Fatalln(err)25 }26 fmt.Println("Container 1 IP address:", ipAddress1)27 fmt.Println("Container 1 port:", port1.Int())28 container2, err := testcontainers.GenericContainer(ctx, provider, req)29 if err != nil {30 log.Fatalln(err)31 }32 defer container2.Terminate(ctx)33 ipAddress2, err := container2.Host(ctx)34 if err != nil {35 log.Fatalln(err)36 }37 port2, err := container2.MappedPort(ctx, "80")38 if err != nil {39 log.Fatalln(err)40 }41 fmt.Println("Container 2 IP address:", ipAddress2)42 fmt.Println("Container 2 port:", port2.Int())43 container3, err := testcontainers.GenericContainer(ctx, provider, req)44 if err != nil {45 log.Fatalln(err)46 }47 defer container3.Terminate(ctx)

Full Screen

Full Screen

TestParallelContainers

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 req := testcontainers.ContainerRequest{4 ExposedPorts: []string{"9200/tcp"},5 WaitingFor: wait.ForLog("started"),6 }7 elastic, err := testcontainers.GenericContainer(context.Background(), testcontainers.GenericContainerRequest{8 })9 if err != nil {10 log.Fatal(err)11 }12 defer elastic.Terminate(context.Background())13 port, err := elastic.MappedPort(context.Background(), "9200")14 if err != nil {15 log.Fatal(err)16 }17 ip, err := elastic.Host(context.Background())18 if err != nil {19 log.Fatal(err)20 }21 fmt.Println(ip, port.Int())22}23import (24func main() {25 req := testcontainers.ContainerRequest{26 ExposedPorts: []string{"9200/tcp"},27 WaitingFor: wait.ForLog("started"),28 }29 elastic, err := testcontainers.GenericContainer(context.Background(), testcontainers.GenericContainerRequest{30 })31 if err != nil {32 log.Fatal(err)33 }34 defer elastic.Terminate(context.Background())35 port, err := elastic.MappedPort(context.Background(), "9200")36 if err != nil {37 log.Fatal(err)38 }39 ip, err := elastic.Host(context.Background())40 if err != nil {41 log.Fatal(err)42 }43 fmt.Println(ip, port.Int

Full Screen

Full Screen

TestParallelContainers

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 Cmd: []string{"tail", "-f", "/dev/null"},6 ExposedPorts: []string{"8080/tcp"},7 WaitingFor: wait.ForListeningPort("8080/tcp"),8 }9 container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{10 })11 if err != nil {12 log.Fatal(err)13 }14 defer container.Terminate(ctx)15 ip, err := container.Host(ctx)16 if err != nil {17 log.Fatal(err)18 }19 port, err := container.MappedPort(ctx, "8080/tcp")20 if err != nil {21 log.Fatal(err)22 }23 fmt.Println(ip, port.Int())24 container2, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{25 })26 if err != nil {27 log.Fatal(err)28 }29 defer container2.Terminate(ctx)30 ip2, err := container2.Host(ctx)31 if err != nil {32 log.Fatal(err)33 }34 port2, err := container2.MappedPort(ctx, "8080/tcp")35 if err != nil {36 log.Fatal(err)37 }38 fmt.Println(ip2, port2.Int())39 time.Sleep(2 * time.Minute)40}

Full Screen

Full Screen

TestParallelContainers

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 req := testcontainers.ContainerRequest{4 ExposedPorts: []string{"6379/tcp"},5 WaitingFor: wait.ForListeningPort("6379/tcp"),6 }7 ctx := context.Background()8 redisContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{9 })10 if err != nil {11 panic(err)12 }13 defer redisContainer.Terminate(ctx)14 ip, err := redisContainer.Host(ctx)15 if err != nil {16 panic(err)17 }18 port, err := redisContainer.MappedPort(ctx, "6379/tcp")19 if err != nil {20 panic(err)21 }22 containerID, err := redisContainer.ContainerID(ctx)23 if err != nil {24 panic(err)25 }26 containerName, err := redisContainer.ContainerName(ctx)27 if err != nil {28 panic(err)29 }30 containerInfo, err := redisContainer.ContainerInfo(ctx)31 if err != nil {32 panic(err)33 }34 containerState, err := redisContainer.ContainerState(ctx)35 if err != nil {36 panic(err)37 }38 containerLogs, err := redisContainer.Logs(ctx)39 if err != nil {40 panic(err)41 }42 containerImage, err := redisContainer.Image(ctx)43 if err != nil {44 panic(err)45 }46 containerPlatform, err := redisContainer.Platform(ctx)47 if err != nil {48 panic(err)49 }50 containerLabels, err := redisContainer.Labels(ctx)51 if err != nil {52 panic(err)53 }54 containerEnv, err := redisContainer.Env(ctx)55 if err != nil {56 panic(err)57 }58 containerIP, err := redisContainer.IPAddress(ctx)59 if err != nil {60 panic(err)61 }

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