95 public static boolean canTestDocker() throws Exception {
96 if (isDockerEngineAvailable()) {
97 return true;
98 } else {
99 throw new SkippedException("Docker engine is not available on this system");
100 }
101 }
102
103
104 /**
105 * Simple check - is docker engine available, accessible and usable.
106 * Run basic docker command: 'docker ps' - list docker instances.
107 * If docker engine is available and accesible then true is returned
108 * and we can proceed with testing docker.
109 *
110 * @return true if docker engine is available and usable
111 * @throws Exception
112 */
113 private static boolean isDockerEngineAvailableCheck() throws Exception {
114 try {
115 execute(Platform.DOCKER_COMMAND, "ps")
116 .shouldHaveExitValue(0)
117 .shouldContain("CONTAINER")
118 .shouldContain("IMAGE");
119 } catch (Exception e) {
120 return false;
121 }
122 return true;
123 }
124
125
126 /**
127 * Build a docker image that contains JDK under test.
128 * The jdk will be placed under the "/jdk/" folder inside the docker file system.
129 *
130 * @param imageName name of the image to be created, including version tag
131 * @param dockerfile name of the dockerfile residing in the test source;
132 * we check for a platform specific dockerfile as well
133 * and use this one in case it exists
134 * @param buildDirName name of the docker build/staging directory, which will
135 * be created in the jtreg's scratch folder
158
159 /**
160 * Build a docker image based on given docker file and docker build directory.
161 *
162 * @param imageName name of the image to be created, including version tag
163 * @param dockerfile path to the Dockerfile to be used for building the docker
164 * image. The specified dockerfile will be copied to the docker build
165 * directory as 'Dockerfile'
166 * @param buildDir build directory; it should already contain all the content
167 * needed to build the docker image.
168 * @throws Exception
169 */
170 public static void
171 buildDockerImage(String imageName, Path dockerfile, Path buildDir) throws Exception {
172
173 generateDockerFile(buildDir.resolve("Dockerfile"),
174 DockerfileConfig.getBaseImageName(),
175 DockerfileConfig.getBaseImageVersion());
176 try {
177 // Build the docker
178 execute(Platform.DOCKER_COMMAND, "build", "--no-cache", "--tag", imageName, buildDir.toString())
179 .shouldHaveExitValue(0);
180 } catch (Exception e) {
181 // If docker image building fails there is a good chance it happens due to environment and/or
182 // configuration other than product failure. Throw jtreg skipped exception in such case
183 // instead of failing the test.
184 throw new SkippedException("Building docker image failed. Details: \n" + e.getMessage());
185 }
186 }
187
188
189 /**
190 * Build the docker command to run java inside a container
191 *
192 * @param DockerRunOptions optins for running docker
193 *
194 * @return command
195 * @throws Exception
196 */
197 public static List<String> buildJavaCommand(DockerRunOptions opts) throws Exception {
198 List<String> cmd = new ArrayList<>();
199
200 cmd.add(Platform.DOCKER_COMMAND);
201 cmd.add("run");
202 if (opts.tty)
203 cmd.add("--tty=true");
204 if (opts.removeContainerAfterUse)
205 cmd.add("--rm");
206
207 cmd.addAll(opts.dockerOpts);
208 cmd.add(opts.imageNameAndTag);
209 cmd.add(opts.command);
210
211 cmd.addAll(opts.javaOpts);
212 if (opts.appendTestJavaOptions) {
213 Collections.addAll(cmd, Utils.getTestJavaOpts());
214 }
215
216 cmd.add(opts.classToRun);
217 cmd.addAll(opts.classParams);
218
219 return cmd;
220 }
222 /**
223 * Run Java inside the docker image with specified parameters and options.
224 *
225 * @param DockerRunOptions optins for running docker
226 *
227 * @return output of the run command
228 * @throws Exception
229 */
230 public static OutputAnalyzer dockerRunJava(DockerRunOptions opts) throws Exception {
231 return execute(buildJavaCommand(opts));
232 }
233
234
235 /**
236 * Remove docker image
237 *
238 * @param DockerRunOptions optins for running docker
239 * @throws Exception
240 */
241 public static void removeDockerImage(String imageNameAndTag) throws Exception {
242 execute(Platform.DOCKER_COMMAND, "rmi", "--force", imageNameAndTag);
243 }
244
245
246
247 /**
248 * Convenience method - express command as sequence of strings
249 *
250 * @param command to execute
251 * @return The output from the process
252 * @throws Exception
253 */
254 public static OutputAnalyzer execute(List<String> command) throws Exception {
255 return execute(command.toArray(new String[command.size()]));
256 }
257
258
259 /**
260 * Execute a specified command in a process, report diagnostic info.
261 *
262 * @param command to be executed
|
95 public static boolean canTestDocker() throws Exception {
96 if (isDockerEngineAvailable()) {
97 return true;
98 } else {
99 throw new SkippedException("Docker engine is not available on this system");
100 }
101 }
102
103
104 /**
105 * Simple check - is docker engine available, accessible and usable.
106 * Run basic docker command: 'docker ps' - list docker instances.
107 * If docker engine is available and accesible then true is returned
108 * and we can proceed with testing docker.
109 *
110 * @return true if docker engine is available and usable
111 * @throws Exception
112 */
113 private static boolean isDockerEngineAvailableCheck() throws Exception {
114 try {
115 execute(Platform.Docker.DOCKER_COMMAND, "ps")
116 .shouldHaveExitValue(0)
117 .shouldContain("CONTAINER")
118 .shouldContain("IMAGE");
119 } catch (Exception e) {
120 return false;
121 }
122 return true;
123 }
124
125
126 /**
127 * Build a docker image that contains JDK under test.
128 * The jdk will be placed under the "/jdk/" folder inside the docker file system.
129 *
130 * @param imageName name of the image to be created, including version tag
131 * @param dockerfile name of the dockerfile residing in the test source;
132 * we check for a platform specific dockerfile as well
133 * and use this one in case it exists
134 * @param buildDirName name of the docker build/staging directory, which will
135 * be created in the jtreg's scratch folder
158
159 /**
160 * Build a docker image based on given docker file and docker build directory.
161 *
162 * @param imageName name of the image to be created, including version tag
163 * @param dockerfile path to the Dockerfile to be used for building the docker
164 * image. The specified dockerfile will be copied to the docker build
165 * directory as 'Dockerfile'
166 * @param buildDir build directory; it should already contain all the content
167 * needed to build the docker image.
168 * @throws Exception
169 */
170 public static void
171 buildDockerImage(String imageName, Path dockerfile, Path buildDir) throws Exception {
172
173 generateDockerFile(buildDir.resolve("Dockerfile"),
174 DockerfileConfig.getBaseImageName(),
175 DockerfileConfig.getBaseImageVersion());
176 try {
177 // Build the docker
178 execute(Platform.Docker.DOCKER_COMMAND, "build", "--no-cache", "--tag", imageName, buildDir.toString())
179 .shouldHaveExitValue(0);
180 } catch (Exception e) {
181 // If docker image building fails there is a good chance it happens due to environment and/or
182 // configuration other than product failure. Throw jtreg skipped exception in such case
183 // instead of failing the test.
184 throw new SkippedException("Building docker image failed. Details: \n" + e.getMessage());
185 }
186 }
187
188
189 /**
190 * Build the docker command to run java inside a container
191 *
192 * @param DockerRunOptions optins for running docker
193 *
194 * @return command
195 * @throws Exception
196 */
197 public static List<String> buildJavaCommand(DockerRunOptions opts) throws Exception {
198 List<String> cmd = new ArrayList<>();
199
200 cmd.add(Platform.Docker.DOCKER_COMMAND);
201 cmd.add("run");
202 if (opts.tty)
203 cmd.add("--tty=true");
204 if (opts.removeContainerAfterUse)
205 cmd.add("--rm");
206
207 cmd.addAll(opts.dockerOpts);
208 cmd.add(opts.imageNameAndTag);
209 cmd.add(opts.command);
210
211 cmd.addAll(opts.javaOpts);
212 if (opts.appendTestJavaOptions) {
213 Collections.addAll(cmd, Utils.getTestJavaOpts());
214 }
215
216 cmd.add(opts.classToRun);
217 cmd.addAll(opts.classParams);
218
219 return cmd;
220 }
222 /**
223 * Run Java inside the docker image with specified parameters and options.
224 *
225 * @param DockerRunOptions optins for running docker
226 *
227 * @return output of the run command
228 * @throws Exception
229 */
230 public static OutputAnalyzer dockerRunJava(DockerRunOptions opts) throws Exception {
231 return execute(buildJavaCommand(opts));
232 }
233
234
235 /**
236 * Remove docker image
237 *
238 * @param DockerRunOptions optins for running docker
239 * @throws Exception
240 */
241 public static void removeDockerImage(String imageNameAndTag) throws Exception {
242 execute(Platform.Docker.DOCKER_COMMAND, "rmi", "--force", imageNameAndTag);
243 }
244
245
246
247 /**
248 * Convenience method - express command as sequence of strings
249 *
250 * @param command to execute
251 * @return The output from the process
252 * @throws Exception
253 */
254 public static OutputAnalyzer execute(List<String> command) throws Exception {
255 return execute(command.toArray(new String[command.size()]));
256 }
257
258
259 /**
260 * Execute a specified command in a process, report diagnostic info.
261 *
262 * @param command to be executed
|