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.
Merge pull request #10 from jenkinsci/h_cron
[JENKINS-42267] New lint check to produce even load on the system
- Loading branch information
Showing
4 changed files
with
145 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
57 changes: 57 additions & 0 deletions
57
src/main/java/org/jenkins/ci/plugins/jenkinslint/check/TimerTriggerChecker.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,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; | ||
} | ||
} |
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
80 changes: 80 additions & 0 deletions
80
src/test/java/org/jenkins/ci/plugins/jenkinslint/check/TimerTriggerCheckerTestCase.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,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())); | ||
} | ||
} |