adding a test for the headless mode, too.

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@11929 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
kohsuke 2008-09-02 05:09:39 +00:00
parent 8bf27eb3cb
commit 2ee47c68c5
1 changed files with 53 additions and 23 deletions

View File

@ -1,23 +1,22 @@
package hudson.slaves;
import hudson.Util;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import hudson.FilePath;
import hudson.Proc;
import hudson.util.ArgumentListBuilder;
import hudson.remoting.Callable;
import hudson.remoting.Which;
import hudson.Util;
import hudson.model.Computer;
import hudson.model.Node.Mode;
import hudson.model.Slave;
import hudson.model.Computer;
import hudson.remoting.Callable;
import hudson.remoting.Which;
import hudson.util.ArgumentListBuilder;
import org.jvnet.hudson.test.HudsonTestCase;
import java.io.File;
import java.util.List;
import java.util.ArrayList;
import java.net.URL;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import java.util.ArrayList;
import java.util.List;
/**
* @author Kohsuke Kawaguchi
@ -27,28 +26,35 @@ public class JNLPLauncherTest extends HudsonTestCase {
* Starts a JNLP slave agent and makes sure it successfully connects to Hudson.
*/
public void testLaunch() throws Exception {
List<Slave> slaves = new ArrayList<Slave>(hudson.getSlaves());
File dir = Util.createTempDir();
slaves.add(new Slave("test","dummy",dir.getAbsolutePath(),"1", Mode.NORMAL, "",
new JNLPLauncher(), RetentionStrategy.INSTANCE));
hudson.setSlaves(slaves);
Computer c = hudson.getComputer("test");
assertNotNull(c);
Computer c = addTestSlave();
launchJnlpAndVerify(c, buildJnlpArgs(c));
}
HtmlPage p = new WebClient().goTo("computer/test/");
String href = ((HtmlAnchor) p.getElementById("jnlp-link")).getHrefAttribute();
href = new URL(p.getWebResponse().getUrl(),href).toExternalForm();
/**
* Tests the '-headless' option.
* (Although this test doesn't really assert that the agent really is running in a headless mdoe.)
*/
public void testHeadlessLaunch() throws Exception {
Computer c = addTestSlave();
launchJnlpAndVerify(c, buildJnlpArgs(c).add("-arg","-headless"));
}
// launch slave agent
private ArgumentListBuilder buildJnlpArgs(Computer c) throws Exception {
ArgumentListBuilder args = new ArgumentListBuilder();
args.add("java","-jar");
args.add(Which.jarFile(netx.jnlp.runtime.JNLPRuntime.class).getAbsolutePath());
args.add("-nosecurity","-jnlp",href);
args.add("-nosecurity","-jnlp", getJnlpLink(c));
return args;
}
/**
* Launches the JNLP slave agent and asserts its basic operations.
*/
private void launchJnlpAndVerify(Computer c, ArgumentListBuilder args) throws Exception {
Proc proc = createLocalLauncher().launch(args.toCommandArray(),
new String[0], System.out, new FilePath(new File(".")));
try {
// verify that the connection is established, up to 10 secs
for( int i=0; i<100; i++ ) {
Thread.sleep(100);
@ -67,6 +73,30 @@ public class JNLPLauncherTest extends HudsonTestCase {
assertTrue(c.isOffline());
}
/**
* Determines the link to the .jnlp file.
*/
private String getJnlpLink(Computer c) throws Exception {
HtmlPage p = new WebClient().goTo("computer/"+c.getName()+"/");
String href = ((HtmlAnchor) p.getElementById("jnlp-link")).getHrefAttribute();
href = new URL(p.getWebResponse().getUrl(),href).toExternalForm();
return href;
}
/**
* Adds a JNLP {@link Slave} to the system and returns it.
*/
private Computer addTestSlave() throws Exception {
List<Slave> slaves = new ArrayList<Slave>(hudson.getSlaves());
File dir = Util.createTempDir();
slaves.add(new Slave("test","dummy",dir.getAbsolutePath(),"1", Mode.NORMAL, "",
new JNLPLauncher(), RetentionStrategy.INSTANCE));
hudson.setSlaves(slaves);
Computer c = hudson.getComputer("test");
assertNotNull(c);
return c;
}
private static class NoopTask implements Callable<String,RuntimeException> {
public String call() {
return null;