# HG changeset patch # User mseledtsov # Date 1556217952 25200 # Node ID 7f30741331ad6c57489e9eae2a7ff1277e3e12c2 # Parent beaea3c10b0f788a06e360a8381e7990b67a0d87 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 diff -r beaea3c10b0f -r 7f30741331ad test/hotspot/jtreg/containers/docker/TestJFREvents.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_. - // 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 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); } } diff -r beaea3c10b0f -r 7f30741331ad test/lib/jdk/test/lib/containers/docker/DockerTestUtils.java --- 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 cmd = new ArrayList<>(); + public static List buildJavaCommand(DockerRunOptions opts) throws Exception { + List 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)); }