ruby/test/run
2021-11-23 13:19:17 +00:00

269 lines
6.7 KiB
Bash
Executable file

#!/bin/bash -x
#
# The 'run' performs a simple test that verifies that S2I image.
# The main focus here is to excersise the S2I scripts.
#
# IMAGE_NAME specifies a name of the candidate image used for testing.
# The image has to be available before this script is executed.
#
IMAGE_NAME=${IMAGE_NAME-quay.io/centos7/ruby-27-centos7-candidate}
declare -a WEB_SERVERS=(db puma rack)
#declare -a WEB_SERVERS=(db)
# TODO: Make command compatible for Mac users
test_dir="$(readlink -zf $(dirname "${BASH_SOURCE[0]}"))"
image_dir=$(readlink -zf ${test_dir}/..)
test_short_summary=''
TESTSUITE_RESULT=0
TEST_LIST="\
test_docker_run_usage
test_application
test_connection
test_scl_variables_in_dockerfile
test_scl_usage
test_npm_functionality
"
source "${test_dir}/test-lib.sh"
# Read exposed port from image meta data
test_port="$(docker inspect --format='{{range $key, $value := .Config.ExposedPorts }}{{$key}}{{end}}' ${IMAGE_NAME} | sed 's/\/.*//')"
info() {
echo -e "\n\e[1m[INFO] $@...\e[0m\n"
}
image_exists() {
docker inspect $1 &>/dev/null
}
container_exists() {
image_exists $(cat $cid_file)
}
container_ip() {
docker inspect --format="{{ .NetworkSettings.IPAddress }}" $(cat $cid_file)
}
run_s2i_build() {
ct_s2i_build_as_df file://${test_dir}/${1}-test-app ${IMAGE_NAME} ${IMAGE_NAME}-testapp ${s2i_args} $(ct_build_s2i_npm_variables)
}
run_test_application() {
docker run --user=100001 --rm --cidfile=${cid_file} ${IMAGE_NAME}-testapp
}
cleanup() {
local server="$1"
info "Cleaning up the test application image $1"
if [ -f $cid_file ]; then
if container_exists; then
docker stop $(cat $cid_file)
fi
fi
if image_exists ${IMAGE_NAME}-testapp; then
docker rmi -f ${IMAGE_NAME}-testapp
fi
if [ ! -z "${server}" ]; then
rm -rf ${test_dir}/${server}-test-app/.git
fi
if [[ "${server}" == "db" ]]; then
rm -rf ${test_dir}/db-test-app
fi
}
check_result() {
local result="$1"
if [[ "$result" != "0" ]]; then
TESTCASE_RESULT=1
fi
return $result
}
wait_for_cid() {
local max_attempts=10
local sleep_time=1
local attempt=1
local result=1
info "Waiting for application container to start"
while [ $attempt -le $max_attempts ]; do
[ -f $cid_file ] && [ -s $cid_file ] && break
attempt=$(( $attempt + 1 ))
sleep $sleep_time
done
if [ $attempt -gt $max_attempts ]; then
return 1
fi
}
test_s2i_usage() {
info "Testing 's2i usage'"
ct_s2i_usage ${IMAGE_NAME} ${s2i_args} &>/dev/null
check_result "$?"
}
test_docker_run_usage() {
info "Testing 'docker run' usage"
docker run ${IMAGE_NAME} &>/dev/null
check_result "$?"
}
test_connection() {
info "Testing the HTTP connection (http://$(container_ip):${test_port})"
local max_attempts=10
local sleep_time=1
local attempt=1
local result=1
while [ $attempt -le $max_attempts ]; do
response_code=$(curl -s -w %{http_code} -o /dev/null http://$(container_ip):${test_port}/)
status=$?
if [ $status -eq 0 ]; then
if [ $response_code -eq 200 ]; then
echo "SUCCESS, container returned 200"
result=0
fi
break
fi
attempt=$(( $attempt + 1 ))
sleep $sleep_time
done
return $result
}
scl_usage() {
local run_cmd="$1"
local expected="$2"
info "Testing the image SCL enable"
out=$(docker run --rm ${IMAGE_NAME} /bin/bash -c "${run_cmd}")
if ! echo "${out}" | grep -q "${expected}"; then
echo "ERROR[/bin/bash -c "${run_cmd}"] Expected '${expected}', got '${out}'"
return 1
fi
out=$(docker exec $(cat ${cid_file}) /bin/bash -c "${run_cmd}" 2>&1)
if ! echo "${out}" | grep -q "${expected}"; then
echo "ERROR[exec /bin/bash -c "${run_cmd}"] Expected '${expected}', got '${out}'"
return 1
fi
out=$(docker exec $(cat ${cid_file}) /bin/sh -ic "${run_cmd}" 2>&1)
if ! echo "${out}" | grep -q "${expected}"; then
echo "ERROR[exec /bin/sh -ic "${run_cmd}"] Expected '${expected}', got '${out}'"
return 1
fi
return 0
}
function test_scl_usage() {
scl_usage "ruby --version" "ruby ${RUBY_VERSION}."
check_result "$?"
}
function test_npm_functionality() {
info "Testing npm availibility"
ct_npm_works
check_result "$?"
}
function handle_test_case_result() {
local output_format
output_format="%s %s $1\n"
shift
local test_msg
if [ $TESTCASE_RESULT -eq 0 ]; then
test_msg="[PASSED]"
else
test_msg="[FAILED]"
TESTSUITE_RESULT=1
fi
printf -v test_short_summary "${output_format}" "${test_short_summary}" "${test_msg}" $@
}
function run_all_tests() {
local APP_NAME="$1"
for test_case in $TEST_SET; do
info "Running test $test_case ... "
TESTCASE_RESULT=0
$test_case
check_result $?
local test_msg
handle_test_case_result "for '%s' %s" "${APP_NAME}" "$test_case"
[ -n "${FAIL_QUICKLY:-}" ] && {
cleanup "${APP_NAME}"
return 1
}
done;
cleanup "${APP_NAME}"
}
function test_application() {
# Verify that the HTTP connection can be established to test application container
run_test_application &
# Wait for the container to write it's CID file
wait_for_cid
check_result $?
}
function test_scl_variables_in_dockerfile() {
if [ "$OS" == "rhel7" ] || [ "$OS" == "centos7" ]; then
# autocleanup only enabled here as only the following tests so far use it
CID_FILE_DIR=$(mktemp -d)
ct_enable_cleanup
info "Testing variable presence during \`docker exec\`"
ct_check_exec_env_vars
check_result $?
info "Checking if all scl variables are defined in Dockerfile"
ct_check_scl_enable_vars
check_result $?
fi
}
function test_from_dockerfile() {
dockerfile="Dockerfile${1:-}"
TESTCASE_RESULT=0
info "Check building using a $dockerfile"
ct_test_app_dockerfile $test_dir/examples/from-dockerfile/$dockerfile 'https://github.com/sclorg/rails-ex.git' 'Welcome to your Rails application on OpenShift' app-src
check_result $?
handle_test_case_result "test_from_dockerfile for %s" "${dockerfile}"
}
pushd ${test_dir}
if [ -d db-test-app ]; then
rm -rf db-test-app
fi
git clone git://github.com/openshift/ruby-hello-world db-test-app
popd
for server in ${WEB_SERVERS[@]}; do
cid_file=$(mktemp -u --suffix=.cid)
# Since we built the candidate image locally, we don't want S2I attempt to pull
# it from Docker hub
s2i_args="--pull-policy=never"
TESTCASE_RESULT=0
run_s2i_build "${server}"
check_result $?
handle_test_case_result "build of '%s' app" "${server}"
TEST_SET=${TESTS:-$TEST_LIST} run_all_tests "$server"
cleanup "${server}"
done
test_from_dockerfile
test_from_dockerfile ".s2i"
echo "$test_short_summary"
if [ $TESTSUITE_RESULT -eq 0 ] ; then
echo "Tests for ${IMAGE_NAME} succeeded."
else
echo "Tests for ${IMAGE_NAME} failed."
fi
exit $TESTSUITE_RESULT