##(c) 2015 Real-Time Technology Solutions, Inc. All Rights Reserved.
##
## Sample QuerySurge(TM) Run Multiple Test Suites Script in order
## version 0.1

#!/bin/bash

##  ' *******************************************************************************
##  ' ********************** Setup Connection Details *******************************
##  ' *****  Edit the following configuration variables to work with your       *****
##  ' *****  QuerySurge environment and preferences.                            *****
##  ' *******************************************************************************

##  ' ********************* START OF CONFIGURATION VARIABLES ************************

myHostname='127.0.0.1'
myPort=80
myUsername='admin'
#add corresponding username and password to the qsapi.properties file to authenticate

cli="C:\Program Files\QuerySurge/cli/qscli.sh"
cliOptions="--hostname $myHostname --port $myPort --username $myUsername"

suiteIdList="1,2"
ordered="true"

# Jenkins log parse tags - must match error filter in parsing rules file
# ie. error /QSJenkinsFail/
# requires set fail=QSJenkinsFail
#
set failToken=QSJenkinsFail

##  ' ********************* END OF CONFIGURATION VARIABLES ************************

suiteIdArray=(${suiteIdList//,/ })

for suiteId in "${suiteIdArray[@]}"
do
        echo "Running Suite $suiteId..."
        scenarioId=`$cli runTestSuiteById $cliOptions $suiteId`
        echo "The scenario $scenarioId has started"
        if $ordered = "true"
        then
                status=$($cli getScenarioOutcome $cliOptions $scenarioId)
                status=${status// /}
                while [[ "$status" == "NotRun" ]] || [[ "$status" == "Running" ]]
                do
                        sleep 5
						# Get the Scenario Outcomes and write out as tokens to the log
                        echo "Waiting for scenario $scenarioId to finish"
                        status=`$cli getScenarioOutcome $cliOptions $scenarioId`
                        status=${status// /}
                        if [[ $status == "Failed"]]; then 
                                echo "$failToken: Scenario $suiteId failed!"
                        fi
                done
        fi
done

echo "Done!"
