From 3409d0a949782e2402b57dfce37184de10f39d07 Mon Sep 17 00:00:00 2001 From: Alexander Todorov Date: Wed, 6 Nov 2019 14:24:30 +0200 Subject: [PATCH] DRY when setting up, running & parsing results for beakerlib tests Cherry-picked from b197e448ff5d8f6913c36dc7e3f835f3a589e934 Related: rhbz#1769525 --- tests/cli/lib/lib.sh | 29 +++++++++++++++++++++++++++++ tests/test_cli.sh | 27 +++++---------------------- tests/test_image.sh | 22 +++++----------------- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/tests/cli/lib/lib.sh b/tests/cli/lib/lib.sh index 80507661..379ea76c 100755 --- a/tests/cli/lib/lib.sh +++ b/tests/cli/lib/lib.sh @@ -19,6 +19,35 @@ if [ "$COMPOSER_TEST_FAIL_FAST" == "1" ]; then } fi + +setup_beakerlib_env() { + export BEAKERLIB_DIR=$(mktemp -d /tmp/composer-test.XXXXXX) + export BEAKERLIB_JOURNAL=0 +} + +run_beakerlib_tests() { + if [ -z "$*" ]; then + echo "run_beakerlib_tests() requires a test to execute" + else + # execute tests + for TEST in "$@"; do + $TEST + done + fi +} + +parse_beakerlib_results() { + . $BEAKERLIB_DIR/TestResults + + TESTRESULT_RESULT_ECODE="${TESTRESULT_RESULT_ECODE:-}" + if [ $TESTRESULT_RESULT_ECODE != 0 ]; then + echo "Test failed. Leaving log in $BEAKERLIB_DIR" + exit $TESTRESULT_RESULT_ECODE + fi + + rm -rf $BEAKERLIB_DIR +} + export QEMU_BIN="/usr/libexec/qemu-kvm" export QEMU="$QEMU_BIN -machine accel=kvm:tcg" export SSH_PORT=2222 diff --git a/tests/test_cli.sh b/tests/test_cli.sh index adf3b197..e75de4fb 100755 --- a/tests/test_cli.sh +++ b/tests/test_cli.sh @@ -1,11 +1,12 @@ #!/bin/bash # Note: execute this file from the project root directory +# Note: Use test/check-cli && test/check-cloud if you want to +# execute test scenarios by hand! set -eu . $(dirname $0)/cli/lib/lib.sh -export BEAKERLIB_DIR=$(mktemp -d /tmp/composer-test.XXXXXX) CLI="${CLI:-}" function setup_tests { @@ -105,20 +106,9 @@ if [ -e "/var/tmp/test-results" ]; then rm -rf "/var/tmp/test-results" fi -export BEAKERLIB_JOURNAL=0 -export PATH="/usr/local/bin:$PATH" -if [ -z "$*" ]; then - # invoke cli/ tests which can be executed without special preparation - ./tests/cli/test_blueprints_sanity.sh - ./tests/cli/test_compose_sanity.sh -else - # execute other cli tests which need more adjustments in the calling environment - # or can't be executed inside Travis CI - for TEST in "$@"; do - $TEST - done -fi +setup_beakerlib_env +run_beakerlib_tests "$@" if [ -z "$CLI" ]; then # stop lorax-composer and remove /run/weldr/api.socket @@ -133,11 +123,4 @@ else composer_start fi -. $BEAKERLIB_DIR/TestResults - -if [ $TESTRESULT_RESULT_ECODE != 0 ]; then - echo "Test failed. Leaving log in $BEAKERLIB_DIR" - exit $TESTRESULT_RESULT_ECODE -fi - -rm -rf $BEAKERLIB_DIR +parse_beakerlib_results diff --git a/tests/test_image.sh b/tests/test_image.sh index 7d1be48a..edd39e81 100755 --- a/tests/test_image.sh +++ b/tests/test_image.sh @@ -1,26 +1,14 @@ #!/bin/bash # Note: execute this file from the project root directory +# Used for running a beakerlib test script inside a running VM +# without setting up composer first! set -eu . $(dirname $0)/cli/lib/lib.sh -export BEAKERLIB_DIR=$(mktemp -d /tmp/composer-test.XXXXXX) -export BEAKERLIB_JOURNAL=0 -if [ -z "$*" ]; then - echo "test_image.sh requires a test to execute" -else - # execute tests - for TEST in "$@"; do - $TEST - done -fi +setup_beakerlib_env -. $BEAKERLIB_DIR/TestResults +run_beakerlib_tests "$@" -if [ $TESTRESULT_RESULT_ECODE != 0 ]; then - echo "Test failed. Leaving log in $BEAKERLIB_DIR" - exit $TESTRESULT_RESULT_ECODE -fi - -rm -rf $BEAKERLIB_DIR +parse_beakerlib_results