-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Simple summary logging of images pulled during execution #3165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a JVM shutdown hook. This is not the most iron-clad way, but as this logging is informational and not required I believe it's sufficient.