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

# Entry point for TBC Powertrain Cooling Tests

# 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 tbc_pt_cooling_venv ]; then
    rm -rf tbc_pt_cooling_venv
fi

virtualenv -p python3.6 tbc_pt_cooling_venv
. tbc_pt_cooling_venv/bin/activate
python -m pip install -r firmware/validation/tbc_pt_cooling/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

# 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 tbc \
                 > firmware/validation/tbc_pt_cooling/test_functests_tbc.py

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

konveyor /tmp/functest_result_$START_TIME.json tbc functests

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

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