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

# CI 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

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

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

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

virtualenv -p python3.6 emuv4_smoke_env
. emuv4_smoke_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 \
                 --verbose

# Run everything else
# TODO: FWVAL-674 Create CAN bus load tests
# TODO: FWVAL-1326 update marks and conftest.py to pull tests using that instead of specifying
#       specific files here
python -m pytest firmware/validation/emuv4_feature/test_emu_idle_state.py \
                 --verbose --json-report --json-report-file=/tmp/report.json

python -m pytest firmware/validation/emuv4_feature/test_emu_core_apps.py::test_app_start  \
                 --reruns 3 \
                 --reruns-delay 3 \
                 --json-report \
                 --json-report-file="result.json" \
                 --verbose

cd $origin
# TODO:FWVAL-1897 Add back rerun reporing feature
python -m pytest firmware/validation/emuv4_feature/test_emu_mon_gateway.py \
                 --verbose

# adding pose data path validation test to smoke
# todo: Enable after FW-2410 is resolved
#python -m pytest firmware/validation/emu_communication_validation/data_path_validation/test_gps_data_path.py \
#                 -s \
#                 --reruns 3 \
#                 --reruns-delay 3 \
#                 --json-report \
#                 --json-report-file="gps_data_result.json" \
#                 --verbose
#python -m pytest firmware/validation/emu_communication_validation/data_path_validation/test_imu_data_path.py \
#                 -s \
#                 --reruns 3 \
#                 --reruns-delay 3 \
#                 --json-report \
#                 --json-report-file="imu_data_result.json" \
#                 --verbose

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