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

# CI Entry point for BMUv4 functests

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

export VEHICLE_ARCHITECTURE=vh6b/b1

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

# Environment variable injection is done via pytest fixture. See:
# https://confluence.zooxlabs.com/pages/viewpage.action?pageId=161154284

# Run the bazel environment checks
firmware/validation/common/ci/bazel_precheck.sh

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

virtualenv -p python3.6 bmuv4_smoke_env
. bmuv4_smoke_env/bin/activate
python -m pip install -r firmware/validation/bmuv4_feature/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 bmuv4 \
                 > firmware/validation/bmuv4_feature/test_functests_bmuv4.py

# Run functests
set +e
START_TIME=`date +%s`
python -m pytest firmware/validation/bmuv4_feature/test_functests_bmuv4.py \
                 --log-level="WARNING" \
                 --json-report \
                 --json-report-file="/tmp/functest_result_$START_TIME.json" \
                 --verbose

konveyor /tmp/functest_result_$START_TIME.json bmuv4 functests

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

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