Skip to content
Snippets Groups Projects
do_tests 6.61 KiB
Newer Older
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
#!/bin/zsh
# vim: set shiftwidth=4 softtabstop=4:
#
# This script will create a database, install some modules and run tests on it
#
# Arguments:
#
# - db host if any (will use socket if nothing is provided)
# - db user
# - other arguments are forwarded as is to the setup command.
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
#
# Required:
#
# - access to pg database
#
Vincent Hatakeyama's avatar
4.1
Vincent Hatakeyama committed
# Version 4.1
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
here=$(dirname $0)
# assume we are called from the project home
project_home=$PWD
if ! type python3 > /dev/null; then
    python="python2"
else
    python="python3"
fi
# this is the same value as conf/drone/odoo.conf
if [[ -e "${project_home}/conf/drone/odoo.conf" ]];
then
    dbowner=$($python -B -c "from six.moves import configparser ; c = configparser.ConfigParser() ; c.read('${project_home}/conf/drone/odoo.conf') ; print(c.get('options', 'db_user'))")
    unaccent=$($python -B -c "from six.moves import configparser ; c = configparser.ConfigParser() ; c.read('${project_home}/conf/drone/odoo.conf') ; print(c.get('options', 'unaccent') if c.has_option('options', 'unaccent') else False)")
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
# this is the same value as conf/dev/odoo.conf
elif [[ -e "${project_home}/conf/dev/odoo.conf" ]];
    dbowner=$($python -B -c "from six.moves import configparser ; c = configparser.ConfigParser() ; c.read('${project_home}/conf/dev/odoo.conf') ; print(c.get('options', 'db_user'))")
    unaccent=$($python -B -c "from six.moves import configparser ; c = configparser.ConfigParser() ; c.read('${project_home}/conf/dev/odoo.conf') ; print(c.get('options', 'unaccent') if c.has_option('options', 'unaccent') else False)")
modules_to_install=${ODOO_SCRIPTS_MODULE_LIST-$($python -B -c "from six.moves import configparser ; c = configparser.ConfigParser() ; c.read('${project_home}/setup.cfg') ; print(','.join(c.get('odoo_scripts', 'module_list').split()))")}
if [[ -e "${project_home}/setup.cfg" ]];
then
    fail_on_errors=${ODOO_SCRIPTS_FAIL_ON_ERRORS-$($python -B -c "from six.moves import configparser ; c = configparser.ConfigParser() ; c.read('${project_home}/setup.cfg') ; print(c.get('odoo_scripts', 'fail_on_errors') if c.has_option('odoo_scripts', 'fail_on_errors') else True)")}
else
    fail_on_errors=${ODOO_SCRIPTS_FAIL_ON_ERRORS-True}
fi
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed

# color stuff
autoload colors
if [[ "$terminfo[colors]" -gt 7 ]];
then
    echo "[DEBUG] colors"
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
    colors
fi
for COLOR in RED GREEN YELLOW BLUE MAGENTA CYAN BLACK WHITE; do
    eval $COLOR='$fg_no_bold[${(L)COLOR}]'
    eval BOLD_$COLOR='$fg_bold[${(L)COLOR}]'
done
eval RESET='$reset_color'

# argument handling
if [[ $# -gt 0 ]];
then
    echo "[${YELLOW}INFO${RESET}] Using database host $1"
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
    PG_HOST=-h$1
    ODOO_HOST=--db_host=$1
    shift
    if [[ $# -gt 0 ]];
    then
        echo "[${YELLOW}INFO${RESET}] Using database user $1"
        PG_USER=-U$1
        ODOO_USER=--db_user=$1
        dbowner=$1
        shift
        if [[ $# -gt 0 ]];
        then
            dbname=$1
            shift
        else
            dbname=$(basename $project_home)_test
        fi
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
    else
        PG_USER=""
        ODOO_USER=""
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
    fi
else
    PG_HOST=""
    ODOO_HOST=""
    PG_USER=""
    ODOO_USER=""
fi
# This should be a parameter if it was simple to parse
INSTALL_ARGUMENTS="--log-level=${INSTALL_LOG_LEVEL:-warn}"
if [[ -n "$ODOO_WITHOUT_DEMO" ]];
then
    INSTALL_ARGUMENTS="$INSTALL_ARGUMENTS --without-demo=$ODOO_WITHOUT_DEMO"
fi
if [[ -z "$ODOO_NO_DROP" ]];
then
    echo "[${YELLOW}INFO${RESET}] Drop any existing database '$dbname'"
    dropdb $PG_HOST $PG_USER --if-exists $dbname --no-password
    echo "[${YELLOW}INFO${RESET}] Create database '$dbname' with owner $dbowner"
    createdb $PG_HOST $PG_USER -O $dbowner $dbname --no-password || { echo ${RED}FATAL${RESET} - Cannot create test database ; exit 1; }
fi
# TODO should also add all extensions from setup.cfg listed in pg.extensions
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
if [[ "$unaccent" == "True" ]];
then
    echo "[${YELLOW}INFO${RESET}] Add unaccent to '$dbname'"
    psql $PG_HOST $PG_USER $dbname -c "CREATE EXTENSION IF NOT EXISTS unaccent;"
Vincent Hatakeyama's avatar
Vincent Hatakeyama committed
fi
echo "[${YELLOW}INFO${RESET}] Modules to install: $modules_to_install"
echo "[${YELLOW}INFO${RESET}] Install those modules"
logfile=$(tempfile --suffix=.log)
function clean_logfile() {
    rm $logfile
}
trap clean_logfile INT TERM
$here/start $INSTALL_ARGUMENTS -i $modules_to_install -d $dbname --max-cron-threads=0 --stop-after-init $* $ODOO_HOST $ODOO_USER 2>&1 | tee $logfile | sed -e 's/\(.*\) \(INFO\) \(.*\)/\o033[2m\1\o033[22m \o033[32m\o033[7m\2\o033[27m \3\o033[39m/' -e 's/\(.*\) \(DEBUG\) \(.*\)/\o033[2m\1\o033[22m \o033[33m\o033[7m\2\o033[27m \2\o033[39m/' -e 's/\(.*\) \(WARNING\) \(.*\)/\o033[2m\1\o033[22m \o033[33m\o033[7m\2\o033[27m \3\o033[39m/' -e 's/\(.*\) \(ERROR\) \(.*\)/\o033[2m\1\o033[22m \o033[31m\o033[7m\2\o033[27m \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\) \(.*\)/\o033[2m\1\o033[22m \o033[33m\o033[7m\2\o033[27m \3\o033[39m/' -e 's/\(.*\) \(TEST\) \(.*\) \(ERROR\)\(.*\)/\o033[2m\1\o033[22m \o033[34m\o033[7m\2\o033[27m \3\o033[0m \o033[41m\o033[97m\o033[1m\4\o033[49m\o033[31m\5\o033[0m/' -e 's/\(.*\) \(TEST\) \(.*\) \(OK\)/\o033[2m\1\o033[22m \o033[34m\o033[7m\2\o033[27m \3\o033[0m \o033[32m\o033[7m\4\o033[27m\o033[39m/' -e 's/\(.*\) \(TEST\) \(.*\)/\o033[2m\1\o033[22m \o033[34m\o033[7m\2\o033[27m \3 \o033[0m/'
start_status=$pipestatus[1]
    echo "[${RED} KO ${RESET}] Cannot install modules (exit ${start_status})"
criticals=$(grep ' CRITICAL' -F $logfile -c)
# exclude ALTER COLUMN xxx set not null that are not real errors
errors=$(grep ' ERROR' -F $logfile | grep -v "ALTER COLUMN \"\w*\" SET NOT NULL" -c)
warnings=$(grep ' WARNING' -F $logfile -c)
if [[ $criticals -gt 0 ]];
then
    echo "[${RED} KO ${RESET}] Cannot install modules ($criticals CRITICAL messages found)"
    exit 2
fi
        echo "[${MAGENTA}WARN${RESET}] Install modules with $errors ERROR messages"
        echo "[${RED} KO ${RESET}] Cannot install modules ($errors ERROR messages found)"
        exit 1
    if [[ $warnings -gt 0 ]];
    then
        if [[ "${FAIL_ON_WARNING:-False}" == "False" ]];
        then
            echo "[${MAGENTA}WARN${RESET}] Modules installed ($warnings WARNING messages)"
        else
            echo "[${RED} KO ${RESET}] Cannot install modules ($warnings WARNING messages found)"
            exit 1
        fi
    else
        echo "[${GREEN} OK ${RESET}] Modules installed"
    fi
echo "[${YELLOW}INFO${RESET}] Run tests on $dbname"
$here/run_tests $dbname $ODOO_HOST $ODOO_USER $*