github.com/nak3/source-to-image@v1.1.10-0.20180319140719-2ed55639898d/examples/nginx-centos7/test/run (about)

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