Shared filesystem management project for OpenStack.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

run_tests.sh 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #!/bin/bash
  2. set -eu
  3. function usage {
  4. echo "Usage: $0 [OPTION]..."
  5. echo "Run Manila's test suite(s)"
  6. echo ""
  7. echo " -V, --virtual-env Always use virtualenv. Install automatically if not present"
  8. echo " -N, --no-virtual-env Don't use virtualenv. Run tests in local environment"
  9. echo " -s, --no-site-packages Isolate the virtualenv from the global Python environment"
  10. echo " -r, --recreate-db Recreate the test database (deprecated, as this is now the default)."
  11. echo " -n, --no-recreate-db Don't recreate the test database."
  12. echo " -f, --force Force a clean re-build of the virtual environment. Useful when dependencies have been added."
  13. echo " -u, --update Update the virtual environment with any newer package versions"
  14. echo " -p, --pep8 Just run PEP8 and HACKING compliance check"
  15. echo " -P, --no-pep8 Don't run static code checks"
  16. echo " -c, --coverage Generate coverage report"
  17. echo " -d, --debug Run tests with testtools instead of testr. This allows you to use the debugger."
  18. echo " -h, --help Print this usage message"
  19. echo " --hide-elapsed Don't print the elapsed time for each test along with slow test list"
  20. echo " --virtual-env-path <path> Location of the virtualenv directory."
  21. echo " Default: \$(pwd)"
  22. echo " --virtual-env-name <name> Name of the virtualenv directory."
  23. echo " Default: .venv"
  24. echo " --tools-path <dir> Location of the tools directory."
  25. echo " Default: \$(pwd)"
  26. echo " --concurrency <concurrency> How many processes to use when running the tests."
  27. echo " A value of 0 autodetects concurrency from your CPU count."
  28. echo " Default: 1"
  29. echo ""
  30. echo "Note: with no options specified, the script will try to run the tests in a virtual environment,"
  31. echo " If no virtualenv is found, the script will ask if you would like to create one. If you "
  32. echo " prefer to run tests NOT in a virtual environment, simply pass the -N option."
  33. exit
  34. }
  35. function process_options {
  36. i=1
  37. while [ $i -le $# ]; do
  38. case "${!i}" in
  39. -h|--help) usage;;
  40. -V|--virtual-env) always_venv=1; never_venv=0;;
  41. -N|--no-virtual-env) always_venv=0; never_venv=1;;
  42. -s|--no-site-packages) no_site_packages=1;;
  43. -r|--recreate-db) recreate_db=1;;
  44. -n|--no-recreate-db) recreate_db=0;;
  45. -f|--force) force=1;;
  46. -u|--update) update=1;;
  47. -p|--pep8) just_pep8=1;;
  48. -P|--no-pep8) no_pep8=1;;
  49. -c|--coverage) coverage=1;;
  50. -d|--debug) debug=1;;
  51. --virtual-env-path)
  52. (( i++ ))
  53. venv_path=${!i}
  54. ;;
  55. --virtual-env-name)
  56. (( i++ ))
  57. venv_dir=${!i}
  58. ;;
  59. --tools-path)
  60. (( i++ ))
  61. tools_path=${!i}
  62. ;;
  63. --concurrency)
  64. (( i++ ))
  65. concurrency=${!i}
  66. ;;
  67. -*) testropts="$testropts ${!i}";;
  68. *) testrargs="$testrargs ${!i}"
  69. esac
  70. (( i++ ))
  71. done
  72. }
  73. tool_path=${tools_path:-$(pwd)}
  74. venv_path=${venv_path:-$(pwd)}
  75. venv_dir=${venv_name:-.venv}
  76. with_venv=tools/with_venv.sh
  77. always_venv=0
  78. never_venv=0
  79. force=0
  80. no_site_packages=0
  81. installvenvopts=
  82. testrargs=
  83. testropts=
  84. wrapper=""
  85. just_pep8=0
  86. no_pep8=0
  87. coverage=0
  88. debug=0
  89. recreate_db=1
  90. update=0
  91. concurrency=1
  92. process_options $@
  93. # Make our paths available to other scripts we call
  94. export venv_path
  95. export venv_dir
  96. export venv_name
  97. export tools_dir
  98. export venv=${venv_path}/${venv_dir}
  99. if [ $no_site_packages -eq 1 ]; then
  100. installvenvopts="--no-site-packages"
  101. fi
  102. function init_testr {
  103. if [ ! -d .testrepository ]; then
  104. ${wrapper} testr init
  105. fi
  106. }
  107. function run_tests {
  108. # Cleanup *pyc
  109. ${wrapper} find . -type f -name "*.pyc" -delete
  110. if [ $debug -eq 1 ]; then
  111. if [ "$testropts" = "" ] && [ "$testrargs" = "" ]; then
  112. # Default to running all tests if specific test is not
  113. # provided.
  114. testrargs="discover ./manila/tests"
  115. fi
  116. ${wrapper} python -m testtools.run $testropts $testrargs
  117. # Short circuit because all of the testr and coverage stuff
  118. # below does not make sense when running testtools.run for
  119. # debugging purposes.
  120. return $?
  121. fi
  122. if [ $coverage -eq 1 ]; then
  123. TESTRTESTS="$TESTRTESTS --coverage"
  124. else
  125. TESTRTESTS="$TESTRTESTS"
  126. fi
  127. # Just run the test suites in current environment
  128. set +e
  129. testrargs=`echo "$testrargs" | sed -e's/^\s*\(.*\)\s*$/\1/'`
  130. TESTRTESTS="$TESTRTESTS --testr-args='--subunit --concurrency $concurrency $testropts $testrargs'"
  131. if [ setup.cfg -nt manila.egg-info/entry_points.txt ]; then
  132. ${wrapper} python setup.py egg_info
  133. fi
  134. echo "Running \`${wrapper} $TESTRTESTS\`"
  135. if ${wrapper} which subunit-2to1 2>&1 > /dev/null; then
  136. # subunit-2to1 is present, testr subunit stream should be in version 2
  137. # format. Convert to version one before colorizing.
  138. bash -c "${wrapper} $TESTRTESTS | ${wrapper} subunit-2to1 | ${wrapper} tools/colorizer.py"
  139. else
  140. bash -c "${wrapper} $TESTRTESTS | ${wrapper} tools/colorizer.py"
  141. fi
  142. RESULT=$?
  143. set -e
  144. copy_subunit_log
  145. if [ $coverage -eq 1 ]; then
  146. echo "Generating coverage report in covhtml/"
  147. # Don't compute coverage for common code, which is tested elsewhere
  148. ${wrapper} coverage combine
  149. ${wrapper} coverage html --include='manila/*' -d covhtml -i
  150. fi
  151. return $RESULT
  152. }
  153. function copy_subunit_log {
  154. LOGNAME=`cat .testrepository/next-stream`
  155. LOGNAME=$(($LOGNAME - 1))
  156. LOGNAME=".testrepository/${LOGNAME}"
  157. cp $LOGNAME subunit.log
  158. }
  159. function run_pep8 {
  160. echo "Running flake8..."
  161. bash -c "${wrapper} flake8"
  162. }
  163. TESTRTESTS="python setup.py testr"
  164. if [ $never_venv -eq 0 ]; then
  165. # Remove the virtual environment if --force used
  166. if [ $force -eq 1 ]; then
  167. echo "Cleaning virtualenv..."
  168. rm -rf ${venv}
  169. fi
  170. if [ $update -eq 1 ]; then
  171. echo "Updating virtualenv..."
  172. python tools/install_venv.py $installvenvopts
  173. fi
  174. if [ -e ${venv} ]; then
  175. wrapper="${with_venv}"
  176. else
  177. if [ $always_venv -eq 1 ]; then
  178. # Automatically install the virtualenv
  179. python tools/install_venv.py $installvenvopts
  180. wrapper="${with_venv}"
  181. else
  182. echo -e "No virtual environment found...create one? (Y/n) \c"
  183. read use_ve
  184. if [ "x$use_ve" = "xY" -o "x$use_ve" = "x" -o "x$use_ve" = "xy" ]; then
  185. # Install the virtualenv and run the test suite in it
  186. python tools/install_venv.py $installvenvopts
  187. wrapper=${with_venv}
  188. fi
  189. fi
  190. fi
  191. fi
  192. # Delete old coverage data from previous runs
  193. if [ $coverage -eq 1 ]; then
  194. ${wrapper} coverage erase
  195. fi
  196. if [ $just_pep8 -eq 1 ]; then
  197. run_pep8
  198. exit
  199. fi
  200. if [ $recreate_db -eq 1 ]; then
  201. rm -f tests.sqlite
  202. fi
  203. init_testr
  204. run_tests
  205. # NOTE(sirp): we only want to run pep8 when we're running the full-test suite,
  206. # not when we're running tests individually. To handle this, we need to
  207. # distinguish between options (testropts), which begin with a '-', and
  208. # arguments (testrargs).
  209. if [ -z "$testrargs" ]; then
  210. if [ $no_pep8 -eq 0 ]; then
  211. run_pep8
  212. fi
  213. fi