run-ci 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #! /bin/bash
  2. #
  3. # © 2018 Ansgar Burchardt <ansgar@debian.org>
  4. # © 2020 Ivo De Decker <ivodd@debian.org>
  5. # License: GPL-2+
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. # start/end collapsible sections in GitLab's CI
  20. # Reference: https://docs.gitlab.com/ee/ci/pipelines/#custom-collapsible-sections
  21. section_start() {
  22. local name header
  23. name="${1:?}"
  24. header="${2:-}"
  25. echo -e "section_start:$(date +%s):${name}\r\e[0K\e[36;1m${header}\e[0;m"
  26. }
  27. section_end() {
  28. local name
  29. name="${1:?}"
  30. echo -e "section_end:$(date +%s):${name}\r\e[0K"
  31. }
  32. run_apt-get() {
  33. if [ "$UID" = 0 ]
  34. then
  35. apt-get $@
  36. else
  37. echo not running apt-get $@
  38. fi
  39. }
  40. # salsa ci total number of parallel jobs
  41. if [[ ! -v CI_NODE_TOTAL ]]; then
  42. export CI_NODE_TOTAL=1
  43. fi
  44. # salsa ci number for this specific job (from 1 to $CI_NODE_TOTAL)
  45. if [[ ! -v CI_NODE_INDEX ]]; then
  46. export CI_NODE_INDEX=1
  47. fi
  48. MY_CI_COUNTER=0
  49. run_this_test() {
  50. MY_MODULO=$(( $MY_CI_COUNTER % $CI_NODE_TOTAL + 1 ))
  51. echo "run_this_test: $MY_MODULO $MY_CI_COUNTER"
  52. MY_CI_COUNTER=$(( MY_CI_COUNTER + 1 ))
  53. [[ $MY_MODULO = $CI_NODE_INDEX ]]
  54. }
  55. if [ "x$1" = "x--with-coverage" ]; then
  56. RUN_COVERAGE=y
  57. PYTEST_COV_OPTIONS=" --cov-branch --cov --cov-report= "
  58. else
  59. RUN_COVERAGE=
  60. PYTEST_COV_OPTIONS=""
  61. fi
  62. export RUN_COVERAGE
  63. copy_coverage_data() {
  64. t="$1"
  65. if [ "$RUN_COVERAGE" = "y" ]; then
  66. TESTNAME=${t##*/}
  67. DESTDIR=${DAK_CI_OUTPUT_DIR}/coveragedata/data_${TESTNAME}
  68. echo copy coverage data for $TESTNAME to $DESTDIR
  69. mkdir -p $DESTDIR
  70. [ -e .coverage ] && cp .coverage $DESTDIR/.coverage.data.${TESTNAME/}
  71. cp .coverage.* $DESTDIR || true
  72. fi
  73. }
  74. if [ "x$1" = "x--python3" ]; then
  75. DAK_PYTHON3=y
  76. PYTEST=py.test-3
  77. COVERAGE_CMD=python3-coverage
  78. # always run coverage when running python3
  79. RUN_COVERAGE=y
  80. PYTEST_COV_OPTIONS=" --cov-branch --cov --cov-report= "
  81. else
  82. DAK_PYTHON3=
  83. PYTEST=py.test
  84. COVERAGE_CMD=python-coverage
  85. fi
  86. export DAK_PYTHON3
  87. set -eu
  88. if [ ! -f dak/dak.py ]; then
  89. echo >&2 "E: run-ci must be invoked in the root directory of dak"
  90. exit 1
  91. fi
  92. export BASEDIR="$(cd $(dirname "${BASH_SOURCE}")/..; pwd)"
  93. export DAK_ROOT=$BASEDIR
  94. export DAK_CI_OUTPUT_DIR=${BASEDIR}/output
  95. mkdir -p ${DAK_CI_OUTPUT_DIR}
  96. LOGFILE=${DAK_CI_OUTPUT_DIR}/log_job${CI_NODE_INDEX}.txt
  97. section_start install_dep "Install Dependencies"
  98. echo `date` installing packages | tee -a $LOGFILE
  99. run_apt-get update
  100. run_apt-get install -y build-essential
  101. run_apt-get build-dep -y .
  102. python_deps=(
  103. python3-all-dev
  104. python3-apt
  105. python3-coverage
  106. python3-debian
  107. python3-debianbts
  108. python3-ldap
  109. python3-psycopg2
  110. python3-pytest
  111. python3-pytest-cov
  112. python3-rrdtool
  113. python3-six
  114. python3-sqlalchemy
  115. python3-tabulate
  116. python3-yaml
  117. )
  118. if [ "x$DAK_PYTHON3" != "x" ]; then
  119. run_apt-get install -y --no-install-recommends \
  120. 2to3 ${python_deps[@]}
  121. else
  122. run_apt-get install -y --no-install-recommends \
  123. ${python_deps[@]/#python3-/python-}
  124. fi
  125. section_end install_dep
  126. mkdir -p $DAK_ROOT/test-gnupghome
  127. export GNUPGHOME=${DAK_ROOT}/test-gnupghome
  128. cd ${DAK_ROOT}
  129. section_start unit_tests "Unit Tests"
  130. if run_this_test; then
  131. echo `date` running unit tests | tee -a $LOGFILE
  132. $PYTEST -v ${PYTEST_COV_OPTIONS} daklib tests
  133. copy_coverage_data "unit_tests"
  134. echo `date` unit tests done | tee -a $LOGFILE
  135. else
  136. echo "not running unit tests in this job ($CI_NODE_INDEX/$CI_NODE_TOTAL)"
  137. fi
  138. section_end unit_tests
  139. section_start fixtures "Creating Fixtures"
  140. echo `date` making fixtures | tee -a $LOGFILE
  141. make -C tests/fixtures/packages
  142. echo `date` making fixtures done | tee -a $LOGFILE
  143. section_end fixtures
  144. section_start integration_tests "Integration Tests"
  145. TESTS="${DAK_ROOT}/integration-tests/tests/[0-9]*[^~] \
  146. ${DAK_ROOT}/tests/run-dbtests"
  147. for t in $TESTS
  148. do
  149. if run_this_test; then
  150. section_start "${t}" "Running test ${t}"
  151. echo `date` running $t | tee -a $LOGFILE
  152. ./integration-tests/run-tests $t
  153. copy_coverage_data $t
  154. echo `date` running $t done | tee -a $LOGFILE
  155. section_end "${t}"
  156. else
  157. echo "not running test $t in this job ($CI_NODE_INDEX/$CI_NODE_TOTAL)"
  158. fi
  159. done
  160. section_end integration_tests
  161. if [ "$RUN_COVERAGE" = "y" ]; then
  162. section_start coverage "Coverage Report"
  163. ${COVERAGE_CMD} combine --append
  164. ${COVERAGE_CMD} report -m
  165. echo
  166. ${COVERAGE_CMD} html -d ${BASEDIR}/coverage
  167. ${COVERAGE_CMD} annotate -d ${BASEDIR}/coverage/annotated
  168. section_end coverage
  169. fi
  170. echo `date` all done | tee -a $LOGFILE