#!/bin/bash

set -o nounset
set -o errexit
set -o pipefail

# Set the API_URL value using the $SHARED_DIR/console.url file
CONSOLE_URL=$(cat $SHARED_DIR/console.url)  # Test cluster console URL
API_URL="https://api.${CONSOLE_URL#"https://console-openshift-console.apps."}:6443"  # Test cluster API URL
RESULTS_FILE="/spring-boot-openshift-interop-tests/junit-report.xml"  # The JUnit results file generated by the tests
LOGS_FOLDER="/spring-boot-openshift-interop-tests/test-sb/log"  # Folder where additional test logs are held
USERNAME="kubeadmin"  # Username to execute tests with
PASSWORD=$(cat $SHARED_DIR/kubeadmin-password)  # Corisponding password to use with $USERNAME

# Archive results function
function archive-results() {
    if [[ -f "${RESULTS_FILE}" ]] && [[ ! -f "${ARTIFACT_DIR}/junit_springboot_interop_results.xml" ]]; then
        echo "Copying ${RESULTS_FILE} to ${ARTIFACT_DIR}/junit_springboot_interop_results.xml..."
        cp "${RESULTS_FILE}" "${ARTIFACT_DIR}/junit_springboot_interop_results.xml"

        echo "Copying ${LOGS_FOLDER} to ${ARTIFACT_DIR}/logs..."
        cp -r "${LOGS_FOLDER}" "${ARTIFACT_DIR}/"
    fi
}

# Execute tests
echo "Executing tests..."
trap archive-results SIGINT SIGTERM ERR EXIT
/bin/bash /spring-boot-openshift-interop-tests/interop.sh ${API_URL} springboot ${USERNAME} ${PASSWORD}

