< prev index next >

test/testlibrary/com/oracle/java/testlibrary/DockerTestUtils.java

Print this page
rev 9061 : 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


 150      * @param dockerfile  path to the Dockerfile to be used for building the docker
 151      *        image. The specified dockerfile will be copied to the docker build
 152      *        directory as 'Dockerfile'
 153      * @param buildDir  build directory; it should already contain all the content
 154      *        needed to build the docker image.
 155      * @throws Exception
 156      */
 157     public static void
 158         buildDockerImage(String imageName, Path dockerfile, Path buildDir) throws Exception {
 159         // Copy docker file to the build dir
 160         Files.copy(dockerfile, buildDir.resolve("Dockerfile"));
 161 
 162         // Build the docker
 163         execute("docker", "build", "--no-cache", "--tag", imageName, buildDir.toString())
 164             .shouldHaveExitValue(0)
 165             .shouldContain("Successfully built");
 166     }
 167 
 168 
 169     /**
 170      * Run Java inside the docker image with specified parameters and options.
 171      *
 172      * @param DockerRunOptions optins for running docker
 173      *
 174      * @return output of the run command
 175      * @throws Exception
 176      */
 177     public static OutputAnalyzer dockerRunJava(DockerRunOptions opts) throws Exception {
 178         ArrayList<String> cmd = new ArrayList<>();
 179 
 180         cmd.add("docker");
 181         cmd.add("run");
 182         if (opts.tty)
 183             cmd.add("--tty=true");
 184         if (opts.removeContainerAfterUse)
 185             cmd.add("--rm");
 186 
 187         cmd.addAll(opts.dockerOpts);
 188         cmd.add(opts.imageNameAndTag);
 189         cmd.add(opts.command);
 190 
 191         cmd.addAll(opts.javaOpts);
 192         if (opts.appendTestJavaOptions) {
 193             Collections.addAll(cmd, Utils.getTestJavaOpts());
 194         }
 195 
 196         cmd.add(opts.classToRun);
 197         cmd.addAll(opts.classParams);
 198         return execute(cmd);
 199     }
 200 











 201 
 202      /**
 203      * Remove docker image
 204      *
 205      * @param DockerRunOptions optins for running docker
 206      * @return output of the command
 207      * @throws Exception
 208      */
 209     public static OutputAnalyzer removeDockerImage(String imageNameAndTag) throws Exception {
 210         return execute("docker", "rmi", "--force", imageNameAndTag);
 211     }
 212 
 213 
 214 
 215     /**
 216      * Convenience method - express command as sequence of strings
 217      *
 218      * @param command to execute
 219      * @return The output from the process
 220      * @throws Exception




 150      * @param dockerfile  path to the Dockerfile to be used for building the docker
 151      *        image. The specified dockerfile will be copied to the docker build
 152      *        directory as 'Dockerfile'
 153      * @param buildDir  build directory; it should already contain all the content
 154      *        needed to build the docker image.
 155      * @throws Exception
 156      */
 157     public static void
 158         buildDockerImage(String imageName, Path dockerfile, Path buildDir) throws Exception {
 159         // Copy docker file to the build dir
 160         Files.copy(dockerfile, buildDir.resolve("Dockerfile"));
 161 
 162         // Build the docker
 163         execute("docker", "build", "--no-cache", "--tag", imageName, buildDir.toString())
 164             .shouldHaveExitValue(0)
 165             .shouldContain("Successfully built");
 166     }
 167 
 168 
 169     /**
 170      * Build the docker command to run java inside a container
 171      *
 172      * @param DockerRunOptions optins for running docker
 173      *
 174      * @return command
 175      * @throws Exception
 176      */
 177     public static List<String> buildJavaCommand(DockerRunOptions opts) throws Exception {
 178         List<String> cmd = new ArrayList<>();
 179 
 180         cmd.add("docker");
 181         cmd.add("run");
 182         if (opts.tty)
 183             cmd.add("--tty=true");
 184         if (opts.removeContainerAfterUse)
 185             cmd.add("--rm");
 186 
 187         cmd.addAll(opts.dockerOpts);
 188         cmd.add(opts.imageNameAndTag);
 189         cmd.add(opts.command);
 190 
 191         cmd.addAll(opts.javaOpts);
 192         if (opts.appendTestJavaOptions) {
 193             Collections.addAll(cmd, Utils.getTestJavaOpts());
 194         }
 195 
 196         cmd.add(opts.classToRun);
 197         cmd.addAll(opts.classParams);
 198         return cmd;
 199     }
 200 
 201     /**
 202      * Run Java inside the docker image with specified parameters and options.
 203      *
 204      * @param DockerRunOptions optins for running docker
 205      *
 206      * @return output of the run command
 207      * @throws Exception
 208      */
 209     public static OutputAnalyzer dockerRunJava(DockerRunOptions opts) throws Exception {
 210         return execute(buildJavaCommand(opts));
 211     }
 212 
 213      /**
 214      * Remove docker image
 215      *
 216      * @param DockerRunOptions optins for running docker
 217      * @return output of the command
 218      * @throws Exception
 219      */
 220     public static OutputAnalyzer removeDockerImage(String imageNameAndTag) throws Exception {
 221         return execute("docker", "rmi", "--force", imageNameAndTag);
 222     }
 223 
 224 
 225 
 226     /**
 227      * Convenience method - express command as sequence of strings
 228      *
 229      * @param command to execute
 230      * @return The output from the process
 231      * @throws Exception


< prev index next >