|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# The 'run' performs a simple test that verifies that STI image. |
| 4 | +# The main focus here is to excersise the STI scripts. |
| 5 | +# |
| 6 | +# IMAGE_NAME specifies a name of the candidate image used for testing. |
| 7 | +# The image has to be available before this script is executed. |
| 8 | +# |
| 9 | +IMAGE_NAME=${IMAGE_NAME-openshift/python33-centos7-candidate} |
| 10 | + |
| 11 | +declare -a WEB_APPS=(standalone setup) |
| 12 | + |
| 13 | +# TODO: Make command compatible for Mac users |
| 14 | +test_dir="$(readlink -zf $(dirname "${BASH_SOURCE[0]}"))" |
| 15 | +image_dir=$(readlink -zf ${test_dir}/..) |
| 16 | +scripts_url="file://${image_dir}/.sti/bin" |
| 17 | + |
| 18 | +# TODO: This should be part of the image metadata |
| 19 | +test_port=8080 |
| 20 | + |
| 21 | +info() { |
| 22 | + echo -e "\n\e[1m[INFO] $@...\e[0m\n" |
| 23 | +} |
| 24 | + |
| 25 | +image_exists() { |
| 26 | + docker inspect $1 &>/dev/null |
| 27 | +} |
| 28 | + |
| 29 | +container_exists() { |
| 30 | + image_exists $(cat $cid_file) |
| 31 | +} |
| 32 | + |
| 33 | +container_ip() { |
| 34 | + docker inspect --format="{{ .NetworkSettings.IPAddress }}" $(cat $cid_file) |
| 35 | +} |
| 36 | + |
| 37 | +run_sti_build() { |
| 38 | + sti build ${sti_args} file://${test_dir}/${1}-test-app ${IMAGE_NAME} ${IMAGE_NAME}-testapp |
| 39 | +} |
| 40 | + |
| 41 | +prepare() { |
| 42 | + if ! image_exists ${IMAGE_NAME}; then |
| 43 | + echo "ERROR: The image ${IMAGE_NAME} must exist before this script is executed." |
| 44 | + exit 1 |
| 45 | + fi |
| 46 | + # TODO: STI build require the application is a valid 'GIT' repository, we |
| 47 | + # should remove this restriction in the future when a file:// is used. |
| 48 | + info "Build the ${1}-test application image" |
| 49 | + pushd ${test_dir}/${1}-test-app >/dev/null |
| 50 | + git init |
| 51 | + git config user.email "build@localhost" && git config user.name "builder" |
| 52 | + git add -A && git commit -m "Sample commit" |
| 53 | + popd >/dev/null |
| 54 | +} |
| 55 | + |
| 56 | +run_test_application() { |
| 57 | + docker run --rm --cidfile=${cid_file} -p ${test_port} ${IMAGE_NAME}-testapp |
| 58 | +} |
| 59 | + |
| 60 | +cleanup() { |
| 61 | + info "Cleaning up the test application image" |
| 62 | + if [ -f $cid_file ]; then |
| 63 | + if container_exists; then |
| 64 | + docker stop $(cat $cid_file) |
| 65 | + fi |
| 66 | + fi |
| 67 | + if image_exists ${IMAGE_NAME}-testapp; then |
| 68 | + docker rmi ${IMAGE_NAME}-testapp |
| 69 | + fi |
| 70 | + rm -rf ${test_dir}/${1}-test-app/.git |
| 71 | +} |
| 72 | + |
| 73 | +check_result() { |
| 74 | + local result="$1" |
| 75 | + if [[ "$result" != "0" ]]; then |
| 76 | + info "TEST FAILED (${result})" |
| 77 | + cleanup |
| 78 | + exit $result |
| 79 | + fi |
| 80 | +} |
| 81 | + |
| 82 | +wait_for_cid() { |
| 83 | + local max_attempts=10 |
| 84 | + local sleep_time=1 |
| 85 | + local attempt=1 |
| 86 | + local result=1 |
| 87 | + info "Waiting for application container to start" |
| 88 | + while [ $attempt -le $max_attempts ]; do |
| 89 | + [ -f $cid_file ] && break |
| 90 | + attempt=$(( $attempt + 1 )) |
| 91 | + sleep $sleep_time |
| 92 | + done |
| 93 | +} |
| 94 | + |
| 95 | +test_sti_usage() { |
| 96 | + info "Testing 'sti usage'" |
| 97 | + sti usage ${sti_args} ${IMAGE_NAME} &>/dev/null |
| 98 | +} |
| 99 | + |
| 100 | +test_docker_run_usage() { |
| 101 | + info "Testing 'docker run' usage" |
| 102 | + docker run ${IMAGE_NAME} &>/dev/null |
| 103 | +} |
| 104 | + |
| 105 | + |
| 106 | +test_connection() { |
| 107 | + info "Testing the HTTP connection (http://$(container_ip):${test_port})" |
| 108 | + local max_attempts=10 |
| 109 | + local sleep_time=1 |
| 110 | + local attempt=1 |
| 111 | + local result=1 |
| 112 | + while [ $attempt -le $max_attempts ]; do |
| 113 | + response_code=$(curl -s -w %{http_code} -o /dev/null http://$(container_ip):${test_port}/) |
| 114 | + status=$? |
| 115 | + if [ $status -eq 0 ]; then |
| 116 | + if [ $response_code -eq 200 ]; then |
| 117 | + result=0 |
| 118 | + fi |
| 119 | + break |
| 120 | + fi |
| 121 | + attempt=$(( $attempt + 1 )) |
| 122 | + sleep $sleep_time |
| 123 | + done |
| 124 | + return $result |
| 125 | +} |
| 126 | + |
| 127 | +for app in ${WEB_APPS[@]}; do |
| 128 | + cid_file=$(mktemp -u --suffix=.cid) |
| 129 | + |
| 130 | + # Since we built the candidate image locally, we don't want STI attempt to pull |
| 131 | + # it from Docker hub |
| 132 | + sti_args="--forcePull=false -s ${scripts_url}" |
| 133 | + |
| 134 | + prepare ${app} |
| 135 | + run_sti_build ${app} |
| 136 | + check_result $? |
| 137 | + |
| 138 | + # Verify the 'usage' script is working properly when running the base image with 'sti usage ...' |
| 139 | + test_sti_usage |
| 140 | + check_result $? |
| 141 | + |
| 142 | + # Verify the 'usage' script is working properly when running the base image with 'docker run ...' |
| 143 | + test_docker_run_usage |
| 144 | + check_result $? |
| 145 | + |
| 146 | + # Verify that the HTTP connection can be established to test application container |
| 147 | + run_test_application & |
| 148 | + |
| 149 | + # Wait for the container to write it's CID file |
| 150 | + wait_for_cid |
| 151 | + |
| 152 | + test_connection |
| 153 | + check_result $? |
| 154 | + |
| 155 | + info "All tests for the ${app}-test-app finished successfully." |
| 156 | + cleanup ${app} |
| 157 | +done |
| 158 | + |
| 159 | +info "All tests finished successfully." |
0 commit comments