Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-71406] Flake in ExecutorTest.apiPermissions caused by builds left running #8157

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions test/src/test/java/hudson/model/ComputerSetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,19 @@ public void testTerminatedNodeStatusPageDoesNotShowTrace() throws Exception {
p.setAssignedNode(agent);

Future<FreeStyleBuild> 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
Expand All @@ -167,15 +170,18 @@ public void testTerminatedNodeAjaxExecutorsDoesNotShowTrace() throws Exception {
p.setAssignedNode(agent);

Future<FreeStyleBuild> 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);
}
}
}
46 changes: 26 additions & 20 deletions test/src/test/java/hudson/model/ComputerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,19 @@ public void testTerminatedNodeStatusPageDoesNotShowTrace() throws Exception {
p.setAssignedNode(agent);

Future<FreeStyleBuild> 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
Expand All @@ -248,16 +251,19 @@ public void testTerminatedNodeAjaxExecutorsDoesNotShowTrace() throws Exception {
p.setAssignedNode(agent);

Future<FreeStyleBuild> 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);
}
}

}
145 changes: 86 additions & 59 deletions test/src/test/java/hudson/model/ExecutorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -82,22 +95,25 @@ public void abortCause() throws Exception {
FreeStyleProject p = j.createFreeStyleProject();

Future<FreeStyleBuild> 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
Expand All @@ -107,19 +123,23 @@ public void disconnectCause() throws Exception {
p.setAssignedNode(slave);

Future<FreeStyleBuild> 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")
Expand All @@ -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
Expand All @@ -161,19 +185,22 @@ public void disconnectCause_WithoutTrace() throws Exception {
p.setAssignedNode(slave);

Future<FreeStyleBuild> 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
Expand Down