#!/usr/bin/env bash
#
# Entrypoint to run integration tests

# Default environment variables
export TEST_SUITE="${TEST_SUITE:=oss}"
export PYTHON_VERSION="${PYTHON_VERSION:=3.9}"
export PYTHON_CONNECTION_CLASS="${PYTHON_CONNECTION_CLASS:=Urllib3HttpConnection}"
export CLUSTER="${CLUSTER:-opensearch}"
export SECURE_INTEGRATION="${1:-false}"
export OPENSEARCH_VERSION="${2:-latest}"
export TEST_PATTERN="${3:-}"
if [[ "$SECURE_INTEGRATION" == "true" ]]; then
    export OPENSEARCH_URL_EXTENSION="https"
else
    export OPENSEARCH_URL_EXTENSION="http"
fi

export IS_UNRELEASED=false
if [[ "$OPENSEARCH_VERSION" == *"SNAPSHOT" ]]; then
  IS_UNRELEASED=true
fi

echo -e "\033[1m>>>>> Unreleased is $IS_UNRELEASED >>>>>>>>>>>>>>>>>>>>>>>>>>>>>\033[0m"
script_path=$(dirname $(realpath $0))
source $script_path/functions/imports.sh
set -euo pipefail

echo -e "\033[1m>>>>> Start server container >>>>>>>>>>>>>>>>>>>>>>>>>>>>>\033[0m"
DETACH=true bash .ci/run-opensearch.sh

if [[ -n "$RUNSCRIPTS" ]]; then
  for RUNSCRIPT in ${RUNSCRIPTS//,/ } ; do
    echo -e "\033[1m>>>>> Running run-$RUNSCRIPT.sh >>>>>>>>>>>>>>>>>>>>>>>>>>>>>\033[0m"
    CONTAINER_NAME=${RUNSCRIPT} \
      DETACH=true \
      bash .ci/run-${RUNSCRIPT}.sh
  done
fi

echo -e "\033[1m>>>>> Repository specific tests >>>>>>>>>>>>>>>>>>>>>>>>>>>>>\033[0m"
bash .ci/run-repository.sh
