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