Blame test/test-lib-openshift.sh

ff811fd
# Set of functions for testing docker images in OpenShift using 'oc' command
ff811fd
ff811fd
# ct_os_get_status
ff811fd
# --------------------
ff811fd
# Returns status of all objects to make debugging easier.
ff811fd
function ct_os_get_status() {
ff811fd
  oc get all
ff811fd
  oc status
ff811fd
}
ff811fd
ff811fd
# ct_os_print_logs
ff811fd
# --------------------
ff811fd
# Returns status of all objects and logs from all pods.
ff811fd
function ct_os_print_logs() {
ff811fd
  ct_os_get_status
ff811fd
  while read pod_name; do
ff811fd
    echo "INFO: printing logs for pod ${pod_name}"
ff811fd
    oc logs ${pod_name}
ff811fd
  done < <(oc get pods --no-headers=true -o custom-columns=NAME:.metadata.name)
ff811fd
}
ff811fd
ff811fd
# ct_os_enable_print_logs
ff811fd
# --------------------
ff811fd
# Enables automatic printing of pod logs on ERR.
ff811fd
function ct_os_enable_print_logs() {
ff811fd
  set -E
ff811fd
  trap ct_os_print_logs ERR
ff811fd
}
ff811fd
ff811fd
# ct_get_public_ip
ff811fd
# --------------------
ff811fd
# Returns best guess for the IP that the node is accessible from other computers.
ff811fd
# This is a bit funny heuristic, simply goes through all IPv4 addresses that
ff811fd
# hostname -I returns and de-prioritizes IP addresses commonly used for local
ff811fd
# addressing. The rest of addresses are taken as public with higher probability.
ff811fd
function ct_get_public_ip() {
ff811fd
  local hostnames=$(hostname -I)
ff811fd
  local public_ip=''
ff811fd
  local found_ip
ff811fd
  for guess_exp in '127\.0\.0\.1' '192\.168\.[0-9\.]*' '172\.[0-9\.]*' \
ff811fd
                   '10\.[0-9\.]*' '[0-9\.]*' ; do
ff811fd
    found_ip=$(echo "${hostnames}" | grep -oe "${guess_exp}")
ff811fd
    if [ -n "${found_ip}" ] ; then
ff811fd
      hostnames=$(echo "${hostnames}" | sed -e "s/${found_ip}//")
ff811fd
      public_ip="${found_ip}"
ff811fd
    fi
ff811fd
  done
ff811fd
  if [ -z "${public_ip}" ] ; then
ff811fd
    echo "ERROR: public IP could not be guessed." >&2
ff811fd
    return 1
ff811fd
  fi
ff811fd
  echo "${public_ip}"
ff811fd
}
ff811fd
ff811fd
# ct_os_run_in_pod POD_NAME CMD
ff811fd
# --------------------
ff811fd
# Runs [cmd] in the pod specified by prefix [pod_prefix].
ff811fd
# Arguments: pod_name - full name of the pod
ff811fd
# Arguments: cmd - command to be run in the pod
ff811fd
function ct_os_run_in_pod() {
ff811fd
  local pod_name="$1" ; shift
ff811fd
ff811fd
  oc exec "$pod_name" -- "$@"
ff811fd
}
ff811fd
ff811fd
# ct_os_get_service_ip SERVICE_NAME
ff811fd
# --------------------
ff811fd
# Returns IP of the service specified by [service_name].
ff811fd
# Arguments: service_name - name of the service
ff811fd
function ct_os_get_service_ip() {
ff811fd
  local service_name="${1}" ; shift
ff811fd
  oc get "svc/${service_name}" -o yaml | grep clusterIP | \
ff811fd
     cut -d':' -f2 | grep -oe '172\.30\.[0-9\.]*'
ff811fd
}
ff811fd
ff811fd
ff811fd
# ct_os_get_all_pods_status
ff811fd
# --------------------
ff811fd
# Returns status of all pods.
ff811fd
function ct_os_get_all_pods_status() {
ff811fd
  oc get pods -o custom-columns=Ready:status.containerStatuses[0].ready,NAME:.metadata.name
ff811fd
}
ff811fd
ff811fd
# ct_os_get_all_pods_name
ff811fd
# --------------------
ff811fd
# Returns the full name of all pods.
ff811fd
function ct_os_get_all_pods_name() {
ff811fd
  oc get pods --no-headers -o custom-columns=NAME:.metadata.name
ff811fd
}
ff811fd
ff811fd
# ct_os_get_pod_status POD_PREFIX
ff811fd
# --------------------
ff811fd
# Returns status of the pod specified by prefix [pod_prefix].
ff811fd
# Note: Ignores -build and -deploy pods
ff811fd
# Arguments: pod_prefix - prefix or whole ID of the pod
ff811fd
function ct_os_get_pod_status() {
ff811fd
  local pod_prefix="${1}" ; shift
ff811fd
  ct_os_get_all_pods_status | grep -e "${pod_prefix}" | grep -Ev "(build|deploy)$" \
ff811fd
                            | awk '{print $1}' | head -n 1
ff811fd
}
ff811fd
ff811fd
# ct_os_get_pod_name POD_PREFIX
ff811fd
# --------------------
ff811fd
# Returns the full name of pods specified by prefix [pod_prefix].
ff811fd
# Note: Ignores -build and -deploy pods
ff811fd
# Arguments: pod_prefix - prefix or whole ID of the pod
ff811fd
function ct_os_get_pod_name() {
ff811fd
  local pod_prefix="${1}" ; shift
ff811fd
  ct_os_get_all_pods_name | grep -e "^${pod_prefix}" | grep -Ev "(build|deploy)$"
ff811fd
}
ff811fd
ff811fd
# ct_os_get_pod_ip POD_NAME
ff811fd
# --------------------
ff811fd
# Returns the ip of the pod specified by [pod_name].
ff811fd
# Arguments: pod_name - full name of the pod
ff811fd
function ct_os_get_pod_ip() {
ff811fd
  local pod_name="${1}"
ff811fd
  oc get pod "$pod_name" --no-headers -o custom-columns=IP:status.podIP
ff811fd
}
ff811fd
ff811fd
# ct_os_check_pod_readiness POD_PREFIX STATUS
ff811fd
# --------------------
ff811fd
# Checks whether the pod is ready.
ff811fd
# Arguments: pod_prefix - prefix or whole ID of the pod
ff811fd
# Arguments: status - expected status (true, false)
ff811fd
function ct_os_check_pod_readiness() {
ff811fd
  local pod_prefix="${1}" ; shift
ff811fd
  local status="${1}" ; shift
ff811fd
  test "$(ct_os_get_pod_status ${pod_prefix})" == "${status}"
ff811fd
}
ff811fd
ff811fd
# ct_os_wait_pod_ready POD_PREFIX TIMEOUT
ff811fd
# --------------------
ff811fd
# Wait maximum [timeout] for the pod becomming ready.
ff811fd
# Arguments: pod_prefix - prefix or whole ID of the pod
ff811fd
# Arguments: timeout - how many seconds to wait seconds
ff811fd
function ct_os_wait_pod_ready() {
ff811fd
  local pod_prefix="${1}" ; shift
ff811fd
  local timeout="${1}" ; shift
ff811fd
  SECONDS=0
ff811fd
  echo -n "Waiting for ${pod_prefix} pod becoming ready ..."
ff811fd
  while ! ct_os_check_pod_readiness "${pod_prefix}" "true" ; do
ff811fd
    echo -n "."
ff811fd
    [ ${SECONDS} -gt ${timeout} ] && echo " FAIL" && return 1
ff811fd
    sleep 3
ff811fd
  done
ff811fd
  echo " DONE"
ff811fd
}
ff811fd
ff811fd
# ct_os_wait_rc_ready POD_PREFIX TIMEOUT
ff811fd
# --------------------
ff811fd
# Wait maximum [timeout] for the rc having desired number of replicas ready.
ff811fd
# Arguments: pod_prefix - prefix of the replication controller
ff811fd
# Arguments: timeout - how many seconds to wait seconds
ff811fd
function ct_os_wait_rc_ready() {
ff811fd
  local pod_prefix="${1}" ; shift
ff811fd
  local timeout="${1}" ; shift
ff811fd
  SECONDS=0
ff811fd
  echo -n "Waiting for ${pod_prefix} pod becoming ready ..."
ff811fd
  while ! test "$((oc get --no-headers statefulsets; oc get --no-headers rc) 2>/dev/null \
ff811fd
                 | grep "^${pod_prefix}" | awk '$2==$3 {print "ready"}')" == "ready" ; do
ff811fd
    echo -n "."
ff811fd
    [ ${SECONDS} -gt ${timeout} ] && echo " FAIL" && return 1
ff811fd
    sleep 3
ff811fd
  done
ff811fd
  echo " DONE"
ff811fd
}
ff811fd
ff811fd
# ct_os_deploy_pure_image IMAGE [ENV_PARAMS, ...]
ff811fd
# --------------------
ff811fd
# Runs [image] in the openshift and optionally specifies env_params
ff811fd
# as environment variables to the image.
ff811fd
# Arguments: image - prefix or whole ID of the pod to run the cmd in
ff811fd
# Arguments: env_params - environment variables parameters for the images.
ff811fd
function ct_os_deploy_pure_image() {
ff811fd
  local image="${1}" ; shift
ff811fd
  # ignore error exit code, because oc new-app returns error when image exists
ff811fd
  oc new-app ${image} "$@" || :
ff811fd
  # let openshift cluster to sync to avoid some race condition errors
ff811fd
  sleep 3
ff811fd
}
ff811fd
ff811fd
# ct_os_deploy_s2i_image IMAGE APP [ENV_PARAMS, ... ]
ff811fd
# --------------------
ff811fd
# Runs [image] and [app] in the openshift and optionally specifies env_params
ff811fd
# as environment variables to the image.
ff811fd
# Arguments: image - prefix or whole ID of the pod to run the cmd in
ff811fd
# Arguments: app - url or local path to git repo with the application sources.
ff811fd
# Arguments: env_params - environment variables parameters for the images.
ff811fd
function ct_os_deploy_s2i_image() {
ff811fd
  local image="${1}" ; shift
ff811fd
  local app="${1}" ; shift
ff811fd
  # ignore error exit code, because oc new-app returns error when image exists
ff811fd
  oc new-app "${image}~${app}" "$@" || :
ff811fd
ff811fd
  # let openshift cluster to sync to avoid some race condition errors
ff811fd
  sleep 3
ff811fd
}
ff811fd
ff811fd
# ct_os_deploy_template_image TEMPLATE [ENV_PARAMS, ...]
ff811fd
# --------------------
ff811fd
# Runs template in the openshift and optionally gives env_params to use
ff811fd
# specific values in the template.
ff811fd
# Arguments: template - prefix or whole ID of the pod to run the cmd in
ff811fd
# Arguments: env_params - environment variables parameters for the template.
ff811fd
# Example usage: ct_os_deploy_template_image mariadb-ephemeral-template.yaml \
ff811fd
#                                            DATABASE_SERVICE_NAME=mysql-57-centos7 \
ff811fd
#                                            DATABASE_IMAGE=mysql-57-centos7 \
ff811fd
#                                            MYSQL_USER=testu \
ff811fd
#                                            MYSQL_PASSWORD=testp \
ff811fd
#                                            MYSQL_DATABASE=testdb
ff811fd
function ct_os_deploy_template_image() {
ff811fd
  local template="${1}" ; shift
ff811fd
  oc process -f "${template}" "$@" | oc create -f -
ff811fd
  # let openshift cluster to sync to avoid some race condition errors
ff811fd
  sleep 3
ff811fd
}
ff811fd
ff811fd
# _ct_os_get_uniq_project_name
ff811fd
# --------------------
ff811fd
# Returns a uniq name of the OpenShift project.
ff811fd
function _ct_os_get_uniq_project_name() {
ff811fd
  local r
ff811fd
  while true ; do
ff811fd
    r=${RANDOM}
ff811fd
    mkdir /var/tmp/sclorg-test-${r} &>/dev/null && echo sclorg-test-${r} && break
ff811fd
  done
ff811fd
}
ff811fd
ff811fd
# ct_os_new_project [PROJECT]
ff811fd
# --------------------
ff811fd
# Creates a new project in the openshfit using 'os' command.
ff811fd
# Arguments: project - project name, uses a new random name if omitted
ff811fd
# Expects 'os' command that is properly logged in to the OpenShift cluster.
ff811fd
# Not using mktemp, because we cannot use uppercase characters.
ff811fd
function ct_os_new_project() {
ff811fd
  if [ "${CT_SKIP_NEW_PROJECT:-false}" == 'true' ] ; then
ff811fd
    echo "Creating project skipped."
ff811fd
    return
ff811fd
  fi
ff811fd
  local project_name="${1:-$(_ct_os_get_uniq_project_name)}" ; shift || :
ff811fd
  oc new-project ${project_name}
ff811fd
  # let openshift cluster to sync to avoid some race condition errors
ff811fd
  sleep 3
ff811fd
}
ff811fd
ff811fd
# ct_os_delete_project [PROJECT]
ff811fd
# --------------------
ff811fd
# Deletes the specified project in the openshfit
ff811fd
# Arguments: project - project name, uses the current project if omitted
ff811fd
function ct_os_delete_project() {
ff811fd
  if [ "${CT_SKIP_NEW_PROJECT:-false}" == 'true' ] ; then
ff811fd
    echo "Deleting project skipped, cleaning objects only."
ff811fd
    ct_delete_all_objects
ff811fd
    return
ff811fd
  fi
ff811fd
  local project_name="${1:-$(oc project -q)}" ; shift || :
ff811fd
  oc delete project "${project_name}"
ff811fd
}
ff811fd
ff811fd
# ct_delete_all_objects
ff811fd
# -----------------
ff811fd
# Deletes all objects within the project.
ff811fd
# Handy when we have one project and want to run more tests.
ff811fd
function ct_delete_all_objects() {
ff811fd
  for x in bc builds dc is isimage istag po pv pvc rc routes secrets svc ; do
ff811fd
    oc delete $x --all
ff811fd
  done
ff811fd
  # for some objects it takes longer to be really deleted, so a dummy sleep
ff811fd
  # to avoid some races when other test can see not-yet-deleted objects and can fail
ff811fd
  sleep 10
ff811fd
}
ff811fd
ff811fd
# ct_os_docker_login
ff811fd
# --------------------
ff811fd
# Logs in into docker daemon
ff811fd
# Uses global REGISRTY_ADDRESS environment variable for arbitrary registry address.
ff811fd
# Does not do anything if REGISTRY_ADDRESS is set.
ff811fd
function ct_os_docker_login() {
ff811fd
  [ -n "${REGISTRY_ADDRESS:-}" ] && "REGISTRY_ADDRESS set, not trying to docker login." && return 0
ff811fd
  # docker login fails with "404 page not found" error sometimes, just try it more times
ff811fd
  for i in `seq 12` ; do
ff811fd
    docker login -u developer -p $(oc whoami -t) ${REGISRTY_ADDRESS:-172.30.1.1:5000} && return 0 || :
ff811fd
    sleep 5
ff811fd
  done
ff811fd
  return 1
ff811fd
}
ff811fd
ff811fd
# ct_os_upload_image IMAGE [IMAGESTREAM]
ff811fd
# --------------------
ff811fd
# Uploads image from local registry to the OpenShift internal registry.
ff811fd
# Arguments: image - image name to upload
ff811fd
# Arguments: imagestream - name and tag to use for the internal registry.
ff811fd
#                          In the format of name:tag ($image_name:latest by default)
ff811fd
# Uses global REGISRTY_ADDRESS environment variable for arbitrary registry address.
ff811fd
function ct_os_upload_image() {
ff811fd
  local input_name="${1}" ; shift
ff811fd
  local image_name=${input_name##*/}
ff811fd
  local imagestream=${1:-$image_name:latest}
ff811fd
  local output_name="${REGISRTY_ADDRESS:-172.30.1.1:5000}/$(oc project -q)/$imagestream"
ff811fd
ff811fd
  ct_os_docker_login
ff811fd
  docker tag ${input_name} ${output_name}
ff811fd
  docker push ${output_name}
ff811fd
}
ff811fd
ff811fd
# ct_os_install_in_centos
ff811fd
# --------------------
ff811fd
# Installs os cluster in CentOS
ff811fd
function ct_os_install_in_centos() {
ff811fd
  yum install -y centos-release-openshift-origin
ff811fd
  yum install -y wget git net-tools bind-utils iptables-services bridge-utils\
ff811fd
                 bash-completion origin-clients docker origin-clients
ff811fd
}
ff811fd
ff811fd
# ct_os_cluster_up [DIR, IS_PUBLIC, CLUSTER_VERSION]
ff811fd
# --------------------
ff811fd
# Runs the local OpenShift cluster using 'oc cluster up' and logs in as developer.
ff811fd
# Arguments: dir - directory to keep configuration data in, random if omitted
ff811fd
# Arguments: is_public - sets either private or public hostname for web-UI,
ff811fd
#                        use "true" for allow remote access to the web-UI,
ff811fd
#                        "false" is default
ff811fd
# Arguments: cluster_version - version of the OpenShift cluster to use, empty
ff811fd
#                              means default version of `oc`; example value: 3.7;
ff811fd
#                              also can be specified outside by OC_CLUSTER_VERSION
ff811fd
function ct_os_cluster_up() {
ff811fd
  ct_os_cluster_running && echo "Cluster already running. Nothing is done." && return 0
ff811fd
  ct_os_logged_in && echo "Already logged in to a cluster. Nothing is done." && return 0
ff811fd
ff811fd
  mkdir -p /var/tmp/openshift
ff811fd
  local dir="${1:-$(mktemp -d /var/tmp/openshift/os-data-XXXXXX)}" ; shift || :
ff811fd
  local is_public="${1:-'false'}" ; shift || :
ff811fd
  local default_cluster_version=${OC_CLUSTER_VERSION:-}
ff811fd
  local cluster_version=${1:-${default_cluster_version}} ; shift || :
ff811fd
  if ! grep -qe '--insecure-registry.*172\.30\.0\.0' /etc/sysconfig/docker ; then
ff811fd
    sed -i "s|OPTIONS='|OPTIONS='--insecure-registry 172.30.0.0/16 |" /etc/sysconfig/docker
ff811fd
  fi
ff811fd
ff811fd
  systemctl stop firewalld || :
ff811fd
  setenforce 0
ff811fd
  iptables -F
ff811fd
ff811fd
  systemctl restart docker
ff811fd
  local cluster_ip="127.0.0.1"
ff811fd
  [ "${is_public}" == "true" ] && cluster_ip=$(ct_get_public_ip)
ff811fd
ff811fd
  if [ -n "${cluster_version}" ] ; then
ff811fd
    # if $cluster_version is not set, we simply use oc that is available
ff811fd
    ct_os_set_path_oc "${cluster_version}"
ff811fd
  fi
ff811fd
ff811fd
  mkdir -p ${dir}/{config,data,pv}
ff811fd
  case $(oc version| head -n 1) in
ff811fd
    "oc v3.1"?.*)
ff811fd
      oc cluster up --base-dir="${dir}/data" --public-hostname="${cluster_ip}"
ff811fd
      ;;
ff811fd
    "oc v3."*)
ff811fd
      oc cluster up --host-data-dir="${dir}/data" --host-config-dir="${dir}/config" \
ff811fd
                    --host-pv-dir="${dir}/pv" --use-existing-config --public-hostname="${cluster_ip}"
ff811fd
      ;;
ff811fd
    *)
ff811fd
      echo "ERROR: Unexpected oc version." >&2
ff811fd
      return 1
ff811fd
      ;;
ff811fd
  esac
ff811fd
  oc version
ff811fd
  oc login -u system:admin
ff811fd
  oc project default
ff811fd
  ct_os_wait_rc_ready docker-registry 180
ff811fd
  ct_os_wait_rc_ready router 30
ff811fd
  oc login -u developer -p developer
ff811fd
  # let openshift cluster to sync to avoid some race condition errors
ff811fd
  sleep 3
ff811fd
}
ff811fd
ff811fd
# ct_os_cluster_down
ff811fd
# --------------------
ff811fd
# Shuts down the local OpenShift cluster using 'oc cluster down'
ff811fd
function ct_os_cluster_down() {
ff811fd
  oc cluster down
ff811fd
}
ff811fd
ff811fd
# ct_os_cluster_running
ff811fd
# --------------------
ff811fd
# Returns 0 if oc cluster is running
ff811fd
function ct_os_cluster_running() {
ff811fd
  oc cluster status &>/dev/null
ff811fd
}
ff811fd
ff811fd
# ct_os_logged_in
ff811fd
# ---------------
ff811fd
# Returns 0 if logged in to a cluster (remote or local)
ff811fd
function ct_os_logged_in() {
ff811fd
  oc whoami >/dev/null
ff811fd
}
ff811fd
ff811fd
# ct_os_set_path_oc OC_VERSION
ff811fd
# --------------------
ff811fd
# This is a trick that helps using correct version of the `oc`:
ff811fd
# The input is version of the openshift in format v3.6.0 etc.
ff811fd
# If the currently available version of oc is not of this version,
ff811fd
# it first takes a look into /usr/local/oc-<ver>/bin directory,
ff811fd
# and if not found there it downloads the community release from github.
ff811fd
# In the end the PATH variable is changed, so the other tests can still use just 'oc'.
ff811fd
# Arguments: oc_version - X.Y part of the version of OSE (e.g. 3.9)
ff811fd
function ct_os_set_path_oc() {
ff811fd
  local oc_version=$(ct_os_get_latest_ver $1)
ff811fd
  local oc_path
ff811fd
ff811fd
  if oc version | grep -q "oc ${oc_version%.*}." ; then
ff811fd
    echo "Binary oc found already available in version ${oc_version}: `which oc` Doing noting."
ff811fd
    return 0
ff811fd
  fi
ff811fd
ff811fd
  # first check whether we already have oc available in /usr/local
ff811fd
  local installed_oc_path="/usr/local/oc-${oc_version%.*}/bin"
ff811fd
ff811fd
  if [ -x "${installed_oc_path}/oc" ] ; then
ff811fd
    oc_path="${installed_oc_path}"
ff811fd
    echo "Binary oc found in ${installed_oc_path}" >&2
ff811fd
  else
ff811fd
    # oc not available in /usr/local, try to download it from github (community release)
ff811fd
    oc_path="/tmp/oc-${oc_version}-bin"
ff811fd
    ct_os_download_upstream_oc "${oc_version}" "${oc_path}"
ff811fd
  fi
ff811fd
  if [ -z "${oc_path}/oc" ] ; then
ff811fd
    echo "ERROR: oc not found installed, nor downloaded" >&1
ff811fd
    return 1
ff811fd
  fi
ff811fd
  export PATH="${oc_path}:${PATH}"
ff811fd
  if ! oc version | grep -q "oc ${oc_version%.*}." ; then
ff811fd
    echo "ERROR: something went wrong, oc located at ${oc_path}, but oc of version ${oc_version} not found in PATH ($PATH)" >&1
ff811fd
    return 1
ff811fd
  else
ff811fd
    echo "PATH set correctly, binary oc found in version ${oc_version}: `which oc`"
ff811fd
  fi
ff811fd
}
ff811fd
ff811fd
# ct_os_get_latest_ver VERSION_PART_X
ff811fd
# --------------------
ff811fd
# Returns full version (vX.Y.Z) from part of the version (X.Y)
ff811fd
# Arguments: vxy - X.Y part of the version
ff811fd
# Returns vX.Y.Z variant of the version
ff811fd
function ct_os_get_latest_ver(){
ff811fd
  local vxy="v$1"
ff811fd
  for vz in {3..0} ; do
ff811fd
    curl -sif "https://github.com/openshift/origin/releases/tag/${vxy}.${vz}" >/dev/null && echo "${vxy}.${vz}" && return 0
ff811fd
  done
ff811fd
  echo "ERROR: version ${vxy} not found in https://github.com/openshift/origin/tags" >&2
ff811fd
  return 1
ff811fd
}
ff811fd
ff811fd
# ct_os_download_upstream_oc OC_VERSION OUTPUT_DIR
ff811fd
# --------------------
ff811fd
# Downloads a particular version of openshift-origin-client-tools from
ff811fd
# github into specified output directory
ff811fd
# Arguments: oc_version - version of OSE (e.g. v3.7.2)
ff811fd
# Arguments: output_dir - output directory
ff811fd
function ct_os_download_upstream_oc() {
ff811fd
  local oc_version=$1
ff811fd
  local output_dir=$2
ff811fd
ff811fd
  # check whether we already have the binary in place
ff811fd
  [ -x "${output_dir}/oc" ] && return 0
ff811fd
ff811fd
  mkdir -p "${output_dir}"
ff811fd
  # using html output instead of https://api.github.com/repos/openshift/origin/releases/tags/${oc_version},
ff811fd
  # because API is limited for number of queries if not authenticated
ff811fd
  tarball=$(curl -si "https://github.com/openshift/origin/releases/tag/${oc_version}" | grep -o -e "openshift-origin-client-tools-${oc_version}-[a-f0-9]*-linux-64bit.tar.gz" | head -n 1)
ff811fd
ff811fd
  # download, unpack the binaries and then put them into output directory
ff811fd
  echo "Downloading https://github.com/openshift/origin/releases/download/${oc_version}/${tarball} into ${output_dir}/" >&2
ff811fd
  curl -sL https://github.com/openshift/origin/releases/download/${oc_version}/"${tarball}" | tar -C "${output_dir}" -xz
ff811fd
  mv -f "${output_dir}"/"${tarball%.tar.gz}"/* "${output_dir}/"
ff811fd
ff811fd
  rmdir "${output_dir}"/"${tarball%.tar.gz}"
ff811fd
}
ff811fd
ff811fd
ff811fd
# ct_os_test_s2i_app_func IMAGE APP CONTEXT_DIR CHECK_CMD [OC_ARGS]
ff811fd
# --------------------
ff811fd
# Runs [image] and [app] in the openshift and optionally specifies env_params
ff811fd
# as environment variables to the image. Then check the container by arbitrary
ff811fd
# function given as argument (such an argument may include <IP> string,
ff811fd
# that will be replaced with actual IP).
ff811fd
# Arguments: image - prefix or whole ID of the pod to run the cmd in  (compulsory)
ff811fd
# Arguments: app - url or local path to git repo with the application sources  (compulsory)
ff811fd
# Arguments: context_dir - sub-directory inside the repository with the application sources (compulsory)
ff811fd
# Arguments: check_command - CMD line that checks whether the container works (compulsory; '<IP>' will be replaced with actual IP)
ff811fd
# Arguments: oc_args - all other arguments are used as additional parameters for the `oc new-app`
ff811fd
#            command, typically environment variables (optional)
ff811fd
function ct_os_test_s2i_app_func() {
ff811fd
  local image_name=${1}
ff811fd
  local app=${2}
ff811fd
  local context_dir=${3}
ff811fd
  local check_command=${4}
ff811fd
  local oc_args=${5:-}
ff811fd
  local import_image=${6:-}
ff811fd
  local image_name_no_namespace=${image_name##*/}
ff811fd
  local service_name="${image_name_no_namespace}-testing"
ff811fd
  local image_tagged="${image_name_no_namespace}:${VERSION}"
ff811fd
ff811fd
  if [ $# -lt 4 ] || [ -z "${1}" -o -z "${2}" -o -z "${3}" -o -z "${4}" ]; then
ff811fd
    echo "ERROR: ct_os_test_s2i_app_func() requires at least 4 arguments that cannot be emtpy." >&2
ff811fd
    return 1
ff811fd
  fi
ff811fd
ff811fd
  ct_os_new_project
ff811fd
  # Create a specific imagestream tag for the image so that oc cannot use anything else
ff811fd
  if [ "${CT_SKIP_UPLOAD_IMAGE:-false}" == 'true' ] ; then
ff811fd
    if [ -n "${import_image}" ] ; then
ff811fd
      echo "Importing image ${import_image} as ${image_name}:${VERSION}"
ff811fd
      oc import-image ${image_name}:${VERSION} --from ${import_image} --confirm
ff811fd
    else
ff811fd
      echo "Uploading and importing image skipped."
ff811fd
    fi
ff811fd
  else
ff811fd
    if [ -n "${import_image}" ] ; then
ff811fd
      echo "Warning: Import image ${import_image} requested, but uploading image ${image_name} instead."
ff811fd
    fi
ff811fd
    ct_os_upload_image "${image_name}" "${image_tagged}"
ff811fd
  fi
ff811fd
ff811fd
  local app_param="${app}"
ff811fd
  if [ -d "${app}" ] ; then
ff811fd
    # for local directory, we need to copy the content, otherwise too smart os command
ff811fd
    # pulls the git remote repository instead
ff811fd
    app_param=$(ct_obtain_input "${app}")
ff811fd
  fi
ff811fd
ff811fd
  ct_os_deploy_s2i_image "${image_tagged}" "${app_param}" \
ff811fd
                          --context-dir="${context_dir}" \
ff811fd
                          --name "${service_name}" \
ff811fd
                          ${oc_args}
ff811fd
ff811fd
  if [ -d "${app}" ] ; then
ff811fd
    # in order to avoid weird race seen sometimes, let's wait shortly
ff811fd
    # before starting the build explicitly
ff811fd
    sleep 5
ff811fd
    oc start-build "${service_name}" --from-dir="${app_param}"
ff811fd
  fi
ff811fd
ff811fd
  ct_os_wait_pod_ready "${service_name}" 300
ff811fd
ff811fd
  local ip=$(ct_os_get_service_ip "${service_name}")
ff811fd
  local check_command_exp=$(echo "$check_command" | sed -e "s/<IP>/$ip/g")
ff811fd
ff811fd
  echo "  Checking APP using $check_command_exp ..."
ff811fd
  local result=0
ff811fd
  eval "$check_command_exp" || result=1
ff811fd
ff811fd
  if [ $result -eq 0 ] ; then
ff811fd
    echo "  Check passed."
ff811fd
  else
ff811fd
    echo "  Check failed."
ff811fd
  fi
ff811fd
ff811fd
  ct_os_delete_project
ff811fd
  return $result
ff811fd
}
ff811fd
ff811fd
# ct_os_test_s2i_app IMAGE APP CONTEXT_DIR EXPECTED_OUTPUT [PORT, PROTOCOL, RESPONSE_CODE, OC_ARGS, ... ]
ff811fd
# --------------------
ff811fd
# Runs [image] and [app] in the openshift and optionally specifies env_params
ff811fd
# as environment variables to the image. Then check the http response.
ff811fd
# Arguments: image - prefix or whole ID of the pod to run the cmd in (compulsory)
ff811fd
# Arguments: app - url or local path to git repo with the application sources (compulsory)
ff811fd
# Arguments: context_dir - sub-directory inside the repository with the application sources (compulsory)
ff811fd
# Arguments: expected_output - PCRE regular expression that must match the response body (compulsory)
ff811fd
# Arguments: port - which port to use (optional; default: 8080)
ff811fd
# Arguments: protocol - which protocol to use (optional; default: http)
ff811fd
# Arguments: response_code - what http response code to expect (optional; default: 200)
ff811fd
# Arguments: oc_args - all other arguments are used as additional parameters for the `oc new-app`
ff811fd
#            command, typically environment variables (optional)
ff811fd
function ct_os_test_s2i_app() {
ff811fd
  local image_name=${1}
ff811fd
  local app=${2}
ff811fd
  local context_dir=${3}
ff811fd
  local expected_output=${4}
ff811fd
  local port=${5:-8080}
ff811fd
  local protocol=${6:-http}
ff811fd
  local response_code=${7:-200}
ff811fd
  local oc_args=${8:-}
ff811fd
  local import_image=${9:-}
ff811fd
ff811fd
  if [ $# -lt 4 ] || [ -z "${1}" -o -z "${2}" -o -z "${3}" -o -z "${4}" ]; then
ff811fd
    echo "ERROR: ct_os_test_s2i_app() requires at least 4 arguments that cannot be emtpy." >&2
ff811fd
    return 1
ff811fd
  fi
ff811fd
ff811fd
  ct_os_test_s2i_app_func "${image_name}" \
ff811fd
                          "${app}" \
ff811fd
                          "${context_dir}" \
ff811fd
                          "ct_os_test_response_internal '${protocol}://<IP>:${port}' '${response_code}' '${expected_output}'" \
ff811fd
                          "${oc_args}" "${import_image}"
ff811fd
}
ff811fd
ff811fd
# ct_os_test_template_app_func IMAGE APP IMAGE_IN_TEMPLATE CHECK_CMD [OC_ARGS]
ff811fd
# --------------------
ff811fd
# Runs [image] and [app] in the openshift and optionally specifies env_params
ff811fd
# as environment variables to the image. Then check the container by arbitrary
ff811fd
# function given as argument (such an argument may include <IP> string,
ff811fd
# that will be replaced with actual IP).
ff811fd
# Arguments: image_name - prefix or whole ID of the pod to run the cmd in  (compulsory)
ff811fd
# Arguments: template - url or local path to a template to use (compulsory)
ff811fd
# Arguments: name_in_template - image name used in the template
ff811fd
# Arguments: check_command - CMD line that checks whether the container works (compulsory; '<IP>' will be replaced with actual IP)
ff811fd
# Arguments: oc_args - all other arguments are used as additional parameters for the `oc new-app`
ff811fd
#            command, typically environment variables (optional)
ff811fd
# Arguments: other_images - some templates need other image to be pushed into the OpenShift registry,
ff811fd
#            specify them in this parameter as "<image>|<tag>", where "<image>" is a full image name
ff811fd
#            (including registry if needed) and "<tag>" is a tag under which the image should be available
ff811fd
#            in the OpenShift registry.
ff811fd
function ct_os_test_template_app_func() {
ff811fd
  local image_name=${1}
ff811fd
  local template=${2}
ff811fd
  local name_in_template=${3}
ff811fd
  local check_command=${4}
ff811fd
  local oc_args=${5:-}
ff811fd
  local other_images=${6:-}
ff811fd
  local import_image=${7:-}
ff811fd
ff811fd
  if [ $# -lt 4 ] || [ -z "${1}" -o -z "${2}" -o -z "${3}" -o -z "${4}" ]; then
ff811fd
    echo "ERROR: ct_os_test_template_app_func() requires at least 4 arguments that cannot be emtpy." >&2
ff811fd
    return 1
ff811fd
  fi
ff811fd
ff811fd
  local service_name="${name_in_template}-testing"
ff811fd
  local image_tagged="${name_in_template}:${VERSION}"
ff811fd
ff811fd
  ct_os_new_project
ff811fd
ff811fd
  # Create a specific imagestream tag for the image so that oc cannot use anything else
ff811fd
  if [ "${CT_SKIP_UPLOAD_IMAGE:-false}" == 'true' ] ; then
ff811fd
    if [ -n "${import_image}" ] ; then
ff811fd
      echo "Importing image ${import_image} as ${image_name}:${VERSION}"
ff811fd
      oc import-image ${image_name}:${VERSION} --from ${import_image} --confirm
ff811fd
    else
ff811fd
      echo "Uploading and importing image skipped."
ff811fd
    fi
ff811fd
  else
ff811fd
    if [ -n "${import_image}" ] ; then
ff811fd
      echo "Warning: Import image ${import_image} requested, but uploading image ${image_name} instead."
ff811fd
    fi
ff811fd
    ct_os_upload_image "${image_name}" "${image_tagged}"
ff811fd
ff811fd
    # upload also other images, that template might need (list of pairs in the format <image>|<tag>
ff811fd
    local images_tags_a
ff811fd
    local i_t
ff811fd
    for i_t in ${other_images} ; do
ff811fd
      echo "${i_t}"
ff811fd
      IFS='|' read -ra image_tag_a <<< "${i_t}"
ff811fd
      docker pull "${image_tag_a[0]}"
ff811fd
      ct_os_upload_image "${image_tag_a[0]}" "${image_tag_a[1]}"
ff811fd
    done
ff811fd
  fi
ff811fd
ff811fd
  local local_template=$(ct_obtain_input "${template}")
ff811fd
  local namespace=${CT_NAMESPACE:-$(oc project -q)}
ff811fd
  oc new-app ${local_template} \
ff811fd
             --name "${name_in_template}" \
ff811fd
             -p NAMESPACE="${namespace}" \
ff811fd
             ${oc_args}
ff811fd
ff811fd
  ct_os_wait_pod_ready "${service_name}" 300
ff811fd
ff811fd
  local ip=$(ct_os_get_service_ip "${service_name}")
ff811fd
  local check_command_exp=$(echo "$check_command" | sed -e "s/<IP>/$ip/g")
ff811fd
ff811fd
  echo "  Checking APP using $check_command_exp ..."
ff811fd
  local result=0
ff811fd
  eval "$check_command_exp" || result=1
ff811fd
ff811fd
  if [ $result -eq 0 ] ; then
ff811fd
    echo "  Check passed."
ff811fd
  else
ff811fd
    echo "  Check failed."
ff811fd
  fi
ff811fd
ff811fd
  ct_os_delete_project
ff811fd
  return $result
ff811fd
}
ff811fd
ff811fd
# params:
ff811fd
# ct_os_test_template_app IMAGE APP IMAGE_IN_TEMPLATE EXPECTED_OUTPUT [PORT, PROTOCOL, RESPONSE_CODE, OC_ARGS, ... ]
ff811fd
# --------------------
ff811fd
# Runs [image] and [app] in the openshift and optionally specifies env_params
ff811fd
# as environment variables to the image. Then check the http response.
ff811fd
# Arguments: image_name - prefix or whole ID of the pod to run the cmd in (compulsory)
ff811fd
# Arguments: template - url or local path to a template to use (compulsory)
ff811fd
# Arguments: name_in_template - image name used in the template
ff811fd
# Arguments: expected_output - PCRE regular expression that must match the response body (compulsory)
ff811fd
# Arguments: port - which port to use (optional; default: 8080)
ff811fd
# Arguments: protocol - which protocol to use (optional; default: http)
ff811fd
# Arguments: response_code - what http response code to expect (optional; default: 200)
ff811fd
# Arguments: oc_args - all other arguments are used as additional parameters for the `oc new-app`
ff811fd
#            command, typically environment variables (optional)
ff811fd
# Arguments: other_images - some templates need other image to be pushed into the OpenShift registry,
ff811fd
#            specify them in this parameter as "<image>|<tag>", where "<image>" is a full image name
ff811fd
#            (including registry if needed) and "<tag>" is a tag under which the image should be available
ff811fd
#            in the OpenShift registry.
ff811fd
function ct_os_test_template_app() {
ff811fd
  local image_name=${1}
ff811fd
  local template=${2}
ff811fd
  local name_in_template=${3}
ff811fd
  local expected_output=${4}
ff811fd
  local port=${5:-8080}
ff811fd
  local protocol=${6:-http}
ff811fd
  local response_code=${7:-200}
ff811fd
  local oc_args=${8:-}
ff811fd
  local other_images=${9:-}
ff811fd
  local import_image=${10:-}
ff811fd
ff811fd
  if [ $# -lt 4 ] || [ -z "${1}" -o -z "${2}" -o -z "${3}" -o -z "${4}" ]; then
ff811fd
    echo "ERROR: ct_os_test_template_app() requires at least 4 arguments that cannot be emtpy." >&2
ff811fd
    return 1
ff811fd
  fi
ff811fd
ff811fd
  ct_os_test_template_app_func "${image_name}" \
ff811fd
                               "${template}" \
ff811fd
                               "${name_in_template}" \
ff811fd
                               "ct_os_test_response_internal '${protocol}://<IP>:${port}' '${response_code}' '${expected_output}'" \
ff811fd
                               "${oc_args}" \
ff811fd
                               "${other_images}" \
ff811fd
                               "${import_image}"
ff811fd
}
ff811fd
ff811fd
# ct_os_test_image_update IMAGE_NAME OLD_IMAGE ISTAG CHECK_FUNCTION OC_ARGS
ff811fd
# --------------------
ff811fd
# Runs an image update test with [image] uploaded to [is] imagestream
ff811fd
# and checks the services using an arbitrary function provided in [check_function].
ff811fd
# Arguments: image_name - prefix or whole ID of the pod to run the cmd in (compulsory)
ff811fd
# Arguments: old_image - valid name of the image from the registry
ff811fd
# Arguments: istag - imagestream to upload the images into (compulsory)
ff811fd
# Arguments: check_function - command to be run to check functionality of created services (compulsory)
ff811fd
# Arguments: oc_args - arguments to use during oc new-app (compulsory)
ff811fd
ct_os_test_image_update() {
ff811fd
  local image_name=$1; shift
ff811fd
  local old_image=$1; shift
ff811fd
  local istag=$1; shift
ff811fd
  local check_function=$1; shift
ff811fd
  local service_name=${image_name##*/}
ff811fd
  local ip="" check_command_exp=""
ff811fd
ff811fd
  echo "Running image update test for: $image_name"
ff811fd
  ct_os_new_project
ff811fd
ff811fd
  # Get current image from repository and create an imagestream
ff811fd
  docker pull "$old_image:latest" 2>/dev/null
ff811fd
  ct_os_upload_image "$old_image" "$istag"
ff811fd
ff811fd
  # Setup example application with curent image
ff811fd
  oc new-app "$@" --name "$service_name"
ff811fd
  ct_os_wait_pod_ready "$service_name" 60
ff811fd
ff811fd
  # Check application output
ff811fd
  ip=$(ct_os_get_service_ip "$service_name")
ff811fd
  check_command_exp=${check_function//<IP>/$ip}
ff811fd
  ct_assert_cmd_success "$check_command_exp"
ff811fd
ff811fd
  # Tag built image into the imagestream and wait for rebuild
ff811fd
  ct_os_upload_image "$image_name" "$istag"
ff811fd
  ct_os_wait_pod_ready "${service_name}-2" 60
ff811fd
ff811fd
  # Check application output
ff811fd
  ip=$(ct_os_get_service_ip "$service_name")
ff811fd
  check_command_exp=${check_function//<IP>/$ip}
ff811fd
  ct_assert_cmd_success "$check_command_exp"
ff811fd
ff811fd
  ct_os_delete_project
ff811fd
}
ff811fd
ff811fd
# ct_os_deploy_cmd_image IMAGE_NAME
ff811fd
# --------------------
ff811fd
# Runs a special command pod, a pod that does nothing, but includes utilities for testing.
ff811fd
# A typical usage is a mysql pod that includes mysql commandline, that we need for testing.
ff811fd
# Running commands inside this command pod is done via ct_os_cmd_image_run function.
ff811fd
# The pod is not run again if already running.
ff811fd
# Arguments: image_name - image to be used as a command pod
ff811fd
function ct_os_deploy_cmd_image() {
ff811fd
  local image_name=${1}
ff811fd
  oc get pod command-app &>/dev/null && echo "command POD already running" && return 0
ff811fd
  echo "command POD not running yet, will start one called command-app"
ff811fd
  oc create -f - <
ff811fd
apiVersion: v1
ff811fd
kind: Pod
ff811fd
metadata:
ff811fd
  name: command-app
ff811fd
spec:
ff811fd
  containers:
ff811fd
  - name: command-container
ff811fd
    image: "${image_name}"
ff811fd
    command: ["sleep"]
ff811fd
    args: ["3h"]
ff811fd
  restartPolicy: OnFailure
ff811fd
EOF
ff811fd
ff811fd
  SECONDS=0
ff811fd
  echo -n "Waiting for command POD ."
ff811fd
  while [ $SECONDS -lt 180 ] ; do
ff811fd
    sout="$(ct_os_cmd_image_run 'echo $((11*11))')"
ff811fd
    grep -q '^121$' <<< "$sout" && echo "DONE" && return 0 || :
ff811fd
    sleep 3
ff811fd
    echo -n "."
ff811fd
  done
ff811fd
  echo "FAIL"
ff811fd
  return 1
ff811fd
}
ff811fd
ff811fd
# ct_os_cmd_image_run CMD [ ARG ... ]
ff811fd
# --------------------
ff811fd
# Runs a command CMD inside a special command pod
ff811fd
# Arguments: cmd - shell command with args to run in a pod
ff811fd
function ct_os_cmd_image_run() {
ff811fd
  oc exec command-app -- bash -c "$@"
ff811fd
}
ff811fd
ff811fd
# ct_os_test_response_internal
ff811fd
# ----------------
ff811fd
# Perform GET request to the application container, checks output with
ff811fd
# a reg-exp and HTTP response code.
ff811fd
# That all is done inside an image in the cluster, so the function is used
ff811fd
# typically in clusters that are not accessible outside.
ff811fd
# The interanal image is a python image that should include the most of the useful commands.
ff811fd
# The check is repeated until timeout.
ff811fd
# Argument: url - request URL path
ff811fd
# Argument: expected_code - expected HTTP response code
ff811fd
# Argument: body_regexp - PCRE regular expression that must match the response body
ff811fd
# Argument: max_attempts - Optional number of attempts (default: 20), three seconds sleep between
ff811fd
# Argument: ignore_error_attempts - Optional number of attempts when we ignore error output (default: 10)
ff811fd
ct_os_test_response_internal() {
ff811fd
  local url="$1"
ff811fd
  local expected_code="$2"
ff811fd
  local body_regexp="$3"
ff811fd
  local max_attempts=${4:-20}
ff811fd
  local ignore_error_attempts=${5:-10}
ff811fd
ff811fd
  : "  Testing the HTTP(S) response for <${url}>"
ff811fd
  local sleep_time=3
ff811fd
  local attempt=1
ff811fd
  local result=1
ff811fd
  local status
ff811fd
  local response_code
ff811fd
  local response_file=$(mktemp /tmp/ct_test_response_XXXXXX)
ff811fd
  local util_image_name='python:3.6'
ff811fd
ff811fd
  ct_os_deploy_cmd_image "${util_image_name}"
ff811fd
ff811fd
  while [ ${attempt} -le ${max_attempts} ]; do
ff811fd
    ct_os_cmd_image_run "curl --connect-timeout 10 -s -w '%{http_code}' '${url}'" >${response_file} && status=0 || status=1
ff811fd
    if [ ${status} -eq 0 ]; then
ff811fd
      response_code=$(cat ${response_file} | tail -c 3)
ff811fd
      if [ "${response_code}" -eq "${expected_code}" ]; then
ff811fd
        result=0
ff811fd
      fi
ff811fd
      cat ${response_file} | grep -qP -e "${body_regexp}" || result=1;
ff811fd
      # Some services return 40x code until they are ready, so let's give them
ff811fd
      # some chance and not end with failure right away
ff811fd
      # Do not wait if we already have expected outcome though
ff811fd
      if [ ${result} -eq 0 -o ${attempt} -gt ${ignore_error_attempts} -o ${attempt} -eq ${max_attempts} ] ; then
ff811fd
        break
ff811fd
      fi
ff811fd
    fi
ff811fd
    attempt=$(( ${attempt} + 1 ))
ff811fd
    sleep ${sleep_time}
ff811fd
  done
ff811fd
  rm -f ${response_file}
ff811fd
  return ${result}
ff811fd
}
ff811fd
ff811fd
# ct_os_get_image_from_pod
ff811fd
# ------------------------
ff811fd
# Print image identifier from an existing pod to stdout
ff811fd
# Argument: pod_prefix - prefix or full name of the pod to get image from
ff811fd
ct_os_get_image_from_pod() {
ff811fd
  local pod_prefix=$1 ; shift
ff811fd
  local pod_name=$(ct_os_get_pod_name $pod_prefix)
ff811fd
  oc get "po/${pod_name}" -o yaml | sed -ne 's/^\s*image:\s*\(.*\)\s*$/\1/ p' | head -1
ff811fd
}
ff811fd
ff811fd
# ct_os_check_cmd_internal
ff811fd
# ----------------
ff811fd
# Runs a specified command, checks exit code and compares the output with expected regexp.
ff811fd
# That all is done inside an image in the cluster, so the function is used
ff811fd
# typically in clusters that are not accessible outside.
ff811fd
# The check is repeated until timeout.
ff811fd
# Argument: util_image_name - name of the image in the cluster that is used for running the cmd
ff811fd
# Argument: service_name - kubernetes' service name to work with (IP address is taken from this one)
ff811fd
# Argument: check_command - command that is run within the util_image_name container
ff811fd
# Argument: expected_content_match - regexp that must be in the output (use .* to ignore check)
ff811fd
# Argument: timeout - number of seconds to wait till the check succeeds
ff811fd
function ct_os_check_cmd_internal() {
ff811fd
  local util_image_name=$1 ; shift
ff811fd
  local service_name=$1 ; shift
ff811fd
  local check_command=$1 ; shift
ff811fd
  local expected_content_match=${1:-.*} ; shift
ff811fd
  local timeout=${1:-60} ; shift || :
ff811fd
ff811fd
  : "  Service ${service_name} check ..."
ff811fd
ff811fd
  local output
ff811fd
  local ret
ff811fd
  local ip=$(ct_os_get_service_ip "${service_name}")
ff811fd
  local check_command_exp=$(echo "$check_command" | sed -e "s/<IP>/$ip/g")
ff811fd
ff811fd
  ct_os_deploy_cmd_image $(ct_os_get_image_from_pod "${util_image_name}" | head -n 1)
ff811fd
  SECONDS=0
ff811fd
ff811fd
  echo -n "Waiting for ${service_name} service becoming ready ..."
ff811fd
  while true ; do
ff811fd
    output=$(ct_os_cmd_image_run "$check_command_exp")
ff811fd
    ret=$?
ff811fd
    echo "${output}" | grep -qe "${expected_content_match}" || ret=1
ff811fd
    if [ ${ret} -eq 0 ] ; then
ff811fd
      echo " PASS"
ff811fd
      return 0
ff811fd
    fi
ff811fd
    echo -n "."
ff811fd
    [ ${SECONDS} -gt ${timeout} ] && break
ff811fd
    sleep 3
ff811fd
  done
ff811fd
  echo " FAIL"
ff811fd
  return 1
ff811fd
}
ff811fd