test/lib/jdk/test/lib/containers/docker/DockerTestUtils.java
author bobv
Tue, 12 Jun 2018 18:51:45 -0400
changeset 50545 292a4a87c321
parent 48957 af49718d8cf2
child 54293 dae71635cc3a
permissions -rw-r--r--
8203357: Container Metrics Reviewed-by: mchung, dholmes, mseledtsov, rehn
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
/*
50545
292a4a87c321 8203357: Container Metrics
bobv
parents: 48957
diff changeset
     2
 * Copyright (c) 2017, 2018, 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;
48880
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 47615
diff changeset
    38
import jdk.test.lib.Platform;
47615
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    39
import jdk.test.lib.Utils;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    40
import jdk.test.lib.process.OutputAnalyzer;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    41
import jdk.test.lib.process.ProcessTools;
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
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    49
    // Diagnostics: set to true to enable more diagnostic info
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    50
    private static final boolean DEBUG = false;
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
    /**
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    53
     * 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
    54
     * 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
    55
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    56
     * @return true if docker engine is available
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    57
     * @throws Exception
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    58
     */
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    59
    public static boolean isDockerEngineAvailable() throws Exception {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    60
        if (wasDockerEngineChecked)
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    61
            return isDockerEngineAvailable;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    62
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    63
        isDockerEngineAvailable = isDockerEngineAvailableCheck();
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    64
        wasDockerEngineChecked = true;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    65
        return isDockerEngineAvailable;
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
    /**
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    70
     * 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
    71
     * will print the appropriate message when not available.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    72
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    73
     * @return true if docker engine is available
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    74
     * @throws Exception
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    75
     */
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    76
    public static boolean canTestDocker() throws Exception {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    77
        if (isDockerEngineAvailable()) {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    78
            return true;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    79
        } else {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    80
            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
    81
            System.out.println("This test is SKIPPED");
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    82
            return false;
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
    /**
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    88
     * Simple check - is docker engine available, accessible and usable.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    89
     * Run basic docker command: 'docker ps' - list docker instances.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    90
     * 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
    91
     * and we can proceed with testing docker.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    92
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    93
     * @return true if docker engine is available and usable
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    94
     * @throws Exception
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    95
     */
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    96
    private static boolean isDockerEngineAvailableCheck() throws Exception {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    97
        try {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    98
            execute("docker", "ps")
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
    99
                .shouldHaveExitValue(0)
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   100
                .shouldContain("CONTAINER")
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   101
                .shouldContain("IMAGE");
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   102
        } catch (Exception e) {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   103
            return false;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   104
        }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   105
        return true;
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
    /**
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   110
     * Build a docker image that contains JDK under test.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   111
     * 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
   112
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   113
     * @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
   114
     * @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
   115
     *                      we check for a platform specific dockerfile as well
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 47615
diff changeset
   116
     *                      and use this one in case it exists
47615
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   117
     * @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
   118
     *                      be created in the jtreg's scratch folder
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   119
     * @throws Exception
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
    public static void
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   122
        buildJdkDockerImage(String imageName, String dockerfile, String buildDirName)
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   123
            throws Exception {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   124
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   125
        Path buildDir = Paths.get(".", buildDirName);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   126
        if (Files.exists(buildDir)) {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   127
            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
   128
        }
48880
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 47615
diff changeset
   129
        // check for the existance of a platform specific docker file as well
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 47615
diff changeset
   130
        String platformSpecificDockerfile = dockerfile + "-" + Platform.getOsArch();
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 47615
diff changeset
   131
        if (Files.exists(Paths.get(Utils.TEST_SRC, platformSpecificDockerfile))) {
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 47615
diff changeset
   132
          dockerfile = platformSpecificDockerfile;
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 47615
diff changeset
   133
        }
47615
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   134
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   135
        Path jdkSrcDir = Paths.get(Utils.TEST_JDK);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   136
        Path jdkDstDir = buildDir.resolve("jdk");
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
        Files.createDirectories(jdkDstDir);
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
        // 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
   141
        // This step is required for building a docker image.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   142
        Files.walkFileTree(jdkSrcDir, new CopyFileVisitor(jdkSrcDir, jdkDstDir));
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   143
        buildDockerImage(imageName, Paths.get(Utils.TEST_SRC, dockerfile), buildDir);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   144
    }
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
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
     * 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
   149
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   150
     * @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
   151
     * @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
   152
     *        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
   153
     *        directory as 'Dockerfile'
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   154
     * @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
   155
     *        needed to build the docker image.
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   156
     * @throws Exception
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
    public static void
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   159
        buildDockerImage(String imageName, Path dockerfile, Path buildDir) throws Exception {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   160
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   161
        // Copy docker file to the build dir
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   162
        Files.copy(dockerfile, buildDir.resolve("Dockerfile"));
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
        // Build the docker
48957
af49718d8cf2 8197412: Enable docker container related tests for linux s390x
mbaesken
parents: 48880
diff changeset
   165
        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
   166
            .shouldHaveExitValue(0)
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   167
            .shouldContain("Successfully built");
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
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
    /**
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   172
     * 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
   173
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   174
     * @param DockerRunOptions optins for running docker
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   175
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   176
     * @return output of the run command
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   177
     * @throws Exception
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   178
     */
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   179
    public static OutputAnalyzer dockerRunJava(DockerRunOptions opts) throws Exception {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   180
        ArrayList<String> cmd = new ArrayList<>();
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   181
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   182
        cmd.add("docker");
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   183
        cmd.add("run");
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   184
        if (opts.tty)
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   185
            cmd.add("--tty=true");
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   186
        if (opts.removeContainerAfterUse)
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   187
            cmd.add("--rm");
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
        cmd.addAll(opts.dockerOpts);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   190
        cmd.add(opts.imageNameAndTag);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   191
        cmd.add(opts.command);
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.addAll(opts.javaOpts);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   194
        if (opts.appendTestJavaOptions) {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   195
            Collections.addAll(cmd, Utils.getTestJavaOpts());
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   196
        }
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
        cmd.add(opts.classToRun);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   199
        cmd.addAll(opts.classParams);
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
        return execute(cmd);
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
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   204
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   205
     /**
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   206
     * Remove docker image
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
     * @param DockerRunOptions optins for running docker
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   209
     * @return output of the command
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   210
     * @throws Exception
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
    public static OutputAnalyzer removeDockerImage(String imageNameAndTag) throws Exception {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   213
        return execute("docker", "rmi", "--force", imageNameAndTag);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   214
    }
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
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   217
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   218
    /**
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   219
     * Convenience method - express command as sequence of strings
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   220
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   221
     * @param command to execute
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   222
     * @return The output from the process
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   223
     * @throws Exception
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
    public static OutputAnalyzer execute(List<String> command) throws Exception {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   226
        return execute(command.toArray(new String[command.size()]));
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
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
     * 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
   232
     *
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   233
     * @param command to be executed
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   234
     * @return The output from the process
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   235
     * @throws Exception
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
    public static OutputAnalyzer execute(String... command) throws Exception {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   238
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   239
        ProcessBuilder pb = new ProcessBuilder(command);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   240
        System.out.println("[COMMAND]\n" + Utils.getCommandLine(pb));
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
        long started = System.currentTimeMillis();
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   243
        OutputAnalyzer output = new OutputAnalyzer(pb.start());
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
        System.out.println("[ELAPSED: " + (System.currentTimeMillis() - started) + " ms]");
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   246
        System.out.println("[STDERR]\n" + output.getStderr());
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   247
        System.out.println("[STDOUT]\n" + output.getStdout());
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   248
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   249
        return output;
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
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   252
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   253
    private static class CopyFileVisitor extends SimpleFileVisitor<Path> {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   254
        private final Path src;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   255
        private final Path dst;
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
        public CopyFileVisitor(Path src, Path dst) {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   258
            this.src = src;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   259
            this.dst = dst;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   260
        }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   261
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
        @Override
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   264
        public FileVisitResult preVisitDirectory(Path file,
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   265
                BasicFileAttributes attrs) throws IOException {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   266
            Path dstDir = dst.resolve(src.relativize(file));
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   267
            if (!dstDir.toFile().exists()) {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   268
                Files.createDirectories(dstDir);
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
            return FileVisitResult.CONTINUE;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   271
        }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   272
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   273
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   274
        @Override
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   275
        public FileVisitResult visitFile(Path file,
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   276
                BasicFileAttributes attrs) throws IOException {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   277
            if (!file.toFile().isFile()) {
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   278
                return FileVisitResult.CONTINUE;
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
            Path dstFile = dst.resolve(src.relativize(file));
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   281
            Files.copy(file, dstFile, StandardCopyOption.COPY_ATTRIBUTES);
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   282
            return FileVisitResult.CONTINUE;
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   283
        }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   284
    }
6210a27c3704 8181592: [TESTBUG] Docker test utils and docker jdk basic test
mseledtsov
parents:
diff changeset
   285
}