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 and/or tests search paths"
17  echo "  \${TESTS_FILE} (w one line per test/path, 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
44sh_filter="(/_|run_parallel|compile|generate_coverage_report.sh|/ci\.)"
45
46if [ -n "${TESTS_FILE}" ]; then
47	#remove comments and empty lines from file
48	search_pattern=$(sed 's/#.*$//;/^$/d' "${TESTS_FILE}") || exit 1
49	all_cases=`find ${search_pattern} -name "*.sh" | grep -Ev "${sh_filter}"`
50elif [ -n "${TESTS_LIST}" ]; then
51	all_cases=${TESTS_LIST}
52else
53	SEARCH_PATH="${SEARCH_PATH:-.}"
54	all_cases=`find ${SEARCH_PATH} -name "*.sh" | grep -Ev "${sh_filter}"`
55	#we dont run ourselves
56fi
57
58set -u
59
60RESULTS_FILE="${RESULTS_FILE:-`pwd`/../RunResults.xml}"
61tmp_res_file=tmp.xml
62
63all_cases_a=( $all_cases )
64n_cases=$((${#all_cases_a[@]}))
65
66mkdir -p $(dirname ${RESULTS_FILE})
67touch ${RESULTS_FILE}
68echo "Attempting to run ${n_cases} cases (logging to \
69 `realpath ${RESULTS_FILE}`)"
70
71export CLEAN_XML="sed -E -e 's/&/\&amp;/g' -e 's/</\&lt;/g' -e 's/>/\&gt;/g' \
72                  -e 's/\"/&quot;/g' -e $'s/\x1b\[[0-9;]*[a-zA-Z]//g'"
73
74echo -n "" > $tmp_res_file
75
76if [ `command -v parallel` ]; then
77  if [ ${n_cases} -gt 0 ]; then
78    parallel '
79    echo "<testcase name=\"{}\" time=\"0\">"
80    start=$(date +%s%N)
81    {} $@ &> {#}.log ; result=$?
82    dur=$(($(date +%s%N) - $start))
83    dur_s=$(awk -vdur=$dur "BEGIN { printf(\"%0.3f\", dur/1000000000)}")
84    if [ $result -ne 0 ]; then
85      (>&2 echo -e "\e[91m{} FAILED\e[39m ($dur_s s)")
86      (>&2 cat {#}.log)
87      echo "<failure message=\"failed\" type=\"failure\">"
88      cat {#}.log | eval $CLEAN_XML
89      echo "</failure>"
90      rm {#}.log
91      echo "</testcase>"
92      exit 1
93    else
94      (>&2 echo -e "{} PASSED ($dur_s s)")
95      rm {#}.log
96      echo "</testcase>"
97    fi
98    ' ::: $all_cases >> $tmp_res_file ; err=$?
99  fi
100else #fallback in case parallel is not installed
101  for case in $all_cases; do
102    echo "<testcase name=\"$case\" time=\"0\">" >> $tmp_res_file
103    $case $@ &> $i.log
104    if [ $? -ne 0 ]; then
105      echo -e "\e[91m$case FAILED\e[39m"
106      cat $i.log
107      echo "<failure message=\"failed\" type=\"failure\">" >> $tmp_res_file
108      cat $i.log | eval $CLEAN_XML >> $tmp_res_file
109      echo "</failure>" >> $tmp_res_file
110      let "err++"
111    else
112      echo -e "$case PASSED"
113    fi
114    echo "</testcase>" >> $tmp_res_file
115    rm $i.log
116    let i=i+1
117  done
118fi
119echo -e "</testsuite>\n</testsuites>\n" >> $tmp_res_file
120dur=$(($SECONDS - $start))
121echo -e "<testsuites>\n<testsuite errors=\"0\" failures=\"$err\"\
122 name=\"bsim tests\" skip=\"0\" tests=\"$n_cases\" time=\"$dur\">" \
123 | cat - $tmp_res_file > $RESULTS_FILE
124rm $tmp_res_file
125
126exit $err
127