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 JFR network related events inside a container; make sure
  28  *          the reported host ip and host name are correctly reported within
  29  *          the container.
  30  * @requires docker.support
  31  * @library /test/lib
  32  * @modules java.base/jdk.internal.misc
  33  *          java.management
  34  *          jdk.jartool/sun.tools.jar
  35  * @build JfrNetwork
  36  * @run driver TestJFRNetworkEvents
  37  */
  38 import jdk.test.lib.containers.docker.Common;
  39 import jdk.test.lib.containers.docker.DockerRunOptions;
  40 import jdk.test.lib.containers.docker.DockerTestUtils;
  41 import jdk.test.lib.Utils;
  42 
  43 
  44 public class TestJFRNetworkEvents {
  45     private static final String imageName = Common.imageName("jfr-network");
  46     private static final int availableCPUs = Runtime.getRuntime().availableProcessors();
  47 
  48     public static void main(String[] args) throws Exception {
  49         System.out.println("Test Environment: detected availableCPUs = " + availableCPUs);
  50         if (!DockerTestUtils.canTestDocker()) {
  51             return;
  52         }
  53 
  54         DockerTestUtils.buildJdkDockerImage(imageName, "Dockerfile-BasicTest", "jdk-docker");
  55 
  56         try {
  57             runTest("jdk.SocketWrite");
  58         } finally {
  59             DockerTestUtils.removeDockerImage(imageName);
  60         }
  61     }
  62 
  63     private static void runTest(String event) throws Exception {
  64         DockerRunOptions opts = new DockerRunOptions(imageName, "/jdk/bin/java", "JfrNetwork")
  65         .addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
  66         .addJavaOpts("-cp", "/test-classes/")
  67         .addDockerOpts("--hostname", JfrNetwork.HOST_NAME)
  68         .addClassOptions(event);
  69     DockerTestUtils.dockerRunJava(opts)
  70         .shouldHaveExitValue(0)
  71         .shouldContain(JfrNetwork.JFR_REPORTED_CONTAINER_HOSTNAME_TAG + JfrNetwork.HOST_NAME);
  72     }
  73 }