Best Testcontainers-go code snippet using testcontainers.GenericContainer
testing_helpers.go
Source:testing_helpers.go
...20 if _, err := provider.CreateNetwork(Ctx, network); err != nil {21 log.Fatal(err)22 }23 }24 zooContainer, err = testcontainers.GenericContainer(Ctx,25 testcontainers.GenericContainerRequest{26 ContainerRequest: testcontainers.ContainerRequest{27 Image: "confluentinc/cp-zookeeper:" + cpTestVersion,28 ExposedPorts: []string{"2181/tcp"},29 WaitingFor: wait.ForListeningPort("2181/tcp"),30 Name: "zookeeper" + networkName,31 Env: map[string]string{32 "ZOOKEEPER_CLIENT_PORT": "2181",33 "ZOOKEEPER_TICK_TIME": "2000",34 },35 Networks: []string{networkName},36 },37 Started: true,38 })39 CheckFail(err, "Zookeeper was not able to start")40 kafkaContainer, err = testcontainers.GenericContainer(Ctx,41 testcontainers.GenericContainerRequest{42 ContainerRequest: testcontainers.ContainerRequest{43 Image: "confluentinc/cp-server:" + cpTestVersion,44 ExposedPorts: []string{"29092/tcp"},45 WaitingFor: wait.ForListeningPort("29092/tcp"),46 Name: "broker" + networkName,47 Env: map[string]string{48 "KAFKA_BROKER_ID": "1",49 "KAFKA_ZOOKEEPER_CONNECT": "zookeeper" + networkName + ":2181",50 "KAFKA_LISTENER_SECURITY_PROTOCOL_MAP": "PLAINTEXT:PLAINTEXT",51 "KAFKA_ADVERTISED_LISTENERS": "PLAINTEXT://broker" + networkName + ":29092",52 "KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR": "1",53 "KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS": "0",54 "KAFKA_CONFLUENT_LICENSE_TOPIC_REPLICATION_FACTOR": "1",55 "KAFKA_CONFLUENT_BALANCER_TOPIC_REPLICATION_FACTOR": "1",56 "KAFKA_TRANSACTION_STATE_LOG_MIN_ISR": "1",57 "KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR": "1",58 },59 Networks: []string{networkName},60 },61 Started: true,62 })63 CheckFail(err, "Kafka was not able to start")64 schemaRegistryContainer, err = testcontainers.GenericContainer(Ctx,65 testcontainers.GenericContainerRequest{66 ContainerRequest: testcontainers.ContainerRequest{67 Image: "confluentinc/cp-schema-registry:" + cpTestVersion,68 ExposedPorts: []string{"8081/tcp"},69 WaitingFor: wait.ForListeningPort("8081/tcp"),70 Name: "schema-registry-src-" + networkName,71 Env: map[string]string{72 "SCHEMA_REGISTRY_HOST_NAME": "schema-registry-src",73 "SCHEMA_REGISTRY_SCHEMA_REGISTRY_GROUP_ID": "schema-src",74 "SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS": "broker" + networkName + ":29092",75 "SCHEMA_REGISTRY_KAFKASTORE_TOPIC": "_schemas",76 "SCHEMA_REGISTRY_KAFKASTORE_TOPIC_REPLICATION_FACTOR": "1",77 "SCHEMA_REGISTRY_MODE_MUTABILITY": "true",78 },79 Networks: []string{networkName},...
container_test.go
Source:container_test.go
...18 tb.Helper()19 const postgresPort = "5432/tcp"20 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)21 tb.Cleanup(cancel)22 container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{23 ContainerRequest: testcontainers.ContainerRequest{24 Image: "docker.io/postgres:alpine",25 ExposedPorts: []string{postgresPort},26 SkipReaper: true,27 AutoRemove: true,28 Env: map[string]string{29 "POSTGRES_USER": dbUser,30 "POSTGRES_PASSWORD": dbPassword,31 "POSTGRES_DB": dbName,32 },33 WaitingFor: wait.ForListeningPort(postgresPort),34 },35 Started: true,36 Logger: testcontainers.TestLogger(tb),37 })38 td.CmpNoError(tb, err, "testcontainers.GenericContainer()")39 tb.Cleanup(func() {40 td.CmpNoError(tb, container.Terminate(context.Background()), "container.Terminate()")41 })42 ep, err := container.PortEndpoint(ctx, postgresPort, "postgres")43 td.CmpNoError(tb, err, "container.PortEndpoint()")44 srv := new(config.Server)45 td.CmpNoError(tb, srv.UnmarshalURL(ep), "srv.UnmarshalURL()")46 srv.Path = append(srv.Path, dbName)47 srv.Credentials = &config.Credentials{48 Username: dbUser,49 Password: values.StringP(dbPassword),50 }51 name, err = container.Name(ctx)52 td.CmpNoError(tb, err, "container.Name()")53 return name, srv54}55func PrepareMariaDBContainer(tb testing.TB) (name string, cfg *config.Server) {56 tb.Helper()57 const mysqlPort = "3306/tcp"58 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)59 tb.Cleanup(cancel)60 container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{61 ContainerRequest: testcontainers.ContainerRequest{62 Image: "docker.io/mariadb:10",63 ExposedPorts: []string{mysqlPort},64 SkipReaper: true,65 AutoRemove: true,66 Env: map[string]string{67 "MARIADB_USER": dbUser,68 "MARIADB_PASSWORD": dbPassword,69 "MARIADB_RANDOM_ROOT_PASSWORD": "1",70 "MARIADB_DATABASE": dbName,71 },72 WaitingFor: wait.ForListeningPort(mysqlPort),73 },74 Started: true,75 Logger: testcontainers.TestLogger(tb),76 })77 td.CmpNoError(tb, err, "testcontainers.GenericContainer()")78 tb.Cleanup(func() {79 td.CmpNoError(tb, container.Terminate(context.Background()), "container.Terminate()")80 })81 ep, err := container.PortEndpoint(ctx, mysqlPort, "mysql")82 td.CmpNoError(tb, err, "container.PortEndpoint()")83 srv := new(config.Server)84 td.CmpNoError(tb, srv.UnmarshalURL(ep), "srv.UnmarshalURL()")85 srv.Path = append(srv.Path, dbName)86 srv.Credentials = &config.Credentials{87 Username: dbUser,88 Password: values.StringP(dbPassword),89 }90 name, err = container.Name(ctx)91 td.CmpNoError(tb, err, "container.Name()")...
base_suite.go
Source:base_suite.go
...41 s.Require().NoError(err)42 }43 }44}45func (s *BaseSuite) GenericContainer(request testcontainers.GenericContainerRequest) (testcontainers.Container, error) {46 request.ReaperImage = constants.ReaperImage47 return testcontainers.GenericContainer(s.ctx, request)48}
GenericContainer
Using AI Code Generation
1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 ExposedPorts: []string{"6379/tcp"},6 WaitingFor: wait.ForListeningPort("6379/tcp"),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 redisPort, err := redis.MappedPort(ctx, "6379")15 if err != nil {16 log.Fatal(err)17 }18 fmt.Println(redisPort.Int())19 time.Sleep(10 * time.Second)20}21require (
GenericContainer
Using AI Code Generation
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.Fatal(err)12 }13 ip, err := redisContainer.Host(ctx)14 if err != nil {15 log.Fatal(err)16 }17 port, err := redisContainer.MappedPort(ctx, "6379")18 if err != nil {19 log.Fatal(err)20 }21 fmt.Println("Redis is available on", ip, ":", port.Int())22 err = redisContainer.Terminate(ctx)23 if err != nil {24 log.Fatal(err)25 }26}
GenericContainer
Using AI Code Generation
1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 Cmd: []string{"echo", "hello world"},6 WaitingFor: wait.ForLog("hello world"),7 }8 resp, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{9 })10 if err != nil {11 log.Fatal(err)12 }13 defer resp.Terminate(ctx)14 fmt.Println("Container ID: ", resp.GetContainerID())15}16import (17func main() {18 ctx := context.Background()19 req := testcontainers.ContainerRequest{20 Cmd: []string{"echo", "hello world"},21 WaitingFor: wait.ForLog("hello world"),22 }23 resp, err := req.NewContainer(ctx)24 if err != nil {25 log.Fatal(err)26 }27 defer resp.Terminate(ctx)28 fmt.Println("Container ID: ", resp.GetContainerID())29}
GenericContainer
Using AI Code Generation
1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 Cmd: []string{"echo", "Hello World"},6 ExposedPorts: []string{"80/tcp"},7 WaitingFor: wait.ForLog("Hello World"),8 }9 resp, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{10 })11 if err != nil {12 log.Fatal(err)13 }14 ip, err := resp.Host(ctx)15 if err != nil {16 log.Fatal(err)17 }18 port, err := resp.MappedPort(ctx, "80")19 if err != nil {20 log.Fatal(err)21 }22 fmt.Printf("Alpine is available at %s:%s", ip, port.Port())23 if err := resp.Terminate(ctx); err != nil {24 log.Fatal(err)25 }26 os.Exit(0)27}
GenericContainer
Using AI Code Generation
1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 ExposedPorts: []string{"8091/tcp", "8092/tcp", "8093/tcp", "8094/tcp", "11207/tcp", "11210/tcp", "11211/tcp", "18091/tcp", "18092/tcp", "18093/tcp", "18094/tcp", "18096/tcp", "18097/tcp"},6 WaitingFor: wait.ForListeningPort("8091/tcp"),7 }8 couchbase, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{9 })10 if err != nil {11 log.Fatal(err)12 }13 host, err := couchbase.Host(ctx)14 if err != nil {15 log.Fatal(err)16 }17 port, err := couchbase.MappedPort(ctx, "8091/tcp")18 if err != nil {19 log.Fatal(err)20 }21 fmt.Println(host, port.Int())22 defer couchbase.Terminate(ctx)23}
GenericContainer
Using AI Code Generation
1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 Cmd: []string{"sh", "-c", "while true; do echo hello world; sleep 1; done"},6 ExposedPorts: []string{"80/tcp"},7 WaitingFor: wait.ForLog("hello world"),8 }9 c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{10 })11 if err != nil {12 log.Fatal(err)13 }14 defer c.Terminate(ctx)15 ip, err := c.Host(ctx)16 if err != nil {17 log.Fatal(err)18 }19 port, err := c.MappedPort(ctx, "80")20 if err != nil {21 log.Fatal(err)22 }23 fmt.Printf("Container is listening on %s:%s24", ip, port.Port())25 cmd := exec.Command("curl", fmt.Sprintf("%s:%s", ip, port.Port()))26 cmd.Run()27}
GenericContainer
Using AI Code Generation
1import (2func TestContainer(t *testing.T) {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 ExposedPorts: []string{"8080"},6 WaitingFor: wait.ForHTTP("/health").WithStartupTimeout(60 * time.Second),7 }8 cont, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{9 })10 if err != nil {11 fmt.Println(err)12 }13 defer cont.Terminate(ctx)14 ip, err := cont.Host(ctx)15 if err != nil {16 fmt.Println(err)17 }18 port, err := cont.MappedPort(ctx, "8080")19 if err != nil {20 fmt.Println(err)21 }22 fmt.Println(ip, port.Int())23}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!