Skip to content

Commit

Permalink
[JENKINS-29412] - Minimal Java Level should be determined dynamically (
Browse files Browse the repository at this point in the history
…#66)

* [JENKINS-29412] - Minimal Java Level should be determined dynamically

Now the plugin uses Jenkins core version.

* [JENKINS-29412] - Integrate tests written by @daniel-beck

* [JENKINS-29412] - Refactor the tests, fix the glitch in the logic

* [JENKINS-29412] - Revert encoding changes

* [JENKINS-29412] - Remove the reflection code for now as we agreed with @daniel-beck

* [JENKINS-29412] - Revert the encoding changes again

* [JENKINS-29412] - Workaround the message encoding issue in the Spanish localization
  • Loading branch information
oleg-nenashev committed Aug 17, 2017
1 parent 38a7e85 commit f0634f1
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 15 deletions.
42 changes: 41 additions & 1 deletion src/main/java/hudson/plugins/sshslaves/JavaProvider.java
Expand Up @@ -27,16 +27,25 @@
import hudson.ExtensionList;
import hudson.ExtensionPoint;
import hudson.slaves.SlaveComputer;
import hudson.model.Hudson;
import hudson.model.TaskListener;
import hudson.util.VersionNumber;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import java.util.List;
import java.util.Collections;
import javax.annotation.Nonnull;
import jenkins.model.Jenkins;

/**
* Guess where Java is.
*/
public abstract class JavaProvider implements ExtensionPoint {

private static final VersionNumber JAVA_LEVEL_7 = new VersionNumber("7");
private static final VersionNumber JAVA_LEVEL_8 = new VersionNumber("8");
private static final VersionNumber JAVA_8_MINIMAL_SINCE = new VersionNumber("2.54");

/**
* @deprecated
* Override {@link #getJavas(SlaveComputer, TaskListener, Connection)} instead.
Expand All @@ -62,4 +71,35 @@ public static ExtensionList<JavaProvider> all() {
return ExtensionList.lookup(JavaProvider.class);
}

/**
* Gets minimal required Java version.
*
* @return Minimal Java version required on the master and agent side.
* It will be {@link #JAVA_LEVEL_7} if the core version cannot be determined due to whatever reason.
* @since TODO
*
*/
@Nonnull
public static VersionNumber getMinJavaLevel() {
// TODO: Use reflection to utilize new core API once JENKINS-43500 is integrated
// TODO: Get rid of it once Jenkins core requirement is bumped
/*try {
Method method = Jenkins.class.getMethod("getJavaMinLevel");
Object res = method.invoke(null);
if (res instanceof VersionNumber) {
return (VersionNumber) res;
}
} catch(SecurityException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
// Fallback to the default behavior, not supported yet
}*/

// Now use the known map of the previous updates
final VersionNumber version = Jenkins.getVersion();
if (version == null) {
// Version cannot be determined, assume it's an old one to retain compatibility.
return JAVA_LEVEL_7;
}

return version.isOlderThan(JAVA_8_MINIMAL_SINCE) ? JAVA_LEVEL_7 : JAVA_LEVEL_8;
}
}
9 changes: 6 additions & 3 deletions src/main/java/hudson/plugins/sshslaves/SSHLauncher.java
Expand Up @@ -134,6 +134,7 @@
import static hudson.Util.*;
import hudson.model.Computer;
import hudson.security.AccessControlled;
import hudson.util.VersionNumber;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import static java.util.logging.Level.*;
Expand Down Expand Up @@ -1251,15 +1252,17 @@ protected String checkJavaVersion(final PrintStream logger, String javaCommand,
getTimestamp(), javaCommand, versionStr));

// parse as a number and we should be OK as all we care about is up through the first dot.
final VersionNumber minJavaLevel = JavaProvider.getMinJavaLevel();
try {
final Number version =
NumberFormat.getNumberInstance(Locale.US).parse(versionStr);
if(version.doubleValue() < 1.5) {
//TODO: burn it with fire
if(version.doubleValue() < Double.parseDouble("1."+minJavaLevel)) {
throw new IOException(Messages
.SSHLauncher_NoJavaFound(line));
.SSHLauncher_NoJavaFound2(line, minJavaLevel.toString()));
}
} catch(final ParseException e) {
throw new IOException(Messages.SSHLauncher_NoJavaFound(line));
throw new IOException(Messages.SSHLauncher_NoJavaFound2(line, minJavaLevel));
}
return javaCommand;
}
Expand Down
Expand Up @@ -10,7 +10,8 @@ SSHLauncher.ConnectionClosed={0} [SSH] Connection closed.
SSHLauncher.ErrorWhileClosingConnection=Exception thrown while closing connection.
SSHLauncher.AbortedDuringConnectionOpen=Slave start aborted.
SSHLauncher.FailedToDetectEnvironment=Failed to detect the environment for automatic JDK installation. Please report this to jenkinsci-users@googlegroups.com: {0}
SSHLauncher.NoJavaFound=Java version {0} was found but 1.5 or later is needed.
SSHLauncher.NoJavaFound=Java version {0} was found but 1.7 or later is needed.
SSHLauncher.NoJavaFound2=Java version {0} was found but 1.{1} or later is needed.
SSHLauncher.NoPrivateKey={0} [SSH] Private key file "{1}" doesn''t exist. Skipping public key authenitcation.
SSHLauncher.JavaVersionResult={0} [SSH] {1} -version returned {2}.
SSHLauncher.OpeningSSHConnection={0} [SSH] Opening SSH connection to {1}.
Expand Down
35 changes: 25 additions & 10 deletions src/test/java/hudson/plugins/sshslaves/SSHLauncherTest.java
Expand Up @@ -52,6 +52,7 @@
import java.util.concurrent.ExecutionException;
import org.jenkinsci.test.acceptance.docker.DockerRule;
import org.jenkinsci.test.acceptance.docker.fixtures.JavaContainer;
import org.junit.Assert;

import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -83,29 +84,34 @@ public void checkJavaVersionOpenJDK7NetBSD() throws Exception {

@Test
public void checkJavaVersionOpenJDK6Linux() throws Exception {
assertTrue("OpenJDK6 on Linux should be supported", checkSupported("openjdk-6-linux.version"));
assertNotSupported("openjdk-6-linux.version");
}

@Test
public void checkJavaVersionSun6Linux() throws Exception {
assertTrue("Sun 6 on Linux should be supported", checkSupported("sun-java-1.6-linux.version"));
assertNotSupported("sun-java-1.6-linux.version");
}

@Test
public void checkJavaVersionSun6Mac() throws Exception {
assertTrue("Sun 6 on Mac should be supported", checkSupported("sun-java-1.6-mac.version"));
assertNotSupported("sun-java-1.6-mac.version");
}

@Test
public void checkJavaVersionSun4Linux() {
try {
checkSupported("sun-java-1.4-linux.version");
fail();
} catch (IOException e) {
//
}
public void testCheckJavaVersionOracle7Mac() throws Exception {
Assert.assertTrue("Oracle 7 on Mac should be supported", checkSupported("oracle-java-1.7-mac.version"));
}

@Test
public void testCheckJavaVersionOracle8Mac() throws Exception {
Assert.assertTrue("Oracle 8 on Mac should be supported", checkSupported("oracle-java-1.8-mac.version"));
}

@Test
public void checkJavaVersionSun4Linux() throws IOException {
assertNotSupported("sun-java-1.4-linux.version");
}

/**
* Returns true if the version is supported.
*
Expand All @@ -126,6 +132,15 @@ private static boolean checkSupported(final String testVersionOutput) throws IOE
return null != result;
}

private static void assertNotSupported(final String testVersionOutput) throws AssertionError, IOException {
try {
checkSupported(testVersionOutput);
fail("Expected version " + testVersionOutput + " to be not supported, but it is supported");
} catch (IOException e) {
// expected
}
}

@Test
public void configurationRoundtrip() throws Exception {
SystemCredentialsProvider.getInstance().getDomainCredentialsMap().put(Domain.global(),
Expand Down
@@ -0,0 +1,3 @@
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)
@@ -0,0 +1,3 @@
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

0 comments on commit f0634f1

Please sign in to comment.