Skip to content

Commit

Permalink
Merge pull request #30 from batmat/JENKINS-50291
Browse files Browse the repository at this point in the history
[JENKINS-50291] Move health-checker.log under the now configurable logs root directory
  • Loading branch information
stephenc committed Apr 6, 2018
2 parents 87988e9 + 1c838cc commit 39718de
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -29,7 +29,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.7</version>
<version>2.37</version>
</parent>

<artifactId>metrics</artifactId>
Expand Down
25 changes: 24 additions & 1 deletion src/main/java/jenkins/metrics/api/Metrics.java
Expand Up @@ -47,11 +47,14 @@
import hudson.security.Permission;
import hudson.security.PermissionGroup;
import hudson.security.PermissionScope;
import hudson.triggers.SafeTimerTask;
import hudson.util.PluginServletFilter;
import hudson.util.StreamTaskListener;
import hudson.util.TimeUnit2;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashSet;
import java.util.Map;
Expand Down Expand Up @@ -548,7 +551,7 @@ public void run() {
if (jenkins == null) {
return;
}
File logFile = new File(new File(jenkins.getRootDir(), "logs"), "health-checker.log");
File logFile = getLogFile(jenkins);
if (!logFile.isFile()) {
File oldFile = new File(jenkins.getRootDir(), HealthChecker.class.getName() + ".log");
if (!logFile.getParentFile().isDirectory() && !logFile.getParentFile().mkdirs()) {
Expand Down Expand Up @@ -592,6 +595,26 @@ public void run() {
}
}

/* package */
static File getLogFile(Jenkins jenkins) {
File logsRoot = new File(jenkins.getRootDir(), "logs");
try {
// FIXME : remove when 2.114+ See JENKINS-50291
final Method getLogsRoot = SafeTimerTask.class.getMethod("getLogsRoot");
logsRoot = (File) getLogsRoot.invoke(null);

} catch (NoSuchMethodException e) {
// Expected on < 2.114
} catch (IllegalAccessException e) {
e.printStackTrace();
// Expected on < 2.114
} catch (InvocationTargetException e) {
e.printStackTrace();
// Expected on < 2.114
}
return new File(logsRoot, "health-checker.log");
}

/**
* The actual periodic work to run asynchronously.
*
Expand Down
57 changes: 57 additions & 0 deletions src/test/java/jenkins/metrics/api/MetricsTest.java
@@ -0,0 +1,57 @@
package jenkins.metrics.api;

import hudson.util.VersionNumber;
import jenkins.model.Jenkins;
import org.junit.AfterClass;
import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.RestartableJenkinsRule;

import java.io.IOException;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class MetricsTest {

private static final String SYSPROP_FOR_LOGS_PATH = "hudson.triggers.SafeTimerTask.logsTargetDir";
@Rule
public final TemporaryFolder folder = new TemporaryFolder();
@Rule
public RestartableJenkinsRule story = new RestartableJenkinsRule();

@BeforeClass
@AfterClass
public static void cleanProperty() throws IOException {
System.clearProperty(SYSPROP_FOR_LOGS_PATH);
}


@Issue("JENKINS-50291")
@Test
public void logsDirectory() {

// Lambda-ify when Java 8+
story.then(new RestartableJenkinsRule.Step() {
@Override
public void run(JenkinsRule j) throws Throwable {
Assume.assumeTrue(Jenkins.getVersion().isNewerThan(new VersionNumber("2.113"))); // JENKINS-50291 introduced in 2.114

assertTrue(Metrics.HealthChecker.getLogFile(j.jenkins).getAbsolutePath().startsWith(j.getInstance().getRootDir().getAbsolutePath()));
System.setProperty("hudson.triggers.SafeTimerTask.logsTargetDir", folder.newFolder().getAbsolutePath());
}
});

story.then(new RestartableJenkinsRule.Step() {
@Override
public void run(JenkinsRule j) throws Throwable {
assertFalse(Metrics.HealthChecker.getLogFile(j.jenkins).getAbsolutePath().startsWith(j.getInstance().getRootDir().getAbsolutePath()));
}
});
}
}

0 comments on commit 39718de

Please sign in to comment.