LauncherTest.java 5.8 KB
Newer Older
1
/*
J
jglick 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * The MIT License
 *
 * Copyright 2009 Sun Microsystems, Inc., Kohsuke Kawaguchi, Jesse Glick.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
23 24 25 26
 */

package hudson;

27
import jenkins.model.Jenkins;
28 29
import hudson.model.StreamBuildListener;
import hudson.model.TaskListener;
30
import hudson.util.ProcessTree;
K
kohsuke 已提交
31 32
import hudson.util.StreamTaskListener;
import hudson.remoting.Callable;
33
import java.io.ByteArrayOutputStream;
34 35

import jenkins.security.MasterToSlaveCallable;
36
import org.apache.commons.io.FileUtils;
37
import org.jvnet.hudson.test.Bug;
38

K
kohsuke 已提交
39
import java.io.File;
40

K
kohsuke 已提交
41
public class LauncherTest extends ChannelTestCase {
42
    @Bug(4611)
43 44 45 46 47 48 49
    public void testRemoteKill() throws Exception {
        if (File.pathSeparatorChar != ':') {
            System.err.println("Skipping, currently Unix-specific test");
            return;
        }

        File tmp = File.createTempFile("testRemoteKill", "");
50
        tmp.delete();
51

K
kohsuke 已提交
52 53
        try {
            FilePath f = new FilePath(french, tmp.getPath());
54
            Launcher l = f.createLauncher(StreamTaskListener.fromStderr());
K
kohsuke 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
            Proc p = l.launch().cmds("sh", "-c", "echo $$$$ > "+tmp+"; sleep 30").stdout(System.out).stderr(System.err).start();
            while (!tmp.exists())
                Thread.sleep(100);
            long start = System.currentTimeMillis();
            p.kill();
            assertTrue(p.join()!=0);
            long end = System.currentTimeMillis();
            assertTrue("join finished promptly", (end - start < 5000));
            french.call(NOOP); // this only returns after the other side of the channel has finished executing cancellation
            Thread.sleep(2000); // more delay to make sure it's gone
            assertNull("process should be gone",ProcessTree.get().get(Integer.parseInt(FileUtils.readFileToString(tmp).trim())));

            // Manual version of test: set up instance w/ one slave. Now in script console
            // new hudson.FilePath(new java.io.File("/tmp")).createLauncher(new hudson.util.StreamTaskListener(System.err)).
            //   launch().cmds("sleep", "1d").stdout(System.out).stderr(System.err).start().kill()
            // returns immediately and pgrep sleep => nothing. But without fix
            // hudson.model.Hudson.instance.nodes[0].rootPath.createLauncher(new hudson.util.StreamTaskListener(System.err)).
            //   launch().cmds("sleep", "1d").stdout(System.out).stderr(System.err).start().kill()
            // hangs and on slave machine pgrep sleep => one process; after manual kill, script returns.
        } finally {
            tmp.delete();
        }
77 78
    }

79
    private static final Callable<Object,RuntimeException> NOOP = new MasterToSlaveCallable<Object,RuntimeException>() {
80
        public Object call() throws RuntimeException {
81 82 83
            return null;
        }
    };
84 85 86 87 88 89 90 91 92 93 94 95 96 97

    @Bug(15733)
    public void testDecorateByEnv() throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        TaskListener l = new StreamBuildListener(baos);
        Launcher base = new Launcher.LocalLauncher(l);
        EnvVars env = new EnvVars("key1", "val1");
        Launcher decorated = base.decorateByEnv(env);
        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();
        String log = baos.toString();
        assertEquals(log, 0, res);
        assertTrue(log, log.contains("val1 val2"));
    }

98
    @Bug(18368)
99 100 101 102 103
    public void testDecoratedByEnvMaintainsIsUnix() throws Exception {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        TaskListener listener = new StreamBuildListener(output);
        Launcher remoteLauncher = new Launcher.RemoteLauncher(listener, Jenkins.MasterComputer.localChannel, false);
        Launcher decorated = remoteLauncher.decorateByEnv(new EnvVars());
104
        assertEquals(false, decorated.isUnix());
105 106
        remoteLauncher = new Launcher.RemoteLauncher(listener, Jenkins.MasterComputer.localChannel, true);
        decorated = remoteLauncher.decorateByEnv(new EnvVars());
107
        assertEquals(true, decorated.isUnix());
108 109
    }

110
    @Bug(18368)
111 112 113 114 115
    public void testDecoratedByPrefixMaintainsIsUnix() throws Exception {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        TaskListener listener = new StreamBuildListener(output);
        Launcher remoteLauncher = new Launcher.RemoteLauncher(listener, Jenkins.MasterComputer.localChannel, false);
        Launcher decorated = remoteLauncher.decorateByPrefix("test");
116
        assertEquals(false, decorated.isUnix());
117 118
        remoteLauncher = new Launcher.RemoteLauncher(listener, Jenkins.MasterComputer.localChannel, true);
        decorated = remoteLauncher.decorateByPrefix("test");
119
        assertEquals(true, decorated.isUnix());
120 121
    }

122
}