Skip to content

Commit

Permalink
[JENKINS-26940] Print message when installer isn't applicable (#2598)
Browse files Browse the repository at this point in the history
* [JENKINS-26940] Print message when installer isn't applicable

* [JENKINS-26940] Only print when none found, add test
  • Loading branch information
daniel-beck authored and oleg-nenashev committed Oct 25, 2016
1 parent 37c4736 commit ae29e6b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
12 changes: 12 additions & 0 deletions core/src/main/java/hudson/tools/InstallerTranslator.java
Expand Up @@ -28,6 +28,7 @@
import hudson.model.Node;
import hudson.model.TaskListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.Semaphore;
Expand All @@ -50,6 +51,9 @@ public String getToolHome(Node node, ToolInstallation tool, TaskListener log) th
if (isp == null) {
return null;
}

ArrayList<String> inapplicableInstallersMessages = new ArrayList<String>();

for (ToolInstaller installer : isp.installers) {
if (installer.appliesTo(node)) {
Semaphore semaphore;
Expand All @@ -69,8 +73,16 @@ public String getToolHome(Node node, ToolInstallation tool, TaskListener log) th
} finally {
semaphore.release();
}
} else {
inapplicableInstallersMessages.add(Messages.CannotBeInstalled(
installer.getDescriptor().getDisplayName(),
tool.getName(),
node.getDisplayName()));
}
}
for (String message : inapplicableInstallersMessages) {
log.getLogger().println(message);
}
return null;
}

Expand Down
1 change: 1 addition & 0 deletions core/src/main/resources/hudson/tools/Messages.properties
Expand Up @@ -37,3 +37,4 @@ JDKInstaller.DescriptorImpl.displayName=Install from java.sun.com
JDKInstaller.DescriptorImpl.doCheckId=Define JDK ID
JDKInstaller.DescriptorImpl.doCheckAcceptLicense=You must agree to the license to download the JDK.
ToolDescriptor.NotADirectory={0} is not a directory on the Jenkins master (but perhaps it exists on some agents)
CannotBeInstalled=Installer "{0}" cannot be used to install "{1}" on the node "{2}"
39 changes: 39 additions & 0 deletions test/src/test/java/hudson/tools/InstallerTranslatorTest.java
Expand Up @@ -35,6 +35,8 @@
import hudson.tasks.Shell;
import hudson.util.StreamTaskListener;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import org.junit.Test;
import static org.junit.Assert.*;
Expand Down Expand Up @@ -98,4 +100,41 @@ public class InstallerTranslatorTest {
r.assertLogContains("/opt/jdk1", b6);
}

@Issue("JENKINS-26940")
@Test
public void testMessageLoggedWhenNoInstallerFound() throws Exception {
final CommandInstaller ci = new CommandInstaller("wrong1", "echo hello", "/opt/jdk");
final BatchCommandInstaller bci = new BatchCommandInstaller("wrong2", "echo hello", "/opt/jdk2");
InstallSourceProperty isp = new InstallSourceProperty(Arrays.asList(ci, bci));

JDK jdk = new JDK("jdk", null, Collections.singletonList(isp));
r.jenkins.getJDKs().add(jdk);


FreeStyleProject p = r.createFreeStyleProject();
p.setJDK(jdk);
p.getBuildersList().add(new Shell("echo $JAVA_HOME"));
FreeStyleBuild b1 = r.buildAndAssertSuccess(p);
r.assertLogContains(hudson.tools.Messages.CannotBeInstalled(ci.getDescriptor().getDisplayName(), jdk.getName(), r.jenkins.getDisplayName()), b1);
r.assertLogContains(hudson.tools.Messages.CannotBeInstalled(bci.getDescriptor().getDisplayName(), jdk.getName(), r.jenkins.getDisplayName()), b1);
}

@Issue("JENKINS-26940")
@Test
public void testNoMessageLoggedWhenAnyInstallerFound() throws Exception {
final CommandInstaller ci = new CommandInstaller("wrong1", "echo hello", "/opt/jdk");
final CommandInstaller ci2 = new CommandInstaller("master", "echo hello", "/opt/jdk2");
InstallSourceProperty isp = new InstallSourceProperty(Arrays.asList(ci, ci2));

JDK jdk = new JDK("jdk", null, Collections.singletonList(isp));
r.jenkins.getJDKs().add(jdk);


FreeStyleProject p = r.createFreeStyleProject();
p.setJDK(jdk);
p.getBuildersList().add(new Shell("echo $JAVA_HOME"));
FreeStyleBuild b1 = r.buildAndAssertSuccess(p);
r.assertLogNotContains(ci.getDescriptor().getDisplayName(), b1);
}

}

0 comments on commit ae29e6b

Please sign in to comment.