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

# CI Entry point for EMUv2 Functests

# Export vehicle architecture variable
export VEHICLE_ARCHITECTURE=vh6a/a

# Export QNX enable variable
export ZOOX_ENABLE_QNX70=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 emu_smoke_env ]; then
    rm -rf emu_smoke_env
fi

virtualenv -p python3.6 emu_smoke_env
. emu_smoke_env/bin/activate
python -m pip install -r firmware/validation/emuv2_smoke/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 emuv2 \
                 > firmware/validation/emuv2_smoke/test_functests_emuv2.py

# Run deploy (needed or functests fail)
# --exitfirst will exit if at the first test that fails (subsequent tests will not run)
# --capture=no is same as -s and will print stderr/stdout to console instead of capturing it
# --verbose will print detailed test output for the test cases (not just dots/Fs)
# and displaying it after all the tests end
python -m pytest firmware/validation/emuv2_smoke/test_deploy.py \
                 --log-cli-level=INFO \
                 --exitfirst \
                 --capture=no \
                 --verbose

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

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