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

# Entry point for EMUv4 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

# Generate DBCs first. If this fails there is no point in continuing to test
# Note that this does not upload DBCs to Artifactory. To enable upload use the
# upload_dbc.sh script instead
firmware/validation/common/ci/generate_dbc.sh

# 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 Smoke Test
python -m pytest firmware/validation/emuv4_feature/test_deploy.py \
                 --log-cli-level=INFO \
                 --exitfirst \
                 --capture=no \
                 --verbose

# Run everything else
# TODO: FWVAL-674 Create CAN bus load tests
python -m pytest firmware/validation/emuv4_feature/test_emu_idle_state.py \
                 --verbose

# Run Remote Control Tests
python -m pytest firmware/validation/emuv4_feature/test_remote_control.py \
                 --verbose

# Cleanup bazel job
bazel clean --expunge
