An OpenStack Compute driver for LXD
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.8KB

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