Skip to content
Snippets Groups Projects
run_tests 8.01 KiB
Newer Older
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
#!/bin/zsh
# vim: set shiftwidth=4 softtabstop=4:
#
# This script is used to run tests on a database with demo data installed.
#
# Either provide the name of the database (first argument) or it will default to <project>_test
# The DB user can be overridden by setting ODOO_SCRIPTS_DB_USER.
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
# Version 20.2.0
source $here/common

tested_modules=${ODOO_SCRIPTS_MODULE_LIST_TESTS-$(read_odoo_scripts_expandable_configuration_key module_list_tests)}

if [[ "$tested_modules" != "" ]];
then 
    update="-i $tested_modules"
    if [[ "$COVERAGE_SOURCE" == "" ]];
    then
      if type python3 > /dev/null;
      then
          COVERAGE_SOURCE="$(python3 -B -c "print(f','.join(f'odoo.addons.{name}' for name in '$tested_modules'.split(',')))")"
      else
          COVERAGE_SOURCE="$(python2 -B -c "print(','.join('odoo.addons.{}'.format(name) for name in '$tested_modules'.split(',')))")"
      fi
    fi
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
dbname=$1
if [[ -z "$dbname" ]];
then
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
else
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
fi
echo "$INFO Using database $dbname for tests"
if [[ -n "$ODOO_SCRIPTS_DB_USER" ]]
then
    echo "$INFO Using db user $ODOO_SCRIPTS_DB_USER"
    db_user_param="--db_user $ODOO_SCRIPTS_DB_USER"
fi

ODOO_TYPE=${ODOO_TYPE:-$(read_odoo_scripts_configuration_key odoo_type odoo8)}

if [ "$ODOO_TYPE" = "odoo8" ];
then
    loglevel="--log-level=info"
else
    loglevel="--log-level=test"
fi
logfile=$(tempfile --suffix=.log)
COVERAGE_SOURCE="${COVERAGE_SOURCE-$project_home}"
if [ "$ODOO_TYPE" = "odoo7" ] || [ "$ODOO_TYPE" = "odoo8" ] || [ "$ODOO_TYPE" = "odoo9" ] || [ "$ODOO_TYPE" = "odoo10" ] ;
    # no coverage in those old versions
else
    export PRE_ODOO_BIN="coverage run --omit=__manifest__.py --source=$COVERAGE_SOURCE --branch "
# colorize also remove database name and thread number
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
alias colorize="sed -e 's/^\(\w*-..-.. ..:..:..,\w*\) \w*/\o033[2m\1\o033[22m/' -e 's/ INFO \w* \(odoo\.\(tests\.runner\|modules\.module\): \)\(.*\)/ \o033[32m\o033[7mINFO\o033[0m \1\o033[34m\3\o033[0m/' -e 's/ ERROR \w* \(odoo\.\(tests\.runner\|modules.module\): \)\(.*\)/ \o033[31m\o033[7mERROR\o033[0m \1\o033[34m\3\o033[0m/' -e 's/ \(INFO\) \(\w*\|?\) \([a-zA-Z0-9._-]*\):\(.*\)/ \o033[32m\o033[7m\1\o033[0m \3:\o033[32m\4\o033[39m/' -e 's/ \(DEBUG\) \w* \([a-zA-Z0-9._-]*\):\(.*\)/ \o033[33m\o033[7m\1\o033[0m \2:\o033[33m\3\o033[39m/' -e 's/ \(WARNING\) \w* \([a-zA-Z0-9._-]*\):\(.*\)/ \o033[33m\o033[7m\1\o033[0m \2:\o033[33m\3\o033[0m/' -e 's/ \(ERROR\) \w* \([a-zA-Z0-9._-]*\):\(.*\)/ \o033[31m\o033[7m\1\o033[0m \2:\o033[31m\3\o033[0m/' -e 's/ FAILED/ \o033[41m\o033[97m\o033[1mFAILED\o033[0m/' -e 's/ FAIL\(.*\)/ \o033[41m\o033[97m\o033[1mFAIL\o033[49m\o033[31m\1\o033[0m/' -e 's/ \(CRITICAL\) \w* \([a-zA-Z0-9._-]*\):\(.*\)/ \o033[33m\o033[7m\1\o033[0m \2:\o033[33m\3\o033[39m/' -e 's/ \(TEST\) \w* \(.*\) \(ERROR\)\(.*\)/ \o033[34m\o033[7m\1\o033[27m \2\o033[0m \o033[41m\o033[97m\o033[1m\3\o033[49m\o033[0m/' -e 's/ \(TEST\) \w* \(.*\) \(OK\)/ \o033[34m\o033[7m\1\o033[27m \2\o033[0m \o033[32m\o033[7m\3\o033[27m\o033[39m/' -e 's/ \(TEST\) \w* \([a-zA-Z0-9._-]*\):\(.*\)/ \o033[34m\o033[7m\1\o033[0m \2:\o033[34m\3\o033[0m/'"
# Catch both stdout & stderr here as Odoo 10 uses stderr, unlike previous Odoo versions.
$here/start $update -d $dbname $db_user_param  --test-enable $loglevel --max-cron=0 --workers=0 --stop-after-init $* 2>&1 | tee $logfile | colorize
start_status=$pipestatus[1]
Houzefa Abbasbhay's avatar
Houzefa Abbasbhay committed
# Odoo >= 14 detection
if [ $(grep -P 'odoo\.tests\.runner: ([[:digit:]]+) failed, [[:digit:]]+ error\(s\) of [[:digit:]]+ tests when loading database' $logfile --count) -gt 0 ];
Houzefa Abbasbhay's avatar
Houzefa Abbasbhay committed
  # Valid with odoo 14, 15, 16
  tests_ran=$(grep -P 'odoo\.tests\.runner: ([[:digit:]]+) failed, [[:digit:]]+ error\(s\) of \K[[:digit:]]+' $logfile -o)
  tests_failures=$(grep -P 'odoo\.tests\.runner: \K([[:digit:]]+)' $logfile -o)
  tests_errors=$(grep -P 'odoo\.tests\.runner: ([[:digit:]]+) failed, \K[[:digit:]]+' $logfile -o)
else
  # Valid with odoo 7, 11, 13
  tests_ran=$(grep -P 'o(penerp|doo).(modules.module|[^ ]+): Ran \K([[:digit:]]+)' $logfile -o | perl -ne '$sum += $_ } { print $sum')
  if [ -z "$tests_ran" ];
  then
      tests_ran=0
  fi
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
  tests_failures=$(grep -P 'o(penerp|doo).(modules.module|[^ ]+): Module [a-zA-Z0-9._-]+: \K([[:digit:]]+)' $logfile -o | perl -ne '$sum += $_ } { print $sum')
  # that might be doable in the perl above
  if [ -z "$tests_failures" ];
  then
      tests_failures=0
  fi
  tests_errors=$(grep -P 'o(penerp|doo).(modules.module|[^ ]+): Module [a-z0-9_]+: [[:digit:]]+ failures, \K([[:digit:]]+)' $logfile -o | perl -ne '$sum += $_ } { print $sum')
  if [ -z "$tests_errors" ];
  then
      tests_errors=0
  fi
  import_errors=$(grep -P 'o(penerp|doo).(modules.module|[^ ]+): Can not `import [a-z0-9_]+`.' $logfile)
  import_errors_number=$(grep -P 'o(penerp|doo).(modules.module|[^ ]+): Can not `import [a-z0-9_]+`.' $logfile -c)
  if [[ "$import_errors_number" -ne 0 ]];
  then
      test_errors=$((test_errors+import_errors_number))
  fi
# create a OpenMetrics metrics file
then
    echo -e "odoo_scripts_tests_ran $tests_ran\nodoo_scripts_tests_failures $tests_failures\nodoo_scripts_tests_error $tests_error\n# EOF\n" > metrics
if [ "$ODOO_TYPE" = 'odoo7' ] || [ "$ODOO_TYPE" = 'odoo8' ] || [ "$ODOO_TYPE" = 'odoo9' ] || [ "$ODOO_TYPE" = 'odoo10' ] || [ "$ODOO_TYPE" = 'odoo11' ];
then
    ok=$(grep ' OK' $logfile -c)
fi
# odoo 13 uses FAIL as the error message, in the form: FAIL: TestReorderingRule.test_reordering_rule
failed=$(grep ' FAIL\(ED\|:\)\| CRITICAL\| ERROR [^ ]* openerp.modules.module: Can not .import openerp\.addons\.\| TEST .* ERROR:\|Failed to initialize database\| ERROR \([[:alnum:]]\|_\)* .\+\.test_.\+ ERROR: \|invalid module names, ignored:' $logfile -c)
if [[ -n "$import_errors_number" ]];
then
    failed=$((failed+$import_errors_number))
fi
warnings=$(grep ' WARNING ' $logfile -c)
# XXX Not sure this is needed anymore with the tests_failures/tests_errors detection
if [[ $failed -eq 0 ]];
then
    # simple way to detect yaml failure
    failed=$(grep 'openerp.modules.loading: At least one test failed when loading the modules.' $logfile -c)
fi
echo "${esc}[2m*****${esc}[22m ${esc}[1mTest results${esc}[21m ${esc}[2m*****${esc}[22m"
echo "$tests_failures failed, $tests_errors error(s) of $tests_ran tests"
if [ "$ODOO_TYPE" = 'odoo7' ] || [ "$ODOO_TYPE" = 'odoo8' ] || [ "$ODOO_TYPE" = 'odoo9' ] || [ "$ODOO_TYPE" = 'odoo10' ] || [ "$ODOO_TYPE" = 'odoo11' ];
    echo "$OK $ok modules passing"
    echo -e "${GREEN}Passes${RESET}:"

    # Highlight the "OK"s in green (32).
    GREP_COLOR='0;32' grep --color=always ' OK' $logfile || true
if [[ $failed -gt 0 ]];
then
    echo -e "${RED}Failures${RESET}:"

    # Highlight the failure matches in red (31).
    GREP_COLOR='0;31' grep --color=always "CRITICAL\|FAIL\(ED\|:\)" $logfile
    GREP_COLOR='0;31' grep --color=always "ERROR [^ ]* openerp\.tools\.yaml_import:" $logfile
    GREP_COLOR='0;31' grep --color=always "ERROR [^ ]* openerp.modules.module: Can not \`import openerp\.addons\..*\`" $logfile
    GREP_COLOR='0;31' grep --color=always "TEST .* ERROR:" $logfile
    # this one is for odoo 13 for tests that produces errors (failures are matched with FAIL: )
    GREP_COLOR='0;31' grep --color=always "ERROR \([[:alnum:]]\|_\)* .\+\.test_.\+ ERROR:" $logfile
    GREP_COLOR='0;31' grep --color=always "invalid module names, ignored" $logfile
    if [[ -n "$import_errors" ]];
    then
if [[ $start_status -ne 0 ]];
then
    echo "$FATAL Tests failed (exit ${start_status})"
    exit $start_status
fi
if [[ $tests_failures -gt 0 ]] || [[ $tests_errors -gt 0 ]];
then
    echo "$FATAL Tests failed (found failure and errors in Ran N failures, M errors)"
if [[ $failed -gt 0 ]];
then
    echo "$FATAL Tests failed (found failure with grep)"
if [[ "${FAIL_ON_WARNING:-False}" != "False" ]];
then
    if [[ $warnings -gt 0 ]];
    then
        echo "$FATAL Tests failed ($warnings WARNING found)"