File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\test\java\hudson\LauncherTest.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright 2009 Sun Microsystems, Inc., Kohsuke Kawaguchi, Jesse Glick.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25 package hudson;
26
27 import hudson.model.StreamBuildListener;
28 import hudson.model.TaskListener;
29 import hudson.util.ProcessTree;
30 import hudson.util.StreamTaskListener;
31 import hudson.remoting.Callable;
32 import java.io.ByteArrayOutputStream;
33 import org.apache.commons.io.FileUtils;
34 import org.jvnet.hudson.test.Bug;
35
36 import java.io.File;
37
38 public class LauncherTest extends ChannelTestCase {
39 @Bug(4611)
40 public void testRemoteKill() throws Exception {
41 if (File.pathSeparatorChar != ':') {
42 System.err.println("Skipping, currently Unix-specific test");
43 return;
44 }
45
46 File tmp = File.createTempFile("testRemoteKill", "");
47 tmp.delete();
48
49 try {
50 FilePath f = new FilePath(french, tmp.getPath());
51 Launcher l = f.createLauncher(StreamTaskListener.fromStderr());
52 Proc p = l.launch().cmds("sh", "-c", "echo $$$$ > "+tmp+"; sleep 30").stdout(System.out).stderr(System.err).start();
53 while (!tmp.exists())
54 Thread.sleep(100);
55 long start = System.currentTimeMillis();
56 p.kill();
57 assertTrue(p.join()!=0);
58 long end = System.currentTimeMillis();
59 assertTrue("join finished promptly", (end - start < 5000));
60 french.call(NOOP); // this only returns after the other side of the channel has finished executing cancellation
61 Thread.sleep(2000); // more delay to make sure it's gone
62 assertNull("process should be gone",ProcessTree.get().get(Integer.parseInt(FileUtils.readFileToString(tmp).trim())));
63
64 // Manual version of test: set up instance w/ one slave. Now in script console
65 // new hudson.FilePath(new java.io.File("/tmp")).createLauncher(new hudson.util.StreamTaskListener(System.err)).
66 // launch().cmds("sleep", "1d").stdout(System.out).stderr(System.err).start().kill()
67 // returns immediately and pgrep sleep => nothing. But without fix
68 // hudson.model.Hudson.instance.nodes[0].rootPath.createLauncher(new hudson.util.StreamTaskListener(System.err)).
69 // launch().cmds("sleep", "1d").stdout(System.out).stderr(System.err).start().kill()
70 // hangs and on slave machine pgrep sleep => one process; after manual kill, script returns.
71 } finally {
72 tmp.delete();
73 }
74 }
75
76 private static final Callable<Object,RuntimeException> NOOP = new Callable<Object,RuntimeException>() {
77 public Object call() throws RuntimeException {
78 return null;
79 }
80 };
81
82 @Bug(15733)
83 public void testDecorateByEnv() throws Exception {
84 ByteArrayOutputStream baos = new ByteArrayOutputStream();
85 TaskListener l = new StreamBuildListener(baos);
86 Launcher base = new Launcher.LocalLauncher(l);
87 EnvVars env = new EnvVars("key1", "val1");
88 Launcher decorated = base.decorateByEnv(env);
89 int res = decorated.launch().envs("key2=val2").cmds(Functions.isWindows() ? new String[] {"cmd", "/q", "/c", "echo %key1% %key2%"} : new String[] {"sh", "-c", "echo $key1 $key2"}).stdout(l).join();
90 String log = baos.toString();
91 assertEquals(log, 0, res);
92 assertTrue(log, log.contains("val1 val2"));
93 }
94
95 }
96