Skip to content
Permalink
Browse files

[JENKINS-45938] Initial approach

Change-Id: Id3af67394a7a4876ea048b6f588d68c2ba2f25b7
  • Loading branch information
v1v committed Aug 2, 2017
1 parent 283af5f commit 09badf14452747ed9a727bfc9fcb8808f890219c
@@ -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>

@@ -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;
@@ -27,25 +28,42 @@ 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;
}
}
}
@@ -63,4 +81,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("BuildTimeoutWrapper")) {
found = true;
}
}
} else {
found = false;
}
return found;
}

}
@@ -75,6 +75,46 @@
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()));

0 comments on commit 09badf1

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