|
| 1 | +package org.testcontainers.containers; |
| 2 | + |
| 3 | +import ch.qos.logback.classic.Logger; |
| 4 | +import ch.qos.logback.classic.spi.ILoggingEvent; |
| 5 | +import ch.qos.logback.core.AppenderBase; |
| 6 | +import com.google.common.collect.Lists; |
| 7 | +import org.junit.After; |
| 8 | +import org.junit.Before; |
| 9 | +import org.junit.Test; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | +import org.testcontainers.utility.TestcontainersConfiguration; |
| 12 | + |
| 13 | +import java.io.File; |
| 14 | +import java.io.IOException; |
| 15 | +import java.nio.file.Files; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Optional; |
| 19 | + |
| 20 | +import static org.junit.Assert.*; |
| 21 | + |
| 22 | +public class ComposeContainerTest { |
| 23 | + private TestLogAppender testLogAppender; |
| 24 | + private Logger rootLogger; |
| 25 | + |
| 26 | + @Before |
| 27 | + public void setup() { |
| 28 | + testLogAppender = new TestLogAppender(); |
| 29 | + testLogAppender.start(); |
| 30 | + rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); |
| 31 | + rootLogger.addAppender(testLogAppender); |
| 32 | + } |
| 33 | + |
| 34 | + @After |
| 35 | + public void tearDown() { |
| 36 | + rootLogger.detachAppender(testLogAppender); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testWithCustomDockerImage() throws IOException { |
| 41 | + TestcontainersConfiguration.getInstance().updateUserConfig("compose.container.image", "docker:25.0.2"); |
| 42 | + ComposeContainer composeContainer = new ComposeContainer(Lists.newArrayList(new File("src/test/resources/docker-compose-imagename-parsing-v2.yml"))); |
| 43 | + composeContainer.start(); |
| 44 | + System.clearProperty("compose.container.image"); |
| 45 | + List<String> logs = testLogAppender.getLogs(); |
| 46 | + composeContainer.stop(); |
| 47 | + assertNotNull(logs); |
| 48 | + Optional<String> verification = logs.stream().filter(line -> line.contains("Creating container for image: docker:25.0.2")).findFirst(); |
| 49 | + assertTrue(verification.isPresent()); |
| 50 | + TestcontainersConfiguration.getInstance().updateUserConfig("compose.container.image", ""); |
| 51 | + } |
| 52 | + |
| 53 | + private static class TestLogAppender extends AppenderBase<ILoggingEvent> { |
| 54 | + private final List<String> logs = new ArrayList<>(); |
| 55 | + |
| 56 | + @Override |
| 57 | + protected void append(ILoggingEvent eventObject) { |
| 58 | + logs.add(eventObject.getFormattedMessage()); |
| 59 | + } |
| 60 | + |
| 61 | + public List<String> getLogs() { |
| 62 | + return logs; |
| 63 | + } |
| 64 | + |
| 65 | + public void clearLogs() { |
| 66 | + logs.clear(); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments