8222888: [TESTBUG] docker/TestJFREvents.java fails due to "RuntimeException: JAVA_MAIN_CLASS_ is not defined"
authormseledtsov
Thu, 25 Apr 2019 11:45:52 -0700
changeset 54625 7f30741331ad
parent 54624 beaea3c10b0f
child 54626 3fb6efa2c9f1
8222888: [TESTBUG] docker/TestJFREvents.java fails due to "RuntimeException: JAVA_MAIN_CLASS_ is not defined" Summary: Introduced unique environment variable Reviewed-by: egahlin, lmesnik, sgehwolf
test/hotspot/jtreg/containers/docker/TestJFREvents.java
test/lib/jdk/test/lib/containers/docker/DockerTestUtils.java
--- a/test/hotspot/jtreg/containers/docker/TestJFREvents.java	Thu Apr 25 12:33:19 2019 -0400
+++ b/test/hotspot/jtreg/containers/docker/TestJFREvents.java	Thu Apr 25 11:45:52 2019 -0700
@@ -36,6 +36,7 @@
  * @build JfrReporter
  * @run driver TestJFREvents
  */
+import java.util.List;
 import jdk.test.lib.containers.docker.Common;
 import jdk.test.lib.containers.docker.DockerRunOptions;
 import jdk.test.lib.containers.docker.DockerTestUtils;
@@ -46,6 +47,8 @@
 
 public class TestJFREvents {
     private static final String imageName = Common.imageName("jfr-events");
+    private static final String TEST_ENV_VARIABLE = "UNIQUE_VARIABLE_ABC592903XYZ";
+    private static final String TEST_ENV_VALUE = "unique_value_abc592903xyz";
     private static final int availableCPUs = Runtime.getRuntime().availableProcessors();
 
     public static void main(String[] args) throws Exception {
@@ -119,26 +122,29 @@
     }
 
 
-    // JTReg always defines the environment variable JAVA_MAIN_CLASS_<SOME_NUMBER>.
-    // This variable fits well for use in this test, since it is rather unique.
-    private static String getTestEnvironmentVariable() throws Exception {
-        for (String key : System.getenv().keySet()) {
-            if (key.startsWith("JAVA_MAIN_CLASS")) {
-                return key;
-            }
-        }
-        throw new RuntimeException("JAVA_MAIN_CLASS_* is not defined");
-    }
-
-
     private static void testEnvironmentVariables() throws Exception {
         Common.logNewTestCase("EnvironmentVariables");
 
-        DockerTestUtils.dockerRunJava(
+        List<String> cmd = DockerTestUtils.buildJavaCommand(
                                       commonDockerOpts()
-                                      .addClassOptions("jdk.InitialEnvironmentVariable"))
-            .shouldHaveExitValue(0)
+                                      .addClassOptions("jdk.InitialEnvironmentVariable"));
+
+        ProcessBuilder pb = new ProcessBuilder(cmd);
+        // Container has JAVA_HOME defined via the Dockerfile; make sure
+        // it is reported by JFR event.
+        // Environment variable set in host system should not be visible inside a container,
+        // and should not be reported by JFR.
+        pb.environment().put(TEST_ENV_VARIABLE, TEST_ENV_VALUE);
+
+        System.out.println("[COMMAND]\n" + Utils.getCommandLine(pb));
+        OutputAnalyzer out = new OutputAnalyzer(pb.start());
+        System.out.println("[STDERR]\n" + out.getStderr());
+        System.out.println("[STDOUT]\n" + out.getStdout());
+
+        out.shouldHaveExitValue(0)
             .shouldContain("key = JAVA_HOME")
-            .shouldNotContain(getTestEnvironmentVariable());
+            .shouldContain("value = /jdk")
+            .shouldNotContain(TEST_ENV_VARIABLE)
+            .shouldNotContain(TEST_ENV_VALUE);
     }
 }
--- a/test/lib/jdk/test/lib/containers/docker/DockerTestUtils.java	Thu Apr 25 12:33:19 2019 -0400
+++ b/test/lib/jdk/test/lib/containers/docker/DockerTestUtils.java	Thu Apr 25 11:45:52 2019 -0700
@@ -184,15 +184,15 @@
 
 
     /**
-     * Run Java inside the docker image with specified parameters and options.
+     * Build the docker command to run java inside a container
      *
      * @param DockerRunOptions optins for running docker
      *
-     * @return output of the run command
+     * @return command
      * @throws Exception
      */
-    public static OutputAnalyzer dockerRunJava(DockerRunOptions opts) throws Exception {
-        ArrayList<String> cmd = new ArrayList<>();
+    public static List<String> buildJavaCommand(DockerRunOptions opts) throws Exception {
+        List<String> cmd = new ArrayList<>();
 
         cmd.add(DOCKER_COMMAND);
         cmd.add("run");
@@ -213,7 +213,19 @@
         cmd.add(opts.classToRun);
         cmd.addAll(opts.classParams);
 
-        return execute(cmd);
+        return cmd;
+    }
+
+    /**
+     * Run Java inside the docker image with specified parameters and options.
+     *
+     * @param DockerRunOptions optins for running docker
+     *
+     * @return output of the run command
+     * @throws Exception
+     */
+    public static OutputAnalyzer dockerRunJava(DockerRunOptions opts) throws Exception {
+        return execute(buildJavaCommand(opts));
     }