How to use starting method of org.testcontainers.containers.DockerComposeContainer class

Best Testcontainers-java code snippet using org.testcontainers.containers.DockerComposeContainer.starting

Source:ContainerTestSuite.java Github

copy

Full Screen

1/**2 * Copyright © 2016-2022 The Thingsboard Authors3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package org.thingsboard.server.msa;17import lombok.extern.slf4j.Slf4j;18import org.apache.commons.io.FileUtils;19import org.junit.ClassRule;20import org.junit.extensions.cpsuite.ClasspathSuite;21import org.junit.runner.RunWith;22import org.testcontainers.containers.DockerComposeContainer;23import org.testcontainers.containers.wait.strategy.Wait;24import java.io.File;25import java.io.IOException;26import java.nio.charset.StandardCharsets;27import java.time.Duration;28import java.util.UUID;29import static org.hamcrest.CoreMatchers.containsString;30import static org.hamcrest.CoreMatchers.is;31import static org.hamcrest.CoreMatchers.not;32import static org.hamcrest.MatcherAssert.assertThat;33import static org.junit.Assert.fail;34@RunWith(ClasspathSuite.class)35@ClasspathSuite.ClassnameFilters({"org.thingsboard.server.msa.*Test"})36@Slf4j37public class ContainerTestSuite {38 private static final String SOURCE_DIR = "./../../docker/";39 private static final String TB_CORE_LOG_REGEXP = ".*Starting polling for events.*";40 private static final String TRANSPORTS_LOG_REGEXP = ".*Going to recalculate partitions.*";41 private static DockerComposeContainer<?> testContainer;42 @ClassRule43 public static ThingsBoardDbInstaller installTb = new ThingsBoardDbInstaller();44 @ClassRule45 public static DockerComposeContainer getTestContainer() {46 if (testContainer == null) {47 boolean skipTailChildContainers = Boolean.valueOf(System.getProperty("blackBoxTests.skipTailChildContainers"));48 try {49 final String targetDir = FileUtils.getTempDirectoryPath() + "/" + "ContainerTestSuite-" + UUID.randomUUID() + "/";50 log.info("targetDir {}", targetDir);51 FileUtils.copyDirectory(new File(SOURCE_DIR), new File(targetDir));52 replaceInFile(targetDir + "docker-compose.yml", " container_name: \"${LOAD_BALANCER_NAME}\"", "", "container_name");53 class DockerComposeContainerImpl<SELF extends DockerComposeContainer<SELF>> extends DockerComposeContainer<SELF> {54 public DockerComposeContainerImpl(File... composeFiles) {55 super(composeFiles);56 }57 @Override58 public void stop() {59 super.stop();60 tryDeleteDir(targetDir);61 }62 }63 testContainer = new DockerComposeContainerImpl<>(64 new File(targetDir + "docker-compose.yml"),65 new File(targetDir + "docker-compose.postgres.yml"),66 new File(targetDir + "docker-compose.postgres.volumes.yml"),67 new File(targetDir + "docker-compose.kafka.yml"))68 .withPull(false)69 .withLocalCompose(true)70 .withTailChildContainers(!skipTailChildContainers)71 .withEnv(installTb.getEnv())72 .withEnv("LOAD_BALANCER_NAME", "")73 .withExposedService("haproxy", 80, Wait.forHttp("/swagger-ui.html").withStartupTimeout(Duration.ofSeconds(400)))74 .waitingFor("tb-core1", Wait.forLogMessage(TB_CORE_LOG_REGEXP, 1).withStartupTimeout(Duration.ofSeconds(400)))75 .waitingFor("tb-core2", Wait.forLogMessage(TB_CORE_LOG_REGEXP, 1).withStartupTimeout(Duration.ofSeconds(400)))76 .waitingFor("tb-http-transport1", Wait.forLogMessage(TRANSPORTS_LOG_REGEXP, 1).withStartupTimeout(Duration.ofSeconds(400)))77 .waitingFor("tb-http-transport2", Wait.forLogMessage(TRANSPORTS_LOG_REGEXP, 1).withStartupTimeout(Duration.ofSeconds(400)))78 .waitingFor("tb-mqtt-transport1", Wait.forLogMessage(TRANSPORTS_LOG_REGEXP, 1).withStartupTimeout(Duration.ofSeconds(400)))79 .waitingFor("tb-mqtt-transport2", Wait.forLogMessage(TRANSPORTS_LOG_REGEXP, 1).withStartupTimeout(Duration.ofSeconds(400)));80 } catch (Exception e) {81 log.error("Failed to create test container", e);82 fail("Failed to create test container");83 }84 }85 return testContainer;86 }87 private static void tryDeleteDir(String targetDir) {88 try {89 log.info("Trying to delete temp dir {}", targetDir);90 FileUtils.deleteDirectory(new File(targetDir));91 } catch (IOException e) {92 log.error("Can't delete temp directory " + targetDir, e);93 }94 }95 /**96 * This workaround is actual until issue will be resolved:97 * Support container_name in docker-compose file #2472 https://github.com/testcontainers/testcontainers-java/issues/247298 * docker-compose files which contain container_name are not supported and the creation of DockerComposeContainer fails due to IllegalStateException.99 * This has been introduced in #1151 as a quick fix for unintuitive feedback. https://github.com/testcontainers/testcontainers-java/issues/1151100 * Using the latest testcontainers and waiting for the fix...101 * */102 private static void replaceInFile(String sourceFilename, String target, String replacement, String verifyPhrase) {103 try {104 File file = new File(sourceFilename);105 String sourceContent = FileUtils.readFileToString(file, StandardCharsets.UTF_8);106 String outputContent = sourceContent.replace(target, replacement);107 assertThat(outputContent, (not(containsString(target))));108 assertThat(outputContent, (not(containsString(verifyPhrase))));109 FileUtils.writeStringToFile(file, outputContent, StandardCharsets.UTF_8);110 assertThat(FileUtils.readFileToString(file, StandardCharsets.UTF_8), is(outputContent));111 } catch (IOException e) {112 log.error("failed to update file " + sourceFilename, e);113 fail("failed to update file");114 }115 }116}...

Full Screen

Full Screen

Source:TestCase.java Github

copy

Full Screen

...33 private static boolean notInitialized = true;34 @Rule35 public TestRule testNameLogger = new TestWatcher() {36 @Override37 protected void starting(final Description description) {38 LOGGER.info("----------------------------------------------------------------------------------");39 LOGGER.info("Starting test: {}::{}", getTestCaseName(description), description.getMethodName());40 LOGGER.info("----------------------------------------------------------------------------------");41 }42 @Override43 protected void failed(final Throwable e, final Description description) {44 LOGGER.info("----------------------------------------------------------------------------------");45 String testCaseName = getTestCaseName(description);46 LOGGER.info("TEST FAILED ({}::{}) ", testCaseName, description.getMethodName(), e);47 LOGGER.info("----------------------------------------------------------------------------------");48 }49 @Override50 protected void succeeded(final Description description) {51 LOGGER.info("----------------------------------------------------------------------------------");...

Full Screen

Full Screen

starting

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.DockerComposeContainer;2import java.io.File;3import org.testcontainers.containers.output.Slf4jLogConsumer;4import org.slf4j.Logger;5import org.slf4j.LoggerFactory;6import org.testcontainers.containers.wait.strategy.Wait;7import org.testcontainers.containers.wait.strategy.WaitAllStrategy;8import org.testcontainers.containers.wait.strategy.WaitStrategy;9import org.testcontainers.containers.wait.strategy.WaitStrategyTarget;10import org.testcontainers.containers.wait.strategy.WaitStrategyTarget.ContainerWaitStrategyTarget;11import java.util.concurrent.TimeUnit;12import java.util.List;13import java.util.ArrayList;14import java.util.Arrays;15import java.util.stream.Collectors;16import java.util.stream.Stream;17import java.util.function.Supplier;18import java.util.concurrent.TimeUnit;19import java.util.concurrent.TimeoutException;20import java.util.concurrent.atomic.AtomicReference;21import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;22import org.testcontainers.containers.wait.strategy.WaitStrategyTarget;23import org.testcontainers.containers.wait.strategy.WaitStrategyTarget.ContainerWaitStrategyTarget;24import org.testcontainers.containers.wait.strategy.WaitAllStrategy;25import org.testcontainers.containers.wait.strategy.WaitAllStrategy.WaitAllStrategyBuilder;26import org.testcontainers.containers.wait.strategy.WaitAllStrategy.WaitAllStrategyTarget;27import org.testcontainers.containers.wait.strategy.WaitAllStrategy.WaitAllStrategyTarget.ContainerWaitAllStrategyTarget;28import org.testcontainers.containers.wait.strategy.WaitAllStrategy.WaitAllStrategyTarget.ServiceWaitAllStrategyTarget;29import org.testcontainers.containers.wait.strategy.WaitAllStrategy.WaitAllStrategyTarget.ServiceWaitAllStrategyTarget.ServiceWaitAllStrategyTargetBuilder;30import org.testcontainers.containers.wait.strategy.WaitAllStrategy.WaitAllStrategyTarget.ServiceWaitAllStrategyTarget.ServiceWaitAllStrategyTargetBuilder.ServiceWaitAllStrategyTargetBuilderWithService;31import org.testcontainers.containers.wait.strategy.WaitAllStrategy.WaitAllStrategyTarget.ServiceWaitAllStrategyTarget.ServiceWaitAllStrategyTargetBuilder.ServiceWaitAllStrategyTargetBuilderWithService.ServiceWaitAllStrategyTargetBuilderWithServiceAndPort;32import org.testcontainers.containers.wait.strategy.WaitAllStrategy.WaitAllStrategyTarget.ServiceWaitAllStrategyTarget.ServiceWaitAllStrategyTargetBuilder.ServiceWaitAllStrategyTargetBuilderWithService.ServiceWaitAllStrategyTargetBuilderWithServiceAndPort.ServiceWaitAllStrategyTargetBuilderWithServiceAndPortAndProtocol;33import org.testcontainers.containers.wait.strategy.WaitAllStrategy.WaitAllStrategyTarget.ServiceWaitAllStrategyTarget.ServiceWaitAllStrategyTargetBuilder.ServiceWaitAllStrategyTargetBuilderWithService.ServiceWaitAllStrategyTargetBuilderWithServiceAnd

Full Screen

Full Screen

starting

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.containers;2import org.junit.Test;3import org.testcontainers.containers.output.Slf4jLogConsumer;4import org.testcontainers.containers.wait.strategy.Wait;5import org.testcontainers.containers.wait.strategy.WaitAllStrategy;6import org.testcontainers.containers.wait.strategy.WaitStrategy;7import org.testcontainers.utility.MountableFile;8import java.io.File;9import java.nio.file.Paths;10import java.time.Duration;11import java.util.HashMap;12import java.util.Map;13import static org.testcontainers.containers.DockerComposeContainer.*;14public class DockerComposeContainerTest {15 public void testDockerComposeContainer() {16 File composeFile = Paths.get("src", "test", "resources", "docker-compose.yml").toFile();17 DockerComposeContainer composeContainer = new DockerComposeContainer(composeFile)18 .withExposedService("test_1", 8080)19 .withExposedService("test_2", 8080)20 .withExposedService("test_3", 8080)21 .withExposedService("test_4", 8080)22 .withExposedService("test_5", 8080)23 .withExposedService("test_6", 8080)24 .withExposedService("test_7", 8080)25 .withExposedService("test_8", 8080)26 .withExposedService("test_9", 8080)27 .withExposedService("test_10", 8080)28 .withExposedService("test_11", 8080)29 .withExposedService("test_12", 8080)30 .withExposedService("test_13", 8080)31 .withExposedService("test_14", 8080)32 .withExposedService("test_15", 8080)33 .withExposedService("test_16", 8080)34 .withExposedService("test_17", 8080)35 .withExposedService("test_18", 8080)36 .withExposedService("test_19", 8080)37 .withExposedService("test_20", 8080)38 .withExposedService("test_21", 8080)39 .withExposedService("test_22", 8080)40 .withExposedService("test_23", 8080)41 .withExposedService("test_24",

Full Screen

Full Screen

starting

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.containers;2import org.testcontainers.containers.output.Slf4jLogConsumer;3import org.testcontainers.containers.wait.strategy.Wait;4import org.testcontainers.containers.wait.strategy.WaitAllStrategy;5import org.testcontainers.containers.wait.strategy.WaitStrategyTarget;6import org.testcontainers.utility.LogUtils;7import java.time.Duration;8import java.util.Map;9public class DockerComposeContainer<SELF extends DockerComposeContainer<SELF>> extends GenericContainer<SELF> implements WaitStrategyTarget {10 private DockerComposeContainer(DockerComposeContainerConfiguration configuration) {11 super(configuration.getImage());12 withLogConsumer(new Slf4jLogConsumer(LogUtils.getLogger()));13 withEnv("COMPOSE_HTTP_TIMEOUT", "200");14 withEnv("COMPOSE_PROJECT_NAME", configuration.getServiceName());15 withCommand("up", "-d");16 withWorkingDirectory("/tmp");17 withFileSystemBind(configuration.getEnvFile().getParent().toString(), "/tmp", BindMode.READ_WRITE);18 waitingFor(new WaitAllStrategy().withStrategy(new DockerComposeContainer.LocalPortCheck(configuration.getServiceName(), configuration.getExposedServicePort())));19 }20 public static DockerComposeContainer from(String envFile) {21 return new DockerComposeContainer(DockerComposeContainerConfiguration.fromEnvFile(envFile));22 }23 public Map<Integer, Integer> getExposedPorts() {24 return getContainerInfo().getNetworkSettings().getPorts().getBindings();25 }26 public String getContainerIpAddress() {27 return getContainerInfo().getNetworkSettings().getIpAddress();28 }29 public Integer getMappedPort(int originalPort) {30 return getExposedPorts().get(originalPort)[0].getHostPort();31 }32 public SELF waitingFor(WaitStrategy waitStrategy) {33 return super.waitingFor(waitStrategy);34 }35 public SELF waitingFor(WaitStrategy waitStrategy, WaitStrategy... additionalWaitStrategies) {36 return super.waitingFor(waitStrategy, additionalWaitStrategies);37 }38 public SELF withStartupTimeout(Duration startupTimeout) {39 return super.withStartupTimeout(startupTimeout);40 }41 public SELF withCommand(String... command) {42 return super.withCommand(command);43 }44 public SELF withExposedPorts(int... ports) {45 return super.withExposedPorts(ports);46 }

Full Screen

Full Screen

starting

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.DockerComposeContainer;2import java.io.File;3import java.util.concurrent.TimeUnit;4public class 1 {5 public static void main(String[] args) throws Exception {6 File composeFile = new File("docker-compose.yml");7 DockerComposeContainer container = new DockerComposeContainer(composeFile);8 container.start();9 TimeUnit.SECONDS.sleep(60);10 container.stop();11 }12}13import org.testcontainers.containers.DockerComposeContainer;14import java.io.File;15import java.util.concurrent.TimeUnit;16public class 2 {17 public static void main(String[] args) throws Exception {18 File composeFile = new File("docker-compose.yml");19 DockerComposeContainer container = new DockerComposeContainer(composeFile);20 container.start();21 TimeUnit.SECONDS.sleep(60);22 container.stop();23 }24}25import org.testcontainers.containers.DockerComposeContainer;26import java.io.File;27import java.util.concurrent.TimeUnit;28public class 3 {29 public static void main(String[] args) throws Exception {30 File composeFile = new File("docker-compose.yml");31 DockerComposeContainer container = new DockerComposeContainer(composeFile);32 container.start();33 TimeUnit.SECONDS.sleep(60);34 container.stop();35 }36}37import org.testcontainers.containers.DockerComposeContainer;38import java.io.File;39import java.util.concurrent.TimeUnit;40public class 4 {41 public static void main(String[] args) throws Exception {42 File composeFile = new File("docker-compose.yml");43 DockerComposeContainer container = new DockerComposeContainer(composeFile);44 container.start();45 TimeUnit.SECONDS.sleep(60);46 container.stop();47 }48}49import org.testcontainers.containers.DockerComposeContainer;50import java.io.File;51import java.util.concurrent.TimeUnit;52public class 5 {53 public static void main(String[] args) throws Exception {54 File composeFile = new File("docker-compose.yml");55 DockerComposeContainer container = new DockerComposeContainer(composeFile);

Full Screen

Full Screen

starting

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.containers;2import org.junit.Test;3import org.testcontainers.containers.DockerComposeContainer;4import java.io.File;5public class DockerComposeContainerTest {6 public void test() {7 File dockerComposeFile = new File("/home/ajay/Downloads/docker-compose.yml");8 DockerComposeContainer container = new DockerComposeContainer(dockerComposeFile)9 .withExposedService("redis_1", 6379)10 .withExposedService("redis_2", 6379)11 .withExposedService("redis_3", 6379);12 container.start();13 }14}15package org.testcontainers.containers;16import org.junit.Test;17import org.testcontainers.containers.GenericContainer;18public class GenericContainerTest {19 public void test() {20 GenericContainer container = new GenericContainer("redis:3.0.2")21 .withExposedPorts(6379);22 container.start();23 }24}25package org.testcontainers.containers;26import org.junit.Test;27import org.testcontainers.containers.JdbcDatabaseContainer;28public class JdbcDatabaseContainerTest {29 public void test() {30 JdbcDatabaseContainer container = new JdbcDatabaseContainer("postgres:9.4.1212")31 .withUsername("postgres")32 .withPassword("postgres")33 .withExposedPorts(5432);34 container.start();35 }36}37package org.testcontainers.containers;38import org.junit.Test;39import org.testcontainers.containers.MySQLContainer;40public class MySQLContainerTest {41 public void test() {42 MySQLContainer container = new MySQLContainer("mysql:5.7.21")43 .withUsername("mysql")44 .withPassword("mysql")45 .withDatabaseName("mysql")46 .withExposedPorts(3306);47 container.start();48 }49}50package org.testcontainers.containers;51import org.junit.Test;52import org.testcontainers.containers

Full Screen

Full Screen

starting

Using AI Code Generation

copy

Full Screen

1package org.testcontainers;2import org.testcontainers.containers.DockerComposeContainer;3public class DockerComposeContainerDemo {4 public static void main(String[] args) {5 DockerComposeContainer dockerComposeContainer = new DockerComposeContainer(new java.io.File("docker-compose.yml"));6 dockerComposeContainer.start();7 }8}9package org.testcontainers;10import org.testcontainers.containers.DockerComposeContainer;11public class DockerComposeContainerDemo {12 public static void main(String[] args) {13 DockerComposeContainer dockerComposeContainer = new DockerComposeContainer(new java.io.File("docker-compose.yml"));14 dockerComposeContainer.withLocalCompose(true);15 dockerComposeContainer.start();16 }17}18package org.testcontainers;19import org.testcontainers.containers.DockerComposeContainer;20public class DockerComposeContainerDemo {21 public static void main(String[] args) {22 DockerComposeContainer dockerComposeContainer = new DockerComposeContainer(new java.io.File("docker-compose.yml"));23 dockerComposeContainer.withScaledService("service-name", 3);24 dockerComposeContainer.start();25 }26}27package org.testcontainers;28import org.testcontainers.containers.DockerComposeContainer;29public class DockerComposeContainerDemo {30 public static void main(String[] args) {31 DockerComposeContainer dockerComposeContainer = new DockerComposeContainer(new java.io.File("docker-compose.yml"));32 dockerComposeContainer.withScaledService("service-name", 3);33 dockerComposeContainer.start();34 }35}36package org.testcontainers;37import org.testcontainers.containers.DockerComposeContainer;38public class DockerComposeContainerDemo {39 public static void main(String[] args) {40 DockerComposeContainer dockerComposeContainer = new DockerComposeContainer(new java.io.File("docker-compose.yml"));41 dockerComposeContainer.withScaledService("service-name", 3);42 dockerComposeContainer.start();43 }44}

Full Screen

Full Screen

starting

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.containers;2import org.junit.Test;3public class ComposeTest {4 public void testCompose() {5 DockerComposeContainer compose = new DockerComposeContainer(new File("docker-compose.yml"))6 compose.start();7 }8}9package org.testcontainers.containers;10import org.junit.Test;11public class ComposeTest {12 public void testCompose() {13 DockerComposeContainer compose = new DockerComposeContainer(new File("docker-compose.yml"))14 compose.start();15 }16}17package org.testcontainers.containers;18import org.junit.Test;19public class ComposeTest {20 public void testCompose() {21 DockerComposeContainer compose = new DockerComposeContainer(new File("docker-compose.yml"))22 compose.start();23 }24}25package org.testcontainers.containers;26import org.junit.Test;27public class ComposeTest {28 public void testCompose() {29 DockerComposeContainer compose = new DockerComposeContainer(new File("docker-compose.yml"))30 compose.start();31 }32}33package org.testcontainers.containers;34import org.junit.Test;35public class ComposeTest {36 public void testCompose() {37 DockerComposeContainer compose = new DockerComposeContainer(new File("docker-compose.yml"))38 compose.start();39 }40}41package org.testcontainers.containers;42import org.junit.Test;

Full Screen

Full Screen

starting

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.DockerComposeContainer;2import org.testcontainers.containers.wait.strategy.Wait;3import java.io.File;4public class Test {5 public static void main(String[] args) {6 DockerComposeContainer container = new DockerComposeContainer(new File("docker-compose.yml"))7 .withLocalCompose(true)8 .withExposedService("my_service_1", 8080, Wait.forListeningPort())9 .withExposedService("my_service_2", 8080, Wait.forListeningPort());10 container.start();11 System.out.println("Service 1 is running at: " + container.getServiceHost("my_service_1", 8080));12 System.out.println("Service 2 is running at: " + container.getServiceHost("my_service_2", 8080));13 container.stop();14 }15}

Full Screen

Full Screen

starting

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.containers;2import org.junit.Test;3import org.testcontainers.containers.DockerComposeContainer;4import java.io.File;5public class TestContainer {6public void test() {

Full Screen

Full Screen

starting

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.DockerComposeContainer;2import java.io.File;3import java.util.ArrayList;4import java.util.List;5public class 1 {6 public static void main(String[] args) {7 File composeFile = new File("docker-compose.yml");8 List<String> services = new ArrayList<>();9 services.add("service1");10 services.add("service2");11 services.add("service3");12 DockerComposeContainer container = new DockerComposeContainer(composeFile).withServices(services);13 container.start();14 }15}

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful