How to use Endpoint method of testcontainers Package

Best Testcontainers-go code snippet using testcontainers.Endpoint

test_utils.go

Source:test_utils.go Github

copy

Full Screen

...23 TestServerJMXPort = "7199"24 JbossJMXPort = "9990"25 JbossJMXUsername = "admin1234"26 JbossJMXPassword = "Password1!"27 TestServerAddDataEndpoint = "/cat"28 TestServerAddDataBatchEndpoint = "/cat_batch"29 TestServerAddCompositeDataEndpoint = "/composite_data_cat"30 TestServerCleanDataEndpoint = "/clear"31 KeystorePassword = "password"32 TruststorePassword = "password"33 JmxUsername = "testuser"34 JmxPassword = "testpassword"35 DefaultTimeoutMs = 1000036)37var (38 PrjDir string39 KeystorePath string40 TruststorePath string41)42func init() {43 path, err := os.Getwd()44 if err != nil {45 panic(err)46 }47 // Configure tests to point to the project's nrjmx build instead of the regular installation.48 PrjDir = filepath.Join(path, "..")49 KeystorePath = filepath.Join(PrjDir, "test-server", "keystore")50 TruststorePath = filepath.Join(PrjDir, "test-server", "truststore")51}52// RunJMXServiceContainer will start a container running test-server with JMX.53func RunJMXServiceContainer(ctx context.Context) (testcontainers.Container, error) {54 var hostnameOpt string55 if !isRunningInDockerContainer() {56 hostnameOpt = "-Djava.rmi.server.hostname=0.0.0.0"57 }58 req := testcontainers.ContainerRequest{59 Image: "test-server:latest",60 ExposedPorts: []string{61 fmt.Sprintf("%[1]s:%[1]s", TestServerPort),62 fmt.Sprintf("%[1]s:%[1]s", TestServerJMXPort),63 },64 Env: map[string]string{65 "JAVA_OPTS": "-Dcom.sun.management.jmxremote.port=" + TestServerJMXPort + " " +66 "-Dcom.sun.management.jmxremote.authenticate=false " +67 "-Dcom.sun.management.jmxremote.local.only=false " +68 "-Dcom.sun.management.jmxremote.ssl=false " +69 "-Dcom.sun.management.jmxremote=true " +70 "-Dcom.sun.management.jmxremote.rmi.port=" + TestServerJMXPort + " " +71 hostnameOpt,72 },73 WaitingFor: wait.ForListeningPort(TestServerPort),74 }75 container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{76 ContainerRequest: req,77 Started: true,78 })79 if err != nil {80 return nil, err81 }82 container.StartLogProducer(ctx)83 container.FollowOutput(&TestLogConsumer{})84 return container, err85}86// RunJMXServiceContainerSSL will start a container running test-server configured with SSL JMX.87func RunJMXServiceContainerSSL(ctx context.Context) (testcontainers.Container, error) {88 var hostnameOpt string89 if !isRunningInDockerContainer() {90 hostnameOpt = "-Djava.rmi.server.hostname=0.0.0.0"91 }92 req := testcontainers.ContainerRequest{93 Image: "test-server:latest",94 ExposedPorts: []string{95 fmt.Sprintf("%[1]s:%[1]s", TestServerPort),96 fmt.Sprintf("%[1]s:%[1]s", TestServerJMXPort),97 },98 Env: map[string]string{99 "JAVA_OPTS": "-Dcom.sun.management.jmxremote.port=" + TestServerJMXPort + " " +100 "-Dcom.sun.management.jmxremote.authenticate=true " +101 "-Dcom.sun.management.jmxremote.ssl=true " +102 "-Dcom.sun.management.jmxremote.ssl.need.client.auth=true " +103 "-Dcom.sun.management.jmxremote.registry.ssl=true " +104 "-Dcom.sun.management.jmxremote=true " +105 "-Dcom.sun.management.jmxremote.rmi.port=" + TestServerJMXPort + " " +106 "-Dcom.sun.management.jmxremote.local.only=false " +107 "-Djavax.net.ssl.keyStore=/keystore " +108 "-Djavax.net.ssl.keyStorePassword=password " +109 "-Djavax.net.ssl.trustStore=/truststore " +110 "-Djavax.net.ssl.trustStorePassword=password " +111 hostnameOpt,112 },113 WaitingFor: wait.ForListeningPort(TestServerPort),114 }115 container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{116 ContainerRequest: req,117 Started: true,118 })119 if err != nil {120 return nil, err121 }122 container.StartLogProducer(ctx)123 container.FollowOutput(&TestLogConsumer{})124 return container, err125}126// GetContainerServiceURL will return the url to the test-server running inside the container.127func GetContainerServiceURL(ctx context.Context, container testcontainers.Container, port nat.Port, endpoint string) (string, error) {128 mappedPort, err := container.MappedPort(ctx, port)129 if err != nil {130 return "", err131 }132 var hostIP string133 if isRunningInDockerContainer() {134 if hostIP, err = container.ContainerIP(ctx); err != nil {135 return "", err136 }137 } else {138 if hostIP, err = container.Host(ctx); err != nil {139 return "", err140 }141 }142 return fmt.Sprintf("http://%s:%s%s", hostIP, mappedPort.Port(), endpoint), nil143}144// CleanMBeans will remove all new added MBeans from test-server.145func CleanMBeans(ctx context.Context, container testcontainers.Container) ([]byte, error) {146 url, err := GetContainerServiceURL(ctx, container, TestServerPort, TestServerCleanDataEndpoint)147 if err != nil {148 return nil, err149 }150 return DoHttpRequest(http.MethodPut, url, nil)151}152// AddMBeansBatch will add new MBeans to the test-server.153func AddMBeansBatch(ctx context.Context, container testcontainers.Container, body []map[string]interface{}) ([]byte, error) {154 return addMBeans(ctx, container, body, TestServerAddDataBatchEndpoint)155}156// AddMBeans will add new MBeans to the test-server.157func AddMBeans(ctx context.Context, container testcontainers.Container, body map[string]interface{}) ([]byte, error) {158 return addMBeans(ctx, container, body, TestServerAddDataEndpoint)159}160// AddMBeans will add new MBeans to the test-server.161func AddMCompositeDataBeans(ctx context.Context, container testcontainers.Container, body map[string]interface{}) ([]byte, error) {162 return addMBeans(ctx, container, body, TestServerAddCompositeDataEndpoint)163}164// addMBeans will add new MBeans to the test-server.165func addMBeans(ctx context.Context, container testcontainers.Container, body interface{}, endpointPath string) ([]byte, error) {166 url, err := GetContainerServiceURL(ctx, container, TestServerPort, endpointPath)167 if err != nil {168 return nil, err169 }170 json, err := json.Marshal(body)171 if err != nil {172 return nil, err173 }174 return DoHttpRequest(http.MethodPost, url, json)175}176// TestLogConsumer is used to print container logs to stdout....

Full Screen

Full Screen

redis.go

Source:redis.go Github

copy

Full Screen

...39 })40 if err != nil {41 panic(err)42 }43 endPoint, err := redisC.Endpoint(ctx, "")44 if err != nil {45 panic(err)46 }47 return endPoint, nil48}...

Full Screen

Full Screen

main.go

Source:main.go Github

copy

Full Screen

...23 log.Println(err)24 return25 }26 defer redisC.Terminate(ctx)27 endpoint, err := redisC.Endpoint(ctx, "")28 if err != nil {29 log.Println(err)30 return31 }32 client := redis.NewClient(&redis.Options{33 Addr: endpoint,34 })35 client.Set("hello", "world", time.Second*10)36 val := client.Get("hello").Val()37 fmt.Println(val)38}...

Full Screen

Full Screen

Endpoint

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 ExposedPorts: []string{"5432/tcp"},6 WaitingFor: wait.ForLog("database system is ready to accept connections"),7 }8 postgres, _ := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{9 })10 port, _ := postgres.MappedPort(ctx, "5432")11 fmt.Println(port.Int())12}

Full Screen

Full Screen

Endpoint

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 panic(err)12 }13 redisHost, err := redisContainer.Host(ctx)14 if err != nil {15 panic(err)16 }17 redisPort, err := redisContainer.MappedPort(ctx, "6379")18 if err != nil {19 panic(err)20 }21 fmt.Printf("Redis host: %s, Redis port: %s", redisHost, redisPort)22}23import (24func main() {25 ctx := context.Background()26 req := testcontainers.ContainerRequest{27 ExposedPorts: []string{"6379/tcp"},28 WaitingFor: wait.ForListeningPort("6379/tcp"),29 }30 redisContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{31 })32 if err != nil {33 panic(err)34 }35 redisContainerID, err := redisContainer.GetContainerID(ctx)36 if err != nil {37 panic(err)38 }39 fmt.Printf("Redis container ID: %s", redisContainerID)40}41import (42func main() {43 ctx := context.Background()44 req := testcontainers.ContainerRequest{45 ExposedPorts: []string{"6379/tcp"},46 WaitingFor: wait.ForListeningPort("6379/tcp"),47 }

Full Screen

Full Screen

Endpoint

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 redis, _ := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{9 })10 port, _ := redis.MappedPort(ctx, "6379/tcp")11 fmt.Println(port.Int())12}13import (14func main() {15 ctx := context.Background()16 req := testcontainers.ContainerRequest{17 ExposedPorts: []string{"6379/tcp"},18 WaitingFor: wait.ForListeningPort("6379/tcp"),19 }20 redis, _ := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{21 })22 host, _ := redis.Host(ctx)23 fmt.Println(host)24}

Full Screen

Full Screen

Endpoint

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.Fatal(err)12 }13 defer redisContainer.Terminate(ctx)14 ip, err := redisContainer.Host(ctx)15 if err != nil {16 log.Fatal(err)17 }18 port, err := redisContainer.MappedPort(ctx, "6379")19 if err != nil {20 log.Fatal(err)21 }22 fmt.Println("Redis is available on ", ip, ":", port.Int())23}

Full Screen

Full Screen

Endpoint

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 ExposedPorts: []string{"5432/tcp"},6 WaitingFor: wait.ForListeningPort("5432/tcp"),7 }8 postgresContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{9 })10 if err != nil {11 log.Fatal(err)12 }13 defer postgresContainer.Terminate(ctx)14 ip, err := postgresContainer.Host(ctx)15 if err != nil {16 log.Fatal(err)17 }18 port, err := postgresContainer.MappedPort(ctx, "5432")19 if err != nil {20 log.Fatal(err)21 }22 fmt.Printf("postgres is available on %s:%s23", ip, port.Port())

Full Screen

Full Screen

Endpoint

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 ExposedPorts: []string{"5432/tcp"},6 WaitingFor: wait.ForListeningPort("5432/tcp"),7 }8 postgres, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{9 })10 if err != nil {11 log.Fatalf("Could not start container: %v", err)12 }13 defer postgres.Terminate(ctx)14 ip, err := postgres.Host(ctx)15 if err != nil {16 log.Fatalf("Could not get container IP: %v", err)17 }18 port, err := postgres.MappedPort(ctx, "5432/tcp")19 if err != nil {20 log.Fatalf("Could not get container port: %v", err)21 }22 fmt.Printf("Postgres is available on %s:%s23", ip, port.Port())24 time.Sleep(10 * time.Second)25}

Full Screen

Full Screen

Endpoint

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 panic(err)12 }13 defer mysqlContainer.Terminate(ctx)14 port, err := mysqlContainer.MappedPort(ctx, "3306")15 if err != nil {16 panic(err)17 }18 host, err := mysqlContainer.Host(ctx)19 if err != nil {20 panic(err)21 }22 fmt.Printf("host=%s, port=%s23 ip, err := mysqlContainer.Host(ctx)24 if err != nil {25 panic(err)26 }27 fmt.Printf("ip=%s, port=%s28 endpoint, err := mysqlContainer.Endpoint(ctx, "3306")29 if err != nil {30 panic(err)31 }32 fmt.Printf("endpoint=%s33}

Full Screen

Full Screen

Endpoint

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 os.Setenv("TESTCONTAINERS_RYUK_DISABLED", "true")4 os.Setenv("DOCKER_API_VERSION", "1.40")5 ctx := context.Background()6 req := testcontainers.ContainerRequest{7 ExposedPorts: []string{"8091/tcp", "8092/tcp", "8093/tcp", "8094/tcp", "11207/tcp", "11210/tcp", "11211/tcp"},8 WaitingFor: wait.ForLog("Starting Couchbase Server -- Web UI available at"),9 Env: map[string]string{10 },11 }12 couchbaseContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{13 })14 if err != nil {15 log.Fatalf("Could not start container: %v", err)16 }17 ip, err := couchbaseContainer.Host(ctx)18 if err != nil {19 log.Fatalf("Could not get container IP: %v", err)20 }21 port, err := couchbaseContainer.MappedPort(ctx, "8091")22 if err != nil {23 log.Fatalf("Could not get container port: %v", err)24 }

Full Screen

Full Screen

Endpoint

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 req := testcontainers.ContainerRequest{4 ExposedPorts: []string{"80/tcp"},5 WaitingFor: testcontainers.WaitingForLog("AH00558"),6 }7 ctx := context.Background()8 endpoint, err := req.Endpoint(ctx, "80")9 if err != nil {10 log.Fatal(err)11 }12 fmt.Println(endpoint)13}

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