test/lib/jdk/test/lib/containers/docker/DockerTestUtils.java
author mseledtsov
Tue, 26 Mar 2019 13:25:26 -0700
changeset 54293 dae71635cc3a
parent 50545 292a4a87c321
child 54424 b354ffb03ae4
permissions -rw-r--r--
8221342: [TESTBUG] Generate Dockerfile for docker testing Summary: Dockerfile is generated; introduced properties to specify custom base image Reviewed-by: sgehwolf, dholmes, jiefu
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;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    41
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
public class DockerTestUtils {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    44
    private static final String FS = File.separator;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    45
    private static boolean isDockerEngineAvailable = false;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    46
    private static boolean wasDockerEngineChecked = false;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    47
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    48
    // Diagnostics: set to true to enable more diagnostic info
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    49
    private static final boolean DEBUG = false;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    50
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    51
    /**
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    52
     * 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
    53
     * 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
    54
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    55
     * @return true if docker engine is available
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    56
     * @throws Exception
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    57
     */
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    58
    public static boolean isDockerEngineAvailable() throws Exception {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    59
        if (wasDockerEngineChecked)
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    60
            return isDockerEngineAvailable;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    61
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    62
        isDockerEngineAvailable = isDockerEngineAvailableCheck();
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    63
        wasDockerEngineChecked = true;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    64
        return isDockerEngineAvailable;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    65
    }
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
    /**
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    69
     * 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
    70
     * will print the appropriate message when not available.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    71
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    72
     * @return true if docker engine is available
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    73
     * @throws Exception
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    74
     */
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    75
    public static boolean canTestDocker() throws Exception {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    76
        if (isDockerEngineAvailable()) {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    77
            return true;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    78
        } else {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    79
            System.out.println("Docker engine is not available on this system");
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    80
            System.out.println("This test is SKIPPED");
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    81
            return false;
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
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    86
    /**
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    87
     * Simple check - is docker engine available, accessible and usable.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    88
     * Run basic docker command: 'docker ps' - list docker instances.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    89
     * 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
    90
     * and we can proceed with testing docker.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    91
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    92
     * @return true if docker engine is available and usable
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    93
     * @throws Exception
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    94
     */
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    95
    private static boolean isDockerEngineAvailableCheck() throws Exception {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    96
        try {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    97
            execute("docker", "ps")
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    98
                .shouldHaveExitValue(0)
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    99
                .shouldContain("CONTAINER")
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   100
                .shouldContain("IMAGE");
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   101
        } catch (Exception e) {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   102
            return false;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   103
        }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   104
        return true;
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
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   107
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
     * Build a docker image that contains JDK under test.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   110
     * 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
   111
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   112
     * @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
   113
     * @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
   114
     *                      we check for a platform specific dockerfile as well
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 47615
diff changeset
   115
     *                      and use this one in case it exists
47615
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   116
     * @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
   117
     *                      be created in the jtreg's scratch folder
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   118
     * @throws Exception
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
    public static void
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   121
        buildJdkDockerImage(String imageName, String dockerfile, String buildDirName)
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   122
            throws Exception {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   123
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   124
        Path buildDir = Paths.get(".", buildDirName);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   125
        if (Files.exists(buildDir)) {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   126
            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
   127
        }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   128
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   129
        Path jdkSrcDir = Paths.get(Utils.TEST_JDK);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   130
        Path jdkDstDir = buildDir.resolve("jdk");
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   131
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   132
        Files.createDirectories(jdkDstDir);
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
        // 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
   135
        // This step is required for building a docker image.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   136
        Files.walkFileTree(jdkSrcDir, new CopyFileVisitor(jdkSrcDir, jdkDstDir));
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   137
        buildDockerImage(imageName, Paths.get(Utils.TEST_SRC, dockerfile), buildDir);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   138
    }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   139
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   140
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
     * 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
   143
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   144
     * @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
   145
     * @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
   146
     *        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
   147
     *        directory as 'Dockerfile'
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   148
     * @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
   149
     *        needed to build the docker image.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   150
     * @throws Exception
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   151
     */
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   152
    public static void
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   153
        buildDockerImage(String imageName, Path dockerfile, Path buildDir) throws Exception {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   154
54293
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   155
        generateDockerFile(buildDir.resolve("Dockerfile"),
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   156
                           DockerfileConfig.getBaseImageName(),
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   157
                           DockerfileConfig.getBaseImageVersion());
47615
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   158
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   159
        // Build the docker
48957
af49718d8cf2 8197412: Enable docker container related tests for linux s390x
mbaesken
parents: 48880
diff changeset
   160
        execute("docker", "build", "--no-cache", "--tag", imageName, buildDir.toString())
47615
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   161
            .shouldHaveExitValue(0)
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   162
            .shouldContain("Successfully built");
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   163
    }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   164
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
    /**
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   167
     * Run Java inside the docker image with specified parameters and options.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   168
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   169
     * @param DockerRunOptions optins for running docker
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   170
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   171
     * @return output of the run command
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   172
     * @throws Exception
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   173
     */
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   174
    public static OutputAnalyzer dockerRunJava(DockerRunOptions opts) throws Exception {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   175
        ArrayList<String> cmd = new ArrayList<>();
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   176
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   177
        cmd.add("docker");
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   178
        cmd.add("run");
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   179
        if (opts.tty)
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   180
            cmd.add("--tty=true");
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   181
        if (opts.removeContainerAfterUse)
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   182
            cmd.add("--rm");
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
        cmd.addAll(opts.dockerOpts);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   185
        cmd.add(opts.imageNameAndTag);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   186
        cmd.add(opts.command);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   187
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   188
        cmd.addAll(opts.javaOpts);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   189
        if (opts.appendTestJavaOptions) {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   190
            Collections.addAll(cmd, Utils.getTestJavaOpts());
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   191
        }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   192
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   193
        cmd.add(opts.classToRun);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   194
        cmd.addAll(opts.classParams);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   195
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   196
        return execute(cmd);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   197
    }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   198
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   199
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   200
     /**
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   201
     * Remove docker image
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   202
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   203
     * @param DockerRunOptions optins for running docker
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   204
     * @return output of the command
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   205
     * @throws Exception
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   206
     */
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   207
    public static OutputAnalyzer removeDockerImage(String imageNameAndTag) throws Exception {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   208
        return execute("docker", "rmi", "--force", imageNameAndTag);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   209
    }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   210
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
    /**
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   214
     * Convenience method - express command as sequence of strings
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   215
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   216
     * @param command to execute
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   217
     * @return The output from the process
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   218
     * @throws Exception
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   219
     */
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   220
    public static OutputAnalyzer execute(List<String> command) throws Exception {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   221
        return execute(command.toArray(new String[command.size()]));
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   222
    }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   223
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   224
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   225
    /**
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   226
     * 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
   227
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   228
     * @param command to be executed
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   229
     * @return The output from the process
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   230
     * @throws Exception
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
    public static OutputAnalyzer execute(String... command) throws Exception {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   233
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   234
        ProcessBuilder pb = new ProcessBuilder(command);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   235
        System.out.println("[COMMAND]\n" + Utils.getCommandLine(pb));
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   236
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   237
        long started = System.currentTimeMillis();
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   238
        OutputAnalyzer output = new OutputAnalyzer(pb.start());
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   239
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   240
        System.out.println("[ELAPSED: " + (System.currentTimeMillis() - started) + " ms]");
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   241
        System.out.println("[STDERR]\n" + output.getStderr());
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   242
        System.out.println("[STDOUT]\n" + output.getStdout());
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
        return output;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   245
    }
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
54293
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   248
    private static void generateDockerFile(Path dockerfile, String baseImage,
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   249
                                           String baseImageVersion) throws Exception {
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   250
        String template =
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   251
            "FROM %s:%s\n" +
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   252
            "COPY /jdk /jdk\n" +
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   253
            "ENV JAVA_HOME=/jdk\n" +
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   254
            "CMD [\"/bin/bash\"]\n";
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   255
        String dockerFileStr = String.format(template, baseImage, baseImageVersion);
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   256
        Files.writeString(dockerfile, dockerFileStr);
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   257
    }
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   258
dae71635cc3a 8221342: [TESTBUG] Generate Dockerfile for docker testing
mseledtsov
parents: 50545
diff changeset
   259
47615
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   260
    private static class CopyFileVisitor extends SimpleFileVisitor<Path> {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   261
        private final Path src;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   262
        private final Path dst;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   263
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   264
        public CopyFileVisitor(Path src, Path dst) {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   265
            this.src = src;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   266
            this.dst = dst;
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
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   269
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   270
        @Override
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   271
        public FileVisitResult preVisitDirectory(Path file,
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   272
                BasicFileAttributes attrs) throws IOException {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   273
            Path dstDir = dst.resolve(src.relativize(file));
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   274
            if (!dstDir.toFile().exists()) {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   275
                Files.createDirectories(dstDir);
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
            return FileVisitResult.CONTINUE;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   278
        }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   279
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   280
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   281
        @Override
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   282
        public FileVisitResult visitFile(Path file,
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   283
                BasicFileAttributes attrs) throws IOException {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   284
            if (!file.toFile().isFile()) {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   285
                return FileVisitResult.CONTINUE;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   286
            }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   287
            Path dstFile = dst.resolve(src.relativize(file));
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   288
            Files.copy(file, dstFile, StandardCopyOption.COPY_ATTRIBUTES);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   289
            return FileVisitResult.CONTINUE;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   290
        }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   291
    }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   292
}