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

# Entry point for EMUv4 Nightly Feature Tests

# Export vehicle architecture variable
export VEHICLE_ARCHITECTURE=vh6b/b1

# Export QNX enable variable
export ZOOX_ENABLE_QNX70=1
export ZOOX_ENABLE_HIGHTEC_TRICORE=1

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

# But first, re-install the bazel for this branch (builds are extremely version-specific)
# Run the setup script for dev with --no-ask-pass to bypass sudo prompt
# and tags for bazel upgrade / downgrade
scripts/setup/setup.sh dev --no-ask-pass --tags bazel

# Do a bazel clean --expunge before clearing out bazel cache
bazel clean --expunge

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

# Build Docker containers
cd firmware/validation/docker/net_man
./build.sh emuv4_feature
cd $origin

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

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

# Update dentist protos before the tests have started. This is necessary due
# to dentist being imported before pytest processes it's plug-ins
dentist_proto_updater

# Run Firmware Deployment tests
python -m pytest firmware/validation/emuv4_feature/test_deploy.py \
                 --log-cli-level=INFO \
                 --exitfirst \
                 --capture=no \
                 --verbose

# Run the tests marked with the pytest markers listed below, except the ones
# marked with "diagnostics" marker and tests that are part of core apps.
python -m pytest firmware/validation/emuv4_feature/ \
                -m "(gatewaying_messages or \
                    state_transitions or \
                    power_state_transitions or \
                    manual_control or \
                    remote_control or \
                    autonomous or \
                    uncontrolled_stop or \
                    controlled_stop or \
                    parking_brake or \
                    telltales or \
                    drive_manager or \
                    firefly or \
                    suspension or \
                    robot_monitor) and \
                    not diagnostics" \
                    --ignore=firmware/validation/emuv4_feature/test_emu_core_apps.py \
                    --ignore=firmware/validation/emuv4_feature/test_diagnostics/ \
                    --ignore=firmware/validation/emuv4_feature/test_mdi_monitors/ \
                    --verbose \
                    --log-level=ERROR --json-report --json-report-file=/tmp/report.json

# Cleanup bazel job
bazel clean --expunge
