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

# CI Entry point for EMUv4 Functests

# 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

# 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

# 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 emuv4 \
                 > firmware/validation/emuv4_feature/test_functests_emuv4.py

# Run Smoke Test (needed for some reason)
python -m pytest firmware/validation/emuv4_feature/test_deploy.py \
                 --verbose

# Run functests
START_TIME=`date +%s`
python -m pytest firmware/validation/emuv4_feature/test_functests_emuv4.py \
                 --json-report \
                 --json-report-file="/tmp/functest_result_$START_TIME.json" \
                 --verbose

konveyor /tmp/functest_result_$START_TIME.json emuv4 functests

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