1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 /*
  26  * @test
  27  * @summary Test JCMD with side car pattern.
  28  *          Sidecar is a common pattern used in the cloud environments for monitoring
  29  *          and other uses. In side car pattern the main application/service container
  30  *          is paired with a sidecar container by sharing certain aspects of container
  31  *          namespace such as PID namespace, specific sub-directories, IPC and more.
  32  * @requires docker.support
  33  * @library /test/lib
  34  * @modules java.base/jdk.internal.misc
  35  *          java.management
  36  *          jdk.jartool/sun.tools.jar
  37  * @build EventGeneratorLoop
  38  * @run driver TestJcmdWithSideCar
  39  */
  40 import java.nio.file.Path;
  41 import java.nio.file.Paths;
  42 import java.util.Arrays;
  43 import java.util.ArrayList;
  44 import java.util.List;
  45 import java.util.stream.Collectors;
  46 import jdk.test.lib.Container;
  47 import jdk.test.lib.Utils;
  48 import jdk.test.lib.containers.docker.Common;
  49 import jdk.test.lib.containers.docker.DockerRunOptions;
  50 import jdk.test.lib.containers.docker.DockerTestUtils;
  51 import jdk.test.lib.process.OutputAnalyzer;
  52 
  53 
  54 public class TestJcmdWithSideCar {
  55     private static final String IMAGE_NAME = Common.imageName("jfr-jcmd");
  56     private static final int TIME_TO_RUN_MAIN_PROCESS = (int) (30 * Utils.TIMEOUT_FACTOR); // seconds
  57     private static final String MAIN_CONTAINER_NAME = "test-container-main";
  58 
  59     public static void main(String[] args) throws Exception {
  60         if (!DockerTestUtils.canTestDocker()) {
  61             return;
  62         }
  63 
  64         DockerTestUtils.buildJdkDockerImage(IMAGE_NAME, "Dockerfile-BasicTest", "jdk-docker");
  65 
  66         try {
  67             // Start the loop process in the "main" container, then run test cases
  68             // using a sidecar container.
  69             DockerThread t = startMainContainer();
  70 
  71             waitForMainContainerToStart(500, 10);
  72             t.checkForErrors();
  73 
  74             OutputAnalyzer jcmdOut = testCase01();
  75             long mainProcPid = findProcess(jcmdOut, "EventGeneratorLoop");
  76 
  77             t.assertIsAlive();
  78             testCase02(mainProcPid);
  79 
  80             // JCMD does not work in sidecar configuration, except for "jcmd -l".
  81             // Including this test case to assist in reproduction of the problem.
  82             // t.assertIsAlive();
  83             // testCase03(mainProcPid);
  84 
  85             t.join(TIME_TO_RUN_MAIN_PROCESS * 1000);
  86             t.checkForErrors();
  87         } finally {
  88             DockerTestUtils.removeDockerImage(IMAGE_NAME);
  89         }
  90     }
  91 
  92 
  93     // Run "jcmd -l" in a sidecar container and find a process that runs EventGeneratorLoop
  94     private static OutputAnalyzer testCase01() throws Exception {
  95         return runSideCar(MAIN_CONTAINER_NAME, "/jdk/bin/jcmd", "-l")
  96             .shouldHaveExitValue(0)
  97             .shouldContain("sun.tools.jcmd.JCmd")
  98             .shouldContain("EventGeneratorLoop");
  99     }
 100 
 101     // run jhsdb jinfo <PID> (jhsdb uses PTRACE)
 102     private static void testCase02(long pid) throws Exception {
 103         runSideCar(MAIN_CONTAINER_NAME, "/jdk/bin/jhsdb", "jinfo", "--pid", "" + pid)
 104             .shouldHaveExitValue(0)
 105             .shouldContain("Java System Properties")
 106             .shouldContain("VM Flags");
 107     }
 108 
 109     // test jcmd with some commands (help, start JFR recording)
 110     // JCMD will use signal mechanism and Unix Socket
 111     private static void testCase03(long pid) throws Exception {
 112         runSideCar(MAIN_CONTAINER_NAME, "/jdk/bin/jcmd", "" + pid, "help")
 113             .shouldHaveExitValue(0)
 114             .shouldContain("VM.version");
 115         runSideCar(MAIN_CONTAINER_NAME, "/jdk/bin/jcmd", "" + pid, "JFR.start")
 116             .shouldHaveExitValue(0)
 117             .shouldContain("Started recording");
 118     }
 119 
 120     private static DockerThread startMainContainer() throws Exception {
 121         // start "main" container (the observee)
 122         DockerRunOptions opts = commonDockerOpts("EventGeneratorLoop");
 123         opts.addDockerOpts("--cap-add=SYS_PTRACE")
 124             .addDockerOpts("--name", MAIN_CONTAINER_NAME)
 125             .addDockerOpts("-v", "/tmp")
 126             .addJavaOpts("-XX:+UsePerfData")
 127             .addClassOptions("" + TIME_TO_RUN_MAIN_PROCESS);
 128         DockerThread t = new DockerThread(opts);
 129         t.start();
 130 
 131         return t;
 132     }
 133 
 134     private static void waitForMainContainerToStart(int delayMillis, int count) throws Exception {
 135         boolean started = false;
 136         for(int i=0; i < count; i++) {
 137             try {
 138                 Thread.sleep(delayMillis);
 139             } catch (InterruptedException e) {}
 140             if (isMainContainerRunning()) {
 141                 started = true;
 142                 break;
 143             }
 144         }
 145         if (!started) {
 146             throw new RuntimeException("Main container did not start");
 147         }
 148     }
 149 
 150     private static boolean isMainContainerRunning() throws Exception {
 151         OutputAnalyzer out =
 152             DockerTestUtils.execute(Container.ENGINE_COMMAND,
 153                                     "ps", "--no-trunc",
 154                                     "--filter", "name=" + MAIN_CONTAINER_NAME);
 155         return out.getStdout().contains(MAIN_CONTAINER_NAME);
 156     }
 157 
 158     // JCMD relies on the attach mechanism (com.sun.tools.attach),
 159     // which in turn relies on JVMSTAT mechanism, which puts its mapped
 160     // buffers in /tmp directory (hsperfdata_<user>). Thus, in sidecar
 161     // we mount /tmp via --volumes-from from the main container.
 162     private static OutputAnalyzer runSideCar(String MAIN_CONTAINER_NAME, String whatToRun,
 163                                              String... args) throws Exception {
 164         List<String> cmd = new ArrayList<>();
 165         String[] command = new String[] {
 166             Container.ENGINE_COMMAND, "run",
 167             "--tty=true", "--rm",
 168             "--cap-add=SYS_PTRACE", "--sig-proxy=true",
 169             "--pid=container:" + MAIN_CONTAINER_NAME,
 170             "--volumes-from", MAIN_CONTAINER_NAME,
 171             IMAGE_NAME, whatToRun
 172         };
 173 
 174         cmd.addAll(Arrays.asList(command));
 175         cmd.addAll(Arrays.asList(args));
 176         return DockerTestUtils.execute(cmd);
 177     }
 178 
 179     private static long findProcess(OutputAnalyzer out, String name) throws Exception {
 180         List<String> l = out.asLines()
 181             .stream()
 182             .filter(s -> s.contains(name))
 183             .collect(Collectors.toList());
 184         if (l.isEmpty()) {
 185             throw new RuntimeException("Could not find matching process");
 186         }
 187         String psInfo = l.get(0);
 188         System.out.println("findProcess(): psInfo: " + psInfo);
 189         String pid = psInfo.substring(0, psInfo.indexOf(' '));
 190         System.out.println("findProcess(): pid: " + pid);
 191         return Long.parseLong(pid);
 192     }
 193 
 194     private static DockerRunOptions commonDockerOpts(String className) {
 195         return new DockerRunOptions(IMAGE_NAME, "/jdk/bin/java", className)
 196             .addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
 197             .addJavaOpts("-cp", "/test-classes/");
 198     }
 199 
 200 
 201     static class DockerThread extends Thread {
 202         DockerRunOptions runOpts;
 203         Exception exception;
 204 
 205         DockerThread(DockerRunOptions opts) {
 206             runOpts = opts;
 207         }
 208 
 209         public void run() {
 210             try {
 211                 DockerTestUtils.dockerRunJava(runOpts);
 212             } catch (Exception e) {
 213                 exception = e;
 214             }
 215         }
 216 
 217         public void assertIsAlive() throws Exception {
 218             if (!isAlive()) {
 219                 throw new RuntimeException("DockerThread stopped unexpectedly");
 220             }
 221         }
 222 
 223         public void checkForErrors() throws Exception {
 224             if (exception != null) {
 225                 throw new RuntimeException("DockerThread threw exception"
 226                                            + exception.getMessage());
 227             }
 228         }
 229     }
 230 
 231 }