testsupport.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #!/usr/bin/env bash
  2. # Licensed to the Apache Software Foundation (ASF) under one or more
  3. # contributor license agreements. See the NOTICE file distributed with
  4. # this work for additional information regarding copyright ownership.
  5. # The ASF licenses this file to You under the Apache License, Version 2.0
  6. # (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. testresourcesdir=src/test/resources
  17. combconfsdir=$testresourcesdir/combinationConfigFiles
  18. combtestfile=$testresourcesdir/abfs-combination-test-configs.xml
  19. logdir=dev-support/testlogs
  20. testresultsregex="Results:(\n|.)*?Tests run:"
  21. testresultsfilename=
  22. starttime=
  23. threadcount=
  24. defaultthreadcount=8
  25. properties=
  26. values=
  27. validate() {
  28. if [ -z "$threadcount" ] ; then
  29. threadcount=$defaultthreadcount
  30. fi
  31. numberegex='^[0-9]+$'
  32. if ! [[ $threadcount =~ $numberegex ]] ; then
  33. echo "Exiting. The script param (threadcount) should be a number"
  34. exit -1
  35. fi
  36. if [ -z "$combination" ]; then
  37. echo "Exiting. combination cannot be empty"
  38. exit -1
  39. fi
  40. propertiessize=${#properties[@]}
  41. valuessize=${#values[@]}
  42. if [ "$propertiessize" -lt 1 ] || [ "$valuessize" -lt 1 ] || [ "$propertiessize" -ne "$valuessize" ]; then
  43. echo "Exiting. Both properties and values arrays has to be populated and of same size. Please check for combination $combination"
  44. exit -1
  45. fi
  46. for filename in "${combinations[@]}"; do
  47. if [[ ! -f "$combconfsdir/$filename.xml" ]]; then
  48. echo "Exiting. Combination config file ($combconfsdir/$combination.xml) does not exist."
  49. exit -1
  50. fi
  51. done
  52. }
  53. checkdependencies() {
  54. if ! [ "$(command -v pcregrep)" ]; then
  55. echo "Exiting. pcregrep is required to run the script."
  56. exit -1
  57. fi
  58. if ! [ "$(command -v xmlstarlet)" ]; then
  59. echo "Exiting. xmlstarlet is required to run the script."
  60. exit -1
  61. fi
  62. }
  63. cleancombinationconfigs() {
  64. rm -rf $combconfsdir
  65. mkdir -p $combconfsdir
  66. }
  67. generateconfigs() {
  68. combconffile="$combconfsdir/$combination.xml"
  69. rm -rf "$combconffile"
  70. cat > "$combconffile" << ENDOFFILE
  71. <configuration>
  72. </configuration>
  73. ENDOFFILE
  74. propertiessize=${#properties[@]}
  75. valuessize=${#values[@]}
  76. if [ "$propertiessize" -ne "$valuessize" ]; then
  77. echo "Exiting. Number of properties and values differ for $combination"
  78. exit -1
  79. fi
  80. for ((i = 0; i < propertiessize; i++)); do
  81. key=${properties[$i]}
  82. val=${values[$i]}
  83. changeconf "$key" "$val"
  84. done
  85. formatxml "$combconffile"
  86. }
  87. formatxml() {
  88. xmlstarlet fo -s 2 "$1" > "$1.tmp"
  89. mv "$1.tmp" "$1"
  90. }
  91. setactiveconf() {
  92. if [[ ! -f "$combconfsdir/$combination.xml" ]]; then
  93. echo "Exiting. Combination config file ($combconfsdir/$combination.xml) does not exist."
  94. exit -1
  95. fi
  96. rm -rf $combtestfile
  97. cat > $combtestfile << ENDOFFILE
  98. <configuration>
  99. </configuration>
  100. ENDOFFILE
  101. xmlstarlet ed -P -L -s /configuration -t elem -n include -v "" $combtestfile
  102. xmlstarlet ed -P -L -i /configuration/include -t attr -n href -v "combinationConfigFiles/$combination.xml" $combtestfile
  103. xmlstarlet ed -P -L -i /configuration/include -t attr -n xmlns -v "http://www.w3.org/2001/XInclude" $combtestfile
  104. formatxml $combtestfile
  105. }
  106. changeconf() {
  107. xmlstarlet ed -P -L -d "/configuration/property[name='$1']" "$combconffile"
  108. xmlstarlet ed -P -L -s /configuration -t elem -n propertyTMP -v "" -s /configuration/propertyTMP -t elem -n name -v "$1" -r /configuration/propertyTMP -v property "$combconffile"
  109. if ! xmlstarlet ed -P -L -s "/configuration/property[name='$1']" -t elem -n value -v "$2" "$combconffile"
  110. then
  111. echo "Exiting. Changing config property failed."
  112. exit -1
  113. fi
  114. }
  115. summary() {
  116. {
  117. echo ""
  118. echo "$combination"
  119. echo "========================"
  120. pcregrep -M "$testresultsregex" "$testlogfilename"
  121. } >> "$testresultsfilename"
  122. printf "\n----- Test results -----\n"
  123. pcregrep -M "$testresultsregex" "$testlogfilename"
  124. secondstaken=$((ENDTIME - STARTTIME))
  125. mins=$((secondstaken / 60))
  126. secs=$((secondstaken % 60))
  127. printf "\nTime taken: %s mins %s secs.\n" "$mins" "$secs"
  128. echo "Find test logs for the combination ($combination) in: $testlogfilename"
  129. echo "Find consolidated test results in: $testresultsfilename"
  130. echo "----------"
  131. }
  132. init() {
  133. checkdependencies
  134. if ! mvn clean install -DskipTests
  135. then
  136. echo ""
  137. echo "Exiting. Build failed."
  138. exit -1
  139. fi
  140. starttime=$(date +"%Y-%m-%d_%H-%M-%S")
  141. mkdir -p "$logdir"
  142. testresultsfilename="$logdir/$starttime/Test-Results.txt"
  143. if [[ -z "$combinations" ]]; then
  144. combinations=( $( ls $combconfsdir/*.xml ))
  145. fi
  146. }
  147. runtests() {
  148. parseoptions "$@"
  149. validate
  150. if [ -z "$starttime" ]; then
  151. init
  152. fi
  153. shopt -s nullglob
  154. for combconffile in "${combinations[@]}"; do
  155. STARTTIME=$(date +%s)
  156. combination=$(basename "$combconffile" .xml)
  157. mkdir -p "$logdir/$starttime"
  158. testlogfilename="$logdir/$starttime/Test-Logs-$combination.txt"
  159. printf "\nRunning the combination: %s..." "$combination"
  160. setactiveconf
  161. mvn -T 1C -Dparallel-tests=abfs -Dscale -DtestsThreadCount=$threadcount verify >> "$testlogfilename" || true
  162. ENDTIME=$(date +%s)
  163. summary
  164. done
  165. }
  166. begin() {
  167. cleancombinationconfigs
  168. }
  169. parseoptions() {
  170. runactivate=0
  171. runtests=0
  172. while getopts ":c:a:t:" option; do
  173. case "${option}" in
  174. a)
  175. if [[ "$runactivate" -eq "1" ]]; then
  176. echo "-a Option is not multivalued"
  177. exit 1
  178. fi
  179. runactivate=1
  180. combination=$(basename "$OPTARG" .xml)
  181. ;;
  182. c)
  183. runtests=1
  184. combination=$(basename "$OPTARG" .xml)
  185. combinations+=("$combination")
  186. ;;
  187. t)
  188. threadcount=$OPTARG
  189. ;;
  190. *|?|h)
  191. if [[ -z "$combinations" ]]; then
  192. combinations=( $( ls $combconfsdir/*.xml ))
  193. fi
  194. combstr=""
  195. for combconffile in "${combinations[@]}"; do
  196. combname=$(basename "$combconffile" .xml)
  197. combstr="${combname}, ${combstr}"
  198. done
  199. combstr=${combstr:0:-2}
  200. echo "Usage: $0 [-n] [-a COMBINATION_NAME] [-c COMBINATION_NAME] [-t THREAD_COUNT]"
  201. echo ""
  202. echo "Where:"
  203. echo " -a COMBINATION_NAME Specify the combination name which needs to be activated."
  204. echo " Configured combinations: ${combstr}"
  205. echo " -c COMBINATION_NAME Specify the combination name for test runs"
  206. echo " -t THREAD_COUNT Specify the thread count"
  207. exit 1
  208. ;;
  209. esac
  210. done
  211. if [[ "$runactivate" -eq "1" && "$runtests" -eq "1" ]]; then
  212. echo "Both activate (-a option) and test run combinations (-c option) cannot be specified together"
  213. exit 1
  214. fi
  215. if [[ "$runactivate" -eq "1" ]]; then
  216. setactiveconf
  217. exit 0
  218. fi
  219. }