/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * @test * @summary Test JCMD with containers. * Specifically, start the test JVM inside a container, then use jcmd to find it and * send a simple command to that JVM. * @requires docker.support * @library /test/lib * @modules java.base/jdk.internal.misc * java.management * jdk.jartool/sun.tools.jar * jdk.security.auth * @build SimpleLoop * @run driver TestJcmd */ import com.sun.security.auth.module.UnixSystem; import jdk.test.lib.JDKToolFinder; import jdk.test.lib.Utils; import jdk.test.lib.containers.docker.Common; import jdk.test.lib.containers.docker.DockerRunOptions; import jdk.test.lib.containers.docker.DockerTestUtils; import jdk.test.lib.process.OutputAnalyzer; public class TestJcmd { private static final String IMAGE_NAME = Common.imageName("jcmd"); private static final int TIME_TO_RUN_CHILD_PROCESS = (int) (20 * Utils.TIMEOUT_FACTOR); // seconds public static void main(String[] args) throws Exception { if (!DockerTestUtils.canTestDocker()) { return; } // In order for jcmd to work, the USER name and UID of the observer // need to match the USERNAME/UID of the observed JVM process String additionalDockerFileContent = String.format("RUN useradd %s --uid %d \n", getCurrentUserName(), getCurrentUserId()) + String.format("USER %s \n", getCurrentUserName()); DockerTestUtils.buildJdkDockerImage(IMAGE_NAME, "Dockerfile-BasicTest", "jdk-docker", additionalDockerFileContent); String name = "java-simple-loop"; try { ProcessBuilder pb = containerCommand(name); Process p = pb.start(); DockerTestUtils.waitForContainerToStart(name, 2000, 500, 10); // jcmd -l OutputAnalyzer jcmdOut = testJcmdList(); long pid = Common.findPidFromJcmdOutput(jcmdOut, "SimpleLoop"); System.out.println("PID = " + pid); // jcmd help // This test case currently fails due to JDK-8228343 // testJcmdHelp(pid); p.waitFor(); logProcess("container", pb, new OutputAnalyzer(p)); } finally { DockerTestUtils.removeDockerImage(IMAGE_NAME); } } private static String getCurrentUserName() { String name = System.getProperty("user.name"); System.out.println("getCurrentUserName(): returning " + name); return name; } private static long getCurrentUserId() { long uid = (new UnixSystem()).getUid(); System.out.println("getCurrentUserId(): returning " + uid); return uid; } // Run "jcmd -l" private static OutputAnalyzer testJcmdList() throws Exception { ProcessBuilder pb = new ProcessBuilder(JDKToolFinder.getJDKTool("jcmd"), "-l"); OutputAnalyzer out = new OutputAnalyzer(pb.start()); logProcess("testJcmdList", pb, out); out.shouldHaveExitValue(0) .shouldContain("SimpleLoop"); return out; } // run "jcmd help" private static void testJcmdHelp(long pid) throws Exception { ProcessBuilder pb = new ProcessBuilder(JDKToolFinder.getJDKTool("jcmd"), "" + pid, "help"); OutputAnalyzer out = new OutputAnalyzer(pb.start()); logProcess("testJcmdHelp", pb, out); out.shouldHaveExitValue(0) .shouldContain("VM.version"); } private static void logProcess(String logToken, ProcessBuilder pb, OutputAnalyzer out) { System.out.printf("%s: [COMMAND]:\n%s\n", logToken, Utils.getCommandLine(pb)); System.out.printf("%s: [STDERR]:\n%s\n", logToken, out.getStderr()); System.out.printf("%s: [STDOUT]:\n%s\n", logToken, out.getStdout()); } private static ProcessBuilder containerCommand(String name) throws Exception { // start container with monitored JVM inside DockerRunOptions opts = new DockerRunOptions(IMAGE_NAME, "/jdk/bin/java", "SimpleLoop") .addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/") .addJavaOpts("-cp", "/test-classes/") .addDockerOpts("--cap-add=SYS_PTRACE") .addDockerOpts("--name", name) .addDockerOpts("--sig-proxy=true") .addJavaOpts("-XX:+UsePerfData") .addClassOptions("" + TIME_TO_RUN_CHILD_PROCESS); return new ProcessBuilder(DockerTestUtils.buildJavaCommand(opts)); } }