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

# CI Entry point for EMUv2 Smoke Tests

# A note on environment variables:
# These are now contained in pytest fixtures loaded as pytest_plugins in the conftest.py
# present in this directory and the fixtures are passed to tests/fixtures that require
# these variables to be populated. This is to gain flexibility to run the tests via the
# FWVAL End-to-end Automation Framework.
#
# TL;DR -> Environment variables DO NOT appear in this file anymore.

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

# Export QNX enable variable
export ZOOX_ENABLE_QNX70=1

# Run the bazel precheck steps (shutdown, remove cache)
firmware/validation/common/ci/bazel_precheck.sh

# Build Docker containers
cd firmware/validation/docker/net_man
./build.sh emu
cd $origin

# 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/emu/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 Smoke Test
# --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 everything else
python -m pytest firmware/validation/emu/ \
                 --ignore=firmware/validation/emu/test_deploy.py \
                 --capture=no \
                 --verbose

# Run emu core app tests , Move the entry point of CI to  firmware/validation/emuv2_smoke FWVAl-1242
python -m pytest firmware/validation/emuv2_smoke/test_emu_core_apps.py \
                 --reruns 3 \
                 --reruns-delay 3 \
                 --json-report \
                 --json-report-file="result.json" \
                 --capture=no \
                 --verbose

# upload the rerun failures to testrail
# adding || exit 0 to make sure the script failure doesnt result
# in smoke test failure and jobs after this test are executed
# refer FWVAL-1281 for more info
cd $origin
python  firmware/validation/tools/report_test_rerun_failures.py || exit 0

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