1#!/usr/bin/env bash
2# Copyright 2018 Oticon A/S
3# SPDX-License-Identifier: Apache-2.0
4
5start=$SECONDS
6
7function display_help(){
8  echo "run_parallel.sh [-help] [options]"
9  echo "  Execute all cases which do not start with an _ (underscore)"
10  echo "  [options] will be passed directly to the scripts"
11  echo "  The results will be saved to \${RESULTS_FILE}, by default"
12  echo "  ../RunResults.xml"
13  echo "  Testcases are searched for in \${SEARCH_PATH},"
14  echo "  which by default is the folder the script is run from"
15  echo "  You can instead also provide a space separated test list with \${TESTS_LIST}, "
16  echo "  or an input file including a list of tests \${TESTS_FILE} (w one line"
17  echo "  per test, you can comment lines with #)"
18  echo ""
19  echo "  Examples (run from \${ZEPHYR_BASE}):"
20  echo " * Run all tests found under one folder:"
21  echo "   SEARCH_PATH=tests/bsim/bluetooth/ll/conn/ tests/bsim/run_parallel.sh"
22  echo " * Run all tests found under two separate folders, matching a pattern in the first:"
23  echo "   SEARCH_PATH=\"tests/bsim/bluetooth/ll/conn/tests_scripts/*encr*  tests/bsim/net\"\
24 tests/bsim/run_parallel.sh"
25  echo " * Provide a tests list explicitly from an environment variable"
26  echo "   TESTS_LIST=\
27\"tests/bsim/bluetooth/ll/conn/tests_scripts/basic_conn_encrypted_split_privacy.sh\
28 tests/bsim/bluetooth/ll/conn/tests_scripts/basic_conn_split_low_lat.sh\
29 tests/bsim/bluetooth/ll/conn/tests_scripts/basic_conn_split.sh\" tests/bsim/run_parallel.sh"
30  echo " * Provide a tests list in a file:"
31  echo "   TESTS_FILE=my_tests.txt tests/bsim/run_parallel.sh"
32}
33
34# Parse command line
35if [ $# -ge 1 ]; then
36  if grep -Eiq "(\?|-\?|-h|help|-help|--help)" <<< $1 ; then
37    display_help
38    exit 0
39  fi
40fi
41
42err=0
43i=0
44
45if [ -n "${TESTS_FILE}" ]; then
46	#remove comments and empty lines from file
47	all_cases=$(sed 's/#.*$//;/^$/d' "${TESTS_FILE}")
48elif [ -n "${TESTS_LIST}" ]; then
49	all_cases=${TESTS_LIST}
50else
51	SEARCH_PATH="${SEARCH_PATH:-.}"
52	all_cases=`find ${SEARCH_PATH} -name "*.sh" | \
53	         grep -Ev "(/_|run_parallel|compile.sh|generate_coverage_report.sh)"`
54	#we dont run ourselves
55fi
56
57set -u
58
59RESULTS_FILE="${RESULTS_FILE:-`pwd`/../RunResults.xml}"
60tmp_res_file=tmp.xml
61
62all_cases_a=( $all_cases )
63n_cases=$((${#all_cases_a[@]}))
64touch ${RESULTS_FILE}
65echo "Attempting to run ${n_cases} cases (logging to \
66 `realpath ${RESULTS_FILE}`)"
67
68export CLEAN_XML="sed -E -e 's/&/\&amp;/g' -e 's/</\&lt;/g' -e 's/>/\&gt;/g' \
69                  -e 's/\"/&quot;/g'"
70
71echo -n "" > $tmp_res_file
72
73if [ `command -v parallel` ]; then
74  parallel '
75  echo "<testcase name=\"{}\" time=\"0\">"
76  {} $@ &> {#}.log
77  if [ $? -ne 0 ]; then
78    (>&2 echo -e "\e[91m{} FAILED\e[39m")
79    (>&2 cat {#}.log)
80    echo "<failure message=\"failed\" type=\"failure\">"
81    cat {#}.log | eval $CLEAN_XML
82    echo "</failure>"
83    rm {#}.log
84    echo "</testcase>"
85    exit 1
86  else
87    (>&2 echo -e "{} PASSED")
88    rm {#}.log
89    echo "</testcase>"
90  fi
91  ' ::: $all_cases >> $tmp_res_file ; err=$?
92else #fallback in case parallel is not installed
93  for case in $all_cases; do
94    echo "<testcase name=\"$case\" time=\"0\">" >> $tmp_res_file
95    $case $@ &> $i.log
96    if [ $? -ne 0 ]; then
97      echo -e "\e[91m$case FAILED\e[39m"
98      cat $i.log
99      echo "<failure message=\"failed\" type=\"failure\">" >> $tmp_res_file
100      cat $i.log | eval $CLEAN_XML >> $tmp_res_file
101      echo "</failure>" >> $tmp_res_file
102      let "err++"
103    else
104      echo -e "$case PASSED"
105    fi
106    echo "</testcase>" >> $tmp_res_file
107    rm $i.log
108    let i=i+1
109  done
110fi
111echo -e "</testsuite>\n</testsuites>\n" >> $tmp_res_file
112dur=$(($SECONDS - $start))
113echo -e "<testsuites>\n<testsuite errors=\"0\" failures=\"$err\"\
114 name=\"bsim tests\" skip=\"0\" tests=\"$n_cases\" time=\"$dur\">" \
115 | cat - $tmp_res_file > $RESULTS_FILE
116rm $tmp_res_file
117
118exit $err
119