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