#! /bin/sh
set -x
origin="$PWD"

# CI Entry point for DCU Smoke Tests

# Clean out anything that might be lurking around...
git clean -fdx

# Run the bazel precheck steps (shutdown, remove cache)
firmware/validation/common/ci/bazel_precheck.sh

# Clear out bazel cache
rm -rf ~/.cache/bazel/

# Set up virtualenv
if [ -d dcu_smoke_env ]; then
    rm -rf dcu_smoke_env
fi

virtualenv -p python3.6 dcu_smoke_env
. dcu_smoke_env/bin/activate
python -m pip install -r firmware/validation/dcu_smoke/requirements.txt

# DBC files are generated by the make_dbcs fixture defined in the dbc_builder.py plugin.

# Deploy First!
python -m pytest firmware/validation/dcu_smoke/test_deploy.py \
                 --exitfirst \
                 --verbose

# If test_deploy fails, log the failure and retry.
ret=$?
if [ $ret -eq 1 ]; then
    dt=$(date '+%m/%d/%Y %H:%M:%S')
    echo "$dt : test_deploy failure" >> ~/dcu_smoker_deploy_fail.log

    python -m pytest firmware/validation/dcu_smoke/test_deploy.py \
                     --exitfirst \
                     --verbose
elif [ $ret -ne 0 ]; then
    exit $ret
fi

# If test_deploy fails again, exit the script
ret=$?
if [ $ret -ne 0 ]; then
    exit $ret
fi

set -e

# Now run some smoke tests
python -m pytest firmware/validation/dcu_smoke/ \
                 --ignore=firmware/validation/dcu_smoke/test_deploy.py \
                 --verbose

# Cleanup bazel job
cd $(bazel info workspace)
firmware/validation/common/ci/bazel_postcheck.sh
