How to use TestContainerCreationWaitsForLog method of testcontainers Package

Best Testcontainers-go code snippet using testcontainers.TestContainerCreationWaitsForLog

docker_test.go

Source:docker_test.go Github

copy

Full Screen

...840 if err == nil {841 t.Error("Expected timeout")842 }843}844func TestContainerCreationWaitsForLogContextTimeout(t *testing.T) {845 ctx := context.Background()846 req := ContainerRequest{847 Image: "docker.io/mysql:latest",848 ExposedPorts: []string{"3306/tcp", "33060/tcp"},849 Env: map[string]string{850 "MYSQL_ROOT_PASSWORD": "password",851 "MYSQL_DATABASE": "database",852 },853 WaitingFor: wait.ForLog("test context timeout").WithStartupTimeout(1 * time.Second),854 }855 _, err := GenericContainer(ctx, GenericContainerRequest{856 ProviderType: providerType,857 ContainerRequest: req,858 Started: true,859 })860 if err == nil {861 t.Error("Expected timeout")862 }863}864func TestContainerCreationWaitsForLog(t *testing.T) {865 ctx := context.Background()866 req := ContainerRequest{867 Image: "docker.io/mysql:latest",868 ExposedPorts: []string{"3306/tcp", "33060/tcp"},869 Env: map[string]string{870 "MYSQL_ROOT_PASSWORD": "password",871 "MYSQL_DATABASE": "database",872 },873 WaitingFor: wait.ForLog("port: 3306 MySQL Community Server - GPL"),874 }875 mysqlC, err := GenericContainer(ctx, GenericContainerRequest{876 ProviderType: providerType,877 ContainerRequest: req,878 Started: true,879 })880 require.NoError(t, err)881 terminateContainerOnEnd(t, ctx, mysqlC)882 host, _ := mysqlC.Host(ctx)883 p, _ := mysqlC.MappedPort(ctx, "3306/tcp")884 port := p.Int()885 connectionString := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?tls=skip-verify",886 "root", "password", host, port, "database")887 db, err := sql.Open("mysql", connectionString)888 defer db.Close()889 if err = db.Ping(); err != nil {890 t.Errorf("error pinging db: %+v\n", err)891 }892 _, err = db.Exec("CREATE TABLE IF NOT EXISTS a_table ( \n" +893 " `col_1` VARCHAR(128) NOT NULL, \n" +894 " `col_2` VARCHAR(128) NOT NULL, \n" +895 " PRIMARY KEY (`col_1`, `col_2`) \n" +896 ")")897 if err != nil {898 t.Errorf("error creating table: %+v\n", err)899 }900}901func Test_BuildContainerFromDockerfile(t *testing.T) {902 t.Log("getting context")903 ctx := context.Background()904 t.Log("got context, creating container request")905 req := ContainerRequest{906 FromDockerfile: FromDockerfile{907 Context: "./testresources",908 },909 ExposedPorts: []string{"6379/tcp"},910 WaitingFor: wait.ForLog("Ready to accept connections"),911 }912 t.Log("creating generic container request from container request")913 genContainerReq := GenericContainerRequest{914 ProviderType: providerType,915 ContainerRequest: req,916 Started: true,917 }918 t.Log("creating redis container")919 redisC, err := GenericContainer(ctx, genContainerReq)920 require.NoError(t, err)921 terminateContainerOnEnd(t, ctx, redisC)922 t.Log("created redis container")923 t.Log("getting redis container endpoint")924 endpoint, err := redisC.Endpoint(ctx, "")925 if err != nil {926 t.Fatal(err)927 }928 t.Log("retrieved redis container endpoint")929 client := redis.NewClient(&redis.Options{930 Addr: endpoint,931 })932 t.Log("pinging redis")933 pong, err := client.Ping(ctx).Result()934 require.NoError(t, err)935 t.Log("received response from redis")936 if pong != "PONG" {937 t.Fatalf("received unexpected response from redis: %s", pong)938 }939}940func Test_BuildContainerFromDockerfileWithBuildArgs(t *testing.T) {941 t.Log("getting ctx")942 ctx := context.Background()943 ba := "build args value"944 t.Log("got ctx, creating container request")945 req := ContainerRequest{946 FromDockerfile: FromDockerfile{947 Context: "./testresources",948 Dockerfile: "args.Dockerfile",949 BuildArgs: map[string]*string{950 "FOO": &ba,951 },952 },953 ExposedPorts: []string{"8080/tcp"},954 WaitingFor: wait.ForLog("ready"),955 }956 genContainerReq := GenericContainerRequest{957 ProviderType: providerType,958 ContainerRequest: req,959 Started: true,960 }961 c, err := GenericContainer(ctx, genContainerReq)962 require.NoError(t, err)963 terminateContainerOnEnd(t, ctx, c)964 ep, err := c.Endpoint(ctx, "http")965 if err != nil {966 t.Fatal(err)967 }968 resp, err := http.Get(ep + "/env")969 if err != nil {970 t.Fatal(err)971 }972 body, err := ioutil.ReadAll(resp.Body)973 if err != nil {974 t.Fatal(err)975 }976 assert.Equal(t, 200, resp.StatusCode)977 assert.Equal(t, ba, string(body))978}979func Test_BuildContainerFromDockerfileWithBuildLog(t *testing.T) {980 rescueStdout := os.Stderr981 r, w, _ := os.Pipe()982 os.Stderr = w983 t.Log("getting ctx")984 ctx := context.Background()985 t.Log("got ctx, creating container request")986 req := ContainerRequest{987 FromDockerfile: FromDockerfile{988 Context: "./testresources",989 Dockerfile: "buildlog.Dockerfile",990 PrintBuildLog: true,991 },992 }993 genContainerReq := GenericContainerRequest{994 ProviderType: providerType,995 ContainerRequest: req,996 Started: true,997 }998 c, err := GenericContainer(ctx, genContainerReq)999 require.NoError(t, err)1000 terminateContainerOnEnd(t, ctx, c)1001 _ = w.Close()1002 out, _ := ioutil.ReadAll(r)1003 os.Stdout = rescueStdout1004 temp := strings.Split(string(out), "\n")1005 if !regexp.MustCompile(`(?i)^Step\s*1/1\s*:\s*FROM docker.io/alpine$`).MatchString(temp[0]) {1006 t.Errorf("Expected stdout firstline to be %s. Got '%s'.", "Step 1/1 : FROM docker.io/alpine", temp[0])1007 }1008}1009func TestContainerCreationWaitsForLogAndPortContextTimeout(t *testing.T) {1010 ctx := context.Background()1011 req := ContainerRequest{1012 Image: "docker.io/mysql:latest",1013 ExposedPorts: []string{"3306/tcp", "33060/tcp"},1014 Env: map[string]string{1015 "MYSQL_ROOT_PASSWORD": "password",1016 "MYSQL_DATABASE": "database",1017 },1018 WaitingFor: wait.ForAll(1019 wait.ForLog("I love testcontainers-go"),1020 wait.ForListeningPort("3306/tcp"),1021 ),1022 }1023 _, err := GenericContainer(ctx, GenericContainerRequest{1024 ProviderType: providerType,1025 ContainerRequest: req,1026 Started: true,1027 })1028 if err == nil {1029 t.Fatal("Expected timeout")1030 }1031}1032func TestContainerCreationWaitingForHostPort(t *testing.T) {1033 ctx := context.Background()1034 req := ContainerRequest{1035 Image: nginxAlpineImage,1036 ExposedPorts: []string{nginxDefaultPort},1037 WaitingFor: wait.ForListeningPort(nginxDefaultPort),1038 }1039 nginx, err := GenericContainer(ctx, GenericContainerRequest{1040 ProviderType: providerType,1041 ContainerRequest: req,1042 Started: true,1043 })1044 require.NoError(t, err)1045 terminateContainerOnEnd(t, ctx, nginx)1046}1047func TestContainerCreationWaitingForHostPortWithoutBashThrowsAnError(t *testing.T) {1048 ctx := context.Background()1049 req := ContainerRequest{1050 Image: nginxAlpineImage,1051 ExposedPorts: []string{nginxDefaultPort},1052 WaitingFor: wait.ForListeningPort(nginxDefaultPort),1053 }1054 nginx, err := GenericContainer(ctx, GenericContainerRequest{1055 ProviderType: providerType,1056 ContainerRequest: req,1057 Started: true,1058 })1059 require.NoError(t, err)1060 terminateContainerOnEnd(t, ctx, nginx)1061}1062func TestContainerCreationWaitsForLogAndPort(t *testing.T) {1063 ctx := context.Background()1064 req := ContainerRequest{1065 Image: "docker.io/mysql:latest",1066 ExposedPorts: []string{"3306/tcp", "33060/tcp"},1067 Env: map[string]string{1068 "MYSQL_ROOT_PASSWORD": "password",1069 "MYSQL_DATABASE": "database",1070 },1071 WaitingFor: wait.ForAll(1072 wait.ForLog("port: 3306 MySQL Community Server - GPL"),1073 wait.ForListeningPort("3306/tcp"),1074 ),1075 }1076 mysqlC, err := GenericContainer(ctx, GenericContainerRequest{...

Full Screen

Full Screen

TestContainerCreationWaitsForLog

Using AI Code Generation

copy

Full Screen

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 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.Println(ip)24 fmt.Println(port.Port())25 fmt.Println("Hello World")26}

Full Screen

Full Screen

TestContainerCreationWaitsForLog

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 ExposedPorts: []string{"80/tcp"},6 Cmd: []string{"echo", "hello world"},7 WaitingFor: wait.ForLog("hello world").WithStartupTimeout(1 * time.Second),8 }9 c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{10 })11 if err != nil {12 log.Fatal(err)13 }14 ip, err := c.Host(ctx)15 if err != nil {16 log.Fatal(err)17 }18 port, err := c.MappedPort(ctx, "80")19 if err != nil {20 log.Fatal(err)21 }22 fmt.Printf("Container %s is listening on %s:%s", c.GetContainerID(), ip, port.Port())23}

Full Screen

Full Screen

TestContainerCreationWaitsForLog

Using AI Code Generation

copy

Full Screen

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 container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{9 })10 if err != nil {11 log.Fatal(err)12 }13 id, _ := container.ContainerID(ctx)14 fmt.Println("Container ID: ", id)15 ip, _ := container.Host(ctx)16 fmt.Println("Container IP: ", ip)17 port, _ := container.MappedPort(ctx, "8080/tcp")18 fmt.Println("Container Port: ", port.Int())19 name, _ := container.Name(ctx)20 fmt.Println("Container Name: ", name)21 state, _ := container.State(ctx)22 fmt.Println("Container State: ", state)23 networkSettings, _ := container.NetworkSettings(ctx)24 fmt.Println("Container Network Settings: ", networkSettings)25 exitCode, _ := container.ExitCode(ctx)26 fmt.Println("Container Exit Code: ", exitCode)27 logs, _ := container.Logs(ctx)28 fmt.Println("Container Logs: ", logs)29 stats, _ := container.Stats(ctx)30 fmt.Println("Container Stats: ", stats)31}

Full Screen

Full Screen

TestContainerCreationWaitsForLog

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.ForLog("nginx/1.17.1"),7 }8 nginxContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{9 })10 if err != nil {11 log.Fatalf("Could not start container: %s", err)12 }13 defer nginxContainer.Terminate(ctx)14 ip, err := nginxContainer.Host(ctx)15 if err != nil {16 log.Fatalf("Could not get container IP: %s", err)17 }18 mappedPort, err := nginxContainer.MappedPort(ctx, "80")19 if err != nil {20 log.Fatalf("Could not get container port: %s", err)21 }22 fmt.Printf("Container is listening on IP: %s, port: %s", ip, mappedPort.Port())23}24import (25func main() {26 ctx := context.Background()27 req := testcontainers.ContainerRequest{28 ExposedPorts: []string{"80/tcp"},29 WaitingFor: wait.ForLog("nginx/1.17.1"),30 }

Full Screen

Full Screen

TestContainerCreationWaitsForLog

Using AI Code Generation

copy

Full Screen

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

Full Screen

Full Screen

TestContainerCreationWaitsForLog

Using AI Code Generation

copy

Full Screen

1import (2 func main() {3}4 func TestContainerCreationWaitsForLog (t *testing.T) {5 ctx := context.Background()6 req := testcontainers.ContainerRequest{7 Cmd: [] string { "sh" , "-c" , "echo hello world" },8 ExposedPorts: [] string { "80/tcp" },9 WaitingFor: wait.ForLog( "hello world" ),10 }11 container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{12 })13 if err != nil {14 t.Fatal(err)15 }16 out, err := container.Exec(ctx, [] string { "echo" , "hello world" })17 if err != nil {18 t.Fatal(err)19 }20 fmt.Println(out)21 if err := container.Terminate(ctx); err != nil {22 t.Fatal(err)23 }24}

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