]> git.arvados.org - arvados.git/blob - build/run-tests.sh
12199: Make operator precedence more obvious.
[arvados.git] / build / run-tests.sh
1 #!/bin/bash
2 # Copyright (C) The Arvados Authors. All rights reserved.
3 #
4 # SPDX-License-Identifier: AGPL-3.0
5
6 . `dirname "$(readlink -f "$0")"`/libcloud-pin.sh
7
8 COLUMNS=80
9 . `dirname "$(readlink -f "$0")"`/run-library.sh
10
11 read -rd "\000" helpmessage <<EOF
12 $(basename $0): Install and test Arvados components.
13
14 Exit non-zero if any tests fail.
15
16 Syntax:
17         $(basename $0) WORKSPACE=/path/to/arvados [options]
18
19 Options:
20
21 --skip FOO     Do not test the FOO component.
22 --only FOO     Do not test anything except the FOO component.
23 --temp DIR     Install components and dependencies under DIR instead of
24                making a new temporary directory. Implies --leave-temp.
25 --leave-temp   Do not remove GOPATH, virtualenv, and other temp dirs at exit.
26                Instead, show the path to give as --temp to reuse them in
27                subsequent invocations.
28 --repeat N     Repeat each install/test step until it succeeds N times.
29 --retry        Prompt to retry if an install or test suite fails.
30 --skip-install Do not run any install steps. Just run tests.
31                You should provide GOPATH, GEMHOME, and VENVDIR options
32                from a previous invocation if you use this option.
33 --only-install Run specific install step
34 --short        Skip (or scale down) some slow tests.
35 WORKSPACE=path Arvados source tree to test.
36 CONFIGSRC=path Dir with api server config files to copy into source tree.
37                (If none given, leave config files alone in source tree.)
38 services/api_test="TEST=test/functional/arvados/v1/collections_controller_test.rb"
39                Restrict apiserver tests to the given file
40 sdk/python_test="--test-suite test.test_keep_locator"
41                Restrict Python SDK tests to the given class
42 apps/workbench_test="TEST=test/integration/pipeline_instances_test.rb"
43                Restrict Workbench tests to the given file
44 services/arv-git-httpd_test="-check.vv"
45                Show all log messages, even when tests pass (also works
46                with services/keepstore_test etc.)
47 ARVADOS_DEBUG=1
48                Print more debug messages
49 envvar=value   Set \$envvar to value. Primarily useful for WORKSPACE,
50                *_test, and other examples shown above.
51
52 Assuming --skip-install is not given, all components are installed
53 into \$GOPATH, \$VENDIR, and \$GEMHOME before running any tests. Many
54 test suites depend on other components being installed, and installing
55 everything tends to be quicker than debugging dependencies.
56
57 As a special concession to the current CI server config, CONFIGSRC
58 defaults to $HOME/arvados-api-server if that directory exists.
59
60 More information and background:
61
62 https://arvados.org/projects/arvados/wiki/Running_tests
63
64 Available tests:
65
66 apps/workbench (*)
67 apps/workbench_units (*)
68 apps/workbench_functionals (*)
69 apps/workbench_integration (*)
70 apps/workbench_benchmark
71 apps/workbench_profile
72 cmd/arvados-client
73 doc
74 lib/cli
75 lib/cmd
76 lib/crunchstat
77 services/api
78 services/arv-git-httpd
79 services/crunchstat
80 services/dispatchcloud
81 services/dockercleaner
82 services/fuse
83 services/health
84 services/keep-web
85 services/keepproxy
86 services/keepstore
87 services/keep-balance
88 services/login-sync
89 services/nodemanager
90 services/nodemanager_integration
91 services/crunch-run
92 services/crunch-dispatch-local
93 services/crunch-dispatch-slurm
94 services/ws
95 sdk/cli
96 sdk/pam
97 sdk/python
98 sdk/python:py3
99 sdk/ruby
100 sdk/go/arvados
101 sdk/go/arvadosclient
102 sdk/go/dispatch
103 sdk/go/keepclient
104 sdk/go/health
105 sdk/go/httpserver
106 sdk/go/manifest
107 sdk/go/blockdigest
108 sdk/go/asyncbuf
109 sdk/go/stats
110 sdk/go/crunchrunner
111 sdk/cwl
112 tools/sync-groups
113 tools/crunchstat-summary
114 tools/keep-exercise
115 tools/keep-rsync
116 tools/keep-block-check
117
118 (*) apps/workbench is shorthand for apps/workbench_units +
119     apps/workbench_functionals + apps/workbench_integration
120
121 EOF
122
123 # First make sure to remove any ARVADOS_ variables from the calling
124 # environment that could interfere with the tests.
125 unset $(env | cut -d= -f1 | grep \^ARVADOS_)
126
127 # Reset other variables that could affect our [tests'] behavior by
128 # accident.
129 GITDIR=
130 GOPATH=
131 VENVDIR=
132 VENV3DIR=
133 PYTHONPATH=
134 GEMHOME=
135 PERLINSTALLBASE=
136
137 short=
138 only_install=
139 temp=
140 temp_preserve=
141
142 clear_temp() {
143     if [[ -z "$temp" ]]; then
144         # we didn't even get as far as making a temp dir
145         :
146     elif [[ -z "$temp_preserve" ]]; then
147         rm -rf "$temp"
148     else
149         echo "Leaving behind temp dirs in $temp"
150     fi
151 }
152
153 fatal() {
154     clear_temp
155     echo >&2 "Fatal: $* (encountered in ${FUNCNAME[1]} at ${BASH_SOURCE[1]} line ${BASH_LINENO[0]})"
156     exit 1
157 }
158
159 exit_cleanly() {
160     trap - INT
161     create-plot-data-from-log.sh $BUILD_NUMBER "$WORKSPACE/apps/workbench/log/test.log" "$WORKSPACE/apps/workbench/log/"
162     rotate_logfile "$WORKSPACE/apps/workbench/log/" "test.log"
163     stop_services
164     rotate_logfile "$WORKSPACE/services/api/log/" "test.log"
165     report_outcomes
166     clear_temp
167     exit ${#failures}
168 }
169
170 sanity_checks() {
171     ( [[ -n "$WORKSPACE" ]] && [[ -d "$WORKSPACE/services" ]] ) \
172         || fatal "WORKSPACE environment variable not set to a source directory (see: $0 --help)"
173     echo Checking dependencies:
174     echo -n 'virtualenv: '
175     virtualenv --version \
176         || fatal "No virtualenv. Try: apt-get install virtualenv (on ubuntu: python-virtualenv)"
177     echo -n 'ruby: '
178     ruby -v \
179         || fatal "No ruby. Install >=2.1.9 (using rbenv, rvm, or source)"
180     echo -n 'go: '
181     go version \
182         || fatal "No go binary. See http://golang.org/doc/install"
183     [[ $(go version) =~ go1.([0-9]+) ]] && [[ ${BASH_REMATCH[1]} -ge 8 ]] \
184         || fatal "Go >= 1.8 required. See http://golang.org/doc/install"
185     echo -n 'gcc: '
186     gcc --version | egrep ^gcc \
187         || fatal "No gcc. Try: apt-get install build-essential"
188     echo -n 'fuse.h: '
189     find /usr/include -path '*fuse/fuse.h' | egrep --max-count=1 . \
190         || fatal "No fuse/fuse.h. Try: apt-get install libfuse-dev"
191     echo -n 'gnutls.h: '
192     find /usr/include -path '*gnutls/gnutls.h' | egrep --max-count=1 . \
193         || fatal "No gnutls/gnutls.h. Try: apt-get install libgnutls28-dev"
194     echo -n 'Python2 pyconfig.h: '
195     find /usr/include -path '*/python2*/pyconfig.h' | egrep --max-count=1 . \
196         || fatal "No Python2 pyconfig.h. Try: apt-get install python2.7-dev"
197     echo -n 'Python3 pyconfig.h: '
198     find /usr/include -path '*/python3*/pyconfig.h' | egrep --max-count=1 . \
199         || fatal "No Python3 pyconfig.h. Try: apt-get install python3-dev"
200     echo -n 'nginx: '
201     PATH="$PATH:/sbin:/usr/sbin:/usr/local/sbin" nginx -v \
202         || fatal "No nginx. Try: apt-get install nginx"
203     echo -n 'perl: '
204     perl -v | grep version \
205         || fatal "No perl. Try: apt-get install perl"
206     for mod in ExtUtils::MakeMaker JSON LWP Net::SSL; do
207         echo -n "perl $mod: "
208         perl -e "use $mod; print \"\$$mod::VERSION\\n\"" \
209             || fatal "No $mod. Try: apt-get install perl-modules libcrypt-ssleay-perl libjson-perl libwww-perl"
210     done
211     echo -n 'gitolite: '
212     which gitolite \
213         || fatal "No gitolite. Try: apt-get install gitolite3"
214     echo -n 'npm: '
215     npm --version \
216         || fatal "No npm. Try: wget -O- https://nodejs.org/dist/v6.11.2/node-v6.11.2-linux-x64.tar.xz | sudo tar -C /usr/local -xJf - && sudo ln -s ../node-v6.11.2-linux-x64/bin/{node,npm} /usr/local/bin/"
217     echo -n 'cadaver: '
218     cadaver --version | grep -w cadaver \
219           || fatal "No cadaver. Try: apt-get install cadaver"
220     echo -n 'libattr1 xattr.h: '
221     find /usr/include -path '*/attr/xattr.h' | egrep --max-count=1 . \
222         || fatal "No libattr1 xattr.h. Try: apt-get install libattr1-dev"
223     echo -n 'libcurl curl.h: '
224     find /usr/include -path '*/curl/curl.h' | egrep --max-count=1 . \
225         || fatal "No libcurl curl.h. Try: apt-get install libcurl4-gnutls-dev"
226     echo -n 'libpq libpq-fe.h: '
227     find /usr/include -path '*/postgresql/libpq-fe.h' | egrep --max-count=1 . \
228         || fatal "No libpq libpq-fe.h. Try: apt-get install libpq-dev"
229     echo -n 'services/api/config/database.yml: '
230     if [[ ! -f "$WORKSPACE/services/api/config/database.yml" ]]; then
231             fatal "Please provide a database.yml file for the test suite"
232     else
233             echo "OK"
234     fi
235     echo -n 'postgresql: '
236     psql --version || fatal "No postgresql. Try: apt-get install postgresql postgresql-client-common"
237     echo -n 'phantomjs: '
238     phantomjs --version || fatal "No phantomjs. Try: apt-get install phantomjs"
239     echo -n 'xvfb: '
240     which Xvfb || fatal "No xvfb. Try: apt-get install xvfb"
241     echo -n 'graphviz: '
242     dot -V || fatal "No graphviz. Try: apt-get install graphviz"
243 }
244
245 rotate_logfile() {
246   # i.e.  rotate_logfile "$WORKSPACE/apps/workbench/log/" "test.log"
247   # $BUILD_NUMBER is set by Jenkins if this script is being called as part of a Jenkins run
248   if [[ -f "$1/$2" ]]; then
249     THEDATE=`date +%Y%m%d%H%M%S`
250     mv "$1/$2" "$1/$THEDATE-$BUILD_NUMBER-$2"
251     gzip "$1/$THEDATE-$BUILD_NUMBER-$2"
252   fi
253 }
254
255 declare -a failures
256 declare -A skip
257 declare -A testargs
258 skip[apps/workbench_profile]=1
259
260 while [[ -n "$1" ]]
261 do
262     arg="$1"; shift
263     case "$arg" in
264         --help)
265             echo >&2 "$helpmessage"
266             echo >&2
267             exit 1
268             ;;
269         --skip)
270             skip[$1]=1; shift
271             ;;
272         --only)
273             only="$1"; skip[$1]=""; shift
274             ;;
275         --short)
276             short=1
277             ;;
278         --skip-install)
279             only_install=nothing
280             ;;
281         --only-install)
282             only_install="$1"; shift
283             ;;
284         --temp)
285             temp="$1"; shift
286             temp_preserve=1
287             ;;
288         --leave-temp)
289             temp_preserve=1
290             ;;
291         --repeat)
292             repeat=$((${1}+0)); shift
293             ;;
294         --retry)
295             retry=1
296             ;;
297         *_test=*)
298             suite="${arg%%_test=*}"
299             args="${arg#*=}"
300             testargs["$suite"]="$args"
301             ;;
302         *=*)
303             eval export $(echo $arg | cut -d= -f1)=\"$(echo $arg | cut -d= -f2-)\"
304             ;;
305         *)
306             echo >&2 "$0: Unrecognized option: '$arg'. Try: $0 --help"
307             exit 1
308             ;;
309     esac
310 done
311
312 start_services() {
313     echo 'Starting API, keepproxy, keep-web, ws, arv-git-httpd, and nginx ssl proxy...'
314     if [[ ! -d "$WORKSPACE/services/api/log" ]]; then
315         mkdir -p "$WORKSPACE/services/api/log"
316     fi
317     # Remove empty api.pid file if it exists
318     if [[ -f "$WORKSPACE/tmp/api.pid" && ! -s "$WORKSPACE/tmp/api.pid" ]]; then
319         rm -f "$WORKSPACE/tmp/api.pid"
320     fi
321     cd "$WORKSPACE" \
322         && eval $(python sdk/python/tests/run_test_server.py start --auth admin) \
323         && export ARVADOS_TEST_API_HOST="$ARVADOS_API_HOST" \
324         && export ARVADOS_TEST_API_INSTALLED="$$" \
325         && python sdk/python/tests/run_test_server.py start_keep_proxy \
326         && python sdk/python/tests/run_test_server.py start_keep-web \
327         && python sdk/python/tests/run_test_server.py start_arv-git-httpd \
328         && python sdk/python/tests/run_test_server.py start_ws \
329         && python sdk/python/tests/run_test_server.py start_nginx \
330         && (env | egrep ^ARVADOS)
331 }
332
333 stop_services() {
334     if [[ -z "$ARVADOS_TEST_API_HOST" ]]; then
335         return
336     fi
337     unset ARVADOS_TEST_API_HOST
338     cd "$WORKSPACE" \
339         && python sdk/python/tests/run_test_server.py stop_nginx \
340         && python sdk/python/tests/run_test_server.py stop_arv-git-httpd \
341         && python sdk/python/tests/run_test_server.py stop_ws \
342         && python sdk/python/tests/run_test_server.py stop_keep-web \
343         && python sdk/python/tests/run_test_server.py stop_keep_proxy \
344         && python sdk/python/tests/run_test_server.py stop
345 }
346
347 interrupt() {
348     failures+=("($(basename $0) interrupted)")
349     exit_cleanly
350 }
351 trap interrupt INT
352
353 sanity_checks
354
355 echo "WORKSPACE=$WORKSPACE"
356
357 if [[ -z "$CONFIGSRC" ]] && [[ -d "$HOME/arvados-api-server" ]]; then
358     # Jenkins expects us to use this by default.
359     CONFIGSRC="$HOME/arvados-api-server"
360 fi
361
362 # Clean up .pyc files that may exist in the workspace
363 cd "$WORKSPACE"
364 find -name '*.pyc' -delete
365
366 if [[ -z "$temp" ]]; then
367     temp="$(mktemp -d)"
368 fi
369
370 # Set up temporary install dirs (unless existing dirs were supplied)
371 for tmpdir in VENVDIR VENV3DIR GOPATH GEMHOME PERLINSTALLBASE
372 do
373     if [[ -z "${!tmpdir}" ]]; then
374         eval "$tmpdir"="$temp/$tmpdir"
375     fi
376     if ! [[ -d "${!tmpdir}" ]]; then
377         mkdir "${!tmpdir}" || fatal "can't create ${!tmpdir} (does $temp exist?)"
378     fi
379 done
380
381 setup_ruby_environment() {
382     if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then
383         source "$HOME/.rvm/scripts/rvm"
384         using_rvm=true
385     elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then
386         source "/usr/local/rvm/scripts/rvm"
387         using_rvm=true
388     else
389         using_rvm=false
390     fi
391
392     if [[ "$using_rvm" == true ]]; then
393         # If rvm is in use, we can't just put separate "dependencies"
394         # and "gems-under-test" paths to GEM_PATH: passenger resets
395         # the environment to the "current gemset", which would lose
396         # our GEM_PATH and prevent our test suites from running ruby
397         # programs (for example, the Workbench test suite could not
398         # boot an API server or run arv). Instead, we have to make an
399         # rvm gemset and use it for everything.
400
401         [[ `type rvm | head -n1` == "rvm is a function" ]] \
402             || fatal 'rvm check'
403
404         # Put rvm's favorite path back in first place (overriding
405         # virtualenv, which just put itself there). Ignore rvm's
406         # complaint about not being in first place already.
407         rvm use @default 2>/dev/null
408
409         # Create (if needed) and switch to an @arvados-tests-* gemset,
410         # salting the gemset name so it doesn't interfere with
411         # concurrent builds in other workspaces. Leave the choice of
412         # ruby to the caller.
413         gemset="arvados-tests-$(echo -n "${WORKSPACE}" | md5sum | head -c16)"
414         rvm use "@${gemset}" --create \
415             || fatal 'rvm gemset setup'
416
417         rvm env
418     else
419         # When our "bundle install"s need to install new gems to
420         # satisfy dependencies, we want them to go where "gem install
421         # --user-install" would put them. (However, if the caller has
422         # already set GEM_HOME, we assume that's where dependencies
423         # should be installed, and we should leave it alone.)
424
425         if [ -z "$GEM_HOME" ]; then
426             user_gempath="$(gem env gempath)"
427             export GEM_HOME="${user_gempath%%:*}"
428         fi
429         PATH="$(gem env gemdir)/bin:$PATH"
430
431         # When we build and install our own gems, we install them in our
432         # $GEMHOME tmpdir, and we want them to be at the front of GEM_PATH and
433         # PATH so integration tests prefer them over other versions that
434         # happen to be installed in $user_gempath, system dirs, etc.
435
436         tmpdir_gem_home="$(env - PATH="$PATH" HOME="$GEMHOME" gem env gempath | cut -f1 -d:)"
437         PATH="$tmpdir_gem_home/bin:$PATH"
438         export GEM_PATH="$tmpdir_gem_home"
439
440         echo "Will install dependencies to $(gem env gemdir)"
441         echo "Will install arvados gems to $tmpdir_gem_home"
442         echo "Gem search path is GEM_PATH=$GEM_PATH"
443     fi
444     bundle config || gem install bundler \
445         || fatal 'install bundler'
446 }
447
448 with_test_gemset() {
449     if [[ "$using_rvm" == true ]]; then
450         "$@"
451     else
452         GEM_HOME="$tmpdir_gem_home" GEM_PATH="$tmpdir_gem_home" "$@"
453     fi
454 }
455
456 gem_uninstall_if_exists() {
457     if gem list "$1\$" | egrep '^\w'; then
458         gem uninstall --force --all --executables "$1"
459     fi
460 }
461
462 setup_virtualenv() {
463     local venvdest="$1"; shift
464     if ! [[ -e "$venvdest/bin/activate" ]] || ! [[ -e "$venvdest/bin/pip" ]]; then
465         virtualenv --setuptools "$@" "$venvdest" || fatal "virtualenv $venvdest failed"
466     fi
467     if [[ $("$venvdest/bin/python" --version 2>&1) =~ \ 3\.[012]\. ]]; then
468         # pip 8.0.0 dropped support for python 3.2, e.g., debian wheezy
469         "$venvdest/bin/pip" install 'setuptools>=18.5' 'pip>=7,<8'
470     else
471         "$venvdest/bin/pip" install 'setuptools>=18.5' 'pip>=7'
472     fi
473     # ubuntu1404 can't seem to install mock via tests_require, but it can do this.
474     "$venvdest/bin/pip" install 'mock>=1.0' 'pbr<1.7.0'
475 }
476
477 export PERLINSTALLBASE
478 export PERLLIB="$PERLINSTALLBASE/lib/perl5:${PERLLIB:+$PERLLIB}"
479
480
481 export GOPATH
482 mkdir -p "$GOPATH/src/git.curoverse.com"
483 rmdir -v --parents --ignore-fail-on-non-empty "$GOPATH/src/git.curoverse.com/arvados.git/tmp/GOPATH"
484 for d in \
485     "$GOPATH/src/git.curoverse.com/arvados.git/arvados.git" \
486     "$GOPATH/src/git.curoverse.com/arvados.git"; do
487     [[ -d "$d" ]] && rmdir "$d"
488     [[ -h "$d" ]] && rm "$d"
489 done
490 ln -vsnfT "$WORKSPACE" "$GOPATH/src/git.curoverse.com/arvados.git" \
491     || fatal "symlink failed"
492 go get -v github.com/kardianos/govendor \
493     || fatal "govendor install failed"
494 cd "$GOPATH/src/git.curoverse.com/arvados.git" \
495     || fatal
496 # Remove cached source dirs in workdir. Otherwise, they won't qualify
497 # as +missing or +external below, and we won't be able to detect that
498 # they're missing from vendor/vendor.json.
499 rm -r vendor/*/
500 go get -v -d ...
501 "$GOPATH/bin/govendor" sync \
502     || fatal "govendor sync failed"
503 [[ -z $("$GOPATH/bin/govendor" list +unused +missing +external | tee /dev/stderr) ]] \
504     || fatal "vendor/vendor.json has unused or missing dependencies -- try:
505 * govendor remove +unused
506 * govendor add +missing +external
507 "
508 cd "$WORKSPACE"
509
510
511 setup_virtualenv "$VENVDIR" --python python2.7
512 . "$VENVDIR/bin/activate"
513
514 # Needed for run_test_server.py which is used by certain (non-Python) tests.
515 pip freeze 2>/dev/null | egrep ^PyYAML= \
516     || pip install PyYAML >/dev/null \
517     || fatal "pip install PyYAML failed"
518
519 # Preinstall forked version of libcloud, because nodemanager "pip install"
520 # won't pick it up by default.
521 pip freeze 2>/dev/null | egrep ^apache-libcloud==$LIBCLOUD_PIN \
522     || pip install --pre --ignore-installed https://github.com/curoverse/libcloud/archive/apache-libcloud-$LIBCLOUD_PIN.zip >/dev/null \
523     || fatal "pip install apache-libcloud failed"
524
525 # We need an unreleased (as of 2017-08-17) llfuse bugfix, otherwise our fuse test suite deadlocks.
526 pip freeze | grep -x llfuse==1.2.0 || (
527     set -e
528     yes | pip uninstall llfuse || true
529     cython --version || fatal "no cython; try sudo apt-get install cython"
530     cd "$temp"
531     (cd python-llfuse 2>/dev/null || git clone https://github.com/curoverse/python-llfuse)
532     cd python-llfuse
533     git checkout 620722fd990ea642ddb8e7412676af482c090c0c
534     git checkout setup.py
535     sed -i -e "s:'1\\.2':'1.2.0':" setup.py
536     python setup.py build_cython
537     python setup.py install --force
538 ) || fatal "llfuse fork failed"
539 pip freeze | grep -x llfuse==1.2.0 || fatal "error: installed llfuse 1.2.0 but '$(pip freeze | grep llfuse)' ???"
540
541 # Deactivate Python 2 virtualenv
542 deactivate
543
544 declare -a pythonstuff
545 pythonstuff=(
546     sdk/pam
547     sdk/python
548     sdk/python:py3
549     sdk/cwl
550     services/dockercleaner:py3
551     services/fuse
552     services/nodemanager
553     tools/crunchstat-summary
554     )
555
556 # If Python 3 is available, set up its virtualenv in $VENV3DIR.
557 # Otherwise, skip dependent tests.
558 PYTHON3=$(which python3)
559 if [[ ${?} = 0 ]]; then
560     setup_virtualenv "$VENV3DIR" --python python3
561 else
562     PYTHON3=
563     cat >&2 <<EOF
564
565 Warning: python3 could not be found. Python 3 tests will be skipped.
566
567 EOF
568 fi
569
570 # Reactivate Python 2 virtualenv
571 . "$VENVDIR/bin/activate"
572
573 # Note: this must be the last time we change PATH, otherwise rvm will
574 # whine a lot.
575 setup_ruby_environment
576
577 echo "PATH is $PATH"
578
579 if ! which bundler >/dev/null
580 then
581     gem install --user-install bundler || fatal 'Could not install bundler'
582 fi
583
584 retry() {
585     remain="${repeat}"
586     while :
587     do
588         if ${@}; then
589             if [[ "$remain" -gt 1 ]]; then
590                 remain=$((${remain}-1))
591                 title "Repeating ${remain} more times"
592             else
593                 break
594             fi
595         elif [[ "$retry" == 1 ]]; then
596             read -p 'Try again? [Y/n] ' x
597             if [[ "$x" != "y" ]] && [[ "$x" != "" ]]
598             then
599                 break
600             fi
601         else
602             break
603         fi
604     done
605 }
606
607 do_test() {
608     case "${1}" in
609         apps/workbench_units | apps/workbench_functionals | apps/workbench_integration)
610             suite=apps/workbench
611             ;;
612         services/nodemanager | services/nodemanager_integration)
613             suite=services/nodemanager_suite
614             ;;
615         *)
616             suite="${1}"
617             ;;
618     esac
619     if [[ -z "${skip[$suite]}" && -z "${skip[$1]}" && \
620                 (-z "${only}" || "${only}" == "${suite}" || \
621                  "${only}" == "${1}") ]]; then
622         retry do_test_once ${@}
623     else
624         title "Skipping ${1} tests"
625     fi
626 }
627
628 do_test_once() {
629     unset result
630
631     title "Running $1 tests"
632     timer_reset
633     if [[ "$2" == "go" ]]
634     then
635         covername="coverage-$(echo "$1" | sed -e 's/\//_/g')"
636         coverflags=("-covermode=count" "-coverprofile=$WORKSPACE/tmp/.$covername.tmp")
637         # We do "go get -t" here to catch compilation errors
638         # before trying "go test". Otherwise, coverage-reporting
639         # mode makes Go show the wrong line numbers when reporting
640         # compilation errors.
641         go get -t "git.curoverse.com/arvados.git/$1" && \
642             cd "$GOPATH/src/git.curoverse.com/arvados.git/$1" && \
643             [[ -z "$(gofmt -e -d . | tee /dev/stderr)" ]] && \
644             if [[ -n "${testargs[$1]}" ]]
645         then
646             # "go test -check.vv giturl" doesn't work, but this
647             # does:
648             go test ${short:+-short} ${testargs[$1]}
649         else
650             # The above form gets verbose even when testargs is
651             # empty, so use this form in such cases:
652             go test ${short:+-short} ${coverflags[@]} "git.curoverse.com/arvados.git/$1"
653         fi
654         result=${result:-$?}
655         if [[ -f "$WORKSPACE/tmp/.$covername.tmp" ]]
656         then
657             go tool cover -html="$WORKSPACE/tmp/.$covername.tmp" -o "$WORKSPACE/tmp/$covername.html"
658             rm "$WORKSPACE/tmp/.$covername.tmp"
659         fi
660     elif [[ "$2" == "pip" ]]
661     then
662         tries=0
663         cd "$WORKSPACE/$1" && while :
664         do
665             tries=$((${tries}+1))
666             # $3 can name a path directory for us to use, including trailing
667             # slash; e.g., the bin/ subdirectory of a virtualenv.
668             "${3}python" setup.py ${short:+--short-tests-only} test ${testargs[$1]}
669             result=$?
670             if [[ ${tries} < 3 && ${result} == 137 ]]
671             then
672                 printf '\n*****\n%s tests killed -- retrying\n*****\n\n' "$1"
673                 continue
674             else
675                 break
676             fi
677         done
678     elif [[ "$2" != "" ]]
679     then
680         "test_$2"
681     else
682         "test_$1"
683     fi
684     result=${result:-$?}
685     checkexit $result "$1 tests"
686     title "End of $1 tests (`timer`)"
687     return $result
688 }
689
690 do_install() {
691     if [[ -z "${only_install}" || "${only_install}" == "${1}" ]]; then
692         retry do_install_once ${@}
693     else
694         title "Skipping $1 install"
695     fi
696 }
697
698 do_install_once() {
699     title "Running $1 install"
700     timer_reset
701     if [[ "$2" == "go" ]]
702     then
703         go get -t "git.curoverse.com/arvados.git/$1"
704     elif [[ "$2" == "pip" ]]
705     then
706         # $3 can name a path directory for us to use, including trailing
707         # slash; e.g., the bin/ subdirectory of a virtualenv.
708
709         # Need to change to a different directory after creating
710         # the source dist package to avoid a pip bug.
711         # see https://arvados.org/issues/5766 for details.
712
713         # Also need to install twice, because if it believes the package is
714         # already installed, pip it won't install it.  So the first "pip
715         # install" ensures that the dependencies are met, the second "pip
716         # install" ensures that we've actually installed the local package
717         # we just built.
718         cd "$WORKSPACE/$1" \
719             && "${3}python" setup.py sdist rotate --keep=1 --match .tar.gz \
720             && cd "$WORKSPACE" \
721             && "${3}pip" install --quiet "$WORKSPACE/$1/dist"/*.tar.gz \
722             && "${3}pip" install --quiet --no-deps --ignore-installed "$WORKSPACE/$1/dist"/*.tar.gz
723     elif [[ "$2" != "" ]]
724     then
725         "install_$2"
726     else
727         "install_$1"
728     fi
729     result=$?
730     checkexit $result "$1 install"
731     title "End of $1 install (`timer`)"
732     return $result
733 }
734
735 bundle_install_trylocal() {
736     (
737         set -e
738         echo "(Running bundle install --local. 'could not find package' messages are OK.)"
739         if ! bundle install --local --no-deployment; then
740             echo "(Running bundle install again, without --local.)"
741             bundle install --no-deployment
742         fi
743         bundle package --all
744     )
745 }
746
747 install_doc() {
748     cd "$WORKSPACE/doc" \
749         && bundle_install_trylocal \
750         && rm -rf .site
751 }
752 do_install doc
753
754 install_gem() {
755     gemname=$1
756     srcpath=$2
757     with_test_gemset gem_uninstall_if_exists "$gemname" \
758         && cd "$WORKSPACE/$srcpath" \
759         && bundle_install_trylocal \
760         && gem build "$gemname.gemspec" \
761         && with_test_gemset gem install --no-ri --no-rdoc $(ls -t "$gemname"-*.gem|head -n1)
762 }
763
764 install_ruby_sdk() {
765     install_gem arvados sdk/ruby
766 }
767 do_install sdk/ruby ruby_sdk
768
769 install_perl_sdk() {
770     cd "$WORKSPACE/sdk/perl" \
771         && perl Makefile.PL INSTALL_BASE="$PERLINSTALLBASE" \
772         && make install INSTALLDIRS=perl
773 }
774 do_install sdk/perl perl_sdk
775
776 install_cli() {
777     install_gem arvados-cli sdk/cli
778 }
779 do_install sdk/cli cli
780
781 install_login-sync() {
782     install_gem arvados-login-sync services/login-sync
783 }
784 do_install services/login-sync login-sync
785
786 # Install the Python SDK early. Various other test suites (like
787 # keepproxy) bring up run_test_server.py, which imports the arvados
788 # module. We can't actually *test* the Python SDK yet though, because
789 # its own test suite brings up some of those other programs (like
790 # keepproxy).
791 for p in "${pythonstuff[@]}"
792 do
793     dir=${p%:py3}
794     if [[ ${dir} = ${p} ]]; then
795         if [[ -z ${skip[python2]} ]]; then
796             do_install ${dir} pip
797         fi
798     elif [[ -n ${PYTHON3} ]]; then
799         if [[ -z ${skip[python3]} ]]; then
800             do_install ${dir} pip "$VENV3DIR/bin/"
801         fi
802     fi
803 done
804
805 install_apiserver() {
806     cd "$WORKSPACE/services/api" \
807         && RAILS_ENV=test bundle_install_trylocal
808
809     rm -f config/environments/test.rb
810     cp config/environments/test.rb.example config/environments/test.rb
811
812     if [ -n "$CONFIGSRC" ]
813     then
814         for f in database.yml
815         do
816             cp "$CONFIGSRC/$f" config/ || fatal "$f"
817         done
818     fi
819
820     # Clear out any lingering postgresql connections to the test
821     # database, so that we can drop it. This assumes the current user
822     # is a postgresql superuser.
823     cd "$WORKSPACE/services/api" \
824         && test_database=$(python -c "import yaml; print yaml.load(file('config/database.yml'))['test']['database']") \
825         && psql "$test_database" -c "SELECT pg_terminate_backend (pg_stat_activity.procpid::int) FROM pg_stat_activity WHERE pg_stat_activity.datname = '$test_database';" 2>/dev/null
826
827     mkdir -p "$WORKSPACE/services/api/tmp/pids"
828
829     cert="$WORKSPACE/services/api/tmp/self-signed"
830     if [[ ! -e "$cert.pem" || "$(date -r "$cert.pem" +%s)" -lt 1512659226 ]]; then
831         (
832             dir="$WORKSPACE/services/api/tmp"
833             set -ex
834             openssl req -newkey rsa:2048 -nodes -subj '/C=US/ST=State/L=City/CN=localhost' -out "$cert.csr" -keyout "$cert.key" </dev/null
835             openssl x509 -req -in "$cert.csr" -signkey "$cert.key" -out "$cert.pem" -days 3650 -extfile <(printf 'subjectAltName=DNS:localhost,DNS:::1,DNS:0.0.0.0,DNS:127.0.0.1,IP:::1,IP:0.0.0.0,IP:127.0.0.1')
836         ) || return 1
837     fi
838
839     cd "$WORKSPACE/services/api" \
840         && rm -rf tmp/git \
841         && mkdir -p tmp/git \
842         && cd tmp/git \
843         && tar xf ../../test/test.git.tar \
844         && mkdir -p internal.git \
845         && git --git-dir internal.git init \
846             || return 1
847
848     cd "$WORKSPACE/services/api" \
849         && RAILS_ENV=test bundle exec rake db:drop \
850         && RAILS_ENV=test bundle exec rake db:setup \
851         && RAILS_ENV=test bundle exec rake db:fixtures:load
852 }
853 do_install services/api apiserver
854
855 declare -a gostuff
856 gostuff=(
857     cmd/arvados-client
858     lib/cli
859     lib/cmd
860     lib/crunchstat
861     sdk/go/arvados
862     sdk/go/arvadosclient
863     sdk/go/blockdigest
864     sdk/go/dispatch
865     sdk/go/health
866     sdk/go/httpserver
867     sdk/go/manifest
868     sdk/go/asyncbuf
869     sdk/go/crunchrunner
870     sdk/go/stats
871     services/arv-git-httpd
872     services/crunchstat
873     services/dispatchcloud
874     services/health
875     services/keep-web
876     services/keepstore
877     sdk/go/keepclient
878     services/keep-balance
879     services/keepproxy
880     services/crunch-dispatch-local
881     services/crunch-dispatch-slurm
882     services/crunch-run
883     services/ws
884     tools/keep-block-check
885     tools/keep-exercise
886     tools/keep-rsync
887     tools/sync-groups
888 )
889 for g in "${gostuff[@]}"
890 do
891     do_install "$g" go
892 done
893
894 install_workbench() {
895     cd "$WORKSPACE/apps/workbench" \
896         && mkdir -p tmp/cache \
897         && RAILS_ENV=test bundle_install_trylocal \
898         && RAILS_ENV=test RAILS_GROUPS=assets bundle exec rake npm:install
899 }
900 do_install apps/workbench workbench
901
902 unset http_proxy https_proxy no_proxy
903
904 test_doclinkchecker() {
905     (
906         set -e
907         cd "$WORKSPACE/doc"
908         ARVADOS_API_HOST=qr1hi.arvadosapi.com
909         # Make sure python-epydoc is installed or the next line won't
910         # do much good!
911         PYTHONPATH=$WORKSPACE/sdk/python/ bundle exec rake linkchecker baseurl=file://$WORKSPACE/doc/.site/ arvados_workbench_host=https://workbench.$ARVADOS_API_HOST arvados_api_host=$ARVADOS_API_HOST
912     )
913 }
914 do_test doc doclinkchecker
915
916 stop_services
917
918 test_apiserver() {
919     rm -f "$WORKSPACE/services/api/git-commit.version"
920     cd "$WORKSPACE/services/api" \
921         && env RAILS_ENV=test ${short:+RAILS_TEST_SHORT=1} bundle exec rake test TESTOPTS=-v ${testargs[services/api]}
922 }
923 do_test services/api apiserver
924
925 # Shortcut for when we're only running apiserver tests. This saves a bit of time,
926 # because we don't need to start up the api server for subsequent tests.
927 if [ ! -z "$only" ] && [ "$only" == "services/api" ]; then
928   rotate_logfile "$WORKSPACE/services/api/log/" "test.log"
929   exit_cleanly
930 fi
931
932 start_services || { stop_services; fatal "start_services"; }
933
934 test_ruby_sdk() {
935     cd "$WORKSPACE/sdk/ruby" \
936         && bundle exec rake test TESTOPTS=-v ${testargs[sdk/ruby]}
937 }
938 do_test sdk/ruby ruby_sdk
939
940 test_cli() {
941     cd "$WORKSPACE/sdk/cli" \
942         && mkdir -p /tmp/keep \
943         && KEEP_LOCAL_STORE=/tmp/keep bundle exec rake test TESTOPTS=-v ${testargs[sdk/cli]}
944 }
945 do_test sdk/cli cli
946
947 test_login-sync() {
948     cd "$WORKSPACE/services/login-sync" \
949         && bundle exec rake test TESTOPTS=-v ${testargs[services/login-sync]}
950 }
951 do_test services/login-sync login-sync
952
953 test_nodemanager_integration() {
954     cd "$WORKSPACE/services/nodemanager" \
955         && tests/integration_test.py ${testargs[services/nodemanager_integration]}
956 }
957 do_test services/nodemanager_integration nodemanager_integration
958
959 for p in "${pythonstuff[@]}"
960 do
961     dir=${p%:py3}
962     if [[ ${dir} = ${p} ]]; then
963         if [[ -z ${skip[python2]} ]]; then
964             do_test ${dir} pip
965         fi
966     elif [[ -n ${PYTHON3} ]]; then
967         if [[ -z ${skip[python3]} ]]; then
968             do_test ${dir} pip "$VENV3DIR/bin/"
969         fi
970     fi
971 done
972
973 for g in "${gostuff[@]}"
974 do
975     do_test "$g" go
976 done
977
978 test_workbench_units() {
979     cd "$WORKSPACE/apps/workbench" \
980         && env RAILS_ENV=test ${short:+RAILS_TEST_SHORT=1} bundle exec rake test:units TESTOPTS=-v ${testargs[apps/workbench]}
981 }
982 do_test apps/workbench_units workbench_units
983
984 test_workbench_functionals() {
985     cd "$WORKSPACE/apps/workbench" \
986         && env RAILS_ENV=test ${short:+RAILS_TEST_SHORT=1} bundle exec rake test:functionals TESTOPTS=-v ${testargs[apps/workbench]}
987 }
988 do_test apps/workbench_functionals workbench_functionals
989
990 test_workbench_integration() {
991     cd "$WORKSPACE/apps/workbench" \
992         && env RAILS_ENV=test ${short:+RAILS_TEST_SHORT=1} bundle exec rake test:integration TESTOPTS=-v ${testargs[apps/workbench]}
993 }
994 do_test apps/workbench_integration workbench_integration
995
996
997 test_workbench_benchmark() {
998     cd "$WORKSPACE/apps/workbench" \
999         && env RAILS_ENV=test ${short:+RAILS_TEST_SHORT=1} bundle exec rake test:benchmark ${testargs[apps/workbench_benchmark]}
1000 }
1001 do_test apps/workbench_benchmark workbench_benchmark
1002
1003 test_workbench_profile() {
1004     cd "$WORKSPACE/apps/workbench" \
1005         && env RAILS_ENV=test ${short:+RAILS_TEST_SHORT=1} bundle exec rake test:profile ${testargs[apps/workbench_profile]}
1006 }
1007 do_test apps/workbench_profile workbench_profile
1008
1009 exit_cleanly