Best Testcontainers-java code snippet using org.testcontainers.containers.DockerComposeContainer.logger
Source:IntegrationTests.java
...34 * src/test/resources/docker-compose.yml)35 */36@Category(RequiresDockerServer.class)37public abstract class IntegrationTests {38 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());39 private static boolean startContainers = Optional.ofNullable(System.getenv("START_CONTAINERS"))40 .map(Boolean::valueOf).orElse(true);41 @ClassRule42 public static DockerComposeContainer environment = new Supplier<DockerComposeContainer>() {43 public DockerComposeContainer get() {44 if (startContainers) {45 DockerComposeContainer container = null;46 try {47 container = new DockerComposeContainer(48 new File("target/test-classes/docker-compose.yml"))49 .withExposedService("owner", 8082,50 Wait.forLogMessage(51 // "^.+connected with WoN52 // node:.+https://wonnode:8443/won/resource.*$",53 "^.+connected with WoN node:.+$",54 1)55 .withStartupTimeout(Duration56 .ofSeconds(120)))57 .waitingFor("matcher_service",58 Wait.forLogMessage(59 "^.+\\[akka://ClusterSystem/user/WonNodeControllerActor\\] registered won node .+ and start crawling it.+$",60 1))61 .withLocalCompose(true);62 } catch (Exception e) {63 return null;64 }65 return container;66 } else {67 return null;68 }69 }70 }.get();71 static {72 // the containers in our docker-compose file must be reachable from outside the73 // docker environment74 // by the names they have in the compose config (actually only needed for75 // wonnode, but for completeness,76 // we map all)77 DnsMappingAdder.addDnsMapping("owner", "127.0.0.1");78 DnsMappingAdder.addDnsMapping("wonnode", "127.0.0.1");79 DnsMappingAdder.addDnsMapping("postgres", "127.0.0.1");80 DnsMappingAdder.addDnsMapping("bigdata", "127.0.0.1");81 }82 @BeforeClass83 public static void checkDockerAvailable() {84 Assume.assumeTrue(isDockerAvailable());85 }86 private static boolean isDockerAvailable() {87 try {88 DockerClientFactory.instance().client();89 return true;90 } catch (Throwable ex) {91 return false;92 }93 }94 @BeforeClass95 public static void logContainerStartupMessage() {96 if (startContainers) {97 logger.info("Running tests against containers managed by testcontainers framework");98 logger.info("To run against externally managed containers, set environment variable 'START_CONTAINERS' to 'false'");99 } else {100 logger.info("Running against externally managed containers as environment variable 'START_CONTAINERS' is 'false'");101 }102 }103 @Test(timeout = 10 * 1000)104 public void testOwnerReachable() throws Exception {105 CloseableHttpClient httpclient = getHttpClientThatTrustsAnyCert();106 HttpGet httpget = new HttpGet("https://owner:8082/owner");107 try (CloseableHttpResponse response = httpclient.execute(httpget)) {108 int statusCode = response.getStatusLine().getStatusCode();109 Assert.assertTrue(200 <= statusCode && 300 > statusCode);110 } finally {111 httpclient.close();112 }113 }114 @Test(timeout = 10 * 1000)...
Source:TestContainersFactory.java
1/*2 * MIT License3 *4 * Copyright (c) 2020 engineer365.org5 *6 * Permission is hereby granted, free of charge, to any person obtaining a copy7 * of this software and associated documentation files (the "Software"), to deal8 * in the Software without restriction, including without limitation the rights9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell10 * copies of the Software, and to permit persons to whom the Software is11 * furnished to do so, subject to the following conditions:12 *13 * The above copyright notice and this permission notice shall be included in all14 * copies or substantial portions of the Software.15 *16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE22 * SOFTWARE.23 */24package org.engineer365.test;25import java.io.File;26import org.engineer365.common.misc.ClassHelper;27import org.slf4j.Logger;28import org.slf4j.LoggerFactory;29import org.testcontainers.DockerClientFactory;30import org.testcontainers.containers.DockerComposeContainer;31import org.testcontainers.containers.output.Slf4jLogConsumer;32import org.testcontainers.containers.wait.strategy.Wait;33import com.github.dockerjava.api.DockerClient;34import com.github.dockerjava.api.command.PruneCmd;35import com.github.dockerjava.api.model.PruneResponse;36import com.github.dockerjava.api.model.PruneType;37import lombok.extern.slf4j.Slf4j;38/**39 * éææµè¯ï¼testcontainers + docker-compose40 *41 *42 * TODOï¼43 * 1) èèå»æ端å£å·ç被hardcodeç常é44 * 2) èèhttps://github.com/Playtika/testcontainers-spring-boot45 */46@lombok.Getter47@lombok.Setter48@Slf4j49public class TestContainersFactory {50 public static final TestContainersFactory DEFAULT = new TestContainersFactory();51 int serverPort = 8080;52 int serverManagePort = 8081;53 String serverHealthEndpoint = "/manage/health";54 final Logger serverLogger = LoggerFactory.getLogger(ClassHelper.parseNameSuffix(getClass()) + ".server");55 int mySQLPort = 3306;56 final Logger mysqlLogger = LoggerFactory.getLogger(ClassHelper.parseNameSuffix(getClass()) + ".mysql");57 static {58 pruneDocker();59 }60 /**61 *62 * æ¸
çdockerã63 *64 * å¦æ <Ctrl>+C 强è¡ä¸æ¢æ£å¨æ§è¡çæµè¯,65 * æµè¯æ¶æå建çdocker networkå°±ä¸ä¼è¢«èªå¨éæ¾ï¼ä½æ¯docker networké»è®¤åªè½å建30个ï¼66 * æ以testæ§è¡è¾å¤æ¬¡æ°ä»¥åï¼docker networkä¼èå°½ã67 * 类似çè¿æ容å¨åvolumeçï¼æ以ï¼æ们éè¦éæ¶æ¸
ç68 */69 public synchronized static void pruneDocker() {70 DockerClient client = DockerClientFactory.instance().client();71 try (PruneCmd cmd = client.pruneCmd(PruneType.NETWORKS);) {72 PruneResponse resp = cmd.exec();73 log.info("prune docker network prune: " + resp);74 }75 try (PruneCmd cmd = client.pruneCmd(PruneType.BUILD);) {76 PruneResponse resp = cmd.exec();77 log.info("prune docker build: " + resp);78 }79 try (PruneCmd cmd = client.pruneCmd(PruneType.CONTAINERS);) {80 PruneResponse resp = cmd.exec();81 log.info("prune docker container: " + resp);82 }83 try (PruneCmd cmd = client.pruneCmd(PruneType.VOLUMES);) {84 PruneResponse resp = cmd.exec();85 log.info("prune docker volume: " + resp);86 }87 }88 public DockerComposeContainer<?> build() {89 return build(new File("dev/docker-compose.integration-test.yml"));90 }91 public DockerComposeContainer<?> build(File dockerComposeFile) {92 DockerComposeContainer<?> r = new DockerComposeContainer<>(dockerComposeFile);93 r.withLocalCompose(true);94 r.withExposedService("mysql", getMySQLPort(), Wait.forHealthcheck())95 .withLogConsumer("mysql", new Slf4jLogConsumer(getMysqlLogger()));96 r.withExposedService("server", getServerPort())97 .withExposedService("server", getServerManagePort(), Wait.forHttp(getServerHealthEndpoint()))98 .withLogConsumer("server", new Slf4jLogConsumer(getServerLogger()));99 return r;100 }101}...
Source:BrowserWebdriverDockerComposeTest.java
1package de.phitho.testcontainers.examples.junit5;2import org.junit.jupiter.api.AfterEach;3import org.junit.jupiter.api.BeforeAll;4import org.junit.jupiter.api.Test;5import org.openqa.selenium.WebDriver;6import org.openqa.selenium.chrome.ChromeOptions;7import org.slf4j.Logger;8import org.slf4j.LoggerFactory;9import org.testcontainers.containers.BrowserWebDriverContainer;10import org.testcontainers.containers.DockerComposeContainer;11import org.testcontainers.containers.output.Slf4jLogConsumer;12import org.testcontainers.junit.jupiter.Container;13import org.testcontainers.junit.jupiter.Testcontainers;14import org.testcontainers.lifecycle.TestDescription;15import java.io.File;16import java.util.Optional;17@Testcontainers18public class BrowserWebdriverDockerComposeTest {19 private static final Logger LOGGER = LoggerFactory.getLogger(BrowserWebdriverDockerComposeTest.class);20 private static final int CHRONOGRAF_PORT = 8888;21 @Container22 public final BrowserWebDriverContainer chromeContainer =23 new BrowserWebDriverContainer()24 .withCapabilities(new ChromeOptions())25 .withRecordingMode(BrowserWebDriverContainer.VncRecordingMode.RECORD_ALL, new File("build"));26 @Container27 public static DockerComposeContainer chronograf =28 new DockerComposeContainer(new File("docker-compose.yml"))29 .withExposedService("chronograf", CHRONOGRAF_PORT) // implicitly waits30 .withLocalCompose(true)31 .withLogConsumer("influxdb", new Slf4jLogConsumer(LOGGER)); // check the logs if influx is talking32 private WebDriver chromeDriver;33 @BeforeAll34 public static void beforeAll() {35 org.testcontainers.Testcontainers.exposeHostPorts(CHRONOGRAF_PORT);36 }37 @Test38 void browse_chronograph() {39 chromeDriver = chromeContainer.getWebDriver();40 chromeDriver.get("http://host.testcontainers.internal:"+CHRONOGRAF_PORT);41 }42/*43 VNC recording is broken in JUnit 5, see https://github.com/testcontainers/testcontainers-java/issues/134144 This is a workaround to enable VNC recording.45 */46// @AfterEach47// void tearDown() {48// chromeContainer.afterTest(new TestDescription() {49//50// @Override51// public String getTestId() {52// return "test_id";53// }54//55// @Override56// public String getFilesystemFriendlyName() {57// return "FileName";58// }59// }, Optional.empty());60// }61}...
logger
Using AI Code Generation
1import org.testcontainers.containers.DockerComposeContainer;2import org.testcontainers.containers.wait.strategy.Wait;3import org.testcontainers.containers.wait.strategy.WaitAllStrategy;4import org.testcontainers.containers.wait.strategy.WaitStrategy;5import org.testcontainers.containers.wait.strategy.WaitStrategyTarget;6import org.testcontainers.utility.MountableFile;7import java.io.File;8import java.time.Duration;9import java.util.HashMap;10import java.util.Map;11public class TestContainer {12 private static final String DOCKER_COMPOSE_YML = "docker-compose.yml";13 private static final String DOCKER_COMPOSE_YML_PATH = TestContainer.class.getClassLoader().getResource(DOCKER_COMPOSE_YML).getPath();14 private static final String DOCKER_COMPOSE_ENV_FILE = "docker-compose.env";15 private static final String DOCKER_COMPOSE_ENV_FILE_PATH = TestContainer.class.getClassLoader().getResource(DOCKER_COMPOSE_ENV_FILE).getPath();16 private static final String DOCKER_COMPOSE_PROJECT_NAME = "testcontainers";17 private static final String DOCKER_COMPOSE_PROJECT_NAME_ENV_KEY = "COMPOSE_PROJECT_NAME";18 private static final String DOCKER_COMPOSE_PROJECT_NAME_ENV_VALUE = "testcontainers";19 private static final String DOCKER_COMPOSE_ENV_FILE_ENV_KEY = "COMPOSE_FILE";20 private static final String DOCKER_COMPOSE_ENV_FILE_ENV_VALUE = DOCKER_COMPOSE_ENV_FILE_PATH;21 private static final String DOCKER_COMPOSE_WORKING_DIR_ENV_KEY = "COMPOSE_WORKING_DIR";22 private static final String DOCKER_COMPOSE_WORKING_DIR_ENV_VALUE = new File(DOCKER_COMPOSE_ENV_FILE_PATH).getParent();23 private static final String DOCKER_COMPOSE_LOG_LEVEL_ENV_KEY = "COMPOSE_LOG_LEVEL";24 private static final String DOCKER_COMPOSE_LOG_LEVEL_ENV_VALUE = "DEBUG";25 private static final String DOCKER_COMPOSE_CONFIG_ENV_KEY = "COMPOSE_CONFIG";26 private static final String DOCKER_COMPOSE_CONFIG_ENV_VALUE = DOCKER_COMPOSE_ENV_FILE_PATH;27 private static final String DOCKER_COMPOSE_TLS_VERIFY_ENV_KEY = "DOCKER_TLS_VERIFY";28 private static final String DOCKER_COMPOSE_TLS_VERIFY_ENV_VALUE = "1";29 private static final String DOCKER_COMPOSE_CERT_PATH_ENV_KEY = "DOCKER_CERT_PATH";30 private static final String DOCKER_COMPOSE_CERT_PATH_ENV_VALUE = "/Users/username/.docker/machine/machines/default";
logger
Using AI Code Generation
1import org.testcontainers.containers.DockerComposeContainer;2import org.testcontainers.containers.output.Slf4jLogConsumer;3import org.slf4j.Logger;4import org.slf4j.LoggerFactory;5import java.io.File;6public class 1 {7 public static void main(String[] args) {8 Logger logger = LoggerFactory.getLogger(1.class);9 DockerComposeContainer container = new DockerComposeContainer(new File("docker-compose.yml"))10 .withExposedService("redis_1", 6379)11 .withLogConsumer("redis_1", new Slf4jLogConsumer(logger));12 container.start();13 }14}15 at org.testcontainers.containers.DockerComposeContainer.getServiceNameByPort(DockerComposeContainer.java:283)16 at org.testcontainers.containers.DockerComposeContainer.getServiceHost(DockerComposeContainer.java:243)17 at org.testcontainers.containers.DockerComposeContainer.getServiceHost(DockerComposeContainer.java:237)18 at org.testcontainers.containers.DockerComposeContainer.getServiceHost(DockerComposeContainer.java:230)19 at org.testcontainers.containers.DockerComposeContainer.getServiceHost(DockerComposeContainer.java:225)20 at org.testcontainers.containers.DockerComposeContainer.withExposedService(DockerComposeContainer.java:123)21 at 1.main(1.java:12)22import org.testcontainers.containers.DockerComposeContainer;23import org.testcontainers.containers.output.Slf4jLogConsumer;24import org.slf4j.Logger;25import org.slf4j.LoggerFactory;26import java.io.File;27public class 2 {28 public static void main(String[] args) {29 Logger logger = LoggerFactory.getLogger(2.class);30 DockerComposeContainer container = new DockerComposeContainer(new File("docker-compose.yml"))31 .withLogConsumer("redis_1", new Slf4jLogConsumer(logger));32 container.start();33 }34}35 at org.testcontainers.containers.DockerComposeContainer.getServiceNameByPort(DockerComposeContainer.java:283
logger
Using AI Code Generation
1package org.testcontainers.containers;2import org.junit.Test;3import org.slf4j.Logger;4import org.slf4j.LoggerFactory;5public class DockerComposeContainerTest {6 private static final Logger logger = LoggerFactory.getLogger(DockerComposeContainerTest.class);7 public void test() {8 logger.info("Testing logger");9 }10}11package org.testcontainers.containers;12import org.junit.Test;13import org.slf4j.Logger;14import org.slf4j.LoggerFactory;15public class DockerComposeContainerTest {16 private static final Logger logger = LoggerFactory.getLogger(DockerComposeContainerTest.class);17 public void test() {18 logger.info("Testing logger");19 }20}21package org.testcontainers.containers;22import org.junit.Test;23import org.slf4j.Logger;24import org.slf4j.LoggerFactory;25public class DockerComposeContainerTest {26 private static final Logger logger = LoggerFactory.getLogger(DockerComposeContainerTest.class);27 public void test() {28 logger.info("Testing logger");29 }30}31package org.testcontainers.containers;32import org.junit.Test;33import org.slf4j.Logger;34import org.slf4j.LoggerFactory;35public class DockerComposeContainerTest {36 private static final Logger logger = LoggerFactory.getLogger(DockerComposeContainerTest.class);37 public void test() {38 logger.info("Testing logger");39 }40}41package org.testcontainers.containers;42import org.junit.Test;43import org.slf4j.Logger;44import org.slf4j.LoggerFactory;45public class DockerComposeContainerTest {46 private static final Logger logger = LoggerFactory.getLogger(DockerComposeContainerTest.class);47 public void test() {48 logger.info("Testing logger");49 }50}51package org.testcontainers.containers;52import org.junit.Test;53import org.slf4j.Logger;54import org.slf4j.LoggerFactory;
logger
Using AI Code Generation
1package org.testcontainers.containers;2import org.junit.Test;3import org.slf4j.Logger;4import org.slf4j.LoggerFactory;5public class DockerComposeContainerTest {6 private static final Logger logger = LoggerFactory.getLogger(DockerComposeContainer.class);7 public void test() {8 logger.info("test");9 }10}
logger
Using AI Code Generation
1import org.testcontainers.containers.DockerComposeContainer;2import org.testcontainers.containers.output.OutputFrame;3import org.testcontainers.containers.output.ToStringConsumer;4import java.io.File;5import java.util.concurrent.TimeUnit;6public class Test {7 public static void main(String[] args) {8 DockerComposeContainer container = new DockerComposeContainer(new File("docker-compose.yml"));9 container.start();10 container.followOutput(new ToStringConsumer() {11 public void accept(OutputFrame outputFrame) {12 System.out.println(outputFrame.getUtf8String());13 }14 }, OutputFrame.OutputType.STDOUT);15 container.followOutput(new ToStringConsumer() {16 public void accept(OutputFrame outputFrame) {17 System.out.println(outputFrame.getUtf8String());18 }19 }, OutputFrame.OutputType.STDERR);20 try {21 container.waitingForService("service1", 10, TimeUnit.SECONDS);22 } catch (InterruptedException e) {23 e.printStackTrace();24 }25 container.stop();26 }27}
logger
Using AI Code Generation
1package org.testcontainers.containers;2import java.io.IOException;3import java.nio.file.Files;4import java.nio.file.Path;5import java.nio.file.Paths;6import java.nio.file.StandardOpenOption;7import java.util.logging.Logger;8import org.junit.Test;9public class DockerComposeContainerTest {10 public void test() throws IOException {11 Path composeFile = Paths.get("src/test/resources/docker-compose.yml");12 Path logFile = Paths.get("src/test/resources/docker-compose.log");13 Files.deleteIfExists(logFile);14 Files.createFile(logFile);15 DockerComposeContainer container = new DockerComposeContainer(composeFile)16 .withLocalCompose(true)17 .withLogConsumer(outputFrame -> {18 try {19 Files.write(logFile, (outputFrame.getUtf8String() + "20").getBytes(), StandardOpenOption.APPEND);21 } catch (IOException e) {22 e.printStackTrace();23 }24 });25 container.start();26 }27}28package org.testcontainers.containers;29import java.io.IOException;30import java.nio.file.Files;31import java.nio.file.Path;32import java.nio.file.Paths;33import java.nio.file.StandardOpenOption;34import java.util.logging.Logger;35import org.junit.Test;36public class DockerComposeContainerTest {37 public void test() throws IOException {38 Path composeFile = Paths.get("src/test/resources/docker-compose.yml");39 Path logFile = Paths.get("src/test/resources/docker-compose.log");40 Files.deleteIfExists(logFile);41 Files.createFile(logFile);42 DockerComposeContainer container = new DockerComposeContainer(composeFile)43 .withLocalCompose(true)44 .withLogConsumer(outputFrame -> {45 try {46 Files.write(logFile, (outputFrame.getUtf8String() + "47").getBytes(), StandardOpenOption.APPEND);48 } catch (IOException e) {49 e.printStackTrace();50 }51 });52 container.start();53 }54}55package org.testcontainers.containers;56import java.io.IOException;57import java.nio.file.Files;58import java.nio.file.Path;59import java.nio.file.Paths;60import java.nio.file.StandardOpenOption;61import java.util.logging.Logger;62import org.junit.Test;63public class DockerComposeContainerTest {
logger
Using AI Code Generation
1import org.testcontainers.containers.DockerComposeContainer;2import java.io.File;3import java.io.IOException;4import java.util.logging.Level;5import java.util.logging.Logger;6public class DockerComposeContainerTest {7 public static void main(String[] args) throws IOException {8 Logger logger = Logger.getLogger(DockerComposeContainer.class.getName());9 logger.setLevel(Level.ALL);10 File composeFile = new File("src/test/resources/docker-compose.yml");11 DockerComposeContainer container = new DockerComposeContainer(composeFile);12 container.withLocalCompose(true);13 container.start();14 }15}16import org.testcontainers.containers.GenericContainer;17import java.util.logging.Level;18import java.util.logging.Logger;19public class GenericContainerTest {20 public static void main(String[] args) {21 Logger logger = Logger.getLogger(GenericContainer.class.getName());22 logger.setLevel(Level.ALL);23 GenericContainer container = new GenericContainer("alpine:3.7");24 container.start();25 }26}
logger
Using AI Code Generation
1public class DockerComposeContainerTest {2 private static final Logger LOG = LoggerFactory.getLogger(DockerComposeContainerTest.class);3 public static void main(String[] args) {4 LOG.info("test");5 }6}7public class DockerComposeContainerTest {8 private static final Logger LOG = LoggerFactory.getLogger(DockerComposeContainer.class);9 public static void main(String[] args) {10 LOG.info("test");11 }12}
logger
Using AI Code Generation
1package org.testcontainers.containers;2import org.junit.Test;3import org.slf4j.event.Level;4import java.io.File;5import static org.assertj.core.api.Assertions.assertThat;6public class DockerComposeContainerTest {7 public void testDockerComposeContainer() {8 DockerComposeContainer container = new DockerComposeContainer(new File("src/test/resources/compose-test.yml"))9 .withLocalCompose(true)10 .withExposedService("redis_1", 6379)11 .withExposedService("web_1", 5000);12 container.logger().withLevel(Level.INFO);13 container.start();14 assertThat(container.getServicePort("redis_1", 6379)).isGreaterThan(0);15 assertThat(container.getServicePort("web_1", 5000)).isGreaterThan(0);16 container.stop();17 }18}
logger
Using AI Code Generation
1public class 1 {2 public static void main(String[] args) throws IOException, InterruptedException {3 String logFile = "log.txt";4 File file = new File(logFile);5 FileOutputStream fileOutputStream = new FileOutputStream(file);6 PrintStream printStream = new PrintStream(fileOutputStream);7 DockerComposeContainer container = new DockerComposeContainer(new File("docker-compose.yml"));8 container.withLocalCompose(true);9 container.start();10 container.logger().info("log");
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!!