Fuel UI
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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #!/bin/bash
  2. # Copyright 2013 Mirantis, Inc.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. set -eu
  16. function usage {
  17. echo "Usage: $0 [OPTION]..."
  18. echo "Run Fuel-Web test suite(s)"
  19. echo ""
  20. echo " -h, --help Print this usage message"
  21. echo " -n, --nailgun Run NAILGUN unit/integration tests"
  22. echo " -N, --no-nailgun Don't run NAILGUN unit/integration tests"
  23. echo " -x, --performance Run NAILGUN performance tests"
  24. echo " -p, --flake8 Run FLAKE8 and HACKING compliance check"
  25. echo " -P, --no-flake8 Don't run static code checks"
  26. echo " -t, --tests Run a given test files"
  27. echo " -e, --extensions Run EXTENSIONS unit/integration tests"
  28. echo " -E, --no-extensions Don't run EXTENSIONS unit/integration tests"
  29. echo ""
  30. echo "Note: with no options specified, the script will try to run all available"
  31. echo " tests with all available checks."
  32. exit
  33. }
  34. function process_options {
  35. for arg in $@; do
  36. case "$arg" in
  37. -h|--help) usage;;
  38. -n|--nailgun) nailgun_tests=1;;
  39. -N|--no-nailgun) no_nailgun_tests=1;;
  40. -x|--performance) performance_tests=1;;
  41. -p|--flake8) flake8_checks=1;;
  42. -P|--no-flake8) no_flake8_checks=1;;
  43. -e|--extensions) extensions_tests=1;;
  44. -E|--no-extensions) no_extensions_tests=1;;
  45. -t|--tests) certain_tests=1;;
  46. -*) testropts="$testropts $arg";;
  47. *) testrargs="$testrargs $arg"
  48. esac
  49. done
  50. }
  51. # settings
  52. ROOT=$(dirname `readlink -f $0`)
  53. NAILGUN_ROOT=$ROOT/nailgun
  54. TESTRTESTS="nosetests"
  55. FLAKE8="flake8"
  56. PEP8="pep8"
  57. TOXENV=${TOXENV:-py27}
  58. # test options
  59. testrargs=
  60. testropts="--with-timer --timer-warning=10 --timer-ok=2 --timer-top-n=10"
  61. # nosetest xunit options
  62. NAILGUN_XUNIT=${NAILGUN_XUNIT:-"$ROOT/nailgun.xml"}
  63. EXTENSIONS_XUNIT=${EXTENSIONS_XUNIT:-"$ROOT/extensions.xml"}
  64. TEST_NAILGUN_DB=${TEST_NAILGUN_DB:-nailgun}
  65. ARTIFACTS=${ARTIFACTS:-`pwd`/test_run}
  66. TEST_WORKERS=${TEST_WORKERS:-0}
  67. mkdir -p $ARTIFACTS
  68. # disabled/enabled flags that are set from the cli.
  69. # used for manipulating run logic.
  70. nailgun_tests=0
  71. no_nailgun_tests=0
  72. performance_tests=0
  73. flake8_checks=0
  74. no_flake8_checks=0
  75. extensions_tests=0
  76. no_extensions_tests=0
  77. certain_tests=0
  78. function run_tests {
  79. run_cleanup
  80. # This variable collects all failed tests. It'll be printed in
  81. # the end of this function as a small statistic for user.
  82. local errors=""
  83. # If tests was specified in command line then run only these tests
  84. if [ $certain_tests -eq 1 ]; then
  85. for testfile in $testrargs; do
  86. local testfile=`readlink -f $testfile`
  87. local tf=`echo $testfile | cut -d':' -f1`
  88. if [ ! -e $tf ]; then
  89. echo "ERROR: File or directory $tf not found"
  90. exit 1
  91. fi
  92. run_nailgun_tests $testfile
  93. done
  94. exit
  95. fi
  96. # Enable all tests if none was specified skipping all explicitly disabled tests.
  97. if [[ $nailgun_tests -eq 0 && \
  98. $performance_tests -eq 0 && \
  99. $extensions_tests -eq 0 && \
  100. $flake8_checks -eq 0 ]]; then
  101. if [ $no_nailgun_tests -ne 1 ]; then nailgun_tests=1; fi
  102. if [ $no_flake8_checks -ne 1 ]; then flake8_checks=1; fi
  103. if [ $no_extensions_tests -ne 1 ]; then extensions_tests=1; fi
  104. fi
  105. # Run all enabled tests
  106. if [ $flake8_checks -eq 1 ]; then
  107. echo "Starting Flake8 tests..."
  108. run_flake8 || errors+=" flake8_checks"
  109. fi
  110. if [ $nailgun_tests -eq 1 ] || [ $performance_tests -eq 1 ]; then
  111. echo "Starting Nailgun tests..."
  112. run_nailgun_tests || errors+=" nailgun_tests"
  113. fi
  114. if [ $extensions_tests -eq 1 ]; then
  115. echo "Starting Extensions tests..."
  116. run_extensions_tests || errors+=" extensions_tests"
  117. fi
  118. # print failed tests
  119. if [ -n "$errors" ]; then
  120. echo Failed tests: $errors
  121. exit 1
  122. fi
  123. exit
  124. }
  125. # Run both integration and unit Nailgun's tests.
  126. #
  127. # Arguments:
  128. #
  129. # $@ -- tests to be run; with no arguments all tests will be run
  130. #
  131. # It is supposed that we prepare database (run DBMS and create schema)
  132. # before running tests.
  133. function run_nailgun_tests {
  134. local TESTS="$ROOT/nailgun/nailgun/test"
  135. local result=0
  136. local artifacts=$ARTIFACTS/nailgun
  137. local config=$artifacts/test.yaml
  138. local options="-vv --cleandb --junit-xml $NAILGUN_XUNIT"
  139. if [ $nailgun_tests -eq 1 ] && [ $performance_tests -eq 0 ]; then
  140. options+=" -m 'not performance' "
  141. elif [ $nailgun_tests -eq 0 ] && [ $performance_tests -eq 1 ]; then
  142. options+=" -m performance "
  143. fi
  144. prepare_artifacts $artifacts $config
  145. if [ $# -ne 0 ]; then
  146. TESTS="$@"
  147. fi
  148. if [ ! $TEST_WORKERS -eq 0 ]; then
  149. options+=" -n $TEST_WORKERS"
  150. fi
  151. pushd $ROOT/nailgun >> /dev/null
  152. # # run tests
  153. TOXENV=$TOXENV \
  154. NAILGUN_CONFIG=$config \
  155. tox -- $options $TESTS || result=1
  156. popd >> /dev/null
  157. return $result
  158. }
  159. function run_flake8_subproject {
  160. local DIRECTORY=$1
  161. local result=0
  162. pushd $ROOT/$DIRECTORY >> /dev/null
  163. tox -epep8 || result=1
  164. popd >> /dev/null
  165. return $result
  166. }
  167. # Run tests for Nailgun extensions
  168. function run_extensions_tests {
  169. local EXTENSIONS_PATH="$ROOT/nailgun/nailgun/extensions/"
  170. local NAILGUN_PATH="$ROOT/nailgun/"
  171. local result=0
  172. pushd "${NAILGUN_PATH}" >> /dev/null
  173. TOXENV=$TOXENV \
  174. tox -- -vv "${EXTENSIONS_PATH}" --junit-xml $EXTENSIONS_XUNIT || result=1
  175. popd >> /dev/null
  176. return $result
  177. }
  178. # Check python code with flake8 and pep8.
  179. #
  180. # Some settings description:
  181. #
  182. # * __init__.py --- excluded because it doesn't comply with pep8 standard
  183. # * H302 --- "import only modules. does not import a module" requires to import
  184. # only modules and not functions
  185. # * H802 --- first line of git commit commentary should be less than 50 characters
  186. function run_flake8 {
  187. local result=0
  188. run_flake8_subproject nailgun && \
  189. return $result
  190. }
  191. # Remove temporary files. No need to run manually, since it's
  192. # called automatically in `run_tests` function.
  193. function run_cleanup {
  194. find . -type f -name "*.pyc" -delete
  195. rm -f *.log
  196. rm -f *.pid
  197. }
  198. function prepare_artifacts {
  199. local artifacts=$1
  200. local config=$2
  201. mkdir -p $artifacts
  202. create_settings_yaml $config $artifacts
  203. }
  204. function create_settings_yaml {
  205. local config_path=$1
  206. local artifacts_path=$2
  207. cat > $config_path <<EOL
  208. DEVELOPMENT: 1
  209. STATIC_DIR: ${artifacts_path}/static_compressed
  210. TEMPLATE_DIR: ${artifacts_path}/static_compressed
  211. DATABASE:
  212. name: ${TEST_NAILGUN_DB}
  213. engine: "postgresql"
  214. host: "localhost"
  215. port: "5432"
  216. user: "nailgun"
  217. passwd: "nailgun"
  218. API_LOG: ${artifacts_path}/api.log
  219. APP_LOG: ${artifacts_path}/app.log
  220. EOL
  221. }
  222. # parse command line arguments and run the tests
  223. process_options $@
  224. run_tests