#!/bin/bash
#
# Test the MongoDB 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]})
TEMPLATES="$THISDIR/examples/petset"

source "$THISDIR"/test-lib-openshift.sh

set -exo nounset

test -n "${IMAGE_NAME-}" || false 'make sure $IMAGE_NAME is defined'
test -n "${VERSION-}" || false 'make sure $VERSION is defined'
test -n "${OS-}" || false 'make sure $OS is defined'

ct_os_enable_print_logs

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

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

  local cmd="mongo ${pod_ip}/${database} -u ${user} -p${pass} --eval 'quit()'"
  SECONDS=0

  echo -n "Waiting for ${service_name} service becoming ready ..."
  while ! docker run --rm ${util_image_name} bash -c "${cmd}" ; do
    echo -n "."
    [ ${SECONDS} -gt ${timeout} ] && echo " FAIL" && return 1
    sleep 3
  done
  echo " PASS"
  return 0
}

function test_mongodb_pure_image() {
  local image_name=$1
  local image_name_no_namespace=${image_name##*/}
  local service_name=${image_name_no_namespace}

  ct_os_new_project
  ct_os_upload_image "${image_name}"
  # 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:testing"

  ct_os_deploy_pure_image "$image_name_no_namespace:testing" \
                          --name "${service_name}" \
                          --env MONGODB_ADMIN_PASSWORD=test

  ct_os_wait_pod_ready "${service_name}" 60
  check_mongodb_os_service_connection "${image_name}" "${service_name}" admin test admin 120

  ct_os_delete_project
}

function test_mongodb_replication() {
  local image_name=${1:-centos/mongodb-${VERSION//\.}-centos7}
  local image_name_no_namespace=${image_name##*/}
  local service_name=${image_name_no_namespace}

  ADMIN_PASS=adminPass

  ct_os_new_project
  docker tag "${image_name}" "${image_name}:$VERSION"

  ct_os_deploy_template_image "$TEMPLATES/mongodb-petset-persistent.yaml" \
                              MONGODB_IMAGE="${image_name}:$VERSION" \
                              MONGODB_SERVICE_NAME="${service_name}" \
                              MONGODB_USER=testu \
                              MONGODB_PASSWORD=testp \
                              MONGODB_DATABASE=testdb \
                              MONGODB_ADMIN_PASSWORD=${ADMIN_PASS} \
                              VOLUME_CAPACITY=500M

  ct_os_wait_rc_ready "${service_name}" 600
  for pod in $(ct_os_get_pod_name mongodb); do
    ct_os_wait_pod_ready $pod 300
  done
  check_mongodb_os_service_connection "${image_name}:$VERSION" "${service_name}" admin ${ADMIN_PASS} admin
  check_mongodb_os_service_connection "${image_name}:$VERSION" "${service_name}" testu testp testdb

  ct_os_run_in_pod $(ct_os_get_pod_name mongodb | head -n 1) bash -c 'set -x
. /usr/share/container-scripts/mongodb/common.sh
. /usr/share/container-scripts/mongodb/test-functions.sh
insert_and_wait_for_replication "$(replset_addr admin)" "{a:5, b:10}"'

  ct_os_delete_project
}

ct_os_cluster_up
test_mongodb_pure_image "${IMAGE_NAME}"
test_mongodb_replication "${IMAGE_NAME}"
ct_os_cluster_down

