Best Testcontainers-go code snippet using testcontainers.TestLocalDockerCompose
compose_test.go
Source:compose_test.go
...69 "FOO": "foo",70 "BAR": "bar",71 })72}73func TestLocalDockerCompose(t *testing.T) {74 path := "./testresources/docker-compose-simple.yml"75 identifier := strings.ToLower(uuid.New().String())76 compose := NewLocalDockerCompose([]string{path}, identifier, WithLogger(TestLogger(t)))77 destroyFn := func() {78 err := compose.Down()79 checkIfError(t, err)80 }81 defer destroyFn()82 err := compose.83 WithCommand([]string{"up", "-d"}).84 Invoke()85 checkIfError(t, err)86}87func TestDockerComposeStrategyForInvalidService(t *testing.T) {88 path := "./testresources/docker-compose-simple.yml"89 identifier := strings.ToLower(uuid.New().String())90 compose := NewLocalDockerCompose([]string{path}, identifier, WithLogger(TestLogger(t)))91 destroyFn := func() {92 err := compose.Down()93 checkIfError(t, err)94 }95 defer destroyFn()96 err := compose.97 WithCommand([]string{"up", "-d"}).98 // Appending with _1 as given in the Java Test-Containers Example99 WithExposedService("mysql_1", 13306, wait.NewLogStrategy("started").WithStartupTimeout(10*time.Second).WithOccurrence(1)).100 Invoke()101 assert.NotEqual(t, err.Error, nil, "Expected error to be thrown because service with wait strategy is not running")102 assert.Equal(t, 1, len(compose.Services))103 assert.Contains(t, compose.Services, "nginx")104}105func TestDockerComposeWithWaitLogStrategy(t *testing.T) {106 path := "./testresources/docker-compose-complex.yml"107 identifier := strings.ToLower(uuid.New().String())108 compose := NewLocalDockerCompose([]string{path}, identifier, WithLogger(TestLogger(t)))109 destroyFn := func() {110 err := compose.Down()111 checkIfError(t, err)112 }113 defer destroyFn()114 err := compose.115 WithCommand([]string{"up", "-d"}).116 // Appending with _1 as given in the Java Test-Containers Example117 WithExposedService("mysql_1", 13306, wait.NewLogStrategy("started").WithStartupTimeout(10*time.Second).WithOccurrence(1)).118 Invoke()119 checkIfError(t, err)120 assert.Equal(t, 2, len(compose.Services))121 assert.Contains(t, compose.Services, "nginx")122 assert.Contains(t, compose.Services, "mysql")123}124func TestDockerComposeWithWaitForService(t *testing.T) {125 path := "./testresources/docker-compose-simple.yml"126 identifier := strings.ToLower(uuid.New().String())127 compose := NewLocalDockerCompose([]string{path}, identifier, WithLogger(TestLogger(t)))128 destroyFn := func() {129 err := compose.Down()130 checkIfError(t, err)131 }132 defer destroyFn()133 err := compose.134 WithCommand([]string{"up", "-d"}).135 WithEnv(map[string]string{136 "bar": "BAR",137 }).138 WaitForService("nginx_1", wait.NewHTTPStrategy("/").WithPort("80/tcp").WithStartupTimeout(10*time.Second)).139 Invoke()140 checkIfError(t, err)141 assert.Equal(t, 1, len(compose.Services))142 assert.Contains(t, compose.Services, "nginx")143}144func TestDockerComposeWithWaitHTTPStrategy(t *testing.T) {145 path := "./testresources/docker-compose-simple.yml"146 identifier := strings.ToLower(uuid.New().String())147 compose := NewLocalDockerCompose([]string{path}, identifier, WithLogger(TestLogger(t)))148 destroyFn := func() {149 err := compose.Down()150 checkIfError(t, err)151 }152 defer destroyFn()153 err := compose.154 WithCommand([]string{"up", "-d"}).155 WithEnv(map[string]string{156 "bar": "BAR",157 }).158 WithExposedService("nginx_1", 9080, wait.NewHTTPStrategy("/").WithPort("80/tcp").WithStartupTimeout(10*time.Second)).159 Invoke()160 checkIfError(t, err)161 assert.Equal(t, 1, len(compose.Services))162 assert.Contains(t, compose.Services, "nginx")163}164func TestDockerComposeWithContainerName(t *testing.T) {165 path := "./testresources/docker-compose-container-name.yml"166 identifier := strings.ToLower(uuid.New().String())167 compose := NewLocalDockerCompose([]string{path}, identifier)168 destroyFn := func() {169 err := compose.Down()170 checkIfError(t, err)171 }172 defer destroyFn()173 err := compose.174 WithCommand([]string{"up", "-d"}).175 WithEnv(map[string]string{176 "bar": "BAR",177 }).178 WithExposedService("nginxy", 9080, wait.NewHTTPStrategy("/").WithPort("80/tcp").WithStartupTimeout(10*time.Second)).179 Invoke()180 checkIfError(t, err)181 assert.Equal(t, 1, len(compose.Services))182 assert.Contains(t, compose.Services, "nginx")183}184func TestDockerComposeWithWaitStrategy_NoExposedPorts(t *testing.T) {185 path := "./testresources/docker-compose-no-exposed-ports.yml"186 identifier := strings.ToLower(uuid.New().String())187 compose := NewLocalDockerCompose([]string{path}, identifier, WithLogger(TestLogger(t)))188 destroyFn := func() {189 err := compose.Down()190 checkIfError(t, err)191 }192 defer destroyFn()193 err := compose.194 WithCommand([]string{"up", "-d"}).195 WithExposedService("nginx_1", 9080, wait.ForLog("Configuration complete; ready for start up")).196 Invoke()197 checkIfError(t, err)198 assert.Equal(t, 1, len(compose.Services))199 assert.Contains(t, compose.Services, "nginx")200}201func TestDockerComposeWithMultipleWaitStrategies(t *testing.T) {202 path := "./testresources/docker-compose-complex.yml"203 identifier := strings.ToLower(uuid.New().String())204 compose := NewLocalDockerCompose([]string{path}, identifier, WithLogger(TestLogger(t)))205 destroyFn := func() {206 err := compose.Down()207 checkIfError(t, err)208 }209 defer destroyFn()210 err := compose.211 WithCommand([]string{"up", "-d"}).212 WithExposedService("mysql_1", 13306, wait.NewLogStrategy("started").WithStartupTimeout(10*time.Second)).213 WithExposedService("nginx_1", 9080, wait.NewHTTPStrategy("/").WithPort("80/tcp").WithStartupTimeout(10*time.Second)).214 Invoke()215 checkIfError(t, err)216 assert.Equal(t, 2, len(compose.Services))217 assert.Contains(t, compose.Services, "nginx")218 assert.Contains(t, compose.Services, "mysql")219}220func TestDockerComposeWithFailedStrategy(t *testing.T) {221 path := "./testresources/docker-compose-simple.yml"222 identifier := strings.ToLower(uuid.New().String())223 compose := NewLocalDockerCompose([]string{path}, identifier, WithLogger(TestLogger(t)))224 destroyFn := func() {225 err := compose.Down()226 checkIfError(t, err)227 }228 defer destroyFn()229 err := compose.230 WithCommand([]string{"up", "-d"}).231 WithEnv(map[string]string{232 "bar": "BAR",233 }).234 WithExposedService("nginx_1", 9080, wait.NewHTTPStrategy("/").WithPort("8080/tcp").WithStartupTimeout(5*time.Second)).235 Invoke()236 // Verify that an error is thrown and not nil237 // A specific error message matcher is not asserted since the docker library can change the return message, breaking this test238 assert.NotEqual(t, err.Error, nil, "Expected error to be thrown because of a wrong suplied wait strategy")239 assert.Equal(t, 1, len(compose.Services))240 assert.Contains(t, compose.Services, "nginx")241}242func TestLocalDockerComposeComplex(t *testing.T) {243 path := "./testresources/docker-compose-complex.yml"244 identifier := strings.ToLower(uuid.New().String())245 compose := NewLocalDockerCompose([]string{path}, identifier, WithLogger(TestLogger(t)))246 destroyFn := func() {247 err := compose.Down()248 checkIfError(t, err)249 }250 defer destroyFn()251 err := compose.252 WithCommand([]string{"up", "-d"}).253 Invoke()254 checkIfError(t, err)255 assert.Equal(t, 2, len(compose.Services))256 assert.Contains(t, compose.Services, "nginx")257 assert.Contains(t, compose.Services, "mysql")258}259func TestLocalDockerComposeWithEnvironment(t *testing.T) {260 path := "./testresources/docker-compose-simple.yml"261 identifier := strings.ToLower(uuid.New().String())262 compose := NewLocalDockerCompose([]string{path}, identifier, WithLogger(TestLogger(t)))263 destroyFn := func() {264 err := compose.Down()265 checkIfError(t, err)266 }267 defer destroyFn()268 err := compose.269 WithCommand([]string{"up", "-d"}).270 WithEnv(map[string]string{271 "bar": "BAR",272 }).273 Invoke()274 checkIfError(t, err)275 assert.Equal(t, 1, len(compose.Services))276 assert.Contains(t, compose.Services, "nginx")277 containerNameNginx := compose.Identifier + "_nginx_1"278 present := map[string]string{279 "bar": "BAR",280 }281 absent := map[string]string{}282 assertContainerEnvironmentVariables(t, containerNameNginx, present, absent)283}284func TestLocalDockerComposeWithMultipleComposeFiles(t *testing.T) {285 composeFiles := []string{286 "testresources/docker-compose-simple.yml",287 "testresources/docker-compose-postgres.yml",288 "testresources/docker-compose-override.yml",289 }290 identifier := strings.ToLower(uuid.New().String())291 compose := NewLocalDockerCompose(composeFiles, identifier, WithLogger(TestLogger(t)))292 destroyFn := func() {293 err := compose.Down()294 checkIfError(t, err)295 }296 defer destroyFn()297 err := compose.298 WithCommand([]string{"up", "-d"}).299 WithEnv(map[string]string{300 "bar": "BAR",301 "foo": "FOO",302 }).303 Invoke()304 checkIfError(t, err)305 assert.Equal(t, 3, len(compose.Services))306 assert.Contains(t, compose.Services, "nginx")307 assert.Contains(t, compose.Services, "mysql")308 assert.Contains(t, compose.Services, "postgres")309 containerNameNginx := compose.Identifier + "_nginx_1"310 present := map[string]string{311 "bar": "BAR",312 "foo": "FOO",313 }314 absent := map[string]string{}315 assertContainerEnvironmentVariables(t, containerNameNginx, present, absent)316}317func TestLocalDockerComposeWithVolume(t *testing.T) {318 path := "./testresources/docker-compose-volume.yml"319 identifier := strings.ToLower(uuid.New().String())320 compose := NewLocalDockerCompose([]string{path}, identifier, WithLogger(TestLogger(t)))321 destroyFn := func() {322 err := compose.Down()323 checkIfError(t, err)324 assertVolumeDoesNotExist(t, fmt.Sprintf("%s_mydata", identifier))325 }326 defer destroyFn()327 err := compose.328 WithCommand([]string{"up", "-d"}).329 Invoke()330 checkIfError(t, err)331}...
TestLocalDockerCompose
Using AI Code Generation
1import (2func TestLocalDockerCompose(t *testing.T) {3 compose := testcontainers.NewLocalDockerCompose([]string{"docker-compose.yml"}, "test")4 ctx := context.Background()5 compose.WithCommand([]string{"up", "-d"})6 err := compose.Invoke(ctx)7 if err != nil {8 log.Fatal(err)9 }10 req := testcontainers.ContainerRequest{11 ExposedPorts: []string{"80/tcp"},12 Cmd: []string{"echo", "test"},13 WaitingFor: wait.ForLog("test"),14 }15 c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{16 })17 if err != nil {18 log.Fatal(err)19 }20 ip, err := c.Host(ctx)21 if err != nil {22 log.Fatal(err)23 }24 port, err := c.MappedPort(ctx, "80")25 if err != nil {26 log.Fatal(err)27 }28 fmt.Println(ip)29 fmt.Println(port.Int())30 err = compose.Down(ctx)31 if err != nil {32 log.Fatal(err)33 }34}35import (36func TestLocalDockerCompose(t *testing.T) {37 compose := testcontainers.NewLocalDockerCompose([]string{"docker-compose.yml"}, "test")38 ctx := context.Background()39 compose.WithCommand([]string{"up", "-d"})40 err := compose.Invoke(ctx)41 if err != nil {42 log.Fatal(err)43 }44 req := testcontainers.ContainerRequest{45 ExposedPorts: []string{"80/tcp"},
TestLocalDockerCompose
Using AI Code Generation
1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 ExposedPorts: []string{"27017/tcp"},6 WaitingFor: wait.ForListeningPort("27017/tcp"),7 }8 mongo, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{9 })10 if err != nil {11 log.Fatal(err)12 }13 defer mongo.Terminate(ctx)14 ip, err := mongo.Host(ctx)15 if err != nil {16 log.Fatal(err)17 }18 port, err := mongo.MappedPort(ctx, "27017")19 if err != nil {20 log.Fatal(err)21 }22 fmt.Println("host", ip)23 fmt.Println("port", port.Int())24 time.Sleep(5 * time.Minute)25}
TestLocalDockerCompose
Using AI Code Generation
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 Env: map[string]string{8 },9 }10 postgres, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{11 })12 if err != nil {13 log.Fatalf("Could not start container: %v", err)14 }15 defer postgres.Terminate(ctx)16 ip, err := postgres.Host(ctx)17 if err != nil {18 log.Fatalf("Could not get container IP: %v", err)19 }20 port, err := postgres.MappedPort(ctx, "5432")21 if err != nil {22 log.Fatalf("Could not get mapped port: %v", err)23 }24 fmt.Printf("postgres is available on %s:%s\n", ip, port.Port())25 time.Sleep(10 * time.Second)26}
TestLocalDockerCompose
Using AI Code Generation
1import (2func TestLocalDockerCompose(t *testing.T) {3 ctx := context.Background()4 compose := testcontainers.NewLocalDockerCompose([]string{"docker-compose.yml"}, "compose")5 err := compose.WithCommand([]string{"up", "-d"}).Invoke()6 if err != nil {7 log.Fatalf("Could not start compose: %s", err)8 }9 defer compose.WithCommand([]string{"down"}).Invoke()10 req := testcontainers.ContainerRequest{11 ExposedPorts: []string{"9200/tcp"},12 WaitingFor: wait.ForLog("started"),13 }14 elasticContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{15 })16 if err != nil {17 log.Fatalf("Could not start container: %s", err)18 }19 defer elasticContainer.Terminate(ctx)20 mappedPort, err := elasticContainer.MappedPort(ctx, "9200")21 if err != nil {22 log.Fatalf("Could not get port: %s", err)23 }24 if err != nil {25 log.Fatalf("Could not make request: %s", err)26 }27 defer resp.Body.Close()28 body, err := ioutil.ReadAll(resp.Body)29 if err != nil {30 log.Fatalf("Could not read response: %s", err)31 }32 fmt.Println(string(body))33}34import (35func TestLocalDockerCompose(t *testing.T) {36 ctx := context.Background()
TestLocalDockerCompose
Using AI Code Generation
1import (2func main() {3 ctx := context.Background()4 req := testcontainers.ContainerRequest{5 ExposedPorts: []string{"5432/tcp"},6 WaitingFor: wait.ForListeningPort("5432/tcp"),7 Env: map[string]string{8 },9 }10 postgres, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{11 })12 if err != nil {13 log.Fatal(err)14 }15 ip, err := postgres.Host(ctx)16 if err != nil {17 log.Fatal(err)18 }19 port, err := postgres.MappedPort(ctx, "5432/tcp")20 if err != nil {21 log.Fatal(err)22 }23 fmt.Println(ip, port.Int())24 err = postgres.Terminate(ctx)25 if err != nil {26 log.Fatal(err)27 }28}29import (30func main() {31 ctx := context.Background()32 req := testcontainers.ContainerRequest{33 ExposedPorts: []string{"5432/tcp"},34 WaitingFor: wait.ForListeningPort("5432/tcp"),35 Env: map[string]string{36 },37 }38 postgres, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{39 })40 if err != nil {41 log.Fatal(err)42 }43 ip, err := postgres.Host(ctx)44 if err != nil {45 log.Fatal(err)46 }47 port, err := postgres.MappedPort(ctx, "5432/tcp")48 if err != nil {49 log.Fatal(err)50 }51 fmt.Println(ip, port.Int())
TestLocalDockerCompose
Using AI Code Generation
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 db, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{9 })10 if err != nil {11 log.Fatal(err)12 }13 defer db.Terminate(ctx)14 ip, err := db.Host(ctx)15 if err != nil {16 log.Fatal(err)17 }18 port, err := db.MappedPort(ctx, "5432")19 if err != nil {20 log.Fatal(err)21 }22 fmt.Println(connStr)23}24import (25func main() {26 ctx := context.Background()27 req := testcontainers.ContainerRequest{28 ExposedPorts: []string{"5432/tcp"},29 WaitingFor: wait.ForLog("database system is ready to accept connections"),30 }31 db, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{32 })33 if err != nil {34 log.Fatal(err)35 }36 defer db.Terminate(ctx)37 ip, err := db.Host(ctx)38 if err != nil {39 log.Fatal(err)40 }
TestLocalDockerCompose
Using AI Code Generation
1func TestLocalDockerCompose(t *testing.T) {2 ctx := context.Background()3 compose := testcontainers.NewLocalDockerCompose([]string{"docker-compose.yml"}, "test")4 _, err := compose.WithCommand([]string{"up", "-d"}).Invoke()5 if err != nil {6 log.Fatalf("Failed to start docker compose: %v", err)7 }8 defer compose.WithCommand([]string{"down"}).Invoke()9 container, err := compose.Container(ctx, "test")10 if err != nil {11 log.Fatalf("Failed to get container: %v", err)12 }13 host, err := container.Host(ctx)14 if err != nil {15 log.Fatalf("Failed to get host: %v", err)16 }17 port, err := container.MappedPort(ctx, "8080")18 if err != nil {19 log.Fatalf("Failed to get port: %v", err)20 }21 client := &http.Client{}22 if err != nil {23 log.Fatalf("Failed to create request: %v", err)24 }25 res, err := client.Do(req)26 if err != nil {27 log.Fatalf("Failed to do request: %v", err)28 }29 body, err := ioutil.ReadAll(res.Body)30 if err != nil {31 log.Fatalf("Failed to read body: %v", err)32 }33 res.Body.Close()34 fmt.Println(string(body))35}
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!!