Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-45938] Supporting Run with timeout build (#24)
Change-Id: Id3af67394a7a4876ea048b6f588d68c2ba2f25b7
  • Loading branch information
v1v committed Aug 4, 2017
1 parent 283af5f commit b6adad6
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -163,7 +163,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>build-timeout</artifactId>
<version>1.15</version>
<version>1.18</version>
<scope>test</scope>
</dependency>

Expand Down
Expand Up @@ -3,6 +3,7 @@
import hudson.model.Descriptor;
import hudson.model.Item;
import hudson.model.Project;
import hudson.tasks.Builder;
import hudson.tasks.BuildWrapper;
import hudson.util.DescribableList;
import jenkins.model.Jenkins;
Expand All @@ -27,27 +28,46 @@ public boolean executeCheck(Item item) {
if (Jenkins.getInstance().pluginManager.getPlugin("build-timeout") != null) {
if (item.getClass().getName().endsWith("hudson.maven.MavenModuleSet")) {
try {
// Wrappers
Method method = item.getClass().getMethod("getBuildWrappersList");
DescribableList<BuildWrapper,Descriptor<BuildWrapper>> buildWrapperList = ((DescribableList<BuildWrapper,Descriptor<BuildWrapper>>) method.invoke(item));
notfound = !isTimeout(buildWrapperList);
boolean isWrapperTimeout = isTimeout(buildWrapperList);
// builders
boolean isBuildTimeout = false;
Object getPrebuilders = item.getClass().getMethod("getPrebuilders", null).invoke(item);
if (getPrebuilders instanceof List) {
isBuildTimeout = isBuildStepTimeout((List) getPrebuilders);
}
notfound = !(isBuildTimeout || isWrapperTimeout);
} catch (Exception e) {
LOG.log(Level.WARNING, "Exception " + e.getMessage(), e.getCause());
notfound = false;
}
}
if (item instanceof Project) {
notfound = !isTimeout(((Project) item).getBuildWrappersList());
notfound = !(isTimeout(((Project) item).getBuildWrappersList()) ||
isBuildStepTimeout (((Project)item).getBuilders()));
}
if (item.getClass().getSimpleName().equals("MatrixProject")) {
try {
Object getBuildWrappersList = item.getClass().getMethod("getBuildWrappersList", null).invoke(item);
boolean isWrapperTimeout = false;
if (getBuildWrappersList instanceof List) {
notfound = !isTimeout((List) getBuildWrappersList);
isWrapperTimeout = isTimeout((List) getBuildWrappersList);
}
Object getBuilders = item.getClass().getMethod("getBuilders", null).invoke(item);
boolean isBuildTimeout = false;
if (getBuilders instanceof List) {
isBuildTimeout = isBuildStepTimeout((List) getBuilders);
}
notfound = !(isBuildTimeout || isWrapperTimeout);
}catch (Exception e) {
LOG.log(Level.WARNING, "Exception " + e.getMessage(), e.getCause());
notfound = false;
}
}
} else {
LOG.log(Level.FINE, "It's highly recommended to use the plugin build-timeout");
}
return notfound;
}
Expand All @@ -63,4 +83,19 @@ private boolean isTimeout(List<BuildWrapper> builders) {
}
return status;
}

private boolean isBuildStepTimeout(List<Builder> builders) {
boolean found = false;
if (builders != null && builders.size() > 0 ) {
for (Builder builder : builders) {
if (builder.getClass().getName().endsWith("BuildStepWithTimeout")) {
found = true;
}
}
} else {
found = false;
}
return found;
}

}
Expand Up @@ -75,6 +75,46 @@ public class TimeoutCheckerTestCase extends AbstractTestCase {
project.getBuildWrappersList().add(createLikelyStuckTimeOutStrategy());
assertFalse(checker.executeCheck(project));
}
@Issue("JENKINS-45938")
@Test public void testJobWithBuildStepWithTimeout() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
project.getBuildersList().add(new hudson.tasks.Shell("#!/bin/bash #single line"));
assertTrue(checker.executeCheck(project));
project.delete();
project = j.createFreeStyleProject("AbsoluteTimeOutStrategy");
project.getBuildersList().add(new hudson.plugins.build_timeout.BuildStepWithTimeout(
new hudson.tasks.Shell("#!/bin/bash"),
new AbsoluteTimeOutStrategy("120"),
null));
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createFreeStyleProject("DeadlineTimeOutStrategy");
project.getBuildersList().add(new hudson.plugins.build_timeout.BuildStepWithTimeout(
new hudson.tasks.Shell("#!/bin/bash"),
new DeadlineTimeOutStrategy("120", 120),
null));
assertFalse(checker.executeCheck(project));
}
@Issue("JENKINS-45938")
@Test public void testMatrixProjectWithBuildStepWithTimeout() throws Exception {
MatrixProject project = j.createMatrixProject();
project.getBuildersList().add(new hudson.tasks.Shell("#!/bin/bash #single line"));
assertTrue(checker.executeCheck(project));
project.delete();
project = j.createMatrixProject("AbsoluteTimeOutStrategy");
project.getBuildersList().add(new hudson.plugins.build_timeout.BuildStepWithTimeout(
new hudson.tasks.Shell("#!/bin/bash"),
new AbsoluteTimeOutStrategy("120"),
null));
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createMatrixProject("DeadlineTimeOutStrategy");
project.getBuildersList().add(new hudson.plugins.build_timeout.BuildStepWithTimeout(
new hudson.tasks.Shell("#!/bin/bash"),
new DeadlineTimeOutStrategy("120", 120),
null));
assertFalse(checker.executeCheck(project));
}
@Test public void testControlComment() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
assertFalse(checker.isIgnored(project.getDescription()));
Expand Down

0 comments on commit b6adad6

Please sign in to comment.