blob: a7d4b066f11b2a91f769a6a0c7d11160cf0d0f6a [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Configuration for running (dunit) tests in parallel in Docker containers.
* The container used must hava JAVA_HOME set in it's environment and must
* have 'java' defined on the path. For example, the relevant Dockerfile
* content could be:
*
* ENV JAVA_HOME=/opt/jdk1.8.0_u101
* ENV PATH=$PATH:$JAVA_HOME/bin
*
* In addition, the container must have docker installed.
*
* The plugin can be activated with the Gradle property 'parallelDunit'.
* Additional properties that can be set are:
*
* dunitDockerImage - The docker image used for running parallel dunits. The
* default image is 'bellsoft/liberica-openjdk-debian:8'. The image is required to
* have 'JAVA_HOME' set as an environment variable.
* dunitParallelForks - The number of parallel containers that will be
* launched. The default is 8.
* dunitDockerUser - The user used within the docker container to run tests.
* The default is 'root'.
*/
apply plugin: 'com.github.pedjak.dockerized-test'
if (project.hasProperty('parallelDunit')) {
def pwd = System.getenv('PWD')
def geodeDir = new File(pwd).getCanonicalPath()
project.ext.dunitDockerVolumes = ["${geodeDir}":geodeDir]
}
project.ext.dockerConfig = {
maxParallelForks = dunitParallelForks.toInteger()
docker {
// base image for creating docker containers that execute the tests
image = dunitDockerImage
// volumes mounted to the containers
// in a form: host_dir : container_dir
def gradleHome = System.getenv('GRADLE_USER_HOME') ?: "${System.getenv('HOME')}/.gradle"
volumes = ["${gradleHome}":gradleHome]
// Add volumes configured by top-level build script
volumes << project.dunitDockerVolumes
// specify the user for starting Gradle test worker within the container.
user = dunitDockerUser
beforeContainerCreate = { cmd, client ->
def javaHomeIdx = -1
def pathIdx = -1
def tmpEnv = []
cmd.getEnv().each { tmpEnv << it }
tmpEnv.eachWithIndex { x, j ->
if (x.startsWith('JAVA_HOME')) {
javaHomeIdx = j
}
if (x.startsWith('PATH')) {
pathIdx = j
}
}
// Remove JAVA_HOME and PATH env variables - they might not be the same as the container needs
if (javaHomeIdx >= 0) {
tmpEnv[javaHomeIdx] = 'JAVA_HOME_REMOVED='
}
if (pathIdx >= 0) {
tmpEnv[pathIdx] = 'PATH_REMOVED='
}
if (project.hasProperty('testJVM') && !testJVM.trim().isEmpty()) {
// Docker command is just 'java' so set to full path
tmpEnv << ("JAVA_HOME=${project.testJVM}" as String)
}
// Unfortunately this snippet of code is here and is required by dev-tools/docker/base/entrypoint.sh.
// This allows preserving the outer user inside the running container. Required for Jenkins
// and other environments. There doesn't seem to be a way to pass this environment variable
// in from a Jenkins Gradle job.
if (System.env['LOCAL_USER_ID'] == null) {
def username = System.getProperty("user.name")
def uid = ['id', '-u', username].execute().text.trim()
tmpEnv << ("LOCAL_USER_ID=${uid}" as String)
}
cmd.withEnv(tmpEnv)
// Infer the index of this invocation
def cmdList = cmd.getCmd()
if (project.hasProperty('testJVM') && !testJVM.trim().isEmpty()) {
// Docker command is just 'java' so set to full path
cmdList[0] = ("${project.testJVM}/bin/java" as String)
}
def matcher = (cmdList[cmdList.length - 1] =~ /.*Executor (\d*).*/)
def workdir = new File(cmd.getWorkingDir() + matcher[0][1])
workdir.mkdirs()
cmd.withWorkingDir(workdir.toString())
// copy the classpath file to the working dir
def classPathFileIndex = cmdList.findIndexOf { it =~ /^@.*gradle-worker-classpath.*txt$/ }
if (classPathFileIndex > 0) {
def dst = new File(workdir, "gradle-worker-classpath.txt")
if (!dst.exists()) {
def src = new File(cmdList[classPathFileIndex].substring(1))
dst.write(src.text)
}
cmdList[classPathFileIndex] = '@'+dst.toString()
}
//println cmd
}
}
}
if (project.hasProperty('parallelDunit')) {
uiTest.configure(project.ext.dockerConfig)
repeatUnitTest.configure(project.ext.dockerConfig)
integrationTest.configure(project.ext.dockerConfig)
repeatIntegrationTest.configure(project.ext.dockerConfig)
distributedTest.configure(project.ext.dockerConfig)
repeatDistributedTest.configure(project.ext.dockerConfig)
upgradeTest.configure(project.ext.dockerConfig)
repeatUpgradeTest.configure(project.ext.dockerConfig)
acceptanceTest.configure(project.ext.dockerConfig)
repeatAcceptanceTest.configure(project.ext.dockerConfig)
}