d4afb52fa34382a33bca323ebbdf26b56e89efd4
[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" || return
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_env() {
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
716     if ! . "$VENVDIR/bin/activate"
717     then
718         result=1
719     elif [[ "$2" == "go" ]]
720     then
721         covername="coverage-$(echo "$1" | sed -e 's/\//_/g')"
722         coverflags=("-covermode=count" "-coverprofile=$WORKSPACE/tmp/.$covername.tmp")
723         # We do "go get -t" here to catch compilation errors
724         # before trying "go test". Otherwise, coverage-reporting
725         # mode makes Go show the wrong line numbers when reporting
726         # compilation errors.
727         go get -ldflags "-X main.version=${ARVADOS_VERSION:-$(git log -n1 --format=%H)-dev}" -t "git.curoverse.com/arvados.git/$1" && \
728             cd "$GOPATH/src/git.curoverse.com/arvados.git/$1" && \
729             [[ -z "$(gofmt -e -d . | tee /dev/stderr)" ]] && \
730             if [[ -n "${testargs[$1]}" ]]
731         then
732             # "go test -check.vv giturl" doesn't work, but this
733             # does:
734             go test ${short:+-short} ${testargs[$1]}
735         else
736             # The above form gets verbose even when testargs is
737             # empty, so use this form in such cases:
738             go test ${short:+-short} ${coverflags[@]} "git.curoverse.com/arvados.git/$1"
739         fi
740         result=${result:-$?}
741         if [[ -f "$WORKSPACE/tmp/.$covername.tmp" ]]
742         then
743             go tool cover -html="$WORKSPACE/tmp/.$covername.tmp" -o "$WORKSPACE/tmp/$covername.html"
744             rm "$WORKSPACE/tmp/.$covername.tmp"
745         fi
746     elif [[ "$2" == "pip" ]]
747     then
748         tries=0
749         cd "$WORKSPACE/$1" && while :
750         do
751             tries=$((${tries}+1))
752             # $3 can name a path directory for us to use, including trailing
753             # slash; e.g., the bin/ subdirectory of a virtualenv.
754             "${3}python" setup.py ${short:+--short-tests-only} test ${testargs[$1]}
755             result=$?
756             if [[ ${tries} < 3 && ${result} == 137 ]]
757             then
758                 printf '\n*****\n%s tests killed -- retrying\n*****\n\n' "$1"
759                 continue
760             else
761                 break
762             fi
763         done
764     elif [[ "$2" != "" ]]
765     then
766         "test_$2"
767     else
768         "test_$1"
769     fi
770     result=${result:-$?}
771     checkexit $result "$1 tests"
772     title "test $1 -- `timer`"
773     return $result
774 }
775
776 do_install() {
777     if [[ -n "${skip[install]}" || ( -n "${only_install}" && "${only_install}" != "${1}" && "${only_install}" != "${2}" ) ]]; then
778         return 0
779     fi
780     retry do_install_once ${@}
781 }
782
783 do_install_once() {
784     title "install $1"
785     timer_reset
786
787     if which deactivate >/dev/null; then deactivate; fi
788     if [[ "$1" != "env" ]] && ! . "$VENVDIR/bin/activate"; then
789         result=1
790     elif [[ "$2" == "go" ]]
791     then
792         go get -ldflags "-X main.version=${ARVADOS_VERSION:-$(git log -n1 --format=%H)-dev}" -t "git.curoverse.com/arvados.git/$1"
793     elif [[ "$2" == "pip" ]]
794     then
795         # $3 can name a path directory for us to use, including trailing
796         # slash; e.g., the bin/ subdirectory of a virtualenv.
797
798         # Need to change to a different directory after creating
799         # the source dist package to avoid a pip bug.
800         # see https://arvados.org/issues/5766 for details.
801
802         # Also need to install twice, because if it believes the package is
803         # already installed, pip it won't install it.  So the first "pip
804         # install" ensures that the dependencies are met, the second "pip
805         # install" ensures that we've actually installed the local package
806         # we just built.
807         cd "$WORKSPACE/$1" \
808             && "${3}python" setup.py sdist rotate --keep=1 --match .tar.gz \
809             && cd "$WORKSPACE" \
810             && "${3}pip" install --no-cache-dir --quiet "$WORKSPACE/$1/dist"/*.tar.gz \
811             && "${3}pip" install --no-cache-dir --quiet --no-deps --ignore-installed "$WORKSPACE/$1/dist"/*.tar.gz
812     elif [[ "$2" != "" ]]
813     then
814         "install_$2"
815     else
816         "install_$1"
817     fi
818     result=${result:-$?}
819     checkexit $result "$1 install"
820     title "install $1 -- `timer`"
821     return $result
822 }
823
824 bundle_install_trylocal() {
825     (
826         set -e
827         echo "(Running bundle install --local. 'could not find package' messages are OK.)"
828         if ! bundle install --local --no-deployment; then
829             echo "(Running bundle install again, without --local.)"
830             bundle install --no-deployment
831         fi
832         bundle package --all
833     )
834 }
835
836 install_doc() {
837     cd "$WORKSPACE/doc" \
838         && bundle_install_trylocal \
839         && rm -rf .site
840 }
841
842 install_gem() {
843     gemname=$1
844     srcpath=$2
845     with_test_gemset gem_uninstall_if_exists "$gemname" \
846         && cd "$WORKSPACE/$srcpath" \
847         && bundle_install_trylocal \
848         && gem build "$gemname.gemspec" \
849         && with_test_gemset gem install --no-ri --no-rdoc $(ls -t "$gemname"-*.gem|head -n1)
850 }
851
852 install_sdk/ruby() {
853     install_gem arvados sdk/ruby
854 }
855
856 install_sdk/R() {
857   if [[ "$NEED_SDK_R" = true ]]; then
858     cd "$WORKSPACE/sdk/R" \
859        && Rscript --vanilla install_deps.R
860   fi
861 }
862
863 install_sdk/perl() {
864     cd "$WORKSPACE/sdk/perl" \
865         && perl Makefile.PL INSTALL_BASE="$PERLINSTALLBASE" \
866         && make install INSTALLDIRS=perl
867 }
868
869 install_sdk/cli() {
870     install_gem arvados-cli sdk/cli
871 }
872
873 install_services/login-sync() {
874     install_gem arvados-login-sync services/login-sync
875 }
876
877 install_services/api() {
878     cd "$WORKSPACE/services/api" \
879         && RAILS_ENV=test bundle_install_trylocal
880
881     rm -f config/environments/test.rb
882     cp config/environments/test.rb.example config/environments/test.rb
883
884     if [ -n "$CONFIGSRC" ]
885     then
886         for f in database.yml
887         do
888             cp "$CONFIGSRC/$f" config/ || fatal "$f"
889         done
890     fi
891
892     # Clear out any lingering postgresql connections to the test
893     # database, so that we can drop it. This assumes the current user
894     # is a postgresql superuser.
895     cd "$WORKSPACE/services/api" \
896         && test_database=$(python -c "import yaml; print yaml.load(file('config/database.yml'))['test']['database']") \
897         && 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
898
899     mkdir -p "$WORKSPACE/services/api/tmp/pids"
900
901     cert="$WORKSPACE/services/api/tmp/self-signed"
902     if [[ ! -e "$cert.pem" || "$(date -r "$cert.pem" +%s)" -lt 1512659226 ]]; then
903         (
904             dir="$WORKSPACE/services/api/tmp"
905             set -ex
906             openssl req -newkey rsa:2048 -nodes -subj '/C=US/ST=State/L=City/CN=localhost' -out "$cert.csr" -keyout "$cert.key" </dev/null
907             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')
908         ) || return 1
909     fi
910
911     cd "$WORKSPACE/services/api" \
912         && rm -rf tmp/git \
913         && mkdir -p tmp/git \
914         && cd tmp/git \
915         && tar xf ../../test/test.git.tar \
916         && mkdir -p internal.git \
917         && git --git-dir internal.git init \
918             || return 1
919
920     cd "$WORKSPACE/services/api" \
921         && RAILS_ENV=test bundle exec rake db:drop \
922         && RAILS_ENV=test bundle exec rake db:setup \
923         && RAILS_ENV=test bundle exec rake db:fixtures:load
924 }
925
926 declare -a pythonstuff
927 pythonstuff=(
928     sdk/pam
929     sdk/python
930     sdk/python:py3
931     sdk/cwl
932     sdk/cwl:py3
933     services/dockercleaner:py3
934     services/fuse
935     services/nodemanager
936     tools/crunchstat-summary
937 )
938
939 declare -a gostuff
940 gostuff=(
941     cmd/arvados-client
942     cmd/arvados-server
943     lib/cli
944     lib/cmd
945     lib/controller
946     lib/crunchstat
947     lib/cloud
948     lib/cloud/azure
949     lib/cloud/ec2
950     lib/dispatchcloud
951     lib/dispatchcloud/container
952     lib/dispatchcloud/scheduler
953     lib/dispatchcloud/ssh_executor
954     lib/dispatchcloud/worker
955     sdk/go/arvados
956     sdk/go/arvadosclient
957     sdk/go/auth
958     sdk/go/blockdigest
959     sdk/go/dispatch
960     sdk/go/health
961     sdk/go/httpserver
962     sdk/go/manifest
963     sdk/go/asyncbuf
964     sdk/go/crunchrunner
965     sdk/go/stats
966     services/arv-git-httpd
967     services/crunchstat
968     services/health
969     services/keep-web
970     services/keepstore
971     sdk/go/keepclient
972     services/keep-balance
973     services/keepproxy
974     services/crunch-dispatch-local
975     services/crunch-dispatch-slurm
976     services/crunch-run
977     services/ws
978     tools/keep-block-check
979     tools/keep-exercise
980     tools/keep-rsync
981     tools/sync-groups
982 )
983
984 install_apps/workbench() {
985     cd "$WORKSPACE/apps/workbench" \
986         && mkdir -p tmp/cache \
987         && RAILS_ENV=test bundle_install_trylocal \
988         && RAILS_ENV=test RAILS_GROUPS=assets bundle exec rake npm:install
989 }
990
991 test_doc() {
992     (
993         set -e
994         cd "$WORKSPACE/doc"
995         ARVADOS_API_HOST=qr1hi.arvadosapi.com
996         # Make sure python-epydoc is installed or the next line won't
997         # do much good!
998         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
999     )
1000 }
1001
1002 test_services/api() {
1003     rm -f "$WORKSPACE/services/api/git-commit.version"
1004     cd "$WORKSPACE/services/api" \
1005         && env RAILS_ENV=test ${short:+RAILS_TEST_SHORT=1} bundle exec rake test TESTOPTS=-v ${testargs[services/api]}
1006 }
1007
1008 test_sdk/ruby() {
1009     cd "$WORKSPACE/sdk/ruby" \
1010         && bundle exec rake test TESTOPTS=-v ${testargs[sdk/ruby]}
1011 }
1012
1013 test_sdk/R() {
1014   if [[ "$NEED_SDK_R" = true ]]; then
1015     cd "$WORKSPACE/sdk/R" \
1016         && Rscript --vanilla run_test.R
1017   fi
1018 }
1019
1020 test_sdk/cli() {
1021     cd "$WORKSPACE/sdk/cli" \
1022         && mkdir -p /tmp/keep \
1023         && KEEP_LOCAL_STORE=/tmp/keep bundle exec rake test TESTOPTS=-v ${testargs[sdk/cli]}
1024 }
1025
1026 test_services/login-sync() {
1027     cd "$WORKSPACE/services/login-sync" \
1028         && bundle exec rake test TESTOPTS=-v ${testargs[services/login-sync]}
1029 }
1030
1031 test_services/nodemanager_integration() {
1032     cd "$WORKSPACE/services/nodemanager" \
1033         && tests/integration_test.py ${testargs[services/nodemanager_integration]}
1034 }
1035
1036 test_apps/workbench_units() {
1037     cd "$WORKSPACE/apps/workbench" \
1038         && env RAILS_ENV=test ${short:+RAILS_TEST_SHORT=1} bundle exec rake test:units TESTOPTS=-v ${testargs[apps/workbench]}
1039 }
1040
1041 test_apps/workbench_functionals() {
1042     cd "$WORKSPACE/apps/workbench" \
1043         && env RAILS_ENV=test ${short:+RAILS_TEST_SHORT=1} bundle exec rake test:functionals TESTOPTS=-v ${testargs[apps/workbench]}
1044 }
1045
1046 test_apps/workbench_integration() {
1047     cd "$WORKSPACE/apps/workbench" \
1048         && env RAILS_ENV=test ${short:+RAILS_TEST_SHORT=1} bundle exec rake test:integration TESTOPTS=-v ${testargs[apps/workbench]}
1049 }
1050
1051 test_apps/workbench_benchmark() {
1052     cd "$WORKSPACE/apps/workbench" \
1053         && env RAILS_ENV=test ${short:+RAILS_TEST_SHORT=1} bundle exec rake test:benchmark ${testargs[apps/workbench_benchmark]}
1054 }
1055
1056 test_apps/workbench_profile() {
1057     cd "$WORKSPACE/apps/workbench" \
1058         && env RAILS_ENV=test ${short:+RAILS_TEST_SHORT=1} bundle exec rake test:profile ${testargs[apps/workbench_profile]}
1059 }
1060
1061 install_deps() {
1062     # Install parts needed by test suites
1063     do_install env
1064     do_install cmd/arvados-server go
1065     do_install sdk/cli
1066     do_install sdk/perl
1067     do_install sdk/python pip
1068     do_install sdk/ruby
1069     do_install services/api
1070     do_install services/arv-git-httpd go
1071     do_install services/keepproxy go
1072     do_install services/keepstore go
1073     do_install services/keep-web go
1074     do_install services/ws go
1075 }
1076
1077 install_all() {
1078     do_install env
1079     do_install doc
1080     do_install sdk/ruby
1081     do_install sdk/R
1082     do_install sdk/perl
1083     do_install sdk/cli
1084     do_install services/login-sync
1085     for p in "${pythonstuff[@]}"
1086     do
1087         dir=${p%:py3}
1088         if [[ ${dir} = ${p} ]]; then
1089             if [[ -z ${skip[python2]} ]]; then
1090                 do_install ${dir} pip
1091             fi
1092         elif [[ -n ${PYTHON3} ]]; then
1093             if [[ -z ${skip[python3]} ]]; then
1094                 do_install ${dir} pip "$VENV3DIR/bin/"
1095             fi
1096         fi
1097     done
1098     do_install services/api
1099     for g in "${gostuff[@]}"
1100     do
1101         do_install "$g" go
1102     done
1103     do_install apps/workbench
1104 }
1105
1106 test_all() {
1107     stop_services
1108     do_test services/api
1109
1110     # Shortcut for when we're only running apiserver tests. This saves a bit of time,
1111     # because we don't need to start up the api server for subsequent tests.
1112     if [ ! -z "$only" ] && [ "$only" == "services/api" ]; then
1113         rotate_logfile "$WORKSPACE/services/api/log/" "test.log"
1114         exit_cleanly
1115     fi
1116
1117     do_test doc
1118     do_test sdk/ruby
1119     do_test sdk/R
1120     do_test sdk/cli
1121     do_test services/login-sync
1122     do_test services/nodemanager_integration
1123     for p in "${pythonstuff[@]}"
1124     do
1125         dir=${p%:py3}
1126         if [[ ${dir} = ${p} ]]; then
1127             if [[ -z ${skip[python2]} ]]; then
1128                 do_test ${dir} pip
1129             fi
1130         elif [[ -n ${PYTHON3} ]]; then
1131             if [[ -z ${skip[python3]} ]]; then
1132                 do_test ${dir} pip "$VENV3DIR/bin/"
1133             fi
1134         fi
1135     done
1136
1137     for g in "${gostuff[@]}"
1138     do
1139         do_test "$g" go
1140     done
1141     do_test apps/workbench_units
1142     do_test apps/workbench_functionals
1143     do_test apps/workbench_integration
1144     do_test apps/workbench_benchmark
1145     do_test apps/workbench_profile
1146 }
1147
1148 help_interactive() {
1149     echo "== Interactive commands:"
1150     echo "TARGET                 (short for 'test DIR')"
1151     echo "test TARGET"
1152     echo "test TARGET:py3        (test with python3)"
1153     echo "test TARGET -check.vv  (pass arguments to test)"
1154     echo "install TARGET"
1155     echo "install env            (go/python libs)"
1156     echo "install deps           (go/python libs + arvados components needed for integration tests)"
1157     echo "reset                  (...services used by integration tests)"
1158     echo "exit"
1159     echo "== Test targets:"
1160     echo "${!testfuncargs[@]}" | tr ' ' '\n' | sort | column
1161 }
1162
1163 initialize
1164
1165 declare -A testfuncargs=()
1166 for g in "${gostuff[@]}"; do
1167     testfuncargs[$g]="$g go"
1168 done
1169 for p in "${pythonstuff[@]}"; do
1170     dir=${p%:py3}
1171     if [[ ${dir} = ${p} ]]; then
1172         testfuncargs[$p]="$dir pip $VENVDIR/bin/"
1173     else
1174         testfuncargs[$p]="$dir pip $VENV3DIR/bin/"
1175     fi
1176 done
1177
1178 if [[ -z ${interactive} ]]; then
1179     install_all
1180     test_all
1181 else
1182     skip=()
1183     only=()
1184     only_install=()
1185     if [[ -e "$VENVDIR/bin/activate" ]]; then stop_services; fi
1186     setnextcmd() {
1187         if [[ "$nextcmd" != "install deps" ]]; then
1188             :
1189         elif [[ -e "$VENVDIR/bin/activate" ]]; then
1190             nextcmd="test lib/cmd"
1191         else
1192             nextcmd="install deps"
1193         fi
1194     }
1195     echo
1196     help_interactive
1197     nextcmd="install deps"
1198     setnextcmd
1199     while read -p 'What next? ' -e -i "${nextcmd}" nextcmd; do
1200         read verb target opts <<<"${nextcmd}"
1201         case "${verb}" in
1202             "" | "help")
1203                 help_interactive
1204                 ;;
1205             "exit" | "quit")
1206                 exit_cleanly
1207                 ;;
1208             "reset")
1209                 stop_services
1210                 ;;
1211             *)
1212                 target="${target%/}"
1213                 testargs["$target"]="${opts}"
1214                 case "$target" in
1215                     all | deps)
1216                         ${verb}_${target}
1217                         ;;
1218                     *)
1219                         tt="${testfuncargs[${target}]}"
1220                         tt="${tt:-$target}"
1221                         do_$verb $tt
1222                         ;;
1223                 esac
1224                 ;;
1225         esac
1226         if [[ ${#successes[@]} -gt 0 || ${#failures[@]} -gt 0 ]]; then
1227             report_outcomes
1228             successes=()
1229             failures=()
1230         fi
1231         cd "$WORKSPACE"
1232         setnextcmd
1233     done
1234     echo
1235 fi
1236 exit_cleanly