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

Ensure RealJenkinsRule tests always start on a free port #348

Merged
merged 23 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ THE SOFTWARE.

<properties>
<changelist>999999-SNAPSHOT</changelist>
<jenkins.version>2.204</jenkins.version>
<jenkins.version>2.339</jenkins.version>
Vlatombe marked this conversation as resolved.
Show resolved Hide resolved
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jetty.version>9.4.45.v20220203</jetty.version>
<hamcrest.version>2.2</hamcrest.version>
Expand Down
71 changes: 46 additions & 25 deletions src/main/java/org/jvnet/hudson/test/RealJenkinsRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ public final class RealJenkinsRule implements TestRule {

/**
* TCP/IP port that the server is listening on.
* <p>
* Before the first start, it will be 0. Once started, it is set to the actual port Jenkins is listening to.
* <p>
* Like the home directory, this will be consistent across restarts.
*/
private int port;
Expand Down Expand Up @@ -248,7 +251,6 @@ public RealJenkinsRule withHost(String host) {
if (localData != null) {
new HudsonHomeLoader.Local(description.getTestClass().getMethod(description.getMethodName()), localData.value()).copy(home);
}
port = IOUtil.randomTcpPort();
File plugins = new File(home, "plugins");
plugins.mkdir();
FileUtils.copyURLToFile(RealJenkinsRule.class.getResource("RealJenkinsRuleInit.jpi"), new File(plugins, "RealJenkinsRuleInit.jpi"));
Expand Down Expand Up @@ -423,12 +425,15 @@ public void startJenkins() throws Throwable {
}
String cp = System.getProperty("java.class.path");
FileUtils.writeLines(new File(home, "RealJenkinsRule-cp.txt"), Arrays.asList(cp.split(File.pathSeparator)));
File portFile = new File(home, "jenkins-port.txt");
List<String> argv = new ArrayList<>(Arrays.asList(
new File(System.getProperty("java.home"), "bin/java").getAbsolutePath(),
"-ea",
"-Dhudson.Main.development=true",
"-Dwinstone.portFileName=" + portFile,
"-DRealJenkinsRule.location=" + RealJenkinsRule.class.getProtectionDomain().getCodeSource().getLocation(),
"-DRealJenkinsRule.url=" + getUrl(),
"-DRealJenkinsRule.host=" + host,
"-DRealJenkinsRule.path=/jenkins/",
"-DRealJenkinsRule.description=" + description,
"-DRealJenkinsRule.token=" + token));
if (new DisableOnDebug(null).isDebugging()) {
Expand All @@ -438,7 +443,8 @@ public void startJenkins() throws Throwable {
argv.addAll(Arrays.asList(
"-jar", findJenkinsWar().getAbsolutePath(),
"--enable-future-java",
"--httpPort=" + port, "--httpListenAddress=127.0.0.1",
"--httpPort=" + port,
Vlatombe marked this conversation as resolved.
Show resolved Hide resolved
"--httpListenAddress=127.0.0.1",
"--prefix=/jenkins"));
ProcessBuilder pb = new ProcessBuilder(argv);
System.out.println("Launching: " + pb.command());
Expand All @@ -455,32 +461,37 @@ public void startJenkins() throws Throwable {
// TODO prefix streams with per-test timestamps & port
new StreamCopyThread(description.toString(), proc.getInputStream(), System.out).start();
new StreamCopyThread(description.toString(), proc.getErrorStream(), System.err).start();
URL status = endpoint("status");
int tries = 0;
while (true) {
try {
HttpURLConnection conn = (HttpURLConnection) status.openConnection();
int code = conn.getResponseCode();
if (code == 200) {
conn.getInputStream().close();
break;
} else {
String err = "?";
try (InputStream is = conn.getErrorStream()) {
if (is != null) {
err = IOUtils.toString(is);
if (port != 0 || portFile.exists()) {
if (port == 0) {
port = readPort(portFile);
}
try {
URL status = endpoint("status");
HttpURLConnection conn = (HttpURLConnection) status.openConnection();
int code = conn.getResponseCode();
if (code == 200) {
conn.getInputStream().close();
break;
} else {
String err = "?";
try (InputStream is = conn.getErrorStream()) {
if (is != null) {
err = IOUtils.toString(is, StandardCharsets.UTF_8);
}
} catch (Exception x) {
x.printStackTrace();
}
} catch (Exception x) {
throw new IOException("Response code " + code + " for " + status + ": " + err + " " + conn.getHeaderFields());
}
} catch (Exception x) {
tries++;
if (tries == /* 3m */ 1800) {
throw new AssertionError("Jenkins did not start after 3m");
} else if (tries % /* 1m */ 600 == 0) {
x.printStackTrace();
}
throw new IOException("Response code " + code + " for " + status + ": " + err + " " + conn.getHeaderFields());
}
} catch (Exception x) {
tries++;
if (tries == /* 3m */ 1800) {
throw new AssertionError("Jenkins did not start after 3m");
} else if (tries % /* 1m */ 600 == 0) {
x.printStackTrace();
}
}
Thread.sleep(100);
Expand All @@ -496,6 +507,15 @@ public void startJenkins() throws Throwable {
}
}

private static int readPort(File portFile) throws IOException {
String s = FileUtils.readFileToString(portFile, StandardCharsets.UTF_8);
try {
return Integer.parseInt(s);
} catch (NumberFormatException e) {
throw new AssertionError("Unable to parse port from " + s + ". Jenkins did not start.");
}
}

public void stopJenkins() throws Throwable {
endpoint("exit").openStream().close();
if (!proc.waitFor(60, TimeUnit.SECONDS) ) {
Expand Down Expand Up @@ -651,7 +671,8 @@ public CustomJenkinsRule() throws Exception {
}

@Override public URL getURL() throws IOException {
return new URL(System.getProperty("RealJenkinsRule.url"));
int port = readPort(new File(System.getProperty("winstone.portFileName")));
jglick marked this conversation as resolved.
Show resolved Hide resolved
return new URL("http://" + System.getProperty("RealJenkinsRule.host") + ":" + port + System.getProperty("RealJenkinsRule.path"));
}

@Override public void close() throws Exception {
Expand Down