#!/bin/bash
#
# Test the Redis image in OpenShift.
#
# IMAGE_NAME specifies a name of the candidate image used for testing.
# The image has to be available before this script is executed.
#

THISDIR=$(dirname ${BASH_SOURCE[0]})

source "$THISDIR"/test-lib-openshift.sh
source ${THISDIR}/test-lib-redis.sh

set -eo nounset

trap ct_os_cleanup EXIT SIGINT

ct_os_check_compulsory_vars

ct_os_enable_print_logs

function check_redis_os_service_connection() {
  local util_image_name=$1 ; shift
  local service_name=$1 ; shift
  local pass=$1 ; shift
  local timeout=${1:-60} ; shift || :
  local pod_ip=$(ct_os_get_service_ip ${service_name})

  : "  Service ${service_name} check ..."

  local cmd="timeout 15 redis-cli -h $pod_ip -a $pass ping"
  local expected_value="PONG"
  local output
  local ret
  SECONDS=0

  echo -n "Waiting for ${service_name} service becoming ready ..."
  while true ; do
    output=$(docker run --rm ${util_image_name} bash -c "${cmd}" || :)
    echo "${output}" | grep -qe "${expected_value}" && ret=0 || ret=1
    if [ ${ret} -eq 0 ] ; then
      echo " PASS"
      return 0
    fi
    echo -n "."
    [ ${SECONDS} -gt ${timeout} ] && break
    sleep 3
  done
  echo " FAIL"
  return 1
}

function test_redis_pure_image() {
  local image_name=$1
  local image_name_no_namespace=${image_name##*/}
  local service_name="${image_name_no_namespace%%:*}-testing"

  ct_os_new_project
  # Create a specific imagestream tag for the image so that oc cannot use anything else
  ct_os_upload_image "${image_name}" "$image_name_no_namespace"

  ct_os_deploy_pure_image "$image_name_no_namespace" \
                          --name "${service_name}" \
                          --env REDIS_PASSWORD=pass

  ct_os_wait_pod_ready "${service_name}" 60
  check_redis_os_service_connection "${image_name}" "${service_name}" pass

  ct_os_delete_project
}

function test_redis_template() {
  local image_name=${1:-quay.io/centos7/redis-5-centos7}
  local image_name_no_namespace=${image_name##*/}
  local service_name="${image_name_no_namespace%%:*}-testing"

  ct_os_new_project
  ct_os_upload_image "${image_name}" "redis:$VERSION"

  ct_os_deploy_template_image "$THISDIR/redis-ephemeral-template.json" \
                              NAMESPACE="$(oc project -q)" \
                              REDIS_VERSION="$VERSION" \
                              DATABASE_SERVICE_NAME="${service_name}" \
                              REDIS_PASSWORD=pass

  ct_os_wait_pod_ready "${service_name}" 60
  check_redis_os_service_connection "${image_name}" "${service_name}" pass

  ct_os_delete_project
}

ct_os_cluster_up
test_redis_pure_image "${IMAGE_NAME}"
test_redis_template "${IMAGE_NAME}"

# test with the just built image and an integrated template
test_redis_integration "${IMAGE_NAME}"

# test with a released image and an integrated template
PUBLIC_IMAGE_NAME=${PUBLIC_IMAGE_NAME:-$(ct_get_public_image_name "${OS}" "${BASE_IMAGE_NAME}" "${VERSION}")}

# Try pulling the image first to see if it is accessible
if ct_check_image_availability "$PUBLIC_IMAGE_NAME"; then
  test_redis_integration "${PUBLIC_IMAGE_NAME}"
else
  echo "Warning: ${PUBLIC_IMAGE_NAME} could not be downloaded via 'docker'"
  # ignore possible failure of this test for centos images
  [ "${OS}" == "rhel7" ] && false "ERROR: Failed to pull image"
fi

# Check the imagestream
test_redis_imagestream

OS_TESTSUITE_RESULT=0

ct_os_cluster_down

# vim: set tabstop=2:shiftwidth=2:expandtab:

