|
| 1 | +package jenkins; |
| 2 | + |
| 3 | +import hudson.cli.FullDuplexHttpStream; |
| 4 | +import hudson.model.Computer; |
| 5 | +import hudson.model.Failure; |
| 6 | +import hudson.remoting.Channel; |
| 7 | +import org.junit.Rule; |
| 8 | +import org.junit.Test; |
| 9 | +import org.jvnet.hudson.test.JenkinsRule; |
| 10 | + |
| 11 | +import java.io.FileNotFoundException; |
| 12 | +import java.net.URL; |
| 13 | + |
| 14 | +import static org.junit.Assert.*; |
| 15 | + |
| 16 | +/** |
| 17 | + * @author Kohsuke Kawaguchi |
| 18 | + */ |
| 19 | +public class CLITest { |
| 20 | + @Rule |
| 21 | + public JenkinsRule j = new JenkinsRule(); |
| 22 | + |
| 23 | + /** |
| 24 | + * Checks if the kill switch works correctly |
| 25 | + */ |
| 26 | + @Test |
| 27 | + public void killSwitch() throws Exception { |
| 28 | + // this should succeed, as a control case |
| 29 | + makeHttpCall(); |
| 30 | + makeJnlpCall(); |
| 31 | + |
| 32 | + CLI.DISABLED = true; |
| 33 | + try { |
| 34 | + try { |
| 35 | + makeHttpCall(); |
| 36 | + fail("Should have been rejected"); |
| 37 | + } catch (FileNotFoundException e) { |
| 38 | + // attempt to make a call should fail |
| 39 | + } |
| 40 | + try { |
| 41 | + makeJnlpCall(); |
| 42 | + fail("Should have been rejected"); |
| 43 | + } catch (Exception e) { |
| 44 | + // attempt to make a call should fail |
| 45 | + e.printStackTrace(); |
| 46 | + |
| 47 | + // the current expected failure mode is EOFException, though we don't really care how it fails |
| 48 | + } |
| 49 | + } finally { |
| 50 | + CLI.DISABLED = false; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private void makeHttpCall() throws Exception { |
| 55 | + FullDuplexHttpStream con = new FullDuplexHttpStream(new URL(j.getURL(), "cli")); |
| 56 | + Channel ch = new Channel("test connection", Computer.threadPoolForRemoting, con.getInputStream(), con.getOutputStream()); |
| 57 | + ch.close(); |
| 58 | + } |
| 59 | + |
| 60 | + private void makeJnlpCall() throws Exception { |
| 61 | + int r = hudson.cli.CLI._main(new String[]{"-s",j.getURL().toString(), "version"}); |
| 62 | + if (r!=0) |
| 63 | + throw new Failure("CLI failed"); |
| 64 | + } |
| 65 | +} |
0 commit comments