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

Allow RealJenkinsRule controllers to know surefire.forkNumber #513

Merged
merged 1 commit into from
Nov 21, 2022
Merged
Changes from all 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
17 changes: 13 additions & 4 deletions src/main/java/org/jvnet/hudson/test/RealJenkinsRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -530,14 +530,23 @@ public void startJenkins() throws Throwable {
"--httpPort=" + port, // initially port=0. On subsequent runs, the port is set to the port used allocated randomly on the first run.
"--httpListenAddress=127.0.0.1",
"--prefix=/jenkins"));
ProcessBuilder pb = new ProcessBuilder(argv);
System.out.println("Launching: " + pb.command());
pb.environment().put("JENKINS_HOME", home.getAbsolutePath());
Map<String, String> env = new TreeMap<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice touch to use something better than HashMap here (and consistent with hudson.EnvVars, which also uses TreeMap). It's too bad that ProcessBuilder#environment uses a HashMap internally, scrambling the order of whatever the user passes in. I wish hudson.EnvVars had extended LinkedHashMap (insertion order), because that is how libc's setenv(3) works, but that ship sailed a long time ago.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know the ultimate order is irrelevant, but it is much nicer to print the variables in order below.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but it is much nicer to print the variables in order below.

… which is why I wrote it was a nice touch above.

env.put("JENKINS_HOME", home.getAbsolutePath());
String forkNumber = System.getProperty("surefire.forkNumber");
if (forkNumber != null) {
// https://maven.apache.org/surefire/maven-surefire-plugin/examples/fork-options-and-parallel-execution.html#forked-test-execution
// Otherwise accessible only to the Surefire JVM, not to the Jenkins controller JVM.
env.put("SUREFIRE_FORK_NUMBER", forkNumber);
}
for (Map.Entry<String, String> entry : extraEnv.entrySet()) {
if (entry.getValue() != null) {
pb.environment().put(entry.getKey(), entry.getValue());
env.put(entry.getKey(), entry.getValue());
}
}
// TODO escape spaces like Launcher.printCommandLine, or LabelAtom.escape (beware that QuotedStringTokenizer.quote(String) Javadoc is untrue):
System.out.println(env.entrySet().stream().map(Map.Entry::toString).collect(Collectors.joining(" ")) + " " + argv.stream().collect(Collectors.joining(" ")));
ProcessBuilder pb = new ProcessBuilder(argv);
pb.environment().putAll(env);
// TODO options to set Winstone options, etc.
// TODO pluggable launcher interface to support a Dockerized Jenkins JVM
// TODO if test JVM is running in a debugger, start Jenkins JVM in a debugger also
Expand Down