Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #46 from jglick/JENKINS-25240-redux
[JENKINS-25240] Need to consider even nested jobs for purposes of checking running builds
  • Loading branch information
jglick committed Mar 25, 2016
2 parents 585f91a + be15a91 commit 3857169
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 5 deletions.
Expand Up @@ -193,17 +193,17 @@ public int compare(I i1, I i2) {
});
for (Iterator<I> iterator = candidates.iterator(); iterator.hasNext();) {
I item = iterator.next();
if (item instanceof Job) {
for (Job<?,?> job : item.getAllJobs()) {
// Enumerating all builds is inefficient. But we will most likely delete this job anyway,
// which will have a cost proportional to the number of builds just to delete those files.
for (Run<?,?> build : ((Job<?,?>) item).getBuilds()) {
for (Run<?,?> build : job.getBuilds()) {
if (build.isBuilding()) {
listener.getLogger().printf("Will not remove %s as build #%d is still in progress%n", item.getDisplayName(), build.getNumber());
listener.getLogger().printf("Will not remove %s as %s is still in progress%n", item.getDisplayName(), build.getFullDisplayName());
iterator.remove();
}
String whyKeepLog = build.getWhyKeepLog();
if (whyKeepLog != null) {
listener.getLogger().printf("Will not remove %s as build #%d is marked to not be removed: %s%n", item.getDisplayName(), build.getNumber(), whyKeepLog);
listener.getLogger().printf("Will not remove %s as %s is marked to not be removed: %s%n", item.getDisplayName(), build.getFullDisplayName(), whyKeepLog);
iterator.remove();
}
}
Expand Down
Expand Up @@ -41,6 +41,7 @@
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Rule;
Expand Down Expand Up @@ -112,7 +113,7 @@ public void runningBuild() throws Exception {
d.recompute();
d.assertItemNames(1, "A", "B");
d.getItem("B").getBuildersList().add(new SleepBuilder(Long.MAX_VALUE));
FreeStyleBuild b1 = d.getItem("B").scheduleBuild2(0).getStartCondition().get();
FreeStyleBuild b1 = d.getItem("B").scheduleBuild2(0).waitForStart();
d.kids.remove("B");
d.recompute();
d.assertItemNames(2, "A", "B");
Expand All @@ -132,6 +133,25 @@ public void runningBuild() throws Exception {
d.assertItemNames(5);
}

/** Verify that running branch projects are not deleted even after an organization folder reindex. */
@Issue("JENKINS-25240")
@Test
public void runningBuildMeta() throws Exception {
SecondOrderComputedFolder org = r.jenkins.createProject(SecondOrderComputedFolder.class, "org");
org.metakids.add(Arrays.asList("A", "B"));
org.metakids.add(Arrays.asList("C", "D"));
org.assertItemNames("A+B", "C+D");
FreeStyleProject b = r.jenkins.getItemByFullName("org/A+B/B", FreeStyleProject.class);
b.getBuildersList().add(new SleepBuilder(Long.MAX_VALUE));
FreeStyleBuild b1 = b.scheduleBuild2(0).waitForStart();
org.metakids.remove(0);
org.assertItemNames("A+B", "C+D");
assertTrue(b1.isBuilding());
b1.doStop();
r.assertBuildStatus(Result.ABORTED, r.waitForCompletion(b1));
org.assertItemNames("C+D");
}

@SuppressWarnings({"unchecked", "rawtypes"})
public static class SampleComputedFolder extends ComputedFolder<FreeStyleProject> {

Expand Down Expand Up @@ -209,4 +229,59 @@ public TopLevelItem newInstance(ItemGroup parent, String name) {

}

@SuppressWarnings({"unchecked", "rawtypes"})
public static class SecondOrderComputedFolder extends ComputedFolder<SampleComputedFolder> {

List<List<String>> metakids = new ArrayList<List<String>>();

private SecondOrderComputedFolder(ItemGroup parent, String name) {
super(parent, name);
}

@Override
protected void computeChildren(ChildObserver<SampleComputedFolder> observer, TaskListener listener) throws IOException, InterruptedException {
for (List<String> kids : metakids) {
String childName = StringUtils.join(kids, '+');
listener.getLogger().println("considering " + childName);
SampleComputedFolder d = observer.shouldUpdate(childName);
if (d == null) {
if (observer.mayCreate(childName)) {
listener.getLogger().println("creating a child");
d = new SampleComputedFolder(this, childName);
d.kids = kids;
observer.created(d);
} else {
listener.getLogger().println("not allowed to create a child");
}
} else {
listener.getLogger().println("left existing child");
}
}

}

void assertItemNames(String... names) throws Exception {
scheduleBuild2(0).getFuture().get();
getComputation().writeWholeLogTo(System.out);
Set<String> actual = new TreeSet<String>();
for (SampleComputedFolder d : getItems()) {
d.recompute();
d.assertItemNames(d.round, d.kids.toArray(new String[0]));
actual.add(d.getName());
}
assertEquals(new TreeSet<String>(Arrays.asList(names)).toString(), actual.toString());
}

@TestExtension
public static class DescriptorImpl extends AbstractFolderDescriptor {

@Override
public TopLevelItem newInstance(ItemGroup parent, String name) {
return new SecondOrderComputedFolder(parent, name);
}

}

}

}

0 comments on commit 3857169

Please sign in to comment.