Skip to content
Permalink
Browse files

[JENKINS-42267] New lint check to produce even load on the system

  • Loading branch information...
v1v committed Feb 22, 2017
1 parent 1546940 commit cfaf7d787a97f6c43d58d2bdaadbe247cfcba91a
@@ -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.JobTimerTriggerChecker;
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 JobTimerTriggerChecker());

slaveCheckList.add(new SlaveDescriptionChecker());
slaveCheckList.add(new SlaveVersionChecker());
@@ -0,0 +1,39 @@
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;

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

public JobTimerTriggerChecker() {
super();
this.setDescription("When setting Jenkins Jobs with some Timer trigger configuration use the symbol H (for “hash”) wherever possible \n" +
"to allow periodically scheduled tasks to produce even load on the system.");
this.setSeverity("Low");
}

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 (line.contains("#h") && !found) {
found = true;
}
}
} else {
found = true;
}
} else {
found = false;
}
return found;
}
}
@@ -0,0 +1,75 @@
package org.jenkins.ci.plugins.jenkinslint.check;

import hudson.model.FreeStyleProject;
import hudson.triggers.TimerTrigger;
import hudson.triggers.Trigger;
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;

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

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


@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();
assertFalse(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();
assertTrue(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 cfaf7d7

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