#!/bin/bash

# ******************************************************************************
# EOS - the CERN Disk Storage System
# Copyright (C) 2019 CERN/Switzerland
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# ******************************************************************************

#-------------------------------------------------------------------------------
# Run a given test script and check the return code is successful.
# Arguments: $1  - text description of the test
#            $*  - the script to execute + arguments
#-------------------------------------------------------------------------------
function run_test_script() {
  local description=$1
  shift

  echo "Running ${description} tests"
  eval "$@"

  if [[ $? -ne 0 ]]; then
    echo "error: failed ${description} tests"
    exit 1
  fi
}

#-------------------------------------------------------------------------------
# Execute specific EOS testing scripts
#-------------------------------------------------------------------------------
run_test_script "MQ" eos-mq-tests "${1:-'eos-mq.eoscluster.cern.ch'}"
run_test_script "drain" eos-drain-test localhost
run_test_script "balance" eos-balance-test localhost

if [[ $(eos qos list) ]]; then
  run_test_script "qos" eos-qos-test dockertest
fi

echo "Running instance tests ..."
eos space quota default on
echo "Updating scheduler to be Weighted Round Robin"
eos space config default space.scheduler.type=weightedrr
eos-instance-test

if [[ $? -ne 0 ]]; then
  cat /tmp/eos-instance-test/test-output.log
  exit 1
fi

echo "Final ns stats"
eos ns stat

#-------------------------------------------------------------------------------
# Functional tests upload/download x509/macaroons/tpc/eos token
#-------------------------------------------------------------------------------
HTTPS_DIR="/eos/dockertest/test/https/"
run_test_script "HTTPS" eos-https-functional-test -p 8443 --dir ${HTTPS_DIR}

eos space quota default off
exit 0
