From 3ccf926e6ddf665e610d0a295d0cb0d539e9a3d3 Mon Sep 17 00:00:00 2001
From: Andrei Stepanov <astepano@redhat.com>
Date: Apr 23 2019 13:54:36 +0000
Subject: A script for running beakerlib-tests


Signed-off-by: Andrei Stepanov <astepano@redhat.com>

---

diff --git a/roles/standard-test-beakerlib/files/run-beakerlib-test b/roles/standard-test-beakerlib/files/run-beakerlib-test
new file mode 100755
index 0000000..86bd722
--- /dev/null
+++ b/roles/standard-test-beakerlib/files/run-beakerlib-test
@@ -0,0 +1,141 @@
+#!/bin/bash -efu
+
+# Called from standard-test-beakerlib to run beakerlib tests
+# This script is copied to test-environment.
+
+debug() {
+    if [ -n "$DEBUG" ]; then
+        echo "$*" >&2
+    fi
+}
+
+msg_usage() {
+    cat << EOF
+
+Run beakerlib-libraries test.
+
+Usage:
+$PROG <options>
+
+Options:
+-h, --help              display this help and exit
+-v, --verbose           turn on debug
+-w, --workdir           test environment work dir
+-a, --artifactsdir      test environment dir to store artifacts
+-t, --test              test to run, can be a path to directory or a runtest.sh file
+EOF
+}
+
+# Entry
+
+PROG="${PROG:-${0##*/}}"
+DEBUG="${DEBUG:-}"
+STR_DEBUG="${STR_DEBUG:-}"
+STR_VERBOSE="${STR_VERBOSE:-}"
+STR_BKR_TEST="${STR_BKR_TEST:-}"
+STR_WORKDIR="${STR_WORKDIR:-}"
+STR_ARTIFACTS_DIR="${STR_ARTIFACTS_DIR:-}"
+
+# http://wiki.bash-hackers.org/howto/getopts_tutorial
+opt=$(getopt -n "$0" --options "hvt:w:a:" --longoptions "help,verbose,test:,workdir:,artifactsdir:" -- "$@")
+eval set -- "$opt"
+while [[ $# -gt 0 ]]; do
+    case "$1" in
+        -t|--test)
+            STR_BKR_TEST="$2"
+            shift 2
+            ;;
+        -w|--workdir)
+            STR_WORKDIR="$2"
+            shift 2
+            ;;
+        -a|--artifactsdir)
+            STR_ARTIFACTS_DIR="$2"
+            shift 2
+            ;;
+        -v|--verbose)
+            DEBUG="-v"
+            shift
+            ;;
+        -h|--help)
+            msg_usage
+            exit 0
+            ;;
+        --)
+            shift
+            ;;
+        *)
+            msg_usage
+            exit 1
+    esac
+done
+
+# Entry
+
+if [ -z "$STR_BKR_TEST" ] || [ -z "$STR_WORKDIR" ] || [ -z "$STR_ARTIFACTS_DIR" ]; then
+    echo "Use: $PROG -h for help."
+    exit 0
+fi
+
+debug "Test: $STR_BKR_TEST"
+debug "Work dir: $STR_WORKDIR"
+debug "Artifacts dir: $STR_ARTIFACTS_DIR"
+
+# Up to this point any fail is considered as ci-sytem fail. Exit code != 0.
+# Starting from this point and bellow any fail is considered as a test fail. Exit code == 0.
+
+clean_exit() {
+    rc=$?;
+    trap - SIGINT SIGTERM SIGABRT EXIT # clear the trap
+    echo "Run test $STR_BKR_TEST: done."
+    # Exit code == 0, no matter of the test result.
+    # Close tee pipes
+    for pid in $(ps -o pid --no-headers --ppid $$); do
+        if [ -n "$(ps -p $pid -o pid=)" ]; then
+            kill -s HUP $pid
+        fi
+    done
+    exit 0
+}
+trap clean_exit SIGINT SIGTERM SIGABRT EXIT
+
+# For beakerlib-libraries
+export PATH="$PATH:$STR_WORKDIR"
+mkdir -p "$STR_ARTIFACTS_DIR"
+# OUTPUTFILE has influence on beakerlib-libraries output
+export OUTPUTFILE="$(realpath "$STR_ARTIFACTS_DIR")/$(echo "$STR_BKR_TEST" | sed -e 's/\//-/g')-out.log"
+logfile_stdout="$STR_ARTIFACTS_DIR/$(echo "$STR_BKR_TEST" | sed -e 's/\//-/g').log"
+logfile_stderr="$STR_ARTIFACTS_DIR/$(echo "$STR_BKR_TEST" | sed -e 's/\//-/g')-err.log"
+exec 3>&1 4>&2 1> >(tee -a "$logfile_stdout" >&3) 2> >(tee -a "$logfile_stderr" >&4)
+mkdir -p "$STR_WORKDIR"
+cd "$STR_WORKDIR"
+
+if ! [ -d "$STR_BKR_TEST" ] && ! [ -f "$STR_BKR_TEST" ]; then
+    # Next string goes to .log file
+    echo "FAIL test $STR_BKR_TEST does not appear to be a file or directory"
+    exit 1
+fi
+
+if [ -f "$STR_BKR_TEST" ]; then
+    debug "Running test from file: $STR_BKR_TEST"
+    cd $(dirname "$STR_BKR_TEST")
+    /bin/sh -e ./$(basename "$STR_BKR_TEST") || :
+    exit
+fi
+
+if [ -d "$STR_BKR_TEST" ]; then
+    debug "Running test from directory: $STR_BKR_TEST"
+    cd "$STR_BKR_TEST"
+    # get-test-deps - is part of beakerlib-libraries
+    get-test-deps -i .
+    if [ -f "Makefile" ] && command -p -v "make" >"/dev/null" 2>&1; then
+        debug "Running test from Makefile"
+        make run || :
+    elif [ -f "runtest.sh" ]; then
+        debug "Running test from runtest.sh"
+        /bin/sh -e ./runtest.sh || :
+    else
+        echo "FAIL test $STR_BKR_TEST do not know how to run test"
+    fi
+    exit
+fi
diff --git a/roles/standard-test-beakerlib/tasks/main.yml b/roles/standard-test-beakerlib/tasks/main.yml
index f2a4d1a..4302e53 100644
--- a/roles/standard-test-beakerlib/tasks/main.yml
+++ b/roles/standard-test-beakerlib/tasks/main.yml
@@ -52,29 +52,7 @@
 
 - block:
   - name: Run beakerlib tests
-    shell: |
-      export OUTPUTFILE=/dev/stdout TEST={{ item }}
-      export PATH="$PATH:{{ tenv_workdir }}"
-      logfile={{ remote_artifacts }}/$(echo {{ item }} | sed -e 's/\//-/g').log
-      exec 2>>$logfile 1>>$logfile
-      cd {{ tenv_workdir }}
-      if [ -f {{ item }} ]; then
-         cd $(dirname {{ item }})
-         /bin/sh -e ./$(basename {{ item }})
-      elif [ -d {{ item }} ]; then
-        cd {{ item }}
-        # get-test-deps - is part of beakerlib-libraries
-        get-test-deps -i .
-        if [ -f Makefile ] && command -p -v make >/dev/null 2>&1; then
-          make run
-        elif [ -f runtest.sh ]; then
-          /bin/sh -e ./runtest.sh
-        else
-          echo "FAIL don't know how to run test {{ item }}"
-        fi
-      else
-        echo "FAIL test {{ item }} does not appear to be a file or directory"
-      fi
+    script: run-beakerlib-test --workdir {{ tenv_workdir }} --artifactsdir {{ remote_artifacts }} --test {{ item }}
     with_items:
     - "{{ tests }}"
     - "{{ filter_tests }}"

