Skip to content

Commit

Permalink
Merge pull request jenkinsci#115 from raul-arabaolaza/JENKINS-56312
Browse files Browse the repository at this point in the history
[JENKINS-56312] Prevent adding splits with the wrong JDK
  • Loading branch information
oleg-nenashev committed Feb 28, 2019
2 parents 76930e5 + 661782e commit 7d59f12
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
Expand Up @@ -337,8 +337,19 @@ public String getTestJavaVersion() throws IOException {
LOGGER.info("testJdkHome unset, using java available from the PATH");
javaCmdAbsolutePath = "java";
}
final Process process = new ProcessBuilder().command(javaCmdAbsolutePath, "-fullversion").redirectErrorStream(true).start();
final Process process = new ProcessBuilder().command(javaCmdAbsolutePath, "-XshowSettings:properties -version").redirectErrorStream(true).start();
final String javaVersionOutput = IOUtils.toString(process.getInputStream());
final String[] lines = javaVersionOutput.split("[\\r\\n]+");
for (String line: lines) {
String trimed = line.trim();
if (trimed.contains("java.specification.version")) {
//java.specification.version = version
return trimed.split("=")[1].trim();
}
}
// Default to fullversion output as before
final Process process2 = new ProcessBuilder().command(javaCmdAbsolutePath, "-fullversion").redirectErrorStream(true).start();
final String javaVersionOutput2 = IOUtils.toString(process2.getInputStream());
// Expected format is something like openjdk full version "1.8.0_181-8u181-b13-2~deb9u1-b13"
// We shorten it by removing the "full version" in the middle
return javaVersionOutput.
Expand Down
4 changes: 4 additions & 0 deletions plugins-compat-tester/pom.xml
Expand Up @@ -222,5 +222,9 @@
<artifactId>powermock-api-mockito</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci</groupId>
<artifactId>version-number</artifactId>
</dependency>
</dependencies>
</project>
Expand Up @@ -34,6 +34,8 @@
import hudson.model.UpdateSite.Plugin;
import hudson.util.VersionNumber;
import java.io.BufferedReader;

import io.jenkins.lib.versionnumber.JavaSpecificationVersion;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -76,6 +78,7 @@
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
Expand Down Expand Up @@ -876,6 +879,8 @@ private void updateAllDependents(String parent, Plugin dependent, Map<String,Ver
/** Use JENKINS-47634 to load metadata from jenkins-core.jar if available. */
private void populateSplits(File war) throws IOException {
System.out.println("Checking " + war + " for plugin split metadata…");
System.out.println("Checking jdk version as splits may depend on a jdk version");
JavaSpecificationVersion jdkVersion = new JavaSpecificationVersion(config.getTestJavaVersion()); // From Java 9 onwards there is a standard for versions see JEP-223
try (JarFile jf = new JarFile(war, false)) {
Enumeration<JarEntry> warEntries = jf.entries();
while (warEntries.hasMoreElements()) {
Expand All @@ -888,6 +893,9 @@ private void populateSplits(File war) throws IOException {
while ((entry = jis.getNextJarEntry()) != null) {
if (entry.getName().equals("jenkins/split-plugins.txt")) {
splits = configLines(jis).collect(Collectors.toList());
// Since https://github.com/jenkinsci/jenkins/pull/3865 splits can depend on jdk version
// So make sure we are not applying splits not intended for our JDK
splits = removeSplitsBasedOnJDK(splits, jdkVersion);
System.out.println("found splits: " + splits);
found++;
} else if (entry.getName().equals("jenkins/split-plugin-cycles.txt")) {
Expand All @@ -910,6 +918,24 @@ private void populateSplits(File war) throws IOException {
}
throw new IOException("no jenkins-core-*.jar found in " + war);
}

private List<String> removeSplitsBasedOnJDK(List<String> splits, JavaSpecificationVersion jdkVersion) {
List<String> filterSplits = new LinkedList();
for (String split : splits) {
String[] tokens = split.trim().split("\\s+");
if (tokens.length == 4 ) { // We have a jdk field in the splits file
if (jdkVersion.isNewerThan(new JavaSpecificationVersion(tokens[3]))) {
filterSplits.add(split);
} else {
System.out.println("Not adding " + split + " as split because jdk specified " + tokens[3] + " is newer than running jdk " + jdkVersion);
}
} else {
filterSplits.add(split);
}
}
return filterSplits;
}

// Matches syntax in ClassicPluginStrategy:
private static Stream<String> configLines(InputStream is) throws IOException {
return IOUtils.readLines(is, StandardCharsets.UTF_8).stream().filter(line -> !line.matches("#.*|\\s*"));
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Expand Up @@ -51,6 +51,12 @@
<version>1.625.3</version>
</dependency>

<dependency>
<groupId>org.jenkins-ci</groupId>
<artifactId>version-number</artifactId>
<version>1.6</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down

0 comments on commit 7d59f12

Please sign in to comment.