forked from v1v/jenkinslint-plugin
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-42267] New lint check to produce even load on the system
- Loading branch information
Showing
3 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/org/jenkins/ci/plugins/jenkinslint/check/JobTimerTriggerChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/test/java/org/jenkins/ci/plugins/jenkinslint/check/JobTimerTriggerCheckerTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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())); | ||
} | ||
} |