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.
[FIXED JENKINS-30725] Added build time out check
- Loading branch information
Victor Martinez
committed
Oct 1, 2015
1 parent
55c43de
commit 4d45a94
Showing
4 changed files
with
196 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
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
62 changes: 62 additions & 0 deletions
62
src/main/java/org/jenkins/ci/plugins/jenkinslint/check/TimeoutChecker.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,62 @@ | ||
package org.jenkins.ci.plugins.jenkinslint.check; | ||
|
||
import hudson.matrix.MatrixProject; | ||
import hudson.model.Descriptor; | ||
import hudson.model.Item; | ||
import hudson.model.Project; | ||
import hudson.tasks.BuildWrapper; | ||
import hudson.util.DescribableList; | ||
import jenkins.model.Jenkins; | ||
import org.jenkins.ci.plugins.jenkinslint.model.AbstractCheck; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.List; | ||
import java.util.logging.Level; | ||
|
||
/** | ||
* @author Victor Martinez | ||
*/ | ||
public class TimeoutChecker extends AbstractCheck { | ||
|
||
public TimeoutChecker() { | ||
super(); | ||
this.setDescription("There are some builds which might hang forever and cause inaction in the build queue and slaves. " + | ||
"It's recommended to control those time based on the <a href=https://wiki.jenkins-ci.org/display/JENKINS/Build-timeout+Plugin>>build timeout plugin</a>."); | ||
this.setSeverity("Medium"); | ||
} | ||
|
||
public boolean executeCheck(Item item) { | ||
boolean notfound = true; | ||
if (Jenkins.getInstance().pluginManager.getPlugin("build-timeout") != null) { | ||
if (item.getClass().getName().endsWith("hudson.maven.MavenModuleSet")) { | ||
try { | ||
Method method = item.getClass().getMethod("getBuildWrappersList"); | ||
DescribableList<BuildWrapper,Descriptor<BuildWrapper>> buildWrapperList = ((DescribableList<BuildWrapper,Descriptor<BuildWrapper>>) method.invoke(item)); | ||
notfound = !isTimeout(buildWrapperList); | ||
} catch (Exception e) { | ||
LOG.log(Level.WARNING, "Exception " + e.getMessage(), e.getCause()); | ||
notfound = false; | ||
} | ||
} | ||
if (item instanceof Project) { | ||
notfound = !isTimeout(((Project) item).getBuildWrappersList()); | ||
} | ||
if (item instanceof MatrixProject) { | ||
notfound = !isTimeout(((MatrixProject) item).getBuildWrappersList()); | ||
} | ||
} | ||
return notfound; | ||
} | ||
|
||
private boolean isTimeout(List<BuildWrapper> builders) { | ||
boolean status = false; | ||
if (builders != null && builders.size() > 0 ) { | ||
for (BuildWrapper builder : builders) { | ||
if (builder.getClass().getName().endsWith("BuildTimeoutWrapper")) { | ||
status = true; | ||
} | ||
} | ||
} | ||
return status; | ||
} | ||
} |
125 changes: 125 additions & 0 deletions
125
src/test/java/org/jenkins/ci/plugins/jenkinslint/check/TimeoutCheckerTestCase.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,125 @@ | ||
package org.jenkins.ci.plugins.jenkinslint.check; | ||
|
||
import hudson.matrix.MatrixProject; | ||
import hudson.maven.MavenModuleSet; | ||
import hudson.model.FreeStyleProject; | ||
import hudson.plugins.build_timeout.BuildTimeoutWrapper; | ||
import hudson.plugins.build_timeout.impl.AbsoluteTimeOutStrategy; | ||
import hudson.plugins.build_timeout.impl.DeadlineTimeOutStrategy; | ||
import hudson.plugins.build_timeout.impl.ElasticTimeOutStrategy; | ||
import hudson.plugins.build_timeout.impl.LikelyStuckTimeOutStrategy; | ||
import hudson.plugins.build_timeout.impl.NoActivityTimeOutStrategy; | ||
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; | ||
|
||
/** | ||
* TimeoutChecker Test Case. | ||
* | ||
* @author Victor Martinez | ||
*/ | ||
public class TimeoutCheckerTestCase { | ||
private TimeoutChecker checker = new TimeoutChecker(); | ||
|
||
@Rule public JenkinsRule j = new JenkinsRule(); | ||
@Test public void testDefaultJob() throws Exception { | ||
FreeStyleProject project = j.createFreeStyleProject(); | ||
assertTrue(checker.executeCheck(project)); | ||
} | ||
@Test public void testMavenJobName() throws Exception { | ||
MavenModuleSet project = j.createMavenProject(); | ||
assertTrue(checker.executeCheck(project)); | ||
} | ||
//@Issue("JENKINS-29444") | ||
@Test public void testMatrixProject() throws Exception { | ||
MatrixProject project = j.createMatrixProject(); | ||
assertTrue(checker.executeCheck(project)); | ||
} | ||
@Test public void testMatrixProjectWithTimeout() throws Exception { | ||
MatrixProject project = j.createMatrixProject("NoActivityTimeOut"); | ||
project.getBuildWrappersList().add(createNoActivityTimeOut()); | ||
assertFalse(checker.executeCheck(project)); | ||
project.delete(); | ||
project = j.createMatrixProject("AbsoluteTimeOutStrategy"); | ||
project.getBuildWrappersList().add(createAbsoluteTimeOutStrategy()); | ||
assertFalse(checker.executeCheck(project)); | ||
project.delete(); | ||
project = j.createMatrixProject("DeadlineTimeOutStrategy"); | ||
project.getBuildWrappersList().add(createDeadlineTimeOutStrategy()); | ||
assertFalse(checker.executeCheck(project)); | ||
project.delete(); | ||
project = j.createMatrixProject("ElasticTimeOutStrategy"); | ||
project.getBuildWrappersList().add(createElasticTimeOutStrategy()); | ||
assertFalse(checker.executeCheck(project)); | ||
project.delete(); | ||
project = j.createMatrixProject("LikelyStuckTimeOutStrategy"); | ||
project.getBuildWrappersList().add(createLikelyStuckTimeOutStrategy()); | ||
assertFalse(checker.executeCheck(project)); | ||
} | ||
@Test public void testJobWithTimeout() throws Exception { | ||
FreeStyleProject project = j.createFreeStyleProject("NoActivityTimeOut"); | ||
project.getBuildWrappersList().add(createNoActivityTimeOut()); | ||
assertFalse(checker.executeCheck(project)); | ||
project.delete(); | ||
project = j.createFreeStyleProject("AbsoluteTimeOutStrategy"); | ||
project.getBuildWrappersList().add(createAbsoluteTimeOutStrategy()); | ||
assertFalse(checker.executeCheck(project)); | ||
project.delete(); | ||
project = j.createFreeStyleProject("DeadlineTimeOutStrategy"); | ||
project.getBuildWrappersList().add(createDeadlineTimeOutStrategy()); | ||
assertFalse(checker.executeCheck(project)); | ||
project.delete(); | ||
project = j.createFreeStyleProject("ElasticTimeOutStrategy"); | ||
project.getBuildWrappersList().add(createElasticTimeOutStrategy()); | ||
assertFalse(checker.executeCheck(project)); | ||
project.delete(); | ||
project = j.createFreeStyleProject("LikelyStuckTimeOutStrategy"); | ||
project.getBuildWrappersList().add(createLikelyStuckTimeOutStrategy()); | ||
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())); | ||
} | ||
//@Issue("JENKINS-29427") | ||
@Test public void testAnotherBuilders() throws Exception { | ||
FreeStyleProject project = j.createFreeStyleProject("MsBuildBuilder"); | ||
project.getBuildersList().add(new hudson.plugins.msbuild.MsBuildBuilder("", "", "", true, true, true)); | ||
assertTrue(checker.executeCheck(project)); | ||
project.delete(); | ||
project = j.createFreeStyleProject("Ant"); | ||
project.getBuildersList().add(new hudson.tasks.Ant("","","","","")); | ||
assertTrue(checker.executeCheck(project)); | ||
project.delete(); | ||
} | ||
|
||
private BuildTimeoutWrapper createNoActivityTimeOut() { | ||
NoActivityTimeOutStrategy strategy = new NoActivityTimeOutStrategy("120"); | ||
return new BuildTimeoutWrapper(strategy, null, null); | ||
} | ||
|
||
private BuildTimeoutWrapper createAbsoluteTimeOutStrategy() { | ||
AbsoluteTimeOutStrategy strategy = new AbsoluteTimeOutStrategy("120"); | ||
return new BuildTimeoutWrapper(strategy, null, null); | ||
} | ||
|
||
private BuildTimeoutWrapper createDeadlineTimeOutStrategy() { | ||
DeadlineTimeOutStrategy strategy = new DeadlineTimeOutStrategy("120", 120); | ||
return new BuildTimeoutWrapper(strategy, null, null); | ||
} | ||
|
||
private BuildTimeoutWrapper createElasticTimeOutStrategy() { | ||
ElasticTimeOutStrategy strategy = new ElasticTimeOutStrategy("90","60","5"); | ||
return new BuildTimeoutWrapper(strategy, null, null); | ||
} | ||
|
||
private BuildTimeoutWrapper createLikelyStuckTimeOutStrategy() { | ||
LikelyStuckTimeOutStrategy strategy = new LikelyStuckTimeOutStrategy(); | ||
return new BuildTimeoutWrapper(strategy, null, null); | ||
} | ||
} |