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

# CI Entry point for BMUv2 functests

# Export vehicle architecture variable
export VEHICLE_ARCHITECTURE=vh6a/a

# 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

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

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

# Run bazel query to generate XML file with functests
bazel query 'attr("name", "functest.*", //firmware/...)' --output xml > functest_query.xml

# Generate pytest file for functests
python firmware/validation/non_native_functests/gen_functests_pytest.py functest_query.xml bmuv2 \
                 > firmware/validation/bmuv2_feature/test_functests_bmuv2.py

# Run functests
START_TIME=`date +%s`
python -m pytest firmware/validation/bmuv2_feature/test_functests_bmuv2.py \
                 --log-cli-level=INFO \
                 --json-report \
                 --json-report-file="/tmp/functest_result_$START_TIME.json" \
                 --verbose

# Run deploy to leave ECU in a good state
python -m pytest firmware/validation/bmu/test_deploy.py \
                 --verbose

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