From 8450295f3d584f75378fc57373ee029bd3e8a622 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Tue, 21 Feb 2023 12:46:52 +0100 Subject: [PATCH] Building script: integrate full unit tests and API tests --- tests/api/start_api_server.sh | 25 +++++++++++++++-- tools/build_release.sh | 9 ++++++ tools/diagnostic.sh | 2 +- tools/test_api.sh | 52 +++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 3 deletions(-) create mode 100755 tools/test_api.sh diff --git a/tests/api/start_api_server.sh b/tests/api/start_api_server.sh index 67a3120a..ac4a1ce0 100755 --- a/tests/api/start_api_server.sh +++ b/tests/api/start_api_server.sh @@ -5,8 +5,24 @@ set -e # Le répertoire de ce script: - SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" + +while getopts "p:" opt; do + case "$opt" in + p) + PORT=${OPTARG} + ;; + \?) + echo "Invalid option: -$OPTARG" >&2 + exit 1 + ;; + :) + echo "Option -$OPTARG requires an argument." >&2 + exit 1 + ;; + esac +done + # récupère API_USER et API_PASSWORD source "$SCRIPT_DIR"/.env @@ -22,4 +38,9 @@ flask user-password --password "$API_PASSWORD" "$API_USER" flask edit-role LecteurAPI -a ScoView flask user-role "$API_USER" -a LecteurAPI -flask run --host 0.0.0.0 \ No newline at end of file +if [ -z "$PORT" ] +then + flask run --host 0.0.0.0 +else + flask run --host 0.0.0.0 -p "$PORT" +fi diff --git a/tools/build_release.sh b/tools/build_release.sh index c6c7525e..3efd5989 100755 --- a/tools/build_release.sh +++ b/tools/build_release.sh @@ -42,6 +42,7 @@ ARCH="amd64" FACTORY_DIR="/opt/factory" DEST_DIR="$PACKAGE_NAME"_"$VERSION"-"$RELEASE"_"$ARCH" GIT_RELEASE_URL="https://scodoc.org/git/viennet/ScoDoc/archive/${RELEASE_TAG}.tar.gz" +UNIT_TESTS_DIR="/opt/scodoc" # on lance les tests dans le rep. de travail, pas idéal echo "Le paquet sera $DEST_DIR.deb" echo -n "Est-ce ok ? (y/n) [y] " @@ -59,6 +60,14 @@ SCODOC_USER=scodoc [ -z "$FACTORY_DIR" ] && die "empty FACTORY_DIR" [ "$(id -nu)" != "$SCODOC_USER" ] && die "Erreur: le script $0 doit être lancé par l'utilisateur $SCODOC_USER" +# Tests unitaires lancés dans le répertoire de travail +echo "TESTS UNITAIRES" +(cd "$UNIT_TESTS_DIR"; pytest tests/unit) + +# Tests API +(cd "$UNIT_TESTS_DIR"; tools/test_api.sh) + + # Création répertoire du paquet, et de opt slash="$FACTORY_DIR"/"$DEST_DIR" optdir="$slash"/opt diff --git a/tools/diagnostic.sh b/tools/diagnostic.sh index 0354f129..6aca307b 100755 --- a/tools/diagnostic.sh +++ b/tools/diagnostic.sh @@ -36,7 +36,7 @@ while getopts "anh" opt; do h) echo "Diagnostic installation ScoDoc" echo "Rassemble informations sur le systeme et l'installation ScoDoc" - echo "Usage: $0 [-h] [-n] [-a] [-u] [-d dept]" + echo "Usage: $0 [-h] [-n] [-a] [-u]" echo " -h cette aide" echo " -n pas d'envoi par mail" echo " -a enregistre la bases de donnees (prod)" diff --git a/tools/test_api.sh b/tools/test_api.sh new file mode 100755 index 00000000..f8591db6 --- /dev/null +++ b/tools/test_api.sh @@ -0,0 +1,52 @@ +#!/bin/bash + +# Lance les tests unitaires de l'API +# Ce script lance un serveur scodoc sur le port 5555 +# attend qu'il soit initialisé puis lance les tests client API. +# +# E. Viennet, Fev 2023 + +cd /opt/scodoc +# suppose que le virtual env est bien configuré + +# Utilise un port spécifique pour pouvoir lancer ce test sans couper +# le serveur de dev +PORT=5555 +SERVER_LOG=/tmp/test_api_server.log + +export SCODOC_URL="http://localhost:${PORT}" + +# ------- Check pas de serveur déjà lancé +if nc -z localhost "$PORT" +then + fuser -v "$PORT"/tcp + echo Server already running on port "$PORT" + echo You may want to try: fuser -k "$PORT"/tcp + echo aborting tests + exit 1 +fi + +tests/api/start_api_server.sh -p "$PORT" &> "$SERVER_LOG" & +pid=$! +echo "ScoDoc test server logs are in $SERVER_LOG" +# Wait for server setup +echo -n "Waiting for server" +while ! nc -z localhost "$PORT"; do + echo -n . + sleep 1 +done +echo +echo Server PID "$pid" running on port "$PORT" +# ------------------ + +pytest tests/api + +# ------------------ +echo "Killing server" +kill "$pid" +fuser -k "$PORT"/tcp + + + + +