Glance stores library
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.0KB

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