Skip to content
Permalink
Browse files

Merge pull request #30 from batmat/JENKINS-50291

[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 39718de3379b73abb17bf80261999e5bca9b378a
Showing with 82 additions and 2 deletions.
  1. +1 −1 pom.xml
  2. +24 −1 src/main/java/jenkins/metrics/api/Metrics.java
  3. +57 −0 src/test/java/jenkins/metrics/api/MetricsTest.java
@@ -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>
@@ -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;
@@ -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()) {
@@ -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.
*
@@ -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.
You can’t perform that action at this time.