#!/bin/bash # # 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:-rhscl/nginx-112-rhel7} # TODO: Make command compatible for Mac users test_dir="$(readlink -zf $(dirname "${BASH_SOURCE[0]}"))" image_dir=$(readlink -zf ${test_dir}/..) . $test_dir/test-lib.sh # TODO: This should be part of the image metadata test_port=8080 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} ${IMAGE_NAME} ${IMAGE_NAME}-${1} ${s2i_args} } prepare() { if ! image_exists ${IMAGE_NAME}; then echo "ERROR: The image ${IMAGE_NAME} must exist before this script is executed." exit 1 fi # TODO: S2I build require the application is a valid 'GIT' repository, we # should remove this restriction in the future when a file:// is used. info "Build the test application image" pushd ${test_dir}/${1} >/dev/null git init git config user.email "build@localhost" && git config user.name "builder" git add -A && git commit -m "Sample commit" popd >/dev/null } run_test_application() { run_args=${CONTAINER_ARGS:-} docker run --user=100001 ${run_args} --cidfile=${cid_file} ${IMAGE_NAME}-${1} } cleanup_test_app() { info "Cleaning up the test application" if [ -f $cid_file ]; then if container_exists; then docker stop $(cat $cid_file) docker rm $(cat $cid_file) fi rm $cid_file fi } cleanup() { info "Cleaning up the test application image" if image_exists ${IMAGE_NAME}-${1}; then docker rmi -f ${IMAGE_NAME}-${1} fi rm -rf ${test_dir}/${1}/.git } check_result() { local result="$1" if [[ "$result" != "0" ]]; then info "TEST FAILED (${result})" cleanup $2 exit $result fi } 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 } test_s2i_usage() { info "Testing 's2i usage'" ct_s2i_usage ${IMAGE_NAME} ${s2i_args} &>/dev/null } test_docker_run_usage() { info "Testing 'docker run' usage" docker run ${IMAGE_NAME} &>/dev/null } test_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} 2>&1") if ! echo "${out}" | grep -q "${expected}"; then echo "ERROR[/bin/bash -c "${run_cmd}"] Expected '${expected}', got '${out}'" return 1 fi test_command "$run_cmd" "$expected" return $? } test_command() { local run_cmd="$1" local expected="$2" local message="$3" if [ $message ]; then info ${3} 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 } test_for_output() { local url="$1" local expect="$2" local host="${3-localhost}" local max_attempts=5 local sleep_time=1 local attempt=1 local result=1 info "Testing URL $url with HTTP hostname ${host} produces output $expect" while [ $attempt -le $max_attempts ]; do response=$(curl -s -H "Host: ${host}" ${url}) echo "${response}" status=$? if [ $status -eq 0 ]; then if echo "${response}" | grep -q "${expect}"; then info "Success!" result=0 break else echo "Response: ${response}" fi fi attempt=$(( $attempt + 1 )) sleep $sleep_time done return $result } test_connection() { cat $cid_file info "Testing the HTTP connection (http://$(container_ip):${test_port})" if test_for_output "http://$(container_ip):${test_port}/" "NGINX is working" && test_for_output "http://$(container_ip):${test_port}/" "NGINX2 is working" localhost2 && test_for_output "http://$(container_ip):${test_port}/aliased/index2.html" "NGINX2 is working" && test_for_output "http://$(container_ip):${test_port}/nginx-cfg/default.conf" "404"; then return 0 fi return 1 } test_connection_s2i() { cat $cid_file info "Testing the HTTP connection (http://$(container_ip):${test_port})" if test_for_output "http://$(container_ip):${test_port}/" "NGINX is working" && test_for_output "http://$(container_ip):${test_port}/" "NGINX2 is working" localhost2 && test_for_output "http://$(container_ip):${test_port}/nginx-cfg/default.conf" "404"; then return 0 fi return 1 } test_application() { # Verify that the HTTP connection can be established to test application container run_test_application "test-app" & # Wait for the container to write it's CID file wait_for_cid test_scl_usage "nginx -v" "nginx version: nginx/1.12." check_result $? "test-app" test_connection check_result $? "test-app" cleanup_test_app } test_pre_init_script() { # Verify that the HTTP connection can be established to test application container run_test_application "start-hook-test-app" & # Wait for the container to write it's CID file wait_for_cid test_command "cat /opt/app-root/etc/nginx.d/default.conf | grep 'resolver' | sed -e 's/resolver\(.*\);/\1/' | grep 'DNS_SERVER'" "" check_result $? "start-hook-test-app" test_connection_s2i check_result $? "start-hook-test-app" cleanup_test_app } 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" if [ "$TARGET" == "fedora" ]; then mv ${test_dir}/test-app/nginx.conf{,.bkp} cp ${test_dir}/test-app/nginx.conf{.fedora,} fi prepare "test-app" run_s2i_build "test-app" if [ "$TARGET" == "fedora" ]; then mv ${test_dir}/test-app/nginx.conf{.bkp,} fi check_result $? "test-app" # Verify the 'usage' script is working properly when running the base image with 's2i usage ...' test_s2i_usage check_result $? "test-app" # Verify the 'usage' script is working properly when running the base image with 'docker run ...' test_docker_run_usage check_result $? "test-app" # Test application with default uid test_application # Test application with random uid CONTAINER_ARGS="--user 12345" test_application cleanup "test-app" # Test start-hook script functionality cid_file=$(mktemp -u --suffix=.cid) prepare "start-hook-test-app" run_s2i_build "start-hook-test-app" check_result $? "start-hook-test-app" test_pre_init_script cleanup "start-hook-test-app" info "All tests finished successfully."