Best Testcontainers-go code snippet using testcontainers.Name
testing_kafka_cluster.go
Source:testing_kafka_cluster.go
...58 probeScript, err := ioutil.TempFile("", "probe.sh")59 if err != nil {60 return err61 }62 defer os.Remove(probeScript.Name())63 probeScript.WriteString("#!/bin/bash \n")64 probeScript.WriteString(fmt.Sprintf("id=$(zookeeper-shell localhost:%s ls /brokers/ids | grep \"\\[%s\") \n", ZOOKEEPER_PORT, KAFKA_BROKER_ID))65 probeScript.WriteString(fmt.Sprintf("if [ $id = \"[%s]\" ]; then exit 0; else exit 1; fi", KAFKA_BROKER_ID))66 return kc.zookeeperContainer.CopyFileToContainer(context.Background(), probeScript.Name(), "probe.sh", 0700)67}68func (kc *TestingKafkaCluster) startKafka() error {69 ctx := context.Background()70 kafkaStartFile, err := ioutil.TempFile("", "testcontainers_start.sh")71 if err != nil {72 return err73 }74 defer os.Remove(kafkaStartFile.Name())75 exposedHost, err := kc.GetKafkaHost()76 if err != nil {77 return err78 }79 kafkaStartFile.WriteString("#!/bin/bash \n")80 kafkaStartFile.WriteString(fmt.Sprintf("export KAFKA_ADVERTISED_LISTENERS='PLAINTEXT://%s,BROKER://kafka:%s'\n", exposedHost, KAFKA_BROKER_PORT))81 kafkaStartFile.WriteString(". /etc/confluent/docker/bash-config \n")82 kafkaStartFile.WriteString("/etc/confluent/docker/configure \n")83 kafkaStartFile.WriteString("/etc/confluent/docker/launch \n")84 return kc.kafkaContainer.CopyFileToContainer(ctx, kafkaStartFile.Name(), "testcontainers_start.sh", 0700)85}86func NewTestingKafkaCluster() (*TestingKafkaCluster, error) {87 ctx := context.Background()88 network, err := testcontainers.GenericNetwork(ctx, testcontainers.GenericNetworkRequest{89 NetworkRequest: testcontainers.NetworkRequest{Name: CLUSTER_NETWORK_NAME},90 })91 if err != nil {92 return nil, err93 }94 dockerNetwork := network.(*testcontainers.DockerNetwork)95 zookeeperContainer, err := createZookeeperContainer(dockerNetwork)96 if err != nil {97 return nil, err98 }99 kafkaContainer, err := createKafkaContainer(dockerNetwork)100 if err != nil {101 return nil, err102 }103 return &TestingKafkaCluster{104 kafkaContainer: kafkaContainer,105 zookeeperContainer: zookeeperContainer,106 }, nil107}108func (kc *TestingKafkaCluster) IsAlive() (bool, error) {109 if s, err := kc.zookeeperContainer.Exec(context.Background(), []string{110 "/probe.sh",111 }); err != nil {112 return false, err113 } else if s == 0 {114 return true, nil115 } else {116 return false, nil117 }118}119func createZookeeperContainer(network *testcontainers.DockerNetwork) (testcontainers.Container, error) {120 ctx := context.Background()121 return testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{122 ContainerRequest: testcontainers.ContainerRequest{123 Image: ZOOKEEPER_IMAGE,124 ExposedPorts: []string{ZOOKEEPER_PORT},125 Env: map[string]string{"ZOOKEEPER_CLIENT_PORT": ZOOKEEPER_PORT, "ZOOKEEPER_TICK_TIME": "2000"},126 Networks: []string{network.Name},127 NetworkAliases: map[string][]string{network.Name: {"zookeeper"}},128 },129 })130}131func createKafkaContainer(network *testcontainers.DockerNetwork) (testcontainers.Container, error) {132 ctx := context.Background()133 return testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{134 ContainerRequest: testcontainers.ContainerRequest{135 Image: KAFKA_IMAGE,136 ExposedPorts: []string{KAFKA_CLIENT_PORT},137 Env: map[string]string{138 "KAFKA_BROKER_ID": KAFKA_BROKER_ID,139 "KAFKA_ZOOKEEPER_CONNECT": fmt.Sprintf("zookeeper:%s", ZOOKEEPER_PORT),140 "KAFKA_LISTENERS": fmt.Sprintf("PLAINTEXT://0.0.0.0:%s,BROKER://0.0.0.0:%s", KAFKA_CLIENT_PORT, KAFKA_BROKER_PORT),141 "KAFKA_LISTENER_SECURITY_PROTOCOL_MAP": "BROKER:PLAINTEXT,PLAINTEXT:PLAINTEXT",142 "KAFKA_INTER_BROKER_LISTENER_NAME": "BROKER",143 "KAFKA_OFFESTS_TOPIC_REPLICATION_FACTOR": "1",144 },145 Networks: []string{network.Name},146 NetworkAliases: map[string][]string{network.Name: {"kafka"}},147 Cmd: []string{"sh", "-c", "while [ ! -f /testcontainers_start.sh ]; do sleep 0.1; done; /testcontainers_start.sh"},148 },149 })150}...
kafkacluster.go
Source:kafkacluster.go
...41 kafkaStartFile, err := ioutil.TempFile("", "testcontainers_start.sh")42 if err != nil {43 panic(err)44 }45 defer os.Remove(kafkaStartFile.Name())46 // needs to set KAFKA_ADVERTISED_LISTENERS with the exposed kafka port47 exposedHost := kc.GetKafkaHost()48 kafkaStartFile.WriteString("#!/bin/bash \n")49 kafkaStartFile.WriteString("export KAFKA_ADVERTISED_LISTENERS='PLAINTEXT://" + exposedHost + ",BROKER://kafka:" + KAFKA_BROKER_PORT + "'\n")50 kafkaStartFile.WriteString(". /etc/confluent/docker/bash-config \n")51 kafkaStartFile.WriteString("/etc/confluent/docker/configure \n")52 kafkaStartFile.WriteString("/etc/confluent/docker/launch \n")53 err = kc.kafkaContainer.CopyFileToContainer(ctx, kafkaStartFile.Name(), "testcontainers_start.sh", 0700)54 if err != nil {55 panic(err)56 }57}58func NewKafkaCluster() *KafkaCluster {59 ctx := context.Background()60 // creates a network, so kafka and zookeeper can communicate directly61 network, err := testcontainers.GenericNetwork(ctx, testcontainers.GenericNetworkRequest{62 NetworkRequest: testcontainers.NetworkRequest{Name: CLUSTER_NETWORK_NAME},63 })64 if err != nil {65 panic(err)66 }67 dockerNetwork := network.(*testcontainers.DockerNetwork)68 zookeeperContainer := createZookeeperContainer(dockerNetwork)69 kafkaContainer := createKafkaContainer(dockerNetwork)70 return &KafkaCluster{71 zookeeperContainer: zookeeperContainer,72 kafkaContainer: kafkaContainer,73 }74}75func createZookeeperContainer(network *testcontainers.DockerNetwork) testcontainers.Container {76 ctx := context.Background()77 // creates the zookeeper container, but do not start it yet78 zookeeperContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{79 ContainerRequest: testcontainers.ContainerRequest{80 Image: ZOOKEEPER_IMAGE,81 ExposedPorts: []string{ZOOKEEPER_PORT},82 Env: map[string]string{"ZOOKEEPER_CLIENT_PORT": ZOOKEEPER_PORT, "ZOOKEEPER_TICK_TIME": "2000"},83 Networks: []string{network.Name},84 NetworkAliases: map[string][]string{network.Name: {"zookeeper"}},85 },86 })87 if err != nil {88 panic(err)89 }90 return zookeeperContainer91}92func createKafkaContainer(network *testcontainers.DockerNetwork) testcontainers.Container {93 ctx := context.Background()94 // creates the kafka container, but do not start it yet95 kafkaContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{96 ContainerRequest: testcontainers.ContainerRequest{97 Image: KAFKA_IMAGE,98 ExposedPorts: []string{KAFKA_CLIENT_PORT},99 Env: map[string]string{100 "KAFKA_BROKER_ID": "1",101 "KAFKA_ZOOKEEPER_CONNECT": "zookeeper:" + ZOOKEEPER_PORT,102 "KAFKA_LISTENERS": "PLAINTEXT://0.0.0.0:" + KAFKA_CLIENT_PORT + ",BROKER://0.0.0.0:" + KAFKA_BROKER_PORT,103 "KAFKA_LISTENER_SECURITY_PROTOCOL_MAP": "BROKER:PLAINTEXT,PLAINTEXT:PLAINTEXT",104 "KAFKA_INTER_BROKER_LISTENER_NAME": "BROKER",105 "KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR": "1",106 },107 Networks: []string{network.Name},108 NetworkAliases: map[string][]string{network.Name: {"kafka"}},109 // the container only starts when it finds and run /testcontainers_start.sh110 Cmd: []string{"sh", "-c", "while [ ! -f /testcontainers_start.sh ]; do sleep 0.1; done; /testcontainers_start.sh"},111 },112 })113 if err != nil {114 panic(err)115 }116 return kafkaContainer117}...
testing_helpers.go
Source:testing_helpers.go
...6 "log"7)8var cpTestVersion = "7.1.1"9var Ctx = context.Background()10func GetBaseInfra(networkName string) (zooContainer testcontainers.Container, kafkaContainer testcontainers.Container, schemaRegistryContainer testcontainers.Container) {11 var network = testcontainers.NetworkRequest{12 Name: networkName,13 Driver: "bridge",14 }15 provider, err := testcontainers.NewDockerProvider()16 if err != nil {17 log.Fatal(err)18 }19 if _, err := provider.GetNetwork(Ctx, network); err != nil {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},80 },81 Started: true,82 })83 CheckFail(err, "Source SR was not able to start")84 return zooContainer, kafkaContainer, schemaRegistryContainer85}86// Simple check function that will fail all if there is an error present and allows a custom message to be printed87func CheckFail(e error, msg string) {88 if e != nil {89 log.Println(e)90 log.Fatalln(msg)91 }92}...
Name
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 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")19 if err != nil {20 panic(err)21 }22 fmt.Printf("Redis is available at %s:%s\n", ip, port.Port())23}24import (25func main() {26 ctx := context.Background()27 req := testcontainers.ContainerRequest{28 ExposedPorts: []string{"6379/tcp"},29 WaitingFor: wait.ForListeningPort("6379/tcp"),30 }31 redisContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{32 })33 if err != nil {34 panic(err)35 }36 defer redisContainer.Terminate(ctx)37 fmt.Printf("Redis container ID: %s\n", redisContainer.GetContainerID())38}39import (40func main() {41 ctx := context.Background()42 req := testcontainers.ContainerRequest{43 ExposedPorts: []string{"6379/tcp"},44 WaitingFor: wait.ForListeningPort("6379/tcp"),45 }46 redisContainer, err := testcontainers.GenericContainer(ctx, testcontainers
Name
Using AI Code Generation
1import (2func main() {3 containerName := testcontainers.GenericContainerRequest{4 ContainerRequest: testcontainers.ContainerRequest{5 ExposedPorts: []string{"80/tcp"},6 WaitingFor: testcontainers.WaitingFor{},7 },8 }9 fmt.Println(containerName.Name())10}11import (12func main() {13 containerName := testcontainers.GenericContainerRequest{14 ContainerRequest: testcontainers.ContainerRequest{15 ExposedPorts: []string{"80/tcp"},16 WaitingFor: testcontainers.WaitingFor{},17 },18 }19 fmt.Println(containerName.Image())20}21import (22func main() {23 containerName := testcontainers.GenericContainerRequest{24 ContainerRequest: testcontainers.ContainerRequest{25 ExposedPorts: []string{"80/tcp"},26 WaitingFor: testcontainers.WaitingFor{},27 },28 }29 fmt.Println(containerName.ExposedPorts())30}31import (32func main() {33 containerName := testcontainers.GenericContainerRequest{34 ContainerRequest: testcontainers.ContainerRequest{35 ExposedPorts: []string{"80/tcp"},36 WaitingFor: testcontainers.WaitingFor{},37 },38 }39 fmt.Println(containerName.WaitingFor())40}41import (
Name
Using AI Code Generation
1import (2func main() {3 fmt.Println(testcontainers.Name)4}5import (6func main() {7 fmt.Println(testcontainers.Name)8}9import (10func main() {11 fmt.Println(testcontainers.Name)12}13import (14func main() {15 fmt.Println(testcontainers.Name)16}17import (18func main() {19 fmt.Println(testcontainers.Name)20}21import (22func main() {23 fmt.Println(testcontainers.Name)24}25import (26func main() {27 fmt.Println(testcontainers.Name)28}29import (30func main() {31 fmt.Println(testcontainers.Name)32}33import (34func main() {35 fmt.Println(testcontainers.Name)36}37import (38func main() {39 fmt.Println(testcontainers.Name)40}41import (42func main() {43 fmt.Println(testcontainers.Name)
Name
Using AI Code Generation
1import (2func main() {3 fmt.Println(testcontainers.Name())4}5import (6func main() {7 fmt.Println(testcontainers.DockerImageName())8}9import (10func main() {11 ctx := context.Background()12 req := testcontainers.ContainerRequest{13 Cmd: []string{"sh", "-c", "echo hello world"},14 ExposedPorts: []string{"80/tcp"},15 WaitingFor: wait.ForLog("hello world"),16 }17 container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{18 })19 if err != nil {20 panic(err)21 }22 defer container.Terminate(ctx)23 ip, err := container.Host(ctx)24 if err != nil {25 panic(err)26 }27 port, err := container.MappedPort(ctx, "80")28 if err != nil {29 panic(err)30 }31}
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!!