|
|
41dba18 |
#
|
|
|
41dba18 |
# Test a container image.
|
|
|
41dba18 |
#
|
|
|
41dba18 |
# Always use sourced from a specific container testfile
|
|
|
41dba18 |
#
|
|
|
41dba18 |
# reguires definition of CID_FILE_DIR
|
|
|
41dba18 |
# CID_FILE_DIR=$(mktemp --suffix=<container>_test_cidfiles -d)
|
|
|
41dba18 |
# reguires definition of TEST_LIST
|
|
|
41dba18 |
# TEST_LIST="\
|
|
|
41dba18 |
# ctest_container_creation
|
|
|
41dba18 |
# ctest_doc_content"
|
|
|
41dba18 |
|
|
|
41dba18 |
# Container CI tests
|
|
|
41dba18 |
# abbreviated as "ct"
|
|
|
41dba18 |
|
|
|
41dba18 |
# may be redefined in the specific container testfile
|
|
|
41dba18 |
EXPECTED_EXIT_CODE=0
|
|
|
41dba18 |
|
|
|
41dba18 |
# ct_cleanup
|
|
|
41dba18 |
# --------------------
|
|
|
41dba18 |
# Cleans up containers used during tests. Stops and removes all containers
|
|
|
41dba18 |
# referenced by cid_files in CID_FILE_DIR. Dumps logs if a container exited
|
|
|
41dba18 |
# unexpectedly. Removes the cid_files and CID_FILE_DIR as well.
|
|
|
41dba18 |
# Uses: $CID_FILE_DIR - path to directory containing cid_files
|
|
|
41dba18 |
# Uses: $EXPECTED_EXIT_CODE - expected container exit code
|
|
|
41dba18 |
function ct_cleanup() {
|
|
|
41dba18 |
for cid_file in $CID_FILE_DIR/* ; do
|
|
|
41dba18 |
local container=$(cat $cid_file)
|
|
|
41dba18 |
|
|
|
41dba18 |
: "Stopping and removing container $container..."
|
|
|
41dba18 |
docker stop $container
|
|
|
41dba18 |
exit_status=$(docker inspect -f '{{.State.ExitCode}}' $container)
|
|
|
41dba18 |
if [ "$exit_status" != "$EXPECTED_EXIT_CODE" ]; then
|
|
|
41dba18 |
: "Dumping logs for $container"
|
|
|
41dba18 |
docker logs $container
|
|
|
41dba18 |
fi
|
|
|
41dba18 |
docker rm -v $container
|
|
|
41dba18 |
rm $cid_file
|
|
|
41dba18 |
done
|
|
|
41dba18 |
rmdir $CID_FILE_DIR
|
|
|
41dba18 |
: "Done."
|
|
|
41dba18 |
}
|
|
|
41dba18 |
|
|
|
41dba18 |
# ct_enable_cleanup
|
|
|
41dba18 |
# --------------------
|
|
|
41dba18 |
# Enables automatic container cleanup after tests.
|
|
|
41dba18 |
function ct_enable_cleanup() {
|
|
|
41dba18 |
trap ct_cleanup EXIT SIGINT
|
|
|
41dba18 |
}
|
|
|
41dba18 |
|
|
|
41dba18 |
# ct_get_cid [name]
|
|
|
41dba18 |
# --------------------
|
|
|
41dba18 |
# Prints container id from cid_file based on the name of the file.
|
|
|
41dba18 |
# Argument: name - name of cid_file where the container id will be stored
|
|
|
41dba18 |
# Uses: $CID_FILE_DIR - path to directory containing cid_files
|
|
|
41dba18 |
function ct_get_cid() {
|
|
|
41dba18 |
local name="$1" ; shift || return 1
|
|
|
41dba18 |
echo $(cat "$CID_FILE_DIR/$name")
|
|
|
41dba18 |
}
|
|
|
41dba18 |
|
|
|
41dba18 |
# ct_get_cip [id]
|
|
|
41dba18 |
# --------------------
|
|
|
41dba18 |
# Prints container ip address based on the container id.
|
|
|
41dba18 |
# Argument: id - container id
|
|
|
41dba18 |
function ct_get_cip() {
|
|
|
41dba18 |
local id="$1" ; shift
|
|
|
41dba18 |
docker inspect --format='{{.NetworkSettings.IPAddress}}' $(ct_get_cid "$id")
|
|
|
41dba18 |
}
|
|
|
41dba18 |
|
|
|
41dba18 |
# ct_wait_for_cid [cid_file]
|
|
|
41dba18 |
# --------------------
|
|
|
41dba18 |
# Holds the execution until the cid_file is created. Usually run after container
|
|
|
41dba18 |
# creation.
|
|
|
41dba18 |
# Argument: cid_file - name of the cid_file that should be created
|
|
|
41dba18 |
function ct_wait_for_cid() {
|
|
|
41dba18 |
local cid_file=$1
|
|
|
41dba18 |
local max_attempts=10
|
|
|
41dba18 |
local sleep_time=1
|
|
|
41dba18 |
local attempt=1
|
|
|
41dba18 |
local result=1
|
|
|
41dba18 |
while [ $attempt -le $max_attempts ]; do
|
|
|
41dba18 |
[ -f $cid_file ] && [ -s $cid_file ] && return 0
|
|
|
41dba18 |
: "Waiting for container start..."
|
|
|
41dba18 |
attempt=$(( $attempt + 1 ))
|
|
|
41dba18 |
sleep $sleep_time
|
|
|
41dba18 |
done
|
|
|
41dba18 |
return 1
|
|
|
41dba18 |
}
|
|
|
41dba18 |
|
|
|
41dba18 |
# ct_assert_container_creation_fails [container_args]
|
|
|
41dba18 |
# --------------------
|
|
|
41dba18 |
# The invocation of docker run should fail based on invalid container_args
|
|
|
41dba18 |
# passed to the function. Returns 0 when container fails to start properly.
|
|
|
41dba18 |
# Argument: container_args - all arguments are passed directly to dokcer run
|
|
|
41dba18 |
# Uses: $CID_FILE_DIR - path to directory containing cid_files
|
|
|
41dba18 |
function ct_assert_container_creation_fails() {
|
|
|
41dba18 |
local ret=0
|
|
|
41dba18 |
local max_attempts=10
|
|
|
41dba18 |
local attempt=1
|
|
|
41dba18 |
local cid_file=assert
|
|
|
41dba18 |
set +e
|
|
|
41dba18 |
local old_container_args="${CONTAINER_ARGS-}"
|
|
|
41dba18 |
CONTAINER_ARGS="$@"
|
|
|
41dba18 |
ct_create_container $cid_file
|
|
|
41dba18 |
if [ $? -eq 0 ]; then
|
|
|
41dba18 |
local cid=$(ct_get_cid $cid_file)
|
|
|
41dba18 |
|
|
|
41dba18 |
while [ "$(docker inspect -f '{{.State.Running}}' $cid)" == "true" ] ; do
|
|
|
41dba18 |
sleep 2
|
|
|
41dba18 |
attempt=$(( $attempt + 1 ))
|
|
|
41dba18 |
if [ $attempt -gt $max_attempts ]; then
|
|
|
41dba18 |
docker stop $cid
|
|
|
41dba18 |
ret=1
|
|
|
41dba18 |
break
|
|
|
41dba18 |
fi
|
|
|
41dba18 |
done
|
|
|
41dba18 |
exit_status=$(docker inspect -f '{{.State.ExitCode}}' $cid)
|
|
|
41dba18 |
if [ "$exit_status" == "0" ]; then
|
|
|
41dba18 |
ret=1
|
|
|
41dba18 |
fi
|
|
|
41dba18 |
docker rm -v $cid
|
|
|
41dba18 |
rm $CID_FILE_DIR/$cid_file
|
|
|
41dba18 |
fi
|
|
|
41dba18 |
[ ! -z $old_container_args ] && CONTAINER_ARGS="$old_container_args"
|
|
|
41dba18 |
set -e
|
|
|
41dba18 |
return $ret
|
|
|
41dba18 |
}
|
|
|
41dba18 |
|
|
|
41dba18 |
# ct_create_container [name, command]
|
|
|
41dba18 |
# --------------------
|
|
|
41dba18 |
# Creates a container using the IMAGE_NAME and CONTAINER_ARGS variables. Also
|
|
|
41dba18 |
# stores the container id to a cid_file located in the CID_FILE_DIR, and waits
|
|
|
41dba18 |
# for the creation of the file.
|
|
|
41dba18 |
# Argument: name - name of cid_file where the container id will be stored
|
|
|
41dba18 |
# Argument: command - optional command to be executed in the container
|
|
|
41dba18 |
# Uses: $CID_FILE_DIR - path to directory containing cid_files
|
|
|
41dba18 |
# Uses: $CONTAINER_ARGS - optional arguments passed directly to docker run
|
|
|
41dba18 |
# Uses: $IMAGE_NAME - name of the image being tested
|
|
|
41dba18 |
function ct_create_container() {
|
|
|
41dba18 |
local cid_file="$CID_FILE_DIR/$1" ; shift
|
|
|
41dba18 |
# create container with a cidfile in a directory for cleanup
|
|
|
41dba18 |
docker run --cidfile="$cid_file" -d ${CONTAINER_ARGS:-} $IMAGE_NAME "$@"
|
|
|
41dba18 |
ct_wait_for_cid $cid_file || return 1
|
|
|
41dba18 |
: "Created container $(cat $cid_file)"
|
|
|
41dba18 |
}
|
|
|
41dba18 |
|
|
|
41dba18 |
# ct_scl_usage_old [name, command, expected]
|
|
|
41dba18 |
# --------------------
|
|
|
41dba18 |
# Tests three ways of running the SCL, by looking for an expected string
|
|
|
41dba18 |
# in the output of the command
|
|
|
41dba18 |
# Argument: name - name of cid_file where the container id will be stored
|
|
|
41dba18 |
# Argument: command - executed inside the container
|
|
|
41dba18 |
# Argument: expected - string that is expected to be in the command output
|
|
|
41dba18 |
# Uses: $CID_FILE_DIR - path to directory containing cid_files
|
|
|
41dba18 |
# Uses: $IMAGE_NAME - name of the image being tested
|
|
|
41dba18 |
function ct_scl_usage_old() {
|
|
|
41dba18 |
local name="$1"
|
|
|
41dba18 |
local command="$2"
|
|
|
41dba18 |
local expected="$3"
|
|
|
41dba18 |
local out=""
|
|
|
41dba18 |
: " Testing the image SCL enable"
|
|
|
41dba18 |
out=$(docker run --rm ${IMAGE_NAME} /bin/bash -c "${command}")
|
|
|
41dba18 |
if ! echo "${out}" | grep -q "${expected}"; then
|
|
|
41dba18 |
echo "ERROR[/bin/bash -c "${command}"] Expected '${expected}', got '${out}'" >&2
|
|
|
41dba18 |
return 1
|
|
|
41dba18 |
fi
|
|
|
41dba18 |
out=$(docker exec $(ct_get_cid $name) /bin/bash -c "${command}" 2>&1)
|
|
|
41dba18 |
if ! echo "${out}" | grep -q "${expected}"; then
|
|
|
41dba18 |
echo "ERROR[exec /bin/bash -c "${command}"] Expected '${expected}', got '${out}'" >&2
|
|
|
41dba18 |
return 1
|
|
|
41dba18 |
fi
|
|
|
41dba18 |
out=$(docker exec $(ct_get_cid $name) /bin/sh -ic "${command}" 2>&1)
|
|
|
41dba18 |
if ! echo "${out}" | grep -q "${expected}"; then
|
|
|
41dba18 |
echo "ERROR[exec /bin/sh -ic "${command}"] Expected '${expected}', got '${out}'" >&2
|
|
|
41dba18 |
return 1
|
|
|
41dba18 |
fi
|
|
|
41dba18 |
}
|
|
|
41dba18 |
|
|
|
41dba18 |
# ct_doc_content_old [strings]
|
|
|
41dba18 |
# --------------------
|
|
|
41dba18 |
# Looks for occurence of stirngs in the documentation files and checks
|
|
|
41dba18 |
# the format of the files. Files examined: help.1
|
|
|
41dba18 |
# Argument: strings - strings expected to appear in the documentation
|
|
|
41dba18 |
# Uses: $IMAGE_NAME - name of the image being tested
|
|
|
41dba18 |
function ct_doc_content_old() {
|
|
|
41dba18 |
local tmpdir=$(mktemp -d)
|
|
|
41dba18 |
local f
|
|
|
41dba18 |
: " Testing documentation in the container image"
|
|
|
41dba18 |
# Extract the help files from the container
|
|
|
41dba18 |
for f in help.1 ; do
|
|
|
41dba18 |
docker run --rm ${IMAGE_NAME} /bin/bash -c "cat /${f}" >${tmpdir}/$(basename ${f})
|
|
|
41dba18 |
# Check whether the files contain some important information
|
|
|
41dba18 |
for term in $@ ; do
|
|
|
41dba18 |
if ! cat ${tmpdir}/$(basename ${f}) | grep -F -q -e "${term}" ; then
|
|
|
41dba18 |
echo "ERROR: File /${f} does not include '${term}'." >&2
|
|
|
41dba18 |
return 1
|
|
|
41dba18 |
fi
|
|
|
41dba18 |
done
|
|
|
41dba18 |
# Check whether the files use the correct format
|
|
|
41dba18 |
for term in TH PP SH ; do
|
|
|
41dba18 |
if ! grep -q "^\.${term}" ${tmpdir}/help.1 ; then
|
|
|
41dba18 |
echo "ERROR: /help.1 is probably not in troff or groff format, since '${term}' is missing." >&2
|
|
|
41dba18 |
return 1
|
|
|
41dba18 |
fi
|
|
|
41dba18 |
done
|
|
|
41dba18 |
done
|
|
|
41dba18 |
: " Success!"
|
|
|
41dba18 |
}
|
|
|
41dba18 |
|
|
|
41dba18 |
|
|
|
41dba18 |
# ct_npm_works
|
|
|
41dba18 |
# --------------------
|
|
|
41dba18 |
# Checks existance of the npm tool and runs it.
|
|
|
41dba18 |
function ct_npm_works() {
|
|
|
41dba18 |
local tmpdir=$(mktemp -d)
|
|
|
41dba18 |
: " Testing npm in the container image"
|
|
|
41dba18 |
docker run --rm ${IMAGE_NAME} /bin/bash -c "npm --version" >${tmpdir}/version
|
|
|
41dba18 |
|
|
|
41dba18 |
if [ $? -ne 0 ] ; then
|
|
|
41dba18 |
echo "ERROR: 'npm --version' does not work inside the image ${IMAGE_NAME}." >&2
|
|
|
41dba18 |
return 1
|
|
|
41dba18 |
fi
|
|
|
41dba18 |
|
|
|
41dba18 |
docker run --rm ${IMAGE_NAME} /bin/bash -c "npm install jquery && test -f node_modules/jquery/src/jquery.js"
|
|
|
41dba18 |
if [ $? -ne 0 ] ; then
|
|
|
41dba18 |
echo "ERROR: npm could not install jquery inside the image ${IMAGE_NAME}." >&2
|
|
|
41dba18 |
return 1
|
|
|
41dba18 |
fi
|
|
|
41dba18 |
|
|
|
41dba18 |
: " Success!"
|
|
|
41dba18 |
}
|
|
|
41dba18 |
|
|
|
41dba18 |
|
|
|
41dba18 |
# ct_path_append PATH_VARNAME DIRECTORY
|
|
|
41dba18 |
# -------------------------------------
|
|
|
41dba18 |
# Append DIRECTORY to VARIABLE of name PATH_VARNAME, the VARIABLE must consist
|
|
|
41dba18 |
# of colon-separated list of directories.
|
|
|
41dba18 |
ct_path_append ()
|
|
|
41dba18 |
{
|
|
|
41dba18 |
if eval "test -n \"\${$1-}\""; then
|
|
|
41dba18 |
eval "$1=\$2:\$$1"
|
|
|
41dba18 |
else
|
|
|
41dba18 |
eval "$1=\$2"
|
|
|
41dba18 |
fi
|
|
|
41dba18 |
}
|
|
|
41dba18 |
|
|
|
41dba18 |
|
|
|
41dba18 |
# ct_path_foreach PATH ACTION [ARGS ...]
|
|
|
41dba18 |
# --------------------------------------
|
|
|
41dba18 |
# For each DIR in PATH execute ACTION (path is colon separated list of
|
|
|
41dba18 |
# directories). The particular calls to ACTION will look like
|
|
|
41dba18 |
# '$ ACTION directory [ARGS ...]'
|
|
|
41dba18 |
ct_path_foreach ()
|
|
|
41dba18 |
{
|
|
|
41dba18 |
local dir dirlist action save_IFS
|
|
|
41dba18 |
save_IFS=$IFS
|
|
|
41dba18 |
IFS=:
|
|
|
41dba18 |
dirlist=$1
|
|
|
41dba18 |
action=$2
|
|
|
41dba18 |
shift 2
|
|
|
41dba18 |
for dir in $dirlist; do "$action" "$dir" "$@" ; done
|
|
|
41dba18 |
IFS=$save_IFS
|
|
|
41dba18 |
}
|
|
|
41dba18 |
|
|
|
41dba18 |
|
|
|
41dba18 |
# ct_run_test_list
|
|
|
41dba18 |
# --------------------
|
|
|
41dba18 |
# Execute the tests specified by TEST_LIST
|
|
|
41dba18 |
# Uses: $TEST_LIST - list of test names
|
|
|
41dba18 |
function ct_run_test_list() {
|
|
|
41dba18 |
for test_case in $TEST_LIST; do
|
|
|
41dba18 |
: "Running test $test_case"
|
|
|
41dba18 |
[ -f test/$test_case ] && source test/$test_case
|
|
|
41dba18 |
[ -f ../test/$test_case ] && source ../test/$test_case
|
|
|
41dba18 |
$test_case
|
|
|
41dba18 |
done;
|
|
|
41dba18 |
}
|
|
|
41dba18 |
|
|
|
41dba18 |
# ct_gen_self_signed_cert_pem
|
|
|
41dba18 |
# ---------------------------
|
|
|
41dba18 |
# Generates a self-signed PEM certificate pair into specified directory.
|
|
|
41dba18 |
# Argument: output_dir - output directory path
|
|
|
41dba18 |
# Argument: base_name - base name of the certificate files
|
|
|
41dba18 |
# Resulted files will be those:
|
|
|
41dba18 |
# <output_dir>/<base_name>-cert-selfsigned.pem -- public PEM cert
|
|
|
41dba18 |
# <output_dir>/<base_name>-key.pem -- PEM private key
|
|
|
41dba18 |
ct_gen_self_signed_cert_pem() {
|
|
|
41dba18 |
local output_dir=$1 ; shift
|
|
|
41dba18 |
local base_name=$1 ; shift
|
|
|
41dba18 |
mkdir -p ${output_dir}
|
|
|
41dba18 |
openssl req -newkey rsa:2048 -nodes -keyout ${output_dir}/${base_name}-key.pem -subj '/C=GB/ST=Berkshire/L=Newbury/O=My Server Company' > ${base_name}-req.pem
|
|
|
41dba18 |
openssl req -new -x509 -nodes -key ${output_dir}/${base_name}-key.pem -batch > ${output_dir}/${base_name}-cert-selfsigned.pem
|
|
|
41dba18 |
}
|
|
|
41dba18 |
|
|
|
41dba18 |
# ct_obtain_input FILE|DIR|URL
|
|
|
41dba18 |
# --------------------
|
|
|
41dba18 |
# Either copies a file or a directory to a tmp location for local copies, or
|
|
|
41dba18 |
# downloads the file from remote location.
|
|
|
41dba18 |
# Resulted file path is printed, so it can be later used by calling function.
|
|
|
41dba18 |
# Arguments: input - local file, directory or remote URL
|
|
|
41dba18 |
function ct_obtain_input() {
|
|
|
41dba18 |
local input=$1
|
|
|
41dba18 |
local extension="${input##*.}"
|
|
|
41dba18 |
|
|
|
41dba18 |
# Try to use same extension for the temporary file if possible
|
|
|
41dba18 |
[[ "${extension}" =~ ^[a-z0-9]*$ ]] && extension=".${extension}" || extension=""
|
|
|
41dba18 |
|
|
|
41dba18 |
local output=$(mktemp "/var/tmp/test-input-XXXXXX$extension")
|
|
|
41dba18 |
if [ -f "${input}" ] ; then
|
|
|
41dba18 |
cp -f "${input}" "${output}"
|
|
|
41dba18 |
elif [ -d "${input}" ] ; then
|
|
|
41dba18 |
rm -f "${output}"
|
|
|
41dba18 |
cp -r -LH "${input}" "${output}"
|
|
|
41dba18 |
elif echo "${input}" | grep -qe '^http\(s\)\?://' ; then
|
|
|
41dba18 |
curl "${input}" > "${output}"
|
|
|
41dba18 |
else
|
|
|
41dba18 |
echo "ERROR: file type not known: ${input}" >&2
|
|
|
41dba18 |
return 1
|
|
|
41dba18 |
fi
|
|
|
41dba18 |
echo "${output}"
|
|
|
41dba18 |
}
|
|
|
41dba18 |
|
|
|
41dba18 |
# ct_test_response
|
|
|
41dba18 |
# ----------------
|
|
|
41dba18 |
# Perform GET request to the application container, checks output with
|
|
|
41dba18 |
# a reg-exp and HTTP response code.
|
|
|
41dba18 |
# Argument: url - request URL path
|
|
|
41dba18 |
# Argument: expected_code - expected HTTP response code
|
|
|
41dba18 |
# Argument: body_regexp - PCRE regular expression that must match the response body
|
|
|
41dba18 |
# Argument: max_attempts - Optional number of attempts (default: 20), three seconds sleep between
|
|
|
41dba18 |
# Argument: ignore_error_attempts - Optional number of attempts when we ignore error output (default: 10)
|
|
|
41dba18 |
ct_test_response() {
|
|
|
41dba18 |
local url="$1"
|
|
|
41dba18 |
local expected_code="$2"
|
|
|
41dba18 |
local body_regexp="$3"
|
|
|
41dba18 |
local max_attempts=${4:-20}
|
|
|
41dba18 |
local ignore_error_attempts=${5:-10}
|
|
|
41dba18 |
|
|
|
41dba18 |
: " Testing the HTTP(S) response for <${url}>"
|
|
|
41dba18 |
local sleep_time=3
|
|
|
41dba18 |
local attempt=1
|
|
|
41dba18 |
local result=1
|
|
|
41dba18 |
local status
|
|
|
41dba18 |
local response_code
|
|
|
41dba18 |
local response_file=$(mktemp /tmp/ct_test_response_XXXXXX)
|
|
|
41dba18 |
while [ ${attempt} -le ${max_attempts} ]; do
|
|
|
41dba18 |
curl --connect-timeout 10 -s -w '%{http_code}' "${url}" >${response_file} && status=0 || status=1
|
|
|
41dba18 |
if [ ${status} -eq 0 ]; then
|
|
|
41dba18 |
response_code=$(cat ${response_file} | tail -c 3)
|
|
|
41dba18 |
if [ "${response_code}" -eq "${expected_code}" ]; then
|
|
|
41dba18 |
result=0
|
|
|
41dba18 |
fi
|
|
|
41dba18 |
cat ${response_file} | grep -qP -e "${body_regexp}" || result=1;
|
|
|
41dba18 |
# Some services return 40x code until they are ready, so let's give them
|
|
|
41dba18 |
# some chance and not end with failure right away
|
|
|
41dba18 |
# Do not wait if we already have expected outcome though
|
|
|
41dba18 |
if [ ${result} -eq 0 -o ${attempt} -gt ${ignore_error_attempts} -o ${attempt} -eq ${max_attempts} ] ; then
|
|
|
41dba18 |
break
|
|
|
41dba18 |
fi
|
|
|
41dba18 |
fi
|
|
|
41dba18 |
attempt=$(( ${attempt} + 1 ))
|
|
|
41dba18 |
sleep ${sleep_time}
|
|
|
41dba18 |
done
|
|
|
41dba18 |
rm -f ${response_file}
|
|
|
41dba18 |
return ${result}
|
|
|
41dba18 |
}
|
|
|
41dba18 |
|
|
|
41dba18 |
# ct_registry_from_os OS
|
|
|
41dba18 |
# ----------------
|
|
|
41dba18 |
# Transform operating system string [os] into registry url
|
|
|
41dba18 |
# Argument: OS - string containing the os version
|
|
|
41dba18 |
ct_registry_from_os() {
|
|
|
41dba18 |
local registry=""
|
|
|
41dba18 |
case $1 in
|
|
|
41dba18 |
rhel7)
|
|
|
41dba18 |
registry=registry.access.redhat.com
|
|
|
41dba18 |
;;
|
|
|
41dba18 |
*)
|
|
|
41dba18 |
registry=docker.io
|
|
|
41dba18 |
;;
|
|
|
41dba18 |
esac
|
|
|
41dba18 |
echo "$registry"
|
|
|
41dba18 |
}
|
|
|
41dba18 |
|
|
|
41dba18 |
# ct_assert_cmd_success CMD
|
|
|
41dba18 |
# ----------------
|
|
|
41dba18 |
# Evaluates [cmd] and fails if it does not succeed.
|
|
|
41dba18 |
# Argument: CMD - Command to be run
|
|
|
41dba18 |
function ct_assert_cmd_success() {
|
|
|
41dba18 |
echo "Checking '$*' for success ..."
|
|
|
41dba18 |
if ! eval "$@" &>/dev/null; then
|
|
|
41dba18 |
echo " FAIL"
|
|
|
41dba18 |
return 1
|
|
|
41dba18 |
fi
|
|
|
41dba18 |
echo " PASS"
|
|
|
41dba18 |
return 0
|
|
|
41dba18 |
}
|
|
|
41dba18 |
|
|
|
41dba18 |
# ct_assert_cmd_failure CMD
|
|
|
41dba18 |
# ----------------
|
|
|
41dba18 |
# Evaluates [cmd] and fails if it succeeds.
|
|
|
41dba18 |
# Argument: CMD - Command to be run
|
|
|
41dba18 |
function ct_assert_cmd_failure() {
|
|
|
41dba18 |
echo "Checking '$*' for failure ..."
|
|
|
41dba18 |
if eval "$@" &>/dev/null; then
|
|
|
41dba18 |
echo " FAIL"
|
|
|
41dba18 |
return 1
|
|
|
41dba18 |
fi
|
|
|
41dba18 |
echo " PASS"
|
|
|
41dba18 |
return 0
|
|
|
41dba18 |
}
|
|
|
41dba18 |
|
|
|
41dba18 |
|
|
|
41dba18 |
# ct_random_string [LENGTH=10]
|
|
|
41dba18 |
# ----------------------------
|
|
|
41dba18 |
# Generate pseudorandom alphanumeric string of LENGTH bytes, the
|
|
|
41dba18 |
# default length is 10. The string is printed on stdout.
|
|
|
41dba18 |
ct_random_string()
|
|
|
41dba18 |
(
|
|
|
41dba18 |
export LC_ALL=C
|
|
|
41dba18 |
dd if=/dev/urandom count=1 bs=10k 2>/dev/null \
|
|
|
41dba18 |
| tr -dc 'a-z0-9' \
|
|
|
41dba18 |
| fold -w "${1-10}" \
|
|
|
41dba18 |
| head -n 1
|
|
|
41dba18 |
)
|
|
|
41dba18 |
|
|
|
41dba18 |
# ct_s2i_usage IMG_NAME [S2I_ARGS]
|
|
|
41dba18 |
# ----------------------------
|
|
|
41dba18 |
# Create a container and run the usage script inside
|
|
|
41dba18 |
# Argument: IMG_NAME - name of the image to be used for the container run
|
|
|
41dba18 |
# Argument: S2I_ARGS - Additional list of source-to-image arguments, currently unused.
|
|
|
41dba18 |
ct_s2i_usage()
|
|
|
41dba18 |
{
|
|
|
41dba18 |
local img_name=$1; shift
|
|
|
41dba18 |
local s2i_args="$*";
|
|
|
41dba18 |
local usage_command="/usr/libexec/s2i/usage"
|
|
|
41dba18 |
docker run --rm "$img_name" bash -c "$usage_command"
|
|
|
41dba18 |
}
|
|
|
41dba18 |
|
|
|
41dba18 |
# ct_s2i_build_as_df APP_PATH SRC_IMAGE DST_IMAGE [S2I_ARGS]
|
|
|
41dba18 |
# ----------------------------
|
|
|
41dba18 |
# Create a new s2i app image from local sources in a similar way as source-to-image would have used.
|
|
|
41dba18 |
# Argument: APP_PATH - local path to the app sources to be used in the test
|
|
|
41dba18 |
# Argument: SRC_IMAGE - image to be used as a base for the s2i build
|
|
|
41dba18 |
# Argument: DST_IMAGE - image name to be used during the tagging of the s2i build result
|
|
|
41dba18 |
# Argument: S2I_ARGS - Additional list of source-to-image arguments.
|
|
|
41dba18 |
# Only used to check for pull-policy=never and environment variable definitions.
|
|
|
41dba18 |
ct_s2i_build_as_df()
|
|
|
41dba18 |
{
|
|
|
41dba18 |
local app_path=$1; shift
|
|
|
41dba18 |
local src_image=$1; shift
|
|
|
41dba18 |
local dst_image=$1; shift
|
|
|
41dba18 |
local s2i_args="$*";
|
|
|
41dba18 |
local local_app=upload/src/
|
|
|
41dba18 |
local local_scripts=upload/scripts/
|
|
|
41dba18 |
local user_id=
|
|
|
41dba18 |
local df_name=
|
|
|
41dba18 |
local tmpdir=
|
|
|
41dba18 |
# Use /tmp to not pollute cwd
|
|
|
41dba18 |
tmpdir=$(mktemp -d)
|
|
|
41dba18 |
df_name=$(mktemp -p "$tmpdir" Dockerfile.XXXX)
|
|
|
41dba18 |
pushd "$tmpdir"
|
|
|
41dba18 |
# Check if the image is available locally and try to pull it if it is not
|
|
|
41dba18 |
docker images "$src_image" &>/dev/null || echo "$s2i_args" | grep -q "pull-policy=never" || docker pull "$src_image"
|
|
|
41dba18 |
user_id=$(docker inspect -f "{{.ContainerConfig.User}}" "$src_image")
|
|
|
41dba18 |
# Strip file:// from APP_PATH and copy its contents into current context
|
|
|
41dba18 |
mkdir -p "$local_app"
|
|
|
41dba18 |
cp -r "${app_path/file:\/\//}/." "$local_app"
|
|
|
41dba18 |
[ -d "$local_app/.s2i/bin/" ] && mv "$local_app/.s2i/bin" "$local_scripts"
|
|
|
41dba18 |
# Create a Dockerfile named df_name and fill it with proper content
|
|
|
41dba18 |
#FIXME: Some commands could be combined into a single layer but not sure if worth the trouble for testing purposes
|
|
|
41dba18 |
cat <<EOF >"$df_name"
|
|
|
41dba18 |
FROM $src_image
|
|
|
41dba18 |
LABEL "io.openshift.s2i.build.image"="$src_image" \\
|
|
|
41dba18 |
"io.openshift.s2i.build.source-location"="$app_path"
|
|
|
41dba18 |
USER root
|
|
|
41dba18 |
COPY $local_app /tmp/src
|
|
|
41dba18 |
EOF
|
|
|
41dba18 |
[ -d "$local_scripts" ] && echo "COPY $local_scripts /tmp/scripts" >> "$df_name" &&
|
|
|
41dba18 |
echo "RUN chown -R $user_id:0 /tmp/scripts" >>"$df_name"
|
|
|
41dba18 |
echo "RUN chown -R $user_id:0 /tmp/src" >>"$df_name"
|
|
|
41dba18 |
# Check for custom environment variables inside .s2i/ folder
|
|
|
41dba18 |
if [ -e "$local_app/.s2i/environment" ]; then
|
|
|
41dba18 |
# Remove any comments and add the contents as ENV commands to the Dockerfile
|
|
|
41dba18 |
sed '/^\s*#.*$/d' "$local_app/.s2i/environment" | while read -r line; do
|
|
|
41dba18 |
echo "ENV $line" >>"$df_name"
|
|
|
41dba18 |
done
|
|
|
41dba18 |
fi
|
|
|
41dba18 |
# Filter out env var definitions from $s2i_args and create Dockerfile ENV commands out of them
|
|
|
41dba18 |
echo "$s2i_args" | grep -o -e '\(-e\|--env\)[[:space:]=]\S*=\S*' | sed -e 's/-e /ENV /' -e 's/--env[ =]/ENV /' >>"$df_name"
|
|
|
41dba18 |
echo "USER $user_id" >>"$df_name"
|
|
|
41dba18 |
# If exists, run the custom assemble script, else default to /usr/libexec/s2i/assemble
|
|
|
41dba18 |
if [ -x "$local_scripts/assemble" ]; then
|
|
|
41dba18 |
echo "RUN /tmp/scripts/assemble" >>"$df_name"
|
|
|
41dba18 |
else
|
|
|
41dba18 |
echo "RUN /usr/libexec/s2i/assemble" >>"$df_name"
|
|
|
41dba18 |
fi
|
|
|
41dba18 |
# If exists, set the custom run script as CMD, else default to /usr/libexec/s2i/run
|
|
|
41dba18 |
if [ -x "$local_scripts/run" ]; then
|
|
|
41dba18 |
echo "CMD /tmp/scripts/run" >>"$df_name"
|
|
|
41dba18 |
else
|
|
|
41dba18 |
echo "CMD /usr/libexec/s2i/run" >>"$df_name"
|
|
|
41dba18 |
fi
|
|
|
41dba18 |
# Run the build and tag the result
|
|
|
41dba18 |
docker build -f "$df_name" -t "$dst_image" .
|
|
|
41dba18 |
popd
|
|
|
41dba18 |
}
|