ktestsctl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/bin/bash
  2. . etc/config
  3. ### version for this script
  4. VERSION='1.0.1'
  5. LOGDATE=`date`
  6. ### return non-zero code if at least one test failed
  7. TESTS_FAILED=0
  8. ktestsctl_usage() {
  9. cat <<EOF
  10. $0 [options] cmd [params]
  11. Options:
  12. -m | --mysql ..... prepare mysql database
  13. -q | --quiet ..... execution of unit tests in silent mode
  14. -w | --wait ...... wait at the end of running the unit tests
  15. ...... (for docker it requires interactive mode: docker run -i ...)
  16. Commands:
  17. -- command 'run' --
  18. run .............. run all tests
  19. run <filter> ..... run tests with a filter on unit names
  20. run <unitname> ... run the test unit
  21. -- command 'help' --
  22. help ............. print the help text
  23. -- command 'version' --
  24. version .......... print the version string
  25. Version: $0 ${VERSION}
  26. EOF
  27. }
  28. ktestsctl_mysqld_alive() {
  29. mysqladmin -h localhost ping
  30. }
  31. # prepare mysql server
  32. ktestsctl_mysql() {
  33. echo "=== unit tests - prepare mysql server ==="
  34. if [ -f /etc/redhat-release ] ; then
  35. mysqld_safe &
  36. else
  37. /usr/sbin/mysqld --user=root &
  38. fi
  39. sleep 5
  40. count=0
  41. until ( ktestsctl_mysqld_alive )
  42. do
  43. ((count++))
  44. if [ ${count} -gt 50 ]
  45. then
  46. echo "error: mysqld did not become ready in time"
  47. exit 1
  48. fi
  49. sleep 0.1
  50. done
  51. sleep 1
  52. mysqladmin -u root password "${DBROOTPW}"
  53. INSTALL_EXTRA_TABLES=yes INSTALL_PRESENCE_TABLES=yes \
  54. INSTALL_DBUID_TABLES=yes CHARSET="latin1" kamdbctl create
  55. }
  56. # execute one or more test units
  57. ktestsrun() {
  58. echo "=== unit tests execution start ==="
  59. echo "running test units at: ${LOGDATE}" >${LOGFILE}
  60. echo "" >>${LOGFILE}
  61. if [ $# -lt 1 ] ; then
  62. UNITSLIST=`find units/t* -maxdepth 1 -type d`
  63. else
  64. UNITSLIST=`find units/${1}* -maxdepth 1 -type d`
  65. fi
  66. for tdir in ${UNITSLIST} ; do
  67. if [[ ${tdir} =~ $(echo ^units\/\($(paste -sd'|' excludeunits.txt)\)$) ]]; then
  68. echo
  69. echo "Skipping ${tdir} as requested..."
  70. continue
  71. fi
  72. if [[ -d "${tdir}" && ! -L "${tdir}" ]]; then
  73. tname=`basename ${tdir}`
  74. texec="${tname}.sh"
  75. tpath="${tdir}/${texec}"
  76. if [[ -f "${tpath}" && -x "${tpath}" ]]; then
  77. echo
  78. tsummary=""
  79. if [[ -f "${tdir}/README.md" ]]; then
  80. tsummary=`grep "^Summary: " ${tdir}/README.md | cut -c10-`
  81. fi
  82. if [[ ! -z "${tsummary}" ]]; then
  83. echo "* test unit ${tname}: ${tsummary}" | tee -a ${LOGFILE}
  84. fi
  85. echo "test unit ${tname}: running ..."
  86. cd ${tdir}
  87. if [ "${UNITOUTPUT}" == "no" ]; then
  88. # save stdout and stderr, then redirect to /dev/null
  89. exec 3>&1 4>&2 >/dev/null 2>&1
  90. fi
  91. ./${texec}
  92. ret=$?
  93. if [ "${UNITOUTPUT}" == "no" ]; then
  94. # restore stdout and stderr
  95. exec 1>&3 2>&4
  96. fi
  97. cd ../..
  98. if [ ! "$ret" -eq 0 ] ; then
  99. echo "- test unit ${tname}: failed" | tee -a ${LOGFILE}
  100. TESTS_FAILED=1
  101. else
  102. echo "- test unit ${tname}: ok" | tee -a ${LOGFILE}
  103. fi
  104. fi
  105. fi
  106. done
  107. sleep 1
  108. echo
  109. echo "=== unit tests execution report ==="
  110. echo
  111. cat ${LOGFILE}
  112. echo
  113. echo "=== unit tests execution end ==="
  114. echo
  115. }
  116. #
  117. ##### ================================================ #####
  118. ### evaluate first the options
  119. CMDPARAMS=()
  120. KTESTSWAIT="no"
  121. while [[ $# -gt 0 ]]
  122. do
  123. key="$1"
  124. case $key in
  125. -m|--mysql)
  126. ktestsctl_mysql
  127. shift
  128. ;;
  129. -q|--quiet)
  130. UNITOUTPUT="no"
  131. shift
  132. ;;
  133. -w|--wait)
  134. KTESTSWAIT="yes"
  135. shift
  136. ;;
  137. *)
  138. CMDPARAMS+=("$1")
  139. shift
  140. ;;
  141. esac
  142. done
  143. set -- "${CMDPARAMS[@]}" # restore positional parameters
  144. ### main command switch
  145. #
  146. echo
  147. if [ -n "${KAMRUN}" ] ; then
  148. mkdir -p ${KAMRUN}
  149. chmod a+w ${KAMRUN}
  150. fi
  151. case $1 in
  152. run)
  153. if [ $# -lt 2 ] ; then
  154. ktestsrun
  155. else
  156. ktestsrun "${2}"
  157. fi
  158. if [ "${KTESTSWAIT}" == "yes" ]; then
  159. echo
  160. echo "=== waiting ..."
  161. echo "=== this option requires to use 'docker run -i ...''"
  162. echo "=== to exit: press any key and <enter>"
  163. read -rs -n 1 key
  164. fi
  165. exit $TESTS_FAILED
  166. ;;
  167. help)
  168. ktestsctl_usage
  169. exit 1
  170. ;;
  171. version)
  172. echo "$0 $VERSION"
  173. ;;
  174. *)
  175. ktestsctl_usage
  176. exit 1
  177. ;;
  178. esac