test/lib/jdk/test/lib/containers/docker/DockerTestUtils.java
author mseledtsov
Thu, 25 Apr 2019 11:45:52 -0700
changeset 54625 7f30741331ad
parent 54525 5ae4d3f46537
child 55012 fb0cfce19262
permissions -rw-r--r--
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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47615
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
     1
/*
54293
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
     2
 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
47615
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
     4
 *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
     8
 *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    13
 * accompanied this code).
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    14
 *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    18
 *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    21
 * questions.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    22
 */
50545
292a4a87c321 8203357: Container Metrics
bobv
parents: 48957
diff changeset
    23
47615
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    24
package jdk.test.lib.containers.docker;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    25
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    26
import java.io.File;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    27
import java.io.IOException;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    28
import java.nio.file.Files;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    29
import java.nio.file.FileVisitResult;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    30
import java.nio.file.Path;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    31
import java.nio.file.Paths;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    32
import java.nio.file.SimpleFileVisitor;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    33
import java.nio.file.StandardCopyOption;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    34
import java.nio.file.attribute.BasicFileAttributes;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    35
import java.util.ArrayList;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    36
import java.util.Collections;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    37
import java.util.List;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    38
import jdk.test.lib.Utils;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    39
import jdk.test.lib.process.OutputAnalyzer;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    40
import jdk.test.lib.process.ProcessTools;
54424
b354ffb03ae4 8221710: [TESTBUG] more configurable parameters for docker testing
mseledtsov
parents: 54293
diff changeset
    41
import jtreg.SkippedException;
47615
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    42
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    43
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    44
public class DockerTestUtils {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    45
    private static final String FS = File.separator;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    46
    private static boolean isDockerEngineAvailable = false;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    47
    private static boolean wasDockerEngineChecked = false;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    48
54424
b354ffb03ae4 8221710: [TESTBUG] more configurable parameters for docker testing
mseledtsov
parents: 54293
diff changeset
    49
    // Use this property to specify docker location on your system.
b354ffb03ae4 8221710: [TESTBUG] more configurable parameters for docker testing
mseledtsov
parents: 54293
diff changeset
    50
    // E.g.: "/usr/local/bin/docker".
b354ffb03ae4 8221710: [TESTBUG] more configurable parameters for docker testing
mseledtsov
parents: 54293
diff changeset
    51
    private static final String DOCKER_COMMAND =
b354ffb03ae4 8221710: [TESTBUG] more configurable parameters for docker testing
mseledtsov
parents: 54293
diff changeset
    52
        System.getProperty("jdk.test.docker.command", "docker");
b354ffb03ae4 8221710: [TESTBUG] more configurable parameters for docker testing
mseledtsov
parents: 54293
diff changeset
    53
b354ffb03ae4 8221710: [TESTBUG] more configurable parameters for docker testing
mseledtsov
parents: 54293
diff changeset
    54
    // Set this property to true to retain image after test. By default
b354ffb03ae4 8221710: [TESTBUG] more configurable parameters for docker testing
mseledtsov
parents: 54293
diff changeset
    55
    // images are removed after test execution completes.
b354ffb03ae4 8221710: [TESTBUG] more configurable parameters for docker testing
mseledtsov
parents: 54293
diff changeset
    56
    // Retaining the image can be useful for diagnostics and image inspection.
b354ffb03ae4 8221710: [TESTBUG] more configurable parameters for docker testing
mseledtsov
parents: 54293
diff changeset
    57
    // E.g.: start image interactively: docker run -it <IMAGE_NAME>.
b354ffb03ae4 8221710: [TESTBUG] more configurable parameters for docker testing
mseledtsov
parents: 54293
diff changeset
    58
    public static final boolean RETAIN_IMAGE_AFTER_TEST =
b354ffb03ae4 8221710: [TESTBUG] more configurable parameters for docker testing
mseledtsov
parents: 54293
diff changeset
    59
        Boolean.getBoolean("jdk.test.docker.retain.image");
b354ffb03ae4 8221710: [TESTBUG] more configurable parameters for docker testing
mseledtsov
parents: 54293
diff changeset
    60
b354ffb03ae4 8221710: [TESTBUG] more configurable parameters for docker testing
mseledtsov
parents: 54293
diff changeset
    61
    // Path to a JDK under test.
b354ffb03ae4 8221710: [TESTBUG] more configurable parameters for docker testing
mseledtsov
parents: 54293
diff changeset
    62
    // This may be useful when developing tests on non-Linux platforms.
b354ffb03ae4 8221710: [TESTBUG] more configurable parameters for docker testing
mseledtsov
parents: 54293
diff changeset
    63
    public static final String JDK_UNDER_TEST =
b354ffb03ae4 8221710: [TESTBUG] more configurable parameters for docker testing
mseledtsov
parents: 54293
diff changeset
    64
        System.getProperty("jdk.test.docker.jdk", Utils.TEST_JDK);
b354ffb03ae4 8221710: [TESTBUG] more configurable parameters for docker testing
mseledtsov
parents: 54293
diff changeset
    65
47615
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    66
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    67
    /**
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    68
     * Optimized check of whether the docker engine is available in a given
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    69
     * environment. Checks only once, then remembers the result in a singleton.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    70
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    71
     * @return true if docker engine is available
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    72
     * @throws Exception
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    73
     */
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    74
    public static boolean isDockerEngineAvailable() throws Exception {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    75
        if (wasDockerEngineChecked)
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    76
            return isDockerEngineAvailable;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    77
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    78
        isDockerEngineAvailable = isDockerEngineAvailableCheck();
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    79
        wasDockerEngineChecked = true;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    80
        return isDockerEngineAvailable;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    81
    }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    82
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    83
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    84
    /**
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    85
     * Convenience method, will check if docker engine is available and usable;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    86
     * will print the appropriate message when not available.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    87
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    88
     * @return true if docker engine is available
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    89
     * @throws Exception
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    90
     */
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    91
    public static boolean canTestDocker() throws Exception {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    92
        if (isDockerEngineAvailable()) {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    93
            return true;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    94
        } else {
54424
b354ffb03ae4 8221710: [TESTBUG] more configurable parameters for docker testing
mseledtsov
parents: 54293
diff changeset
    95
            throw new SkippedException("Docker engine is not available on this system");
47615
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    96
        }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    97
    }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    98
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    99
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   100
    /**
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   101
     * Simple check - is docker engine available, accessible and usable.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   102
     * Run basic docker command: 'docker ps' - list docker instances.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   103
     * If docker engine is available and accesible then true is returned
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   104
     * and we can proceed with testing docker.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   105
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   106
     * @return true if docker engine is available and usable
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   107
     * @throws Exception
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   108
     */
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   109
    private static boolean isDockerEngineAvailableCheck() throws Exception {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   110
        try {
54424
b354ffb03ae4 8221710: [TESTBUG] more configurable parameters for docker testing
mseledtsov
parents: 54293
diff changeset
   111
            execute(DOCKER_COMMAND, "ps")
47615
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   112
                .shouldHaveExitValue(0)
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   113
                .shouldContain("CONTAINER")
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   114
                .shouldContain("IMAGE");
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   115
        } catch (Exception e) {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   116
            return false;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   117
        }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   118
        return true;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   119
    }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   120
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   121
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   122
    /**
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   123
     * Build a docker image that contains JDK under test.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   124
     * The jdk will be placed under the "/jdk/" folder inside the docker file system.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   125
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   126
     * @param imageName     name of the image to be created, including version tag
48880
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 47615
diff changeset
   127
     * @param dockerfile    name of the dockerfile residing in the test source;
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 47615
diff changeset
   128
     *                      we check for a platform specific dockerfile as well
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 47615
diff changeset
   129
     *                      and use this one in case it exists
47615
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   130
     * @param buildDirName  name of the docker build/staging directory, which will
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   131
     *                      be created in the jtreg's scratch folder
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   132
     * @throws Exception
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   133
     */
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   134
    public static void
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   135
        buildJdkDockerImage(String imageName, String dockerfile, String buildDirName)
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   136
            throws Exception {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   137
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   138
        Path buildDir = Paths.get(".", buildDirName);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   139
        if (Files.exists(buildDir)) {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   140
            throw new RuntimeException("The docker build directory already exists: " + buildDir);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   141
        }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   142
54424
b354ffb03ae4 8221710: [TESTBUG] more configurable parameters for docker testing
mseledtsov
parents: 54293
diff changeset
   143
        Path jdkSrcDir = Paths.get(JDK_UNDER_TEST);
47615
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   144
        Path jdkDstDir = buildDir.resolve("jdk");
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   145
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   146
        Files.createDirectories(jdkDstDir);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   147
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   148
        // Copy JDK-under-test tree to the docker build directory.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   149
        // This step is required for building a docker image.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   150
        Files.walkFileTree(jdkSrcDir, new CopyFileVisitor(jdkSrcDir, jdkDstDir));
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   151
        buildDockerImage(imageName, Paths.get(Utils.TEST_SRC, dockerfile), buildDir);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   152
    }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   153
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   154
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   155
    /**
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   156
     * Build a docker image based on given docker file and docker build directory.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   157
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   158
     * @param imageName  name of the image to be created, including version tag
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   159
     * @param dockerfile  path to the Dockerfile to be used for building the docker
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   160
     *        image. The specified dockerfile will be copied to the docker build
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   161
     *        directory as 'Dockerfile'
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   162
     * @param buildDir  build directory; it should already contain all the content
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   163
     *        needed to build the docker image.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   164
     * @throws Exception
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   165
     */
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   166
    public static void
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   167
        buildDockerImage(String imageName, Path dockerfile, Path buildDir) throws Exception {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   168
54293
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   169
        generateDockerFile(buildDir.resolve("Dockerfile"),
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   170
                           DockerfileConfig.getBaseImageName(),
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   171
                           DockerfileConfig.getBaseImageVersion());
54525
5ae4d3f46537 8222299: [TESTBUG] move hotspot container tests to hotspot/containers
mseledtsov
parents: 54424
diff changeset
   172
        try {
5ae4d3f46537 8222299: [TESTBUG] move hotspot container tests to hotspot/containers
mseledtsov
parents: 54424
diff changeset
   173
            // Build the docker
5ae4d3f46537 8222299: [TESTBUG] move hotspot container tests to hotspot/containers
mseledtsov
parents: 54424
diff changeset
   174
            execute(DOCKER_COMMAND, "build", "--no-cache", "--tag", imageName, buildDir.toString())
5ae4d3f46537 8222299: [TESTBUG] move hotspot container tests to hotspot/containers
mseledtsov
parents: 54424
diff changeset
   175
                .shouldHaveExitValue(0)
5ae4d3f46537 8222299: [TESTBUG] move hotspot container tests to hotspot/containers
mseledtsov
parents: 54424
diff changeset
   176
                .shouldContain("Successfully built");
5ae4d3f46537 8222299: [TESTBUG] move hotspot container tests to hotspot/containers
mseledtsov
parents: 54424
diff changeset
   177
        } catch (Exception e) {
5ae4d3f46537 8222299: [TESTBUG] move hotspot container tests to hotspot/containers
mseledtsov
parents: 54424
diff changeset
   178
            // If docker image building fails there is a good chance it happens due to environment and/or
5ae4d3f46537 8222299: [TESTBUG] move hotspot container tests to hotspot/containers
mseledtsov
parents: 54424
diff changeset
   179
            // configuration other than product failure. Throw jtreg skipped exception in such case
5ae4d3f46537 8222299: [TESTBUG] move hotspot container tests to hotspot/containers
mseledtsov
parents: 54424
diff changeset
   180
            // instead of failing the test.
5ae4d3f46537 8222299: [TESTBUG] move hotspot container tests to hotspot/containers
mseledtsov
parents: 54424
diff changeset
   181
            throw new SkippedException("Building docker image failed. Details: \n" + e.getMessage());
5ae4d3f46537 8222299: [TESTBUG] move hotspot container tests to hotspot/containers
mseledtsov
parents: 54424
diff changeset
   182
        }
47615
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   183
    }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   184
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   185
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   186
    /**
54625
7f30741331ad 8222888: [TESTBUG] docker/TestJFREvents.java fails due to "RuntimeException: JAVA_MAIN_CLASS_ is not defined"
mseledtsov
parents: 54525
diff changeset
   187
     * Build the docker command to run java inside a container
47615
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   188
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   189
     * @param DockerRunOptions optins for running docker
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   190
     *
54625
7f30741331ad 8222888: [TESTBUG] docker/TestJFREvents.java fails due to "RuntimeException: JAVA_MAIN_CLASS_ is not defined"
mseledtsov
parents: 54525
diff changeset
   191
     * @return command
47615
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   192
     * @throws Exception
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   193
     */
54625
7f30741331ad 8222888: [TESTBUG] docker/TestJFREvents.java fails due to "RuntimeException: JAVA_MAIN_CLASS_ is not defined"
mseledtsov
parents: 54525
diff changeset
   194
    public static List<String> buildJavaCommand(DockerRunOptions opts) throws Exception {
7f30741331ad 8222888: [TESTBUG] docker/TestJFREvents.java fails due to "RuntimeException: JAVA_MAIN_CLASS_ is not defined"
mseledtsov
parents: 54525
diff changeset
   195
        List<String> cmd = new ArrayList<>();
47615
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   196
54424
b354ffb03ae4 8221710: [TESTBUG] more configurable parameters for docker testing
mseledtsov
parents: 54293
diff changeset
   197
        cmd.add(DOCKER_COMMAND);
47615
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   198
        cmd.add("run");
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   199
        if (opts.tty)
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   200
            cmd.add("--tty=true");
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   201
        if (opts.removeContainerAfterUse)
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   202
            cmd.add("--rm");
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   203
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   204
        cmd.addAll(opts.dockerOpts);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   205
        cmd.add(opts.imageNameAndTag);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   206
        cmd.add(opts.command);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   207
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   208
        cmd.addAll(opts.javaOpts);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   209
        if (opts.appendTestJavaOptions) {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   210
            Collections.addAll(cmd, Utils.getTestJavaOpts());
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   211
        }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   212
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   213
        cmd.add(opts.classToRun);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   214
        cmd.addAll(opts.classParams);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   215
54625
7f30741331ad 8222888: [TESTBUG] docker/TestJFREvents.java fails due to "RuntimeException: JAVA_MAIN_CLASS_ is not defined"
mseledtsov
parents: 54525
diff changeset
   216
        return cmd;
7f30741331ad 8222888: [TESTBUG] docker/TestJFREvents.java fails due to "RuntimeException: JAVA_MAIN_CLASS_ is not defined"
mseledtsov
parents: 54525
diff changeset
   217
    }
7f30741331ad 8222888: [TESTBUG] docker/TestJFREvents.java fails due to "RuntimeException: JAVA_MAIN_CLASS_ is not defined"
mseledtsov
parents: 54525
diff changeset
   218
7f30741331ad 8222888: [TESTBUG] docker/TestJFREvents.java fails due to "RuntimeException: JAVA_MAIN_CLASS_ is not defined"
mseledtsov
parents: 54525
diff changeset
   219
    /**
7f30741331ad 8222888: [TESTBUG] docker/TestJFREvents.java fails due to "RuntimeException: JAVA_MAIN_CLASS_ is not defined"
mseledtsov
parents: 54525
diff changeset
   220
     * Run Java inside the docker image with specified parameters and options.
7f30741331ad 8222888: [TESTBUG] docker/TestJFREvents.java fails due to "RuntimeException: JAVA_MAIN_CLASS_ is not defined"
mseledtsov
parents: 54525
diff changeset
   221
     *
7f30741331ad 8222888: [TESTBUG] docker/TestJFREvents.java fails due to "RuntimeException: JAVA_MAIN_CLASS_ is not defined"
mseledtsov
parents: 54525
diff changeset
   222
     * @param DockerRunOptions optins for running docker
7f30741331ad 8222888: [TESTBUG] docker/TestJFREvents.java fails due to "RuntimeException: JAVA_MAIN_CLASS_ is not defined"
mseledtsov
parents: 54525
diff changeset
   223
     *
7f30741331ad 8222888: [TESTBUG] docker/TestJFREvents.java fails due to "RuntimeException: JAVA_MAIN_CLASS_ is not defined"
mseledtsov
parents: 54525
diff changeset
   224
     * @return output of the run command
7f30741331ad 8222888: [TESTBUG] docker/TestJFREvents.java fails due to "RuntimeException: JAVA_MAIN_CLASS_ is not defined"
mseledtsov
parents: 54525
diff changeset
   225
     * @throws Exception
7f30741331ad 8222888: [TESTBUG] docker/TestJFREvents.java fails due to "RuntimeException: JAVA_MAIN_CLASS_ is not defined"
mseledtsov
parents: 54525
diff changeset
   226
     */
7f30741331ad 8222888: [TESTBUG] docker/TestJFREvents.java fails due to "RuntimeException: JAVA_MAIN_CLASS_ is not defined"
mseledtsov
parents: 54525
diff changeset
   227
    public static OutputAnalyzer dockerRunJava(DockerRunOptions opts) throws Exception {
7f30741331ad 8222888: [TESTBUG] docker/TestJFREvents.java fails due to "RuntimeException: JAVA_MAIN_CLASS_ is not defined"
mseledtsov
parents: 54525
diff changeset
   228
        return execute(buildJavaCommand(opts));
47615
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   229
    }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   230
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   231
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   232
     /**
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   233
     * Remove docker image
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   234
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   235
     * @param DockerRunOptions optins for running docker
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   236
     * @throws Exception
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   237
     */
54424
b354ffb03ae4 8221710: [TESTBUG] more configurable parameters for docker testing
mseledtsov
parents: 54293
diff changeset
   238
    public static void removeDockerImage(String imageNameAndTag) throws Exception {
b354ffb03ae4 8221710: [TESTBUG] more configurable parameters for docker testing
mseledtsov
parents: 54293
diff changeset
   239
            execute(DOCKER_COMMAND, "rmi", "--force", imageNameAndTag);
47615
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   240
    }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   241
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   242
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   243
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   244
    /**
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   245
     * Convenience method - express command as sequence of strings
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   246
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   247
     * @param command to execute
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   248
     * @return The output from the process
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   249
     * @throws Exception
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   250
     */
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   251
    public static OutputAnalyzer execute(List<String> command) throws Exception {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   252
        return execute(command.toArray(new String[command.size()]));
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   253
    }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   254
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   255
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   256
    /**
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   257
     * Execute a specified command in a process, report diagnostic info.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   258
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   259
     * @param command to be executed
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   260
     * @return The output from the process
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   261
     * @throws Exception
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   262
     */
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   263
    public static OutputAnalyzer execute(String... command) throws Exception {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   264
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   265
        ProcessBuilder pb = new ProcessBuilder(command);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   266
        System.out.println("[COMMAND]\n" + Utils.getCommandLine(pb));
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   267
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   268
        long started = System.currentTimeMillis();
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   269
        OutputAnalyzer output = new OutputAnalyzer(pb.start());
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   270
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   271
        System.out.println("[ELAPSED: " + (System.currentTimeMillis() - started) + " ms]");
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   272
        System.out.println("[STDERR]\n" + output.getStderr());
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   273
        System.out.println("[STDOUT]\n" + output.getStdout());
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   274
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   275
        return output;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   276
    }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   277
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   278
54293
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   279
    private static void generateDockerFile(Path dockerfile, String baseImage,
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   280
                                           String baseImageVersion) throws Exception {
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   281
        String template =
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   282
            "FROM %s:%s\n" +
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   283
            "COPY /jdk /jdk\n" +
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   284
            "ENV JAVA_HOME=/jdk\n" +
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   285
            "CMD [\"/bin/bash\"]\n";
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   286
        String dockerFileStr = String.format(template, baseImage, baseImageVersion);
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   287
        Files.writeString(dockerfile, dockerFileStr);
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   288
    }
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   289
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   290
47615
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   291
    private static class CopyFileVisitor extends SimpleFileVisitor<Path> {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   292
        private final Path src;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   293
        private final Path dst;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   294
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   295
        public CopyFileVisitor(Path src, Path dst) {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   296
            this.src = src;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   297
            this.dst = dst;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   298
        }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   299
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   300
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   301
        @Override
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   302
        public FileVisitResult preVisitDirectory(Path file,
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   303
                BasicFileAttributes attrs) throws IOException {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   304
            Path dstDir = dst.resolve(src.relativize(file));
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   305
            if (!dstDir.toFile().exists()) {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   306
                Files.createDirectories(dstDir);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   307
            }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   308
            return FileVisitResult.CONTINUE;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   309
        }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   310
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   311
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   312
        @Override
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   313
        public FileVisitResult visitFile(Path file,
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   314
                BasicFileAttributes attrs) throws IOException {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   315
            if (!file.toFile().isFile()) {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   316
                return FileVisitResult.CONTINUE;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   317
            }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   318
            Path dstFile = dst.resolve(src.relativize(file));
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   319
            Files.copy(file, dstFile, StandardCopyOption.COPY_ATTRIBUTES);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   320
            return FileVisitResult.CONTINUE;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   321
        }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   322
    }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   323
}