diff --git a/test/src/test/java/hudson/model/ComputerSetTest.java b/test/src/test/java/hudson/model/ComputerSetTest.java index 703376b19f08..a128c7e5cd51 100644 --- a/test/src/test/java/hudson/model/ComputerSetTest.java +++ b/test/src/test/java/hudson/model/ComputerSetTest.java @@ -147,16 +147,19 @@ public void testTerminatedNodeStatusPageDoesNotShowTrace() throws Exception { p.setAssignedNode(agent); Future r = ExecutorTest.startBlockingBuild(p); - - String message = "It went away"; - p.getLastBuild().getBuiltOn().toComputer().disconnect( - new OfflineCause.ChannelTermination(new RuntimeException(message)) - ); - - WebClient wc = j.createWebClient(); - Page page = wc.getPage(wc.createCrumbedUrl(agent.toComputer().getUrl())); - String content = page.getWebResponse().getContentAsString(); - assertThat(content, not(containsString(message))); + try { + String message = "It went away"; + p.getLastBuild().getBuiltOn().toComputer().disconnect( + new OfflineCause.ChannelTermination(new RuntimeException(message)) + ); + + WebClient wc = j.createWebClient(); + Page page = wc.getPage(wc.createCrumbedUrl(agent.toComputer().getUrl())); + String content = page.getWebResponse().getContentAsString(); + assertThat(content, not(containsString(message))); + } finally { + ExecutorTest.stopRunningBuilds(j); + } } @Test @@ -167,15 +170,18 @@ public void testTerminatedNodeAjaxExecutorsDoesNotShowTrace() throws Exception { p.setAssignedNode(agent); Future r = ExecutorTest.startBlockingBuild(p); - - String message = "It went away"; - p.getLastBuild().getBuiltOn().toComputer().disconnect( - new OfflineCause.ChannelTermination(new RuntimeException(message)) - ); - - WebClient wc = j.createWebClient(); - Page page = wc.getPage(wc.createCrumbedUrl(HasWidgetHelper.getWidget(j.jenkins.getComputer(), ExecutorsWidget.class).orElseThrow().getUrl() + "ajax")); - String content = page.getWebResponse().getContentAsString(); - assertThat(content, not(containsString(message))); + try { + String message = "It went away"; + p.getLastBuild().getBuiltOn().toComputer().disconnect( + new OfflineCause.ChannelTermination(new RuntimeException(message)) + ); + + WebClient wc = j.createWebClient(); + Page page = wc.getPage(wc.createCrumbedUrl(HasWidgetHelper.getWidget(j.jenkins.getComputer(), ExecutorsWidget.class).orElseThrow().getUrl() + "ajax")); + String content = page.getWebResponse().getContentAsString(); + assertThat(content, not(containsString(message))); + } finally { + ExecutorTest.stopRunningBuilds(j); + } } } diff --git a/test/src/test/java/hudson/model/ComputerTest.java b/test/src/test/java/hudson/model/ComputerTest.java index 6d5b9a558561..49e451da5e9d 100644 --- a/test/src/test/java/hudson/model/ComputerTest.java +++ b/test/src/test/java/hudson/model/ComputerTest.java @@ -229,16 +229,19 @@ public void testTerminatedNodeStatusPageDoesNotShowTrace() throws Exception { p.setAssignedNode(agent); Future r = ExecutorTest.startBlockingBuild(p); - - String message = "It went away"; - p.getLastBuild().getBuiltOn().toComputer().disconnect( - new OfflineCause.ChannelTermination(new RuntimeException(message)) - ); - - WebClient wc = j.createWebClient(); - Page page = wc.getPage(wc.createCrumbedUrl(agent.toComputer().getUrl())); - String content = page.getWebResponse().getContentAsString(); - assertThat(content, not(containsString(message))); + try { + String message = "It went away"; + p.getLastBuild().getBuiltOn().toComputer().disconnect( + new OfflineCause.ChannelTermination(new RuntimeException(message)) + ); + + WebClient wc = j.createWebClient(); + Page page = wc.getPage(wc.createCrumbedUrl(agent.toComputer().getUrl())); + String content = page.getWebResponse().getContentAsString(); + assertThat(content, not(containsString(message))); + } finally { + ExecutorTest.stopRunningBuilds(j); + } } @Test @@ -248,16 +251,19 @@ public void testTerminatedNodeAjaxExecutorsDoesNotShowTrace() throws Exception { p.setAssignedNode(agent); Future r = ExecutorTest.startBlockingBuild(p); - - String message = "It went away"; - p.getLastBuild().getBuiltOn().toComputer().disconnect( - new OfflineCause.ChannelTermination(new RuntimeException(message)) - ); - - WebClient wc = j.createWebClient(); - Page page = wc.getPage(wc.createCrumbedUrl(HasWidgetHelper.getWidget(agent.toComputer(), ExecutorsWidget.class).orElseThrow().getUrl() + "ajax")); - String content = page.getWebResponse().getContentAsString(); - assertThat(content, not(containsString(message))); + try { + String message = "It went away"; + p.getLastBuild().getBuiltOn().toComputer().disconnect( + new OfflineCause.ChannelTermination(new RuntimeException(message)) + ); + + WebClient wc = j.createWebClient(); + Page page = wc.getPage(wc.createCrumbedUrl(HasWidgetHelper.getWidget(agent.toComputer(), ExecutorsWidget.class).orElseThrow().getUrl() + "ajax")); + String content = page.getWebResponse().getContentAsString(); + assertThat(content, not(containsString(message))); + } finally { + ExecutorTest.stopRunningBuilds(j); + } } } diff --git a/test/src/test/java/hudson/model/ExecutorTest.java b/test/src/test/java/hudson/model/ExecutorTest.java index 0f83110f52e9..b624557ef137 100644 --- a/test/src/test/java/hudson/model/ExecutorTest.java +++ b/test/src/test/java/hudson/model/ExecutorTest.java @@ -34,6 +34,19 @@ public class ExecutorTest { @Rule public JenkinsRule j = new JenkinsRule(); + public static void stopRunningBuilds(JenkinsRule j) throws InterruptedException { + for (Job job : j.jenkins.allItems(Job.class)) { + for (Run build : job.getBuilds()) { + Executor executor = build.getExecutor(); + if (executor != null) { + System.out.println("Stopping " + build); + executor.interrupt(); + j.waitForCompletion(build); + } + } + } + } + @Test @Issue("JENKINS-4756") public void whenAnExecutorDiesHardANewExecutorTakesItsPlace() throws Exception { @@ -82,22 +95,25 @@ public void abortCause() throws Exception { FreeStyleProject p = j.createFreeStyleProject(); Future r = startBlockingBuild(p); - - User johnny = User.getOrCreateByIdOrFullName("Johnny"); - p.getLastBuild().getExecutor().interrupt(Result.FAILURE, - new UserInterruption(johnny), // test the merge semantics - new UserInterruption(johnny)); - - FreeStyleBuild b = r.get(); - - // make sure this information is recorded - j.assertBuildStatus(Result.FAILURE, j.waitForCompletion(b)); - InterruptedBuildAction iba = b.getAction(InterruptedBuildAction.class); - assertEquals(1, iba.getCauses().size()); - assertEquals(((UserInterruption) iba.getCauses().get(0)).getUser(), johnny); - - // make sure it shows up in the log - j.assertLogContains(johnny.getId(), b); + try { + User johnny = User.getOrCreateByIdOrFullName("Johnny"); + p.getLastBuild().getExecutor().interrupt(Result.FAILURE, + new UserInterruption(johnny), // test the merge semantics + new UserInterruption(johnny)); + + FreeStyleBuild b = r.get(); + + // make sure this information is recorded + j.assertBuildStatus(Result.FAILURE, j.waitForCompletion(b)); + InterruptedBuildAction iba = b.getAction(InterruptedBuildAction.class); + assertEquals(1, iba.getCauses().size()); + assertEquals(((UserInterruption) iba.getCauses().get(0)).getUser(), johnny); + + // make sure it shows up in the log + j.assertLogContains(johnny.getId(), b); + } finally { + ExecutorTest.stopRunningBuilds(j); + } } @Test @@ -107,19 +123,23 @@ public void disconnectCause() throws Exception { p.setAssignedNode(slave); Future r = startBlockingBuild(p); - User johnny = User.getOrCreateByIdOrFullName("Johnny"); - - p.getLastBuild().getBuiltOn().toComputer().disconnect( - new OfflineCause.UserCause(johnny, "Taking offline to break your build") - ); - - FreeStyleBuild b = r.get(); - - j.assertBuildStatus(Result.FAILURE, j.waitForCompletion(b)); - j.assertLogContains("Finished: FAILURE", b); - j.assertLogContains("Build step 'BlockingBuilder' marked build as failure", b); - j.assertLogContains("Agent went offline during the build", b); - j.assertLogContains("Disconnected by Johnny : Taking offline to break your build", b); + try { + User johnny = User.getOrCreateByIdOrFullName("Johnny"); + + p.getLastBuild().getBuiltOn().toComputer().disconnect( + new OfflineCause.UserCause(johnny, "Taking offline to break your build") + ); + + FreeStyleBuild b = r.get(); + + j.assertBuildStatus(Result.FAILURE, j.waitForCompletion(b)); + j.assertLogContains("Finished: FAILURE", b); + j.assertLogContains("Build step 'BlockingBuilder' marked build as failure", b); + j.assertLogContains("Agent went offline during the build", b); + j.assertLogContains("Disconnected by Johnny : Taking offline to break your build", b); + } finally { + ExecutorTest.stopRunningBuilds(j); + } } @Issue("SECURITY-611") @@ -131,26 +151,30 @@ public void apiPermissions() throws Exception { FreeStyleProject publicProject = j.createFreeStyleProject("public-project"); publicProject.setAssignedNode(slave); startBlockingBuild(publicProject); - FreeStyleProject secretProject = j.createFreeStyleProject("secret-project"); - secretProject.setAssignedNode(slave); - startBlockingBuild(secretProject); - j.jenkins.setSecurityRealm(j.createDummySecurityRealm()); - j.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy(). - grant(Jenkins.READ).everywhere().toEveryone(). - grant(Item.READ).onItems(publicProject).toEveryone(). - grant(Item.READ).onItems(secretProject).to("has-security-clearance")); - - JenkinsRule.WebClient wc = j.createWebClient(); - wc.withBasicCredentials("has-security-clearance"); - String api = wc.goTo(slave.toComputer().getUrl() + "api/json?pretty&depth=1", null).getWebResponse().getContentAsString(); - System.out.println(api); - assertThat(api, allOf(containsString("public-project"), containsString("secret-project"))); - - wc = j.createWebClient(); - wc.withBasicCredentials("regular-joe"); - api = wc.goTo(slave.toComputer().getUrl() + "api/json?pretty&depth=1", null).getWebResponse().getContentAsString(); - System.out.println(api); - assertThat(api, allOf(containsString("public-project"), not(containsString("secret-project")))); + try { + FreeStyleProject secretProject = j.createFreeStyleProject("secret-project"); + secretProject.setAssignedNode(slave); + startBlockingBuild(secretProject); + j.jenkins.setSecurityRealm(j.createDummySecurityRealm()); + j.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy(). + grant(Jenkins.READ).everywhere().toEveryone(). + grant(Item.READ).onItems(publicProject).toEveryone(). + grant(Item.READ).onItems(secretProject).to("has-security-clearance")); + + JenkinsRule.WebClient wc = j.createWebClient(); + wc.withBasicCredentials("has-security-clearance"); + String api = wc.goTo(slave.toComputer().getUrl() + "api/json?pretty&depth=1", null).getWebResponse().getContentAsString(); + System.out.println(api); + assertThat(api, allOf(containsString("public-project"), containsString("secret-project"))); + + wc = j.createWebClient(); + wc.withBasicCredentials("regular-joe"); + api = wc.goTo(slave.toComputer().getUrl() + "api/json?pretty&depth=1", null).getWebResponse().getContentAsString(); + System.out.println(api); + assertThat(api, allOf(containsString("public-project"), not(containsString("secret-project")))); + } finally { + ExecutorTest.stopRunningBuilds(j); + } } @Test @@ -161,19 +185,22 @@ public void disconnectCause_WithoutTrace() throws Exception { p.setAssignedNode(slave); Future r = startBlockingBuild(p); - - String message = "It went away"; - p.getLastBuild().getBuiltOn().toComputer().disconnect( - new OfflineCause.ChannelTermination(new RuntimeException(message)) - ); - - OfflineCause offlineCause = p.getLastBuild().getBuiltOn().toComputer().getOfflineCause(); - Assert.assertThat(offlineCause.toString(), not(containsString(message))); + try { + String message = "It went away"; + p.getLastBuild().getBuiltOn().toComputer().disconnect( + new OfflineCause.ChannelTermination(new RuntimeException(message)) + ); + + OfflineCause offlineCause = p.getLastBuild().getBuiltOn().toComputer().getOfflineCause(); + Assert.assertThat(offlineCause.toString(), not(containsString(message))); + } finally { + ExecutorTest.stopRunningBuilds(j); + } } /** - * Start a project with an infinite build step - * + * Start a project with an infinite build step. + * You must clean up, for example with {@link #stopRunningBuilds(JenkinsRule)}. * @param project {@link FreeStyleProject} to start * @return A {@link Future} object represents the started build * @throws Exception if somethink wrong happened