Fuel testing
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 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #!/bin/bash
  2. # Copyright 2015 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-ostf test suite(s)"
  19. echo ""
  20. echo " -p, --flake8 Run FLAKE8 checks"
  21. echo " -P, --no-flake8 Don't run FLAKE8 checks"
  22. echo " -u, --unit Run unit tests"
  23. echo " -U, --no-unit Don't run unit tests"
  24. echo " -i, --integration Run integarion tests"
  25. echo " -I, --no-integration Don't run inteagration tests"
  26. echo " -t, --tests Run a given test files"
  27. echo " -h, --help Print this usage message"
  28. echo " -c, --with-cover Run tests with coverage"
  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. -p|--flake8) flake8_checks=1;;
  39. -P|--no-flake8) no_flake8_checks=1;;
  40. -u|--unit) unit_tests=1;;
  41. -U|--no-unit) no_unit_tests=1;;
  42. -i|--integration) integration_tests=1;;
  43. -I|--no-integration) no_integration_tests=1;;
  44. -t|--tests) certain_tests=1;;
  45. -c|--with-cover) coverage=1;;
  46. -*) testropts="$testropts $arg";;
  47. *) testrargs="$testrargs $arg"
  48. esac
  49. done
  50. }
  51. # settings
  52. ROOT=$(dirname `readlink -f $0`)
  53. # test options
  54. testrargs=
  55. testropts="--with-timer --timer-warning=10 --timer-ok=2 --timer-top-n=10"
  56. # customizable options
  57. ARTIFACTS=${ARTIFACTS:-`pwd`/test_run}
  58. INTEGRATION_XUNIT=${INTEGRATION_XUNIT:-"$ROOT/integration.xml"}
  59. OSTF_SERVER_PORT=${OSRF_SERVER_PORT:-8777}
  60. UNIT_XUNIT=${UNIT_XUNIT:-"$ROOT/unittests.xml"}
  61. mkdir -p $ARTIFACTS
  62. # disabled/enabled flags that are setted from the cli.
  63. # used for manipulating run logic.
  64. flake8_checks=0
  65. no_flake8_checks=0
  66. unit_tests=0
  67. no_unit_tests=0
  68. integration_tests=0
  69. no_integration_tests=0
  70. certain_tests=0
  71. coverage=0
  72. function run_tests {
  73. run_cleanup
  74. # This variable collects all failed tests. It'll be printed in
  75. # the end of this function as a small statistic for user.
  76. local errors=""
  77. # If tests was specified in command line then run only these tests
  78. if [[ $certain_tests -eq 1 ]]; then
  79. local result=0
  80. for testfile in $testrargs; do
  81. local testfile=`readlink -f $testfile`
  82. local tf=`echo $testfile | cut -d':' -f1`
  83. if [ ! -e $tf ]; then
  84. echo "ERROR: File or directory $tf not found"
  85. exit 1
  86. fi
  87. guess_test_run $testfile || result=1
  88. done
  89. exit $result
  90. fi
  91. # Enable all tests if none was specified skipping all explicitly disabled tests.
  92. if [[ $flake8_checks -eq 0 && \
  93. $integration_tests -eq 0 && \
  94. $unit_tests -eq 0 ]]; then
  95. if [[ $no_flake8_checks -ne 1 ]]; then flake8_checks=1; fi
  96. if [[ $no_unit_tests -ne 1 ]]; then unit_tests=1; fi
  97. if [[ $no_integration_tests -ne 1 ]]; then integration_tests=1; fi
  98. fi
  99. # Run all enabled tests
  100. if [[ $flake8_checks -eq 1 ]]; then
  101. run_flake8 || errors+=" flake8_checks"
  102. fi
  103. if [[ $unit_tests -eq 1 ]]; then
  104. run_unit_tests || errors+=" unit_tests"
  105. fi
  106. if [[ $integration_tests -eq 1 ]]; then
  107. run_integration_tests || errors+=" integration_tests"
  108. fi
  109. # print failed tests
  110. if [[ -n "$errors" ]]; then
  111. echo Failed tests: $errors
  112. exit 1
  113. fi
  114. exit 0
  115. }
  116. function guess_test_run {
  117. local errors=""
  118. if [[ $1 == *integration* ]]; then
  119. run_integration_tests $1 || errors=$1
  120. else
  121. run_unit_tests $1 || errors=$1
  122. fi
  123. if [[ -n "${errors}" ]]; then
  124. echo "ERROR: ${errors}"
  125. return 1
  126. fi
  127. }
  128. # Remove temporary files. No need to run manually, since it's
  129. # called automatically in `run_tests` function.
  130. function run_cleanup {
  131. find . -type f -name "*.pyc" -delete
  132. rm -f *.log
  133. rm -f *.pid
  134. }
  135. function run_flake8 {
  136. echo "Starting flake8 checks"
  137. local result=0
  138. tox -e pep8 || result=1
  139. return $result
  140. }
  141. function run_unit_tests {
  142. echo "Starting unit tests"
  143. if [[ $coverage -eq 1 ]]; then
  144. testropts="$testropts --with-coverage --cover-package fuel_plugin"
  145. fi
  146. local TESTS="$ROOT/fuel_plugin/testing/tests/unit"
  147. local options="-vv $testropts --xunit-file $UNIT_XUNIT"
  148. local result=0
  149. if [[ $# -ne 0 ]]; then
  150. TESTS=$@
  151. fi
  152. # run tests
  153. tox -epy26 -- $options $TESTS || result=1
  154. return $result
  155. }
  156. function create_ostf_conf {
  157. local config_path=$1
  158. local artifacts_path=$2
  159. local SERVER_PORT=${3:-$OSTF_SERVER_PORT}
  160. cat > $config_path <<EOL
  161. [adapter]
  162. server_port = $SERVER_PORT
  163. dbpath = postgresql+psycopg2://ostf:ostf@localhost/ostf
  164. log_file = $artifacts_path/ostf.log
  165. EOL
  166. }
  167. function syncdb {
  168. local SERVER_SETTINGS=$1
  169. local RUN_SYNCDB="\
  170. ostf-server \
  171. --debug
  172. --after-initialization-environment-hook
  173. --config-file $SERVER_SETTINGS"
  174. tox -evenv -- $RUN_SYNCDB > /dev/null
  175. }
  176. function cleardb {
  177. local SERVER_SETTINGS=$1
  178. local RUN_CLEARDB="\
  179. ostf-server \
  180. --debug
  181. --clear-db
  182. --config-file $SERVER_SETTINGS"
  183. tox -evenv -- $RUN_CLEARDB > /dev/null
  184. }
  185. function run_integration_tests {
  186. echo "Starting integration tests"
  187. if [[ $coverage -eq 1 ]]; then
  188. testropts="$testropts --with-coverage --cover-package fuel_plugin"
  189. fi
  190. local TESTS="$ROOT/fuel_plugin/testing/tests/integration"
  191. local options="-vv $testropts --xunit-file $INTEGRATION_XUNIT"
  192. local result=0
  193. local artifacts=$ARTIFACTS/integration
  194. local config=$artifacts/ostf.conf
  195. mkdir -p $artifacts
  196. if [[ $# -ne 0 ]]; then
  197. TESTS=$@
  198. fi
  199. create_ostf_conf $config $artifacts
  200. cleardb $config
  201. syncdb $config
  202. # run tests
  203. tox -epy26 -- $options $TESTS || result=1
  204. return $result
  205. }
  206. # parse command line arguments and run the tests
  207. process_options $@
  208. run_tests