#!/bin/bash
set -euo pipefail
set -x
set -v

CPUS=$(nproc)

# Run test cases in parallel - most of it is sleeping making sure timing conflicts don't happen
export MTR_MAX_PARALLEL=$(( ${CPUS} / 2 ))
export MTR_MAX_PARALLEL_MAX=$MTR_MAX_PARALLEL
export MTR_PARALLEL=auto
export MTR_MAX_TEST_FAIL=40
# shared memory in containters limited to 64M until Docker-1.9.2
if [[ ! ${FLAVOUR} =~ facebook* ]]
then
# facebook does O_DIRECT by default of files which fails
# all innodb tests on tmpfs aka MEM
	export MTR_MEM=1
fi
# in minutes - tokudb_parts.partition_alter4_tokudb takes ~18mins - MDEV-10708
export MTR_TESTCASE_TIMEOUT=20

if [ ${BUILD_TYPE} == 'Debug' ];
then
  MTR_EXTRA="--debug-server"
  # doing valgrind on all would just be too much
  if [ ${CC} == 'gcc' ];
  then
    MTR_EXTRA="${MTR_EXTRA} --valgrind"
  fi
else
   MTR_EXTRA=''
fi


cd mysql-test

# facebook have customised tests
if [ -f collections/default.daily ];
then
  source ./collections/default.daily
  exit 0
fi


# galera
if [ -f /source/libgalera_smm.so ];
then
  export WSREP_PROVIDER=/source/libgalera_smm.so
fi


if [ -f disabled.def ];
then
   MTR_EXTRA="${MTR_EXTRA} --skip-test-list=disabled.def"
fi

# For upstream skip these broken tests. Develop - we may want to try to fix them
if [[ ! "${FLAVOUR}" =~ "*-develop" ]];
then
  # rpl.rpl_parallel MDEV-10653
  # encryption.innodb_encryption-page-compression  MDEV-11420
  # main.stat_tables_par MDEV-10804
  # binlog.binlog_stm_ctype_ucs MDEV-12055
  # perfschema.socket_summary_by_event_name_func  	MDEV-10622
  # perfschema.privilege_table_io    MDEV-10679
  # main.mysql_client_test.*  CONC-208
  # encryption.create_or_replace - MDEV-12467
  # encryption.innodb_onlinealter_encryption - MDEV-10099
  # rpl.rpl_semi_sync / binlog_encryption.rpl_semi_sync - MDEV-11220
  # wsrep_info.plugin XXX  potentially because of upstream version numbering
  # innodb.innodb_mysql - https://github.com/MariaDB/server/pull/361
   MTR_EXTRA="
   --skip-test='gis.(geometry_property_function_issimple|gis_bugs_crashes|spatial_utility_function_simplify|spatial_analysis_functions_buffer|spatial_op_testingfunc_mix)'
   --skip-test=parts.partition_alter2_1_myisam
   --skip-test='tokudb_parts.*'
   --skip-test=connect.tbl
   --skip-test=rpl.rpl_parallel
   --skip-test=encryption.innodb_encryption-page-compression
   --skip-test=perfschema.socket_summary_by_event_name_func
   --skip-test=perfschema.privilege_table_io
   --skip-test='main.mysql_client_test.*'
   --skip-test=spider/bg.spider_fixes
   --skip-test=encryption.create_or_replace
   --skip-test=innodb.innodb_mysql
   --skip-test=wsrep_info.plugin
   --skip-test=rpl.rpl_semi_sync
   --skip-test=binlog_encryption.rpl_semi_sync
   --skip-test=encryption.innodb-encryption-disable
   --skip-test=encryption.innodb_onlinealter_encryption
   --skip-test='binlog_encryption.*'"
fi

# gis tests - MySQL on POWER - unfixed

EXIT=0
./mtr ${MTR_EXTRA} --force --big-test --nowarnings \
   || EXIT=$?

tar -jcf /artifacts/test-logs.tar.bz2 var/*/log var/log

exit $EXIT
