Skip to content

Instantly share code, notes, and snippets.

@rajeshtaneja
Created March 20, 2015 09:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajeshtaneja/475d923fd482311c1544 to your computer and use it in GitHub Desktop.
Save rajeshtaneja/475d923fd482311c1544 to your computer and use it in GitHub Desktop.
Run parallel behat
#!/bin/bash
################################
#!# SiteId=behat_whole_suite_m
#!# OutputFormat=moodle_progress,junit
################################
# Optional Params.
if [ -z "${BehatProfileToUseOnDay}" ]; then
BehatProfileToUseOnDay="default default default default default default default"
fi
if [ -z "${SELENIUMPORT}" ]; then
SELENIUMPORT=4444
fi
if [ -z "${PARALLELPROCESS}" ]; then
PARALLELPROCESS=2
fi
if [ -z "${PHPPORT}" ]; then
PHPPORT=8000
fi
################################
homedir=/var/lib/jenkins
moodledir="${homedir}/moodle"
datadir=/srv/moodledata
moodledatadir="${datadir}/data"
rerunfile="$moodledatadir/$SiteId-rerunlist"
#export DISPLAY=:99
# Start phpserver and selenium instance
cd $moodledir
for ((i=0;i<=$PARALLELPROCESS;i+=1)); do
$homedir/scripts/selenium.sh start $(($SELENIUMPORT+$i)) > /dev/null 2>&1 &
$homedir/scripts/phpserver.sh start $(($PHPPORT+$i)) > /dev/null 2>&1 &
done;
cd -
workspacedir="${SiteId}"
if [ ! -d "${homedir}/workspace/UNIQUESTRING_${workspacedir}" ]; then
mkdir -p ${homedir}/workspace/UNIQUESTRING_${workspacedir}
chmod 775 ${homedir}/workspace/UNIQUESTRING_${workspacedir}
fi
if [ ! -d "${homedir}/workspace/UNIQUESTRING_${workspacedir}/junit_reports" ]; then
mkdir -p ${homedir}/workspace/UNIQUESTRING_${workspacedir}/junit_reports
chmod 775 ${homedir}/workspace/UNIQUESTRING_${workspacedir}/junit_reports
fi
if [ -e "${rerunfile}" ]; then
rm $rerunfile
fi
junitreportspath="${homedir}/workspace/UNIQUESTRING_${workspacedir}/junit_reports/${BUILD_ID}"
echo "JUnit reports path: $junitreportspath"
# Find which profile to use.
dayoftheweek=`date +"%u"`
BehatProfileToUseOnDay=(`echo ${BehatProfileToUseOnDay}`);
behatprofiletouse=${BehatProfileToUseOnDay[ $(( ${dayoftheweek} - 1 )) ]}
# Run tests.
cd $moodledir/$SiteId
php admin/tool/behat/cli/run.php --format="$OutputFormat" --out="','$junitreportspath" --rerun="$rerunfile{runprocess}.txt" --replace="{runprocess}"
exitcode=${PIPESTATUS[0]}
# Re-run failed scenarios, to ensure they are true fails.
if [ "${exitcode}" -eq 1 ]; then
exitcode=0
for ((i=1;i<=$BehatParallelRuns;i+=1)); do
thisrerunfile="$rerunfile$i.txt"
if [ -e "${thisrerunfile}" ]; then
if [ -s "${thisrerunfile}" ]; then
echo "---Running behat again for failed steps---"
if [ ! -L $moodledir/$SiteId/behatrun$i ]; then
ln -s $moodledir/$SiteId $moodledir/$SiteId/behatrun$i
fi
vendor/bin/behat --config $moodledatadir/behat_$SiteId$i/behat/behat.yml --format $OutputFormat --out ','$junitreportspath --profile $behatprofiletouse --verbose --rerun $thisrerunfile
exitcode=$(($exitcode+${PIPESTATUS[0]}))
fi
rm $thisrerunfile
fi
done;
fi
for ((i=0;i<=$PARALLELPROCESS;i+=1)); do
$homedir/scripts/selenium.sh stop $(($SELENIUMPORT+$i)) > /dev/null 2>&1 &
$homedir/scripts/phpserver.sh stop $(($PHPPORT+$i)) > /dev/null 2>&1 &
done;
exit $exitcode
#!/bin/bash
########### Variables used ##############
#!# SiteId=behat_whole_suite_m
#!# BranchId=master
#!# BehatConfigFile=behat_config_local_parallel.php.template
#!# DbName=${SiteId}
#!# dbtorun="mysql pgsql mysql pgsql mssql mysql oracle"
#!# mysql="DbType:mysqli DbUser:moodle DbPwd:moodle DbHost:mysql01.test.in.moodle.com"
#!# mariadb="DbType:mariadb DbUser:moodle DbPwd:moodle bHost:mariadb01.test.in.moodle.com"
#!# pgsql="DbType:pgsql DbUser:postgres DbPwd:moodle DbHost:172.21.0.70"
#!# oracle="DbType:oci DbUser:system DbPwd:moodle DbHost:oracle01.test.in.moodle.com:1521/xe"
#!# mssql="DbType:mssql DbUser:sa DbPwd:moodle DbHost:mssql01.test.in.moodle.com"
#########################################
# Optional Params.
if [ -z ${DbName} ]; then
DbName=${SiteId}
fi
if [ -z ${SiteDbPrefix} ]; then
SiteDbPrefix=mdl_
fi
if [ -z ${PhpunitDbPrefix} ]; then
PhpunitDbPrefix=phpunit_
fi
if [ -z ${BehatDbPrefix} ]; then
BehatDbPrefix=behat_
fi
if [ -z "${SELENIUMPORT}" ]; then
SELENIUMPORT=4444
fi
if [ -z "${PARALLELPROCESS}" ]; then
PARALLELPROCESS=2
fi
if [ -z "${PHPPORT}" ]; then
PHPPORT=8000
fi
##########################################
# Change string to array.
dbtorun=(`echo ${dbtorun}`);
mysql=(`echo ${mysql}`);
mariadb=(`echo ${mariadb}`);
pgsql=(`echo ${pgsql}`);
oracle=(`echo ${oracle}`);
mssql=(`echo ${mssql}`);
# Find which db to run today.
dayoftheweek=`date +"%u"`
dbtouse=${dbtorun[ $(( ${dayoftheweek} - 1 )) ]}
#### Set DbName for oracle ####
if [[ ${DbName} == ${SiteId} && "${dbtouse}" == "oracle" ]]; then
str=$(hostname)
echo "Using xe database for oracle"
DbName=xe
PhpunitDbPrefix="p${str: -1}"
BehatDbPrefix="b${str: -1}"
fi
#### Set DbName for mssql ####
if [[ ${DbName} == ${SiteId} && "${dbtouse}" == "mssql" ]]; then
str=$(hostname)
echo "Using moodle for testing mssql"
DbName=moodle
PhpunitDbPrefix="p${str: -2}_"
BehatDbPrefix="b${str: -2}_"
fi
###############################
eval dbtousedetails="(\"\${$dbtouse[@]}\")"
# Set all values.
for dbtouse in ${dbtousedetails[@]} ; do
KEY=${dbtouse%%:*}
VALUE=${dbtouse#*:}
eval ${KEY}=${VALUE}
done
# Parent directory, containing code and everything.
homedir=/var/lib/jenkins
moodledir="${homedir}/moodle"
datadir=/srv/moodledata
moodledatadir="${datadir}/data"
faildumpdir="${datadir}/behatfaildump"
# Create this link so it can be accessed by site.
if [ ! -d "${homedir}/workspace/UNIQUESTRING_behat" ]; then
mkdir -p ${homedir}/workspace/UNIQUESTRING_behat
fi
if [ ! -d "${homedir}/workspace/UNIQUESTRING_behat_$BranchId" ]; then
mkdir -p ${homedir}/workspace/UNIQUESTRING_behat_$BranchId
fi
if [ ! -L "/var/www/html/screenshots_$SiteId" ]; then
ln -s $faildumpdir /var/www/screenshots_$SiteId
fi
# Ensure following directories are there
for ((i=1;i<=$PARALLELPROCESS;i+=1)); do
if [ ! -d "$moodledatadir$i" ]; then
mkdir -m777 $moodledatadir$i;
fi
done;
mkdir -p $moodledatadir
# Resetting wwwroot and dataroot
rm -rf $moodledatadir/$SiteId
rm -rf $moodledatadir/behat_$SiteId
mkdir $moodledatadir/$SiteId
chmod 777 $moodledatadir/$SiteId
mkdir $moodledatadir/behat_$SiteId
chmod 777 $moodledatadir/behat_$SiteId
# Create screenshot directory if not present.
mkdir -p $faildumpdir/$SiteId
chmod 777 $faildumpdir/$SiteId
# Copying from config template.
replacements="%%DbType%%#${DbType}
%%DbHost%%#${DbHost}
%%SiteId%%#${SiteId}
%%DbUser%%#${DbUser}
%%DbPwd%%#${DbPwd}
%%DbName%%#${DbName}
%%DataDir%%#${moodledatadir}
%%FailDumpDir%%#${faildumpdir}
%%SiteDbPrefix%%#${SiteDbPrefix}
%%PhpunitDbPrefix%%#${PhpunitDbPrefix}
%%BehatDbPrefix%%#${BehatDbPrefix}"
# Apply template transformations.
text="$( cat $homedir/configs/$BehatConfigFile )"
for i in ${replacements}; do
text=$( echo "${text}" | sed "s#${i}#g" )
done
for ((i=0;i<$PARALLELPROCESS;i+=1)); do
text=$( echo "${text}" | sed "s/%%PhpPort$(($i+1))%%/$(($PHPPORT+$i))/g" )
text=$( echo "${text}" | sed "s/%%SeleniumPort$(($i+1))%%/$(($SELENIUMPORT+$i))/g" )
done;
# Save the config.php into destination.
echo "${text}" > $moodledir/$SiteId/config.php
cd $moodledir/$SiteId
# Install behat dependencies.
if [ ! -f "$moodledir/$SiteId/composer.phar" ]; then
curl -s https://getcomposer.org/installer | php
fi
php composer.phar install --prefer-source
# Install behat test environment.
php admin/tool/behat/cli/util.php --drop
php admin/tool/behat/cli/init.php -j=$PARALLELPROCESS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment