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 6 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
19 changes: 18 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,22 @@ THE SOFTWARE.
</pluginRepository>
</pluginRepositories>

<dependencyManagement>
<dependencies>
<!-- RequireUpperBoundDeps -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>jenkins-war</artifactId>
<version>2.204</version>
<version>2.322-rc31716.e9b8cbf30063</version><!-- https://github.com/jenkinsci/jenkins/pull/5928 and https://github.com/jenkinsci/winstone/pull/174 -->
jglick marked this conversation as resolved.
Show resolved Hide resolved
<type>executable-war</type>
<exclusions>
<exclusion>
Expand Down Expand Up @@ -148,6 +159,12 @@ THE SOFTWARE.
</exclusion>
</exclusions>
</dependency>
<dependency>
<!-- Removed from jenkins core but still used here apparently... -->
<groupId>commons-digester</groupId>
<artifactId>commons-digester</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.jvnet.hudson</groupId>
<artifactId>embedded-rhino-debugger</artifactId>
Expand Down
70 changes: 0 additions & 70 deletions src/main/java/org/jvnet/hudson/test/IOUtil.java

This file was deleted.

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 @@ -141,6 +141,9 @@ public final class RealJenkinsRule implements TestRule {

/**
* TCP/IP port that the server is listening on.
*
* Before the first start, it will be 0. Once started, it is set to the actual port Jenkins is listening to.
*
Vlatombe marked this conversation as resolved.
Show resolved Hide resolved
* Like the home directory, this will be consistent across restarts.
*/
private int port;
Expand Down Expand Up @@ -245,7 +248,6 @@ public RealJenkinsRule withHost(String host) {
System.out.println("=== Starting " + description);
try {
home = tmp.allocate();
port = IOUtil.randomTcpPort(); // https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers#Dynamic,_private_or_ephemeral_ports
File plugins = new File(home, "plugins");
plugins.mkdir();
FileUtils.copyURLToFile(RealJenkinsRule.class.getResource("RealJenkinsRuleInit.jpi"), new File(plugins, "RealJenkinsRuleInit.jpi"));
Expand Down Expand Up @@ -412,12 +414,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/",
jglick marked this conversation as resolved.
Show resolved Hide resolved
"-DRealJenkinsRule.description=" + description,
"-DRealJenkinsRule.token=" + token));
if (new DisableOnDebug(null).isDebugging()) {
Expand All @@ -426,7 +431,8 @@ public void startJenkins() throws Throwable {
argv.addAll(javaOptions);
argv.addAll(Arrays.asList(
"-jar", findJenkinsWar().getAbsolutePath(),
"--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 @@ -443,32 +449,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);
}
} 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 @@ -484,6 +495,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 @@ -639,7 +659,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