Skip to content
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

Added JenkinsRule.showAgentLogs utility #127

Merged
merged 3 commits into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ THE SOFTWARE.
<parent>
<groupId>org.jenkins-ci</groupId>
<artifactId>jenkins</artifactId>
<version>1.49</version>
<version>1.51</version>
<relativePath />
</parent>

<groupId>org.jenkins-ci.main</groupId>
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/org/jvnet/hudson/test/JenkinsRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,10 @@
import java.nio.channels.ClosedByInterruptException;
import java.util.concurrent.ExecutionException;
import java.util.logging.ConsoleHandler;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import jenkins.model.ParameterizedJobMixIn;
import jenkins.security.MasterToSlaveCallable;
import org.hamcrest.core.IsInstanceOf;
import org.junit.rules.DisableOnDebug;
import org.junit.rules.Timeout;
Expand Down Expand Up @@ -988,6 +990,53 @@ public void waitOnline(Slave s) throws Exception {
}
}

/**
* Same as {@link #showSlaveLogs(Slave, Map)} but taking a preconfigured list of loggers as a convenience.
*/
public void showSlaveLogs(Slave s, LoggerRule loggerRule) throws Exception {
showSlaveLogs(s, loggerRule.getRecordedLevels());
}

/**
* Forward agent logs to standard error of the test process.
* Otherwise log messages would be sent only to {@link Computer#getLogText} etc.,
* or discarded entirely (if below {@link Level#INFO}).
* @param s an <em>online</em> agent
* @param loggers {@link Logger#getName} tied to log level
*/
public void showSlaveLogs(Slave s, Map<String, Level> loggers) throws Exception {
s.getChannel().call(new RemoteLogDumper(s.getNodeName(), loggers));
}

private static final class RemoteLogDumper extends MasterToSlaveCallable<Void, RuntimeException> {
private final String name;
private final Map<String, Level> loggers;
private final TaskListener stderr = StreamTaskListener.fromStderr();
RemoteLogDumper(String name, Map<String, Level> loggers) {
this.name = name;
this.loggers = loggers;
}
@Override public Void call() throws RuntimeException {
Handler handler = new Handler() {
final Formatter formatter = new SupportLogFormatter();
@Override public void publish(LogRecord record) {
if (isLoggable(record)) {
stderr.getLogger().print(formatter.format(record).replaceAll("(?m)^", "[" + name + "] "));
}
}
@Override public void flush() {}
@Override public void close() throws SecurityException {}
};
handler.setLevel(Level.ALL);
loggers.entrySet().forEach(e -> {
Logger logger = Logger.getLogger(e.getKey());
logger.setLevel(e.getValue());
logger.addHandler(handler);
});
return null;
}
}

/**
* Blocks until the ENTER key is hit.
* This is useful during debugging a test so that one can inspect the state of Hudson through the web browser.
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/jvnet/hudson/test/LoggerRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.stream.Collectors;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.hamcrest.Matcher;
Expand Down Expand Up @@ -138,6 +139,10 @@ public LoggerRule recordPackage(Class<?> clazz, Level level) {
return record(clazz.getPackage().getName(), level);
}

Map<String, Level> getRecordedLevels() {
return loggers.keySet().stream().collect(Collectors.toMap(Logger::getName, Logger::getLevel));
}

/**
* Obtains all log records collected so far during this test case.
* You must have first called {@link #capture}.
Expand Down