Skip to content
Permalink
Browse files

Merge pull request #10 from jenkinsci/h_cron

[JENKINS-42267] New lint check to produce even load on the system
  • Loading branch information...
v1v committed Feb 23, 2017
2 parents 1546940 + dbf1c8c commit ced83f2a19a9047569afe46dcec551c599787c71
@@ -17,6 +17,7 @@
import org.jenkins.ci.plugins.jenkinslint.check.JobDescriptionChecker;
import org.jenkins.ci.plugins.jenkinslint.check.JobLogRotatorChecker;
import org.jenkins.ci.plugins.jenkinslint.check.JobNameChecker;
import org.jenkins.ci.plugins.jenkinslint.check.TimerTriggerChecker;
import org.jenkins.ci.plugins.jenkinslint.check.MasterLabelChecker;
import org.jenkins.ci.plugins.jenkinslint.check.MavenJobTypeChecker;
import org.jenkins.ci.plugins.jenkinslint.check.MultibranchJobTypeChecker;
@@ -73,6 +74,7 @@ public void getData() throws IOException {
checkList.add(new TimeoutChecker());
checkList.add(new GroovySystemExitChecker());
checkList.add(new GitRefChecker());
checkList.add(new TimerTriggerChecker());

slaveCheckList.add(new SlaveDescriptionChecker());
slaveCheckList.add(new SlaveVersionChecker());
@@ -0,0 +1,57 @@
package org.jenkins.ci.plugins.jenkinslint.check;

import hudson.model.Item;
import hudson.model.Project;
import hudson.triggers.TimerTrigger;
import org.jenkins.ci.plugins.jenkinslint.model.AbstractCheck;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author Victor Martinez
*/
public class TimerTriggerChecker extends AbstractCheck{

public TimerTriggerChecker() {
super();
this.setDescription(Messages.TimerTriggerCheckerDesc());
this.setSeverity(Messages.TimerTriggerCheckerSeverity());
}

public boolean executeCheck(Item item) {
boolean found = false;
if (item instanceof Project && ((Project) item).getTrigger(TimerTrigger.class) != null ) {
String spec = ((Project) item).getTrigger(TimerTrigger.class).getSpec().toLowerCase();
if (spec.contains("h")) {
String[] myData = spec.split("\n");
for (String line: myData) {
if (!isH(line) && !isComment(line)) {
found = true;
}
}
} else {
found = true;
}
} else {
found = false;
}
return found;
}

private boolean isComment (String line) {
boolean found = false;
Pattern p = Pattern.compile("^\\s*#\\s*.*");
Matcher m = p.matcher(line);
found = m.matches();
return found;
}

private boolean isH (String line) {
boolean found = false;
Pattern p = Pattern.compile("^\\s*h.*");
Matcher m = p.matcher(line);
found = m.matches();
return found;
}
}
@@ -137,6 +137,12 @@ TimeoutCheckerDesc=\

TimeoutCheckerSeverity=Medium

TimerTriggerCheckerDesc=\
When setting Jenkins Jobs with some Timer trigger configuration use the symbol H (for “hash”) wherever possible \
<br/>to allow periodically scheduled tasks to produce even load on the system.\

TimerTriggerCheckerSeverity=Low

WindowsSlaveLaunchCheckerDesc=\
This launch method relies on DCOM and is often associated with \
<a href=https://wiki.jenkins-ci.org/display/JENKINS/Windows+slaves+fail+to+start+via+DCOM>subtle problems</a>. \
@@ -0,0 +1,80 @@
package org.jenkins.ci.plugins.jenkinslint.check;

import hudson.model.FreeStyleProject;
import hudson.triggers.TimerTrigger;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

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

/**
* TimerTriggerCheckerTestCase Test Case.
*
* @author Victor Martinez
*/
public class TimerTriggerCheckerTestCase {
private TimerTriggerChecker checker = new TimerTriggerChecker();

private static final String TIMER_WITHOUT_H = "20 * * * *";
private static final String TIMER_WITH_H = "H/15 * * * *";
private static final String TIMER_WITH_COMMENT = " # H 15 1 * * *";


@Rule public JenkinsRule j = new JenkinsRule();
@Test public void testDefaultJob() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
assertFalse(checker.executeCheck(project));
}
@Test public void testEmptyTriggerLabel() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
assertFalse(checker.executeCheck(project));
}
@Test public void testWithTimerTrigger() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
TimerTrigger newTrigger = new TimerTrigger(TIMER_WITHOUT_H);
project.addTrigger(newTrigger);
project.save();
assertTrue(checker.executeCheck(project));
}
@Test public void testWithHTimerTrigger() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
TimerTrigger newTrigger = new TimerTrigger(TIMER_WITH_H);
project.addTrigger(newTrigger);
project.save();
assertFalse(checker.executeCheck(project));
}
@Test public void testWithMultipleTimerTrigger() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
TimerTrigger newTrigger = new TimerTrigger(TIMER_WITHOUT_H + "\n" + TIMER_WITHOUT_H);
project.addTrigger(newTrigger);
project.save();
assertTrue(checker.executeCheck(project));
}
@Test public void testWithMultipleHTimerTrigger() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
TimerTrigger newTrigger = new TimerTrigger(TIMER_WITHOUT_H + "\n" + TIMER_WITH_H + "\n" + TIMER_WITH_H);
project.addTrigger(newTrigger);
project.save();
assertTrue(checker.executeCheck(project));
}
@Test public void testCommentedTimerTrigger() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
TimerTrigger newTrigger = new TimerTrigger(TIMER_WITHOUT_H + "\n#" + TIMER_WITH_H);
project.addTrigger(newTrigger);
project.save();
project.delete();
assertTrue(checker.executeCheck(project));
newTrigger = new TimerTrigger(TIMER_WITH_COMMENT);
project.addTrigger(newTrigger);
project.save();
assertFalse(checker.executeCheck(project));
}
@Test public void testControlComment() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
assertFalse(checker.isIgnored(project.getDescription()));
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName());
assertTrue(checker.isIgnored(project.getDescription()));
}
}

0 comments on commit ced83f2

Please sign in to comment.
You can’t perform that action at this time.