-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simple summary logging of images pulled during execution
- Loading branch information
Showing
6 changed files
with
127 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
core/src/main/java/org/testcontainers/utility/ImagePullCountLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.testcontainers.utility; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Simple utility to log which images have been pulled by {@link org.testcontainers.Testcontainers} and how many times. | ||
*/ | ||
@Slf4j | ||
public class ImagePullCountLogger { | ||
|
||
private static ImagePullCountLogger instance; | ||
private final Map<String, AtomicInteger> pullCounters = new ConcurrentHashMap<>(); | ||
|
||
public synchronized static ImagePullCountLogger instance() { | ||
if (instance == null) { | ||
instance = new ImagePullCountLogger(); | ||
Runtime.getRuntime().addShutdownHook(new Thread(instance::logStatistics)); | ||
} | ||
|
||
return instance; | ||
} | ||
|
||
@VisibleForTesting | ||
ImagePullCountLogger() { | ||
|
||
} | ||
|
||
public void logStatistics() { | ||
if (pullCounters.size() > 0) { | ||
final String summary = pullCounters.entrySet().stream() | ||
.map(it -> it.getKey() + (it.getValue().intValue() > 1 ? " (" + it.getValue() + " times)" : "")) | ||
.sorted() | ||
.collect(Collectors.joining("\n ", "\n ", "\n")); | ||
|
||
log.info("Testcontainers pulled the following images during execution:{}", summary); | ||
} else { | ||
log.info("Testcontainers did not need to pull any images during execution"); | ||
} | ||
} | ||
|
||
public void recordPull(final String image) { | ||
pullCounters.computeIfAbsent(image, __ -> new AtomicInteger()).incrementAndGet(); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
core/src/test/java/org/testcontainers/utility/ImagePullCountLoggerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.testcontainers.utility; | ||
|
||
import ch.qos.logback.classic.Logger; | ||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import ch.qos.logback.core.read.ListAppender; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class ImagePullCountLoggerTest { | ||
|
||
private ImagePullCountLogger underTest; | ||
private ListAppender<ILoggingEvent> listAppender; | ||
private Logger logger; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
logger = (Logger) LoggerFactory.getLogger(ImagePullCountLogger.class); | ||
listAppender = new ListAppender<>(); | ||
logger.addAppender(listAppender); | ||
listAppender.start(); | ||
} | ||
|
||
@Test | ||
public void testPullCountsLogged() { | ||
underTest = new ImagePullCountLogger(); | ||
|
||
underTest.recordPull("imageA"); | ||
underTest.recordPull("imageA"); | ||
underTest.recordPull("imageB"); | ||
underTest.recordPull("imageC"); | ||
|
||
underTest.logStatistics(); | ||
|
||
assertEquals(1, listAppender.list.size()); | ||
final Optional<String> messages = listAppender.list.stream().map(ILoggingEvent::getFormattedMessage).findFirst(); | ||
assertTrue(messages.isPresent()); | ||
final String message = messages.get(); | ||
assertTrue(message.contains("imageA (2 times)\n")); | ||
assertTrue(message.contains("imageB\n")); | ||
assertTrue(message.contains("imageC\n")); | ||
} | ||
|
||
@Test | ||
public void testNoPullsLogged() { | ||
underTest = new ImagePullCountLogger(); | ||
|
||
underTest.logStatistics(); | ||
|
||
assertEquals(1, listAppender.list.size()); | ||
final Optional<String> messages = listAppender.list.stream().map(ILoggingEvent::getFormattedMessage).findFirst(); | ||
assertTrue(messages.isPresent()); | ||
final String message = messages.get(); | ||
assertEquals("Testcontainers did not need to pull any images during execution", message); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
logger.detachAppender(listAppender); | ||
} | ||
} |