From e7f2706461a93fb993129053356257a8081a51bc Mon Sep 17 00:00:00 2001 From: Alexander Todorov Date: Wed, 6 Nov 2019 14:22:39 +0200 Subject: [PATCH] [tests] Use functions for starting and stopping lorax-composer Cherry-picked from f978d4b01cc33ea9459e8d2bdff39b81a6dd5511 Related: rhbz#1769525 --- tests/cli/lib/lib.sh | 55 ++++++++++++++++++++++++++++++++++++++++++++ tests/test_cli.sh | 32 ++++++++------------------ 2 files changed, 65 insertions(+), 22 deletions(-) diff --git a/tests/cli/lib/lib.sh b/tests/cli/lib/lib.sh index 14974f91..98e788ec 100755 --- a/tests/cli/lib/lib.sh +++ b/tests/cli/lib/lib.sh @@ -1,7 +1,10 @@ #!/usr/bin/env bash +. /usr/share/beakerlib/beakerlib.sh + # Monkey-patch beakerlib to exit on first failure if COMPOSER_TEST_FAIL_FAST is # set. https://github.com/beakerlib/beakerlib/issues/42 +COMPOSER_TEST_FAIL_FAST=${COMPOSER_TEST_FAIL_FAST:-0} if [ "$COMPOSER_TEST_FAIL_FAST" == "1" ]; then eval "original$(declare -f __INTERNAL_LogAndJournalFail)" @@ -37,6 +40,58 @@ boot_image() { done; } +wait_for_composer() { + tries=0 + until curl -m 15 --unix-socket /run/weldr/api.socket http://localhost:4000/api/status | grep 'db_supported.*true'; do + tries=$((tries + 1)) + if [ $tries -gt 50 ]; then + exit 1 + fi + sleep 5 + echo "DEBUG: Waiting for backend API to become ready before testing ..." + done; +} + +composer_start() { + local rc + local params="$@" + + if [[ -z "$CLI" || "$CLI" == "./src/bin/composer-cli" ]]; then + ./src/sbin/lorax-composer $params --sharedir $SHARE_DIR $BLUEPRINTS_DIR & + elif [ -n "$params" ]; then + /usr/sbin/lorax-composer $params /var/lib/lorax/composer/blueprints & + else + # socket stop/start seems to be necessary for a proper service restart + # after a previous direct manual run for it to work properly + systemctl start lorax-composer.socket + systemctl start lorax-composer + fi + rc=$? + + # wait for the backend to become ready + if [ "$rc" -eq 0 ]; then + wait_for_composer + else + rlLogFail "Unable to start lorax-composer (exit code $rc)" + fi + return $rc +} + +composer_stop() { + MANUAL=${MANUAL:-0} + # socket stop/start seems to be necessary for a proper service restart + # after a previous direct manual run for it to work properly + if systemctl list-units | grep -q lorax-composer.socket; then + systemctl stop lorax-composer.socket + fi + + if [[ -z "$CLI" || "$CLI" == "./src/bin/composer-cli" || "$MANUAL" == "1" ]]; then + pkill -9 lorax-composer + rm -f /run/weldr/api.socket + else + systemctl stop lorax-composer + fi +} # a generic helper function unifying the specific checks executed on a running # image instance diff --git a/tests/test_cli.sh b/tests/test_cli.sh index c0949e38..76eb0019 100755 --- a/tests/test_cli.sh +++ b/tests/test_cli.sh @@ -3,6 +3,8 @@ set -eu +. $(dirname $0)/cli/lib/lib.sh + export BEAKERLIB_DIR=$(mktemp -d /tmp/composer-test.XXXXXX) CLI="${CLI:-}" @@ -58,38 +60,25 @@ if [ -z "$CLI" ]; then export top_srcdir=`pwd` . ./tests/testenv.sh - BLUEPRINTS_DIR=`mktemp -d '/tmp/composer-blueprints.XXXXX'` - export BLUEPRINTS_DIR + export BLUEPRINTS_DIR=`mktemp -d '/tmp/composer-blueprints.XXXXX'` cp ./tests/pylorax/blueprints/*.toml $BLUEPRINTS_DIR - SHARE_DIR=`mktemp -d '/tmp/composer-share.XXXXX'` + export SHARE_DIR=`mktemp -d '/tmp/composer-share.XXXXX'` cp -R ./share/* $SHARE_DIR chmod a+rx -R $SHARE_DIR setup_tests $SHARE_DIR $BLUEPRINTS_DIR # start the lorax-composer daemon - ./src/sbin/lorax-composer --sharedir $SHARE_DIR $BLUEPRINTS_DIR & + composer_start else export PACKAGE="composer-cli" export BLUEPRINTS_DIR="/var/lib/lorax/composer/blueprints" - systemctl stop lorax-composer + composer_stop setup_tests /usr/share/lorax /var/lib/lorax/composer/blueprints - systemctl start lorax-composer + composer_start fi -# wait for the backend to become ready -tries=0 -until curl -m 15 --unix-socket /run/weldr/api.socket http://localhost:4000/api/status | grep 'db_supported.*true'; do - tries=$((tries + 1)) - if [ $tries -gt 50 ]; then - exit 1 - fi - sleep 5 - echo "DEBUG: Waiting for backend API to become ready before testing ..." -done; - - export BEAKERLIB_JOURNAL=0 export PATH="/usr/local/bin:$PATH" if [ -z "$*" ]; then @@ -108,15 +97,14 @@ fi if [ -z "$CLI" ]; then # stop lorax-composer and remove /run/weldr/api.socket # only if running against source - pkill -9 lorax-composer - rm -f /run/weldr/api.socket + composer_stop teardown_tests $SHARE_DIR $BLUEPRINTS_DIR else - systemctl stop lorax-composer + composer_stop teardown_tests /usr/share/lorax /var/lib/lorax/composer/blueprints # start lorax-composer again so we can continue with manual or other kinds # of testing on the same system - systemctl start lorax-composer + composer_start fi . $BEAKERLIB_DIR/TestResults