pkubat / container / ruby

Forked from container/ruby 4 years ago
Clone

Blame test/run

c7e3753
#!/bin/bash -x
c7e3753
#
c7e3753
# The 'run' performs a simple test that verifies that S2I image.
c7e3753
# The main focus here is to excersise the S2I scripts.
c7e3753
#
c7e3753
# IMAGE_NAME specifies a name of the candidate image used for testing.
c7e3753
# The image has to be available before this script is executed.
c7e3753
#
c7e3753
IMAGE_NAME=${IMAGE_NAME-centos/ruby-25-centos7-candidate}
c7e3753
c7e3753
declare -a WEB_SERVERS=(db puma rack)
c7e3753
#declare -a WEB_SERVERS=(db)
c7e3753
c7e3753
# TODO: Make command compatible for Mac users
c7e3753
test_dir="$(readlink -zf $(dirname "${BASH_SOURCE[0]}"))"
c7e3753
image_dir=$(readlink -zf ${test_dir}/..)
c7e3753
c7e3753
source "${test_dir}/test-lib.sh"
c7e3753
c7e3753
# Read exposed port from image meta data
c7e3753
test_port="$(docker inspect --format='{{range $key, $value := .ContainerConfig.ExposedPorts }}{{$key}}{{end}}' ${IMAGE_NAME} | sed 's/\/.*//')"
c7e3753
c7e3753
info() {
c7e3753
  echo -e "\n\e[1m[INFO] $@...\e[0m\n"
c7e3753
}
c7e3753
c7e3753
image_exists() {
c7e3753
  docker inspect $1 &>/dev/null
c7e3753
}
c7e3753
c7e3753
container_exists() {
c7e3753
  image_exists $(cat $cid_file)
c7e3753
}
c7e3753
c7e3753
container_ip() {
c7e3753
  docker inspect --format="{{ .NetworkSettings.IPAddress }}" $(cat $cid_file)
c7e3753
}
c7e3753
c7e3753
run_s2i_build() {
c7e3753
  ct_s2i_build_as_df file://${test_dir}/${1}-test-app ${IMAGE_NAME} ${IMAGE_NAME}-testapp ${s2i_args}
c7e3753
}
c7e3753
c7e3753
run_test_application() {
c7e3753
  docker run --user=100001 --rm --cidfile=${cid_file} ${IMAGE_NAME}-testapp
c7e3753
}
c7e3753
c7e3753
cleanup() {
c7e3753
  local server="$1"
c7e3753
  info "Cleaning up the test application image $1"
c7e3753
  if [ -f $cid_file ]; then
c7e3753
    if container_exists; then
c7e3753
      docker stop $(cat $cid_file)
c7e3753
    fi
c7e3753
  fi
c7e3753
  if image_exists ${IMAGE_NAME}-testapp; then
c7e3753
    docker rmi -f ${IMAGE_NAME}-testapp
c7e3753
  fi
c7e3753
  if [ ! -z "${server}" ]; then
c7e3753
    rm -rf ${test_dir}/${server}-test-app/.git
c7e3753
  fi
c7e3753
  if [[ "${server}" == "db" ]]; then
c7e3753
    rm -rf ${test_dir}/db-test-app
c7e3753
  fi
c7e3753
}
c7e3753
c7e3753
check_result() {
c7e3753
  local result="$1"
c7e3753
  if [[ "$result" != "0" ]]; then
c7e3753
    info "TEST FAILED (${result})"
c7e3753
    cleanup
c7e3753
    exit $result
c7e3753
  fi
c7e3753
}
c7e3753
c7e3753
wait_for_cid() {
c7e3753
  local max_attempts=10
c7e3753
  local sleep_time=1
c7e3753
  local attempt=1
c7e3753
  local result=1
c7e3753
  info "Waiting for application container to start"
c7e3753
  while [ $attempt -le $max_attempts ]; do
c7e3753
    [ -f $cid_file ] && [ -s $cid_file ] && break
c7e3753
    attempt=$(( $attempt + 1 ))
c7e3753
    sleep $sleep_time
c7e3753
  done
c7e3753
}
c7e3753
c7e3753
test_s2i_usage() {
c7e3753
  info "Testing 's2i usage'"
c7e3753
  ct_s2i_usage ${IMAGE_NAME} ${s2i_args} &>/dev/null
c7e3753
}
c7e3753
c7e3753
test_docker_run_usage() {
c7e3753
  info "Testing 'docker run' usage"
c7e3753
  docker run ${IMAGE_NAME} &>/dev/null
c7e3753
}
c7e3753
c7e3753
test_connection() {
c7e3753
  info "Testing the HTTP connection (http://$(container_ip):${test_port})"
c7e3753
  local max_attempts=10
c7e3753
  local sleep_time=1
c7e3753
  local attempt=1
c7e3753
  local result=1
c7e3753
  while [ $attempt -le $max_attempts ]; do
c7e3753
    response_code=$(curl -s -w %{http_code} -o /dev/null http://$(container_ip):${test_port}/)
c7e3753
    status=$?
c7e3753
    if [ $status -eq 0 ]; then
c7e3753
      if [ $response_code -eq 200 ]; then
c7e3753
        result=0
c7e3753
      fi
c7e3753
      break
c7e3753
    fi
c7e3753
    attempt=$(( $attempt + 1 ))
c7e3753
    sleep $sleep_time
c7e3753
  done
c7e3753
  return $result
c7e3753
}
c7e3753
c7e3753
test_scl_usage() {
c7e3753
  local run_cmd="$1"
c7e3753
  local expected="$2"
c7e3753
c7e3753
  info "Testing the image SCL enable"
c7e3753
  out=$(docker run --rm ${IMAGE_NAME} /bin/bash -c "${run_cmd}")
c7e3753
  if ! echo "${out}" | grep -q "${expected}"; then
c7e3753
    echo "ERROR[/bin/bash -c "${run_cmd}"] Expected '${expected}', got '${out}'"
c7e3753
    return 1
c7e3753
  fi
c7e3753
  out=$(docker exec $(cat ${cid_file}) /bin/bash -c "${run_cmd}" 2>&1)
c7e3753
  if ! echo "${out}" | grep -q "${expected}"; then
c7e3753
    echo "ERROR[exec /bin/bash -c "${run_cmd}"] Expected '${expected}', got '${out}'"
c7e3753
    return 1
c7e3753
  fi
c7e3753
  out=$(docker exec $(cat ${cid_file}) /bin/sh -ic "${run_cmd}" 2>&1)
c7e3753
  if ! echo "${out}" | grep -q "${expected}"; then
c7e3753
    echo "ERROR[exec /bin/sh -ic "${run_cmd}"] Expected '${expected}', got '${out}'"
c7e3753
    return 1
c7e3753
  fi
c7e3753
}
c7e3753
c7e3753
pushd ${test_dir}
c7e3753
if [ -d db-test-app ]; then
c7e3753
  rm -rf db-test-app
c7e3753
fi
c7e3753
git clone git://github.com/openshift/ruby-hello-world db-test-app
c7e3753
popd
c7e3753
c7e3753
for server in ${WEB_SERVERS[@]}; do
c7e3753
  cid_file=$(mktemp -u --suffix=.cid)
c7e3753
c7e3753
  # Since we built the candidate image locally, we don't want S2I attempt to pull
c7e3753
  # it from Docker hub
c7e3753
  s2i_args="--pull-policy=never"
c7e3753
c7e3753
  run_s2i_build ${server}
c7e3753
  check_result $?
c7e3753
c7e3753
  # Verify the 'usage' script is working properly when running the base image with 's2i usage ...'
c7e3753
  test_s2i_usage
c7e3753
  check_result $?
c7e3753
c7e3753
  # Verify the 'usage' script is working properly when running the base image with 'docker run ...'
c7e3753
  test_docker_run_usage
c7e3753
  check_result $?
c7e3753
c7e3753
  # Verify that the HTTP connection can be established to test application container
c7e3753
  run_test_application &
c7e3753
c7e3753
  # Wait for the container to write it's CID file
c7e3753
  wait_for_cid
c7e3753
c7e3753
  test_connection
c7e3753
  check_result $?
c7e3753
c7e3753
  test_scl_usage "ruby --version" "ruby ${RUBY_VERSION}."
c7e3753
  check_result $?
c7e3753
c7e3753
  info "All tests for the ${server}-test-app finished successfully."
c7e3753
  cleanup ${server}
c7e3753
done
c7e3753
c7e3753
info "Testing npm availibility"
c7e3753
ct_npm_works
c7e3753
check_result $?
c7e3753
c7e3753
info "All tests finished successfully."