github.com/nak3/source-to-image@v1.1.10-0.20180319140719-2ed55639898d/pkg/create/templates/test.go (about) 1 package templates 2 3 // TestRunScript is a simple test script that verifies the S2I image. 4 const TestRunScript = `#!/bin/bash 5 # 6 # The 'run' performs a simple test that verifies the S2I image. 7 # The main focus here is to exercise the S2I scripts. 8 # 9 # For more information see the documentation: 10 # https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md 11 # 12 # IMAGE_NAME specifies a name of the candidate image used for testing. 13 # The image has to be available before this script is executed. 14 # 15 IMAGE_NAME=${IMAGE_NAME-{{.ImageName}}-candidate} 16 17 # Determining system utility executables (darwin compatibility check) 18 READLINK_EXEC="readlink" 19 MKTEMP_EXEC="mktemp" 20 if [[ "$OSTYPE" =~ 'darwin' ]]; then 21 ! type -a "greadlink" &>"/dev/null" || READLINK_EXEC="greadlink" 22 ! type -a "gmktemp" &>"/dev/null" || MKTEMP_EXEC="gmktemp" 23 fi 24 25 test_dir="$($READLINK_EXEC -zf $(dirname "${BASH_SOURCE[0]}"))" 26 image_dir=$($READLINK_EXEC -zf ${test_dir}/..) 27 cid_file=$($MKTEMP_EXEC -u --suffix=.cid) 28 29 # Since we built the candidate image locally, we don't want S2I to attempt to pull 30 # it from Docker hub 31 s2i_args="--pull-policy=never --loglevel=2" 32 33 # Port the image exposes service to be tested 34 test_port=8080 35 36 image_exists() { 37 docker inspect $1 &>/dev/null 38 } 39 40 container_exists() { 41 image_exists $(cat $cid_file) 42 } 43 44 container_ip() { 45 if [ ! -z "$DOCKER_HOST" ] && [[ "$OSTYPE" =~ 'darwin' ]]; then 46 docker-machine ip 47 else 48 docker inspect --format="{{"{{"}} .NetworkSettings.IPAddress {{"}}"}}" $(cat $cid_file) 49 fi 50 } 51 52 container_port() { 53 if [ ! -z "$DOCKER_HOST" ] && [[ "$OSTYPE" =~ 'darwin' ]]; then 54 docker inspect --format="{{"{{"}}(index .NetworkSettings.Ports \"$test_port/tcp\" 0).HostPort{{"}}"}}" "$(cat "${cid_file}")" 55 else 56 echo $test_port 57 fi 58 } 59 60 run_s2i_build() { 61 s2i build --incremental=true ${s2i_args} file://${test_dir}/test-app ${IMAGE_NAME} ${IMAGE_NAME}-testapp 62 } 63 64 prepare() { 65 if ! image_exists ${IMAGE_NAME}; then 66 echo "ERROR: The image ${IMAGE_NAME} must exist before this script is executed." 67 exit 1 68 fi 69 # s2i build requires the application is a valid 'Git' repository 70 pushd ${test_dir}/test-app >/dev/null 71 git init 72 git config user.email "build@localhost" && git config user.name "builder" 73 git add -A && git commit -m "Sample commit" 74 popd >/dev/null 75 run_s2i_build 76 } 77 78 run_test_application() { 79 docker run --rm --cidfile=${cid_file} -p ${test_port} ${IMAGE_NAME}-testapp 80 } 81 82 cleanup() { 83 if [ -f $cid_file ]; then 84 if container_exists; then 85 docker stop $(cat $cid_file) 86 fi 87 fi 88 if image_exists ${IMAGE_NAME}-testapp; then 89 docker rmi ${IMAGE_NAME}-testapp 90 fi 91 } 92 93 check_result() { 94 local result="$1" 95 if [[ "$result" != "0" ]]; then 96 echo "S2I image '${IMAGE_NAME}' test FAILED (exit code: ${result})" 97 cleanup 98 exit $result 99 fi 100 } 101 102 wait_for_cid() { 103 local max_attempts=10 104 local sleep_time=1 105 local attempt=1 106 local result=1 107 while [ $attempt -le $max_attempts ]; do 108 [ -f $cid_file ] && break 109 echo "Waiting for container to start..." 110 attempt=$(( $attempt + 1 )) 111 sleep $sleep_time 112 done 113 } 114 115 test_usage() { 116 echo "Testing 's2i usage'..." 117 s2i usage ${s2i_args} ${IMAGE_NAME} &>/dev/null 118 } 119 120 test_connection() { 121 echo "Testing HTTP connection (http://$(container_ip):$(container_port))" 122 local max_attempts=10 123 local sleep_time=1 124 local attempt=1 125 local result=1 126 while [ $attempt -le $max_attempts ]; do 127 echo "Sending GET request to http://$(container_ip):$(container_port)/" 128 response_code=$(curl -s -w %{http_code} -o /dev/null http://$(container_ip):$(container_port)/) 129 status=$? 130 if [ $status -eq 0 ]; then 131 if [ $response_code -eq 200 ]; then 132 result=0 133 fi 134 break 135 fi 136 attempt=$(( $attempt + 1 )) 137 sleep $sleep_time 138 done 139 return $result 140 } 141 142 # Build the application image twice to ensure the 'save-artifacts' and 143 # 'restore-artifacts' scripts are working properly 144 prepare 145 run_s2i_build 146 check_result $? 147 148 # Verify the 'usage' script is working properly 149 test_usage 150 check_result $? 151 152 # Verify that the HTTP connection can be established to test application container 153 run_test_application & 154 155 # Wait for the container to write its CID file 156 wait_for_cid 157 158 test_connection 159 check_result $? 160 161 cleanup 162 163 ` 164 165 // Makefile contains a sample Makefile which can build and test a container. 166 const Makefile = `IMAGE_NAME = {{.ImageName}} 167 168 .PHONY: build 169 build: 170 docker build -t $(IMAGE_NAME) . 171 172 .PHONY: test 173 test: 174 docker build -t $(IMAGE_NAME)-candidate . 175 IMAGE_NAME=$(IMAGE_NAME)-candidate test/run 176 ` 177 178 // Index contains a sample index.html file. 179 const Index = `<!doctype html> 180 <html> 181 <head> 182 <title>Hello World!</title> 183 </head> 184 <body> 185 <h1>Hello World!</h1> 186 </body> 187 </html> 188 `