/* * Copyright (c) 2021, 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 across container boundary. The JCMD runs on a host system, * while sending commands to a JVM that runs inside a container. * @requires docker.support * @requires vm.flagless * @modules java.base/jdk.internal.misc * java.management * jdk.jartool/sun.tools.jar * @library /test/lib * @build EventGeneratorLoop * @run driver TestJcmd
*/ import java.util.List; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import jdk.test.lib.Container; import jdk.test.lib.JDKToolFinder; import jdk.test.lib.Platform; 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.DockerfileConfig; import jdk.test.lib.containers.docker.DockerTestUtils; import jdk.test.lib.process.OutputAnalyzer; import jdk.test.lib.process.ProcessTools; import jtreg.SkippedException;
// podman versions below 3.3.1 hava a bug where cross-container testing with correct // permissions fails. See JDK-8273216 if (IS_PODMAN && PodmanVersion.VERSION_3_3_1.compareTo(getPodmanVersion()) > 0) { thrownew SkippedException("Podman version too old for this test. Expected >= 3.3.1");
}
// Need to create a custom dockerfile where user name and id, as well as group name and id // of the JVM running in container must match the ones from the inspecting JCMD process.
String content = generateCustomDockerfile();
DockerTestUtils.buildJdkContainerImage(IMAGE_NAME, content);
try {
Process p = startObservedContainer(); long pid = testJcmdGetPid("EventGeneratorLoop");
privatestaticvoid testJcmdHelp(long pid) throws Exception {
System.out.println("TestCase: testJcmdHelp()");
ProcessBuilder pb = new ProcessBuilder(JDKToolFinder.getJDKTool("jcmd"), "" + pid, "help");
OutputAnalyzer out = new OutputAnalyzer(pb.start())
.shouldHaveExitValue(0)
.shouldContain("JFR.start")
.shouldContain("VM.version");
}
privatestaticvoid testVmInfo(long pid) throws Exception {
System.out.println("TestCase: testVmInfo()");
ProcessBuilder pb = new ProcessBuilder(JDKToolFinder.getJDKTool("jcmd"), "" + pid, "VM.info");
OutputAnalyzer out = new OutputAnalyzer(pb.start())
.shouldHaveExitValue(0)
.shouldContain("vm_info")
.shouldContain("VM Arguments");
}
// Need to make sure that user name+id and group name+id are created for the image, and // match the host system. This is necessary to allow proper permission/access for JCMD. // For podman --userns=keep-id is sufficient. privatestatic String generateCustomDockerfile() throws Exception {
StringBuilder sb = new StringBuilder();
sb.append(String.format("FROM %s:%s\n", DockerfileConfig.getBaseImageName(),
DockerfileConfig.getBaseImageVersion()));
sb.append("COPY /jdk /jdk\n");
sb.append("ENV JAVA_HOME=/jdk\n");
if (!IS_PODMAN) { // only needed for docker
String uid = getId("-u");
String gid = getId("-g");
String userName = getId("-un");
String groupName = getId("-gn"); // Only needed when run as regular user. UID == 0 should already exist if (!ROOT_UID.equals(uid)) {
sb.append(String.format("RUN groupadd --gid %s %s \n", gid, groupName));
sb.append(String.format("RUN useradd --uid %s --gid %s %s \n", uid, gid, userName));
sb.append(String.format("USER %s \n", userName));
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.