#!/bin/bash
set -e
export LC_ALL=C.UTF-8
export LANG=C.UTF-8
export PYTEST_OPTIONS="--verbose -r s --timeout-method=thread --timeout=300 --durations=20"

# This is needed for keras, though that dependency doesn't work right now.
export THEANO_FLAGS="base_compiledir=$(pwd)"

cp setup.cfg conftest.py $AUTOPKGTEST_TMP
cp distributed/tests/testegg-1.0.0-py3.4.egg $AUTOPKGTEST_TMP
cp distributed/tests/mytest.pyz /usr/lib/python3/dist-packages/distributed/tests/


SKIP_TESTS="test_localcluster_get_client \
  or test_cluster \
  or test_old_ssh_nprocs_renamed_to_n_workers \
  or test_nprocs_attribute_is_deprecated \
  or test_jupyter_server \
  or test_stack_overflow \
  or test_task_state_instance_are_garbage_collected \
  or test_queue_in_task \
  or test_variable_in_task \
  or test_spillbuffer_oserror \
"

# we run at elevated privledges. this test will always fail.
SKIP_TESTS="$SKIP_TESTS \
  or test_bad_local_directory \
  or test_unwriteable_dask_worker_space \
  or test_unwritable_base_dir \
  or test_nanny_timeout \
"

# problems running the dask scheduler in a schroot
SKIP_TESTS="$SKIP_TESTS \
  or test_preload_file \
  or test_preload_module \
  or test_preload_remote_module \
  or test_preload_config \
  or test_preload_command \
  or test_preload_command_default \
  or test_multiple_workers_2 \
"

for py in $(py3versions -r 2>/dev/null)
do cd "$AUTOPKGTEST_TMP"
  echo "Testing with $py:"
  $py -m pytest -m "not (avoid_ci or slow or flaky)" -k "not ( $SKIP_TESTS )" $PYTEST_OPTIONS --pyargs distributed || true
  ERR=$?
  echo Error code $ERR
  if [ -e ${AUTOPKGTEST_TMP}/.pytest_cache/v/cache/ -a \! -e ${AUTOPKGTEST_TMP}/.pytest_cache/v/cache/lastfailed ]; then
    echo Tests passed; true;
  else
    echo Tests failed; false;
  fi
done
