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

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

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

self

Using AI Code Generation

copy

Full Screen

1 def compose = new DockerComposeContainer(new File("docker-compose.yml"))2 compose.withExposedService("mongo_1", 27017, Wait.forListeningPort())3 compose.withExposedService("mongo_2", 27017, Wait.forListeningPort())4 compose.withExposedService("mongo_3", 27017, Wait.forListeningPort())5 compose.withExposedService("mongo_4", 27017, Wait.forListeningPort())6 compose.withExposedService("mongo_5", 27017, Wait.forListeningPort())7 compose.withExposedService("mongo_6", 27017, Wait.forListeningPort())8 compose.withExposedService("mongo_7", 27017, Wait.forListeningPort())9 compose.withExposedService("mongo_8", 27017, Wait.forListeningPort())10 compose.withExposedService("mongo_9", 27017, Wait.forListeningPort())11 compose.withExposedService("mongo_10", 27017, Wait.forListeningPort())12 compose.withExposedService("mongo_11", 27017, Wait.forListeningPort())13 compose.withExposedService("mongo_12", 27017, Wait.forListeningPort())14 compose.withExposedService("mongo_13", 27017, Wait.forListeningPort())15 compose.withExposedService("mongo_14", 27017, Wait.forListeningPort())16 compose.withExposedService("mongo_15", 27017, Wait.forListeningPort())17 compose.withExposedService("mongo_16", 27017, Wait.forListeningPort())18 compose.withExposedService("mongo_17", 27017, Wait.forListeningPort())19 compose.withExposedService("mongo_18", 27017, Wait.forListeningPort())20 compose.withExposedService("mongo_19", 27017, Wait.forListeningPort())21 compose.withExposedService("mongo_20", 27017, Wait.forListeningPort())22 compose.withExposedService("mongo_21", 27017, Wait.forListeningPort())23 compose.withExposedService("mongo_22", 27017, Wait.forListeningPort())24 compose.withExposedService("mongo_23", 27017, Wait.forListeningPort())25 compose.withExposedService("mongo_24", 27017, Wait.forListeningPort())26 compose.withExposedService("mongo_25", 27017, Wait.forListeningPort())27 compose.withExposedService("mongo_

Full Screen

Full Screen

self

Using AI Code Generation

copy

Full Screen

1public static void main(String[] args) {2 try {3 DockerComposeContainer environment = new DockerComposeContainer(new File("src/test/resources/docker-compose.yml"))4 .withExposedService("selenium_hub", 4444);5 environment.start();6 environment.stop();7 } catch (Exception e) {8 e.printStackTrace();9 }10}11public static void main(String[] args) {12 try {13 DockerComposeContainer environment = new DockerComposeContainer(new File("src/test/resources/docker-compose.yml"))14 .withExposedService("selenium_hub", 4444);15 environment.start();16 environment.stop();17 } catch (Exception e) {18 e.printStackTrace();19 }20}21The DockerComposeContainer class has a lot of methods. We can use these methods to start the containers in different ways. For example, we can use the start() method to start the containers and then use the stop() method to stop the containers. We can also use the start() method to

Full Screen

Full Screen

self

Using AI Code Generation

copy

Full Screen

1DockerComposeContainer compose = new DockerComposeContainer(new File("src/test/resources/docker-compose.yml"))2 .withExposedService("app_1", 8080, Wait.forHttp("/").forStatusCode(200))3 .withLocalCompose(true)4 .withPull(false)5 .withTailChildContainers(true)6 .withScaledService("app_1", 2)7 .withScaledService("app_2", 3)8 .withScaledService("app_3", 4)9 .withScaledService("app_4", 5)10 .withScaledService("app_5", 6)11 .withScaledService("app_6", 7)12 .withScaledService("app_7", 8)13 .withScaledService("app_8", 9)14 .withScaledService("app_9", 10)15 .withScaledService("app_10", 11)16 .withScaledService("app_11", 12)17 .withScaledService("app_12", 13)18 .withScaledService("app_13", 14)19 .withScaledService("app_14", 15)20 .withScaledService("app_15", 16)21 .withScaledService("app_16", 17)22 .withScaledService("app_17", 18)23 .withScaledService("app_18", 19)24 .withScaledService("app_19", 20)25 .withScaledService("app_20", 21)26 .withScaledService("app_21", 22)27 .withScaledService("app_22", 23)28 .withScaledService("app_23", 24)29 .withScaledService("app_24", 25)30 .withScaledService("app_25", 26)31 .withScaledService("app_26", 27)32 .withScaledService("app_27", 28)33 .withScaledService("app_28", 29)34 .withScaledService("app_29", 30)35 .withScaledService("app_30", 31)36 .withScaledService("app_31", 32)37 .withScaledService("app_32", 33)38 .withScaledService("app_

Full Screen

Full Screen

self

Using AI Code Generation

copy

Full Screen

1def compose = new DockerComposeContainer(new File("docker-compose.yml"))2 .withExposedService("test_1", 8080)3 .withExposedService("test_2", 8080)4 .withLocalCompose(true)5 .withPull(false)6 .withTailChildContainers(true)7 .withEnv("TEST", "TEST")8 .withEnv("TEST2", "TEST2")9 .withCommand("-f docker-compose.yml up -d")10 .withScaledService("test_1", 1)11 .withScaledService("test_2", 1)12 .withScaledService("test_3", 1)13 .withScaledService("test_4", 1)14 .withScaledService("test_5", 1)15 .withScaledService("test_6", 1)16 .withScaledService("test_7", 1)17 .withScaledService("test_8", 1)18 .withScaledService("test_9", 1)19 .withScaledService("test_10", 1)20 .withScaledService("test_11", 1)21 .withScaledService("test_12", 1)22 .withScaledService("test_13", 1)23 .withScaledService("test_14", 1)24 .withScaledService("test_15", 1)25 .withScaledService("test_16", 1)26 .withScaledService("test_17", 1)27 .withScaledService("test_18", 1)28 .withScaledService("test_19", 1)29 .withScaledService("test_20", 1)30 .withScaledService("test_21", 1)31 .withScaledService("test_22", 1)32 .withScaledService("test_23", 1)33 .withScaledService("test_24", 1)34 .withScaledService("test_25", 1)35 .withScaledService("test_26", 1)36 .withScaledService("test_27", 1)37 .withScaledService("test_28", 1)38 .withScaledService("test_29", 1)39 .withScaledService("test_30", 1)

Full Screen

Full Screen

self

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.DockerComposeContainer2import org.testcontainers.containers.wait.strategy.Wait3import org.testcontainers.containers.wait.strategy.WaitAllStrategy4import org.testcontainers.containers.wait.strategy.WaitStrategy5import org.testcontainers.containers.wait.strategy.WaitStrategyTarget6import org.testcontainers.containers.wait.strategy.WaitStrategyTargetContainer7import org.testcontainers.containers.wait.strategy.WaitStrategyTargetHostPort8import org.testcontainers.containers.wait.strategy.WaitStrategyTargetHostPortList9import org.testcontainers.containers.wait.strategy.WaitStrategyTargetHostPortListProvider10import org.testcontainers.containers.wait.strategy.WaitStrategyTargetHostPortProvider11import org.testcontainers.containers.wait.strategy.WaitStrategyTargetPortForwarding12import java.io.File13import java.lang.reflect.Method14import java.time.Duration15class DockerComposeContainerCustom<T : DockerComposeContainer<T>>(file: File) : DockerComposeContainer<T>(file) {16 companion object {17 private val setDockerComposeFileMethod: Method = DockerComposeContainer::class.java.getDeclaredMethod("setDockerComposeFile", File::class.java)18 }19 init {20 setDockerComposeFileMethod.invoke(this, file)21 }22 override fun withWaitStrategy(waitStrategy: WaitStrategy): T {23 return super.withWaitStrategy(waitStrategy)24 }25 override fun withScaledService(serviceName: String, numberOfInstances: Int): T {26 return super.withScaledService(serviceName, numberOfInstances)27 }28 override fun withScaledService(serviceName: String, numberOfInstances: Int, waitStrategy: WaitStrategy): T {29 return super.withScaledService(serviceName, numberOfInstances, waitStrategy)30 }31 override fun withScaledService(serviceName: String, numberOfInstances: Int, waitStrategy: WaitStrategy, scale: Int): T {32 return super.withScaledService(serviceName, numberOfInstances, waitStrategy, scale)33 }34 override fun withScaledService(serviceName: String, numberOfInstances: Int, waitStrategy: WaitStrategy, scale: Int, startupTimeout: Duration): T {35 return super.withScaledService(serviceName, numberOfInstances, waitStrategy, scale, startupTimeout)36 }37 override fun withScaledService(serviceName: String, numberOfInstances: Int, waitStrategy: WaitStrategy,

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