Merge branch '7722-build-keep-rsync' closes #7722
[arvados-dev.git] / jenkins / run-tests.sh
1 #!/bin/bash
2
3 . `dirname "$(readlink -f "$0")"`/libcloud-pin
4
5 read -rd "\000" helpmessage <<EOF
6 $(basename $0): Install and test Arvados components.
7
8 Exit non-zero if any tests fail.
9
10 Syntax:
11         $(basename $0) WORKSPACE=/path/to/arvados [options]
12
13 Options:
14
15 --skip FOO     Do not test the FOO component.
16 --only FOO     Do not test anything except the FOO component.
17 --temp DIR     Install components and dependencies under DIR instead of
18                making a new temporary directory. Implies --leave-temp.
19 --leave-temp   Do not remove GOPATH, virtualenv, and other temp dirs at exit.
20                Instead, show the path to give as --temp to reuse them in
21                subsequent invocations.
22 --skip-install Do not run any install steps. Just run tests.
23                You should provide GOPATH, GEMHOME, and VENVDIR options
24                from a previous invocation if you use this option.
25 --only-install Run specific install step
26 WORKSPACE=path Arvados source tree to test.
27 CONFIGSRC=path Dir with api server config files to copy into source tree.
28                (If none given, leave config files alone in source tree.)
29 services/api_test="TEST=test/functional/arvados/v1/collections_controller_test.rb"
30                Restrict apiserver tests to the given file
31 sdk/python_test="--test-suite test.test_keep_locator"
32                Restrict Python SDK tests to the given class
33 apps/workbench_test="TEST=test/integration/pipeline_instances_test.rb"
34                Restrict Workbench tests to the given file
35 services/arv-git-httpd_test="-check.vv"
36                Show all log messages, even when tests pass (also works
37                with services/keepstore_test etc.)
38 ARVADOS_DEBUG=1
39                Print more debug messages
40 envvar=value   Set \$envvar to value. Primarily useful for WORKSPACE,
41                *_test, and other examples shown above.
42
43 Assuming --skip-install is not given, all components are installed
44 into \$GOPATH, \$VENDIR, and \$GEMHOME before running any tests. Many
45 test suites depend on other components being installed, and installing
46 everything tends to be quicker than debugging dependencies.
47
48 As a special concession to the current CI server config, CONFIGSRC
49 defaults to $HOME/arvados-api-server if that directory exists.
50
51 More information and background:
52
53 https://arvados.org/projects/arvados/wiki/Running_tests
54
55 Available tests:
56
57 apps/workbench
58 apps/workbench_benchmark
59 apps/workbench_profile
60 doc
61 services/api
62 services/arv-git-httpd
63 services/crunchstat
64 services/dockercleaner
65 services/fuse
66 services/keep-web
67 services/keepproxy
68 services/keepstore
69 services/login-sync
70 services/nodemanager
71 sdk/cli
72 sdk/pam
73 sdk/python
74 sdk/ruby
75 sdk/go/arvadosclient
76 sdk/go/keepclient
77 sdk/go/streamer
78 tools/keep-rsync
79
80 EOF
81
82 # First make sure to remove any ARVADOS_ variables from the calling
83 # environment that could interfere with the tests.
84 unset $(env | cut -d= -f1 | grep \^ARVADOS_)
85
86 # Reset other variables that could affect our [tests'] behavior by
87 # accident.
88 GITDIR=
89 GOPATH=
90 VENVDIR=
91 VENV3DIR=
92 PYTHONPATH=
93 GEMHOME=
94 PERLINSTALLBASE=
95
96 COLUMNS=80
97
98 skip_install=
99 temp=
100 temp_preserve=
101
102 clear_temp() {
103     if [[ -z "$temp" ]]; then
104         # we didn't even get as far as making a temp dir
105         :
106     elif [[ -z "$temp_preserve" ]]; then
107         rm -rf "$temp"
108     else
109         echo "Leaving behind temp dirs in $temp"
110     fi
111 }
112
113 fatal() {
114     clear_temp
115     echo >&2 "Fatal: $* (encountered in ${FUNCNAME[1]} at ${BASH_SOURCE[1]} line ${BASH_LINENO[0]})"
116     exit 1
117 }
118
119 report_outcomes() {
120     for x in "${successes[@]}"
121     do
122         echo "Pass: $x"
123     done
124
125     if [[ ${#failures[@]} == 0 ]]
126     then
127         echo "All test suites passed."
128     else
129         echo "Failures (${#failures[@]}):"
130         for x in "${failures[@]}"
131         do
132             echo "Fail: $x"
133         done
134     fi
135 }
136
137 exit_cleanly() {
138     trap - INT
139     create-plot-data-from-log.sh $BUILD_NUMBER "$WORKSPACE/apps/workbench/log/test.log" "$WORKSPACE/apps/workbench/log/"
140     rotate_logfile "$WORKSPACE/apps/workbench/log/" "test.log"
141     stop_services
142     rotate_logfile "$WORKSPACE/services/api/log/" "test.log"
143     report_outcomes
144     clear_temp
145     exit ${#failures}
146 }
147
148 sanity_checks() {
149     ( [[ -n "$WORKSPACE" ]] && [[ -d "$WORKSPACE/services" ]] ) \
150         || fatal "WORKSPACE environment variable not set to a source directory (see: $0 --help)"
151     echo Checking dependencies:
152     echo -n 'virtualenv: '
153     virtualenv --version \
154         || fatal "No virtualenv. Try: apt-get install virtualenv (on ubuntu: python-virtualenv)"
155     echo -n 'go: '
156     go version \
157         || fatal "No go binary. See http://golang.org/doc/install"
158     echo -n 'gcc: '
159     gcc --version | egrep ^gcc \
160         || fatal "No gcc. Try: apt-get install build-essential"
161     echo -n 'fuse.h: '
162     find /usr/include -wholename '*fuse/fuse.h' \
163         || fatal "No fuse/fuse.h. Try: apt-get install libfuse-dev"
164     echo -n 'pyconfig.h: '
165     find /usr/include -name pyconfig.h | egrep --max-count=1 . \
166         || fatal "No pyconfig.h. Try: apt-get install python-dev"
167     echo -n 'nginx: '
168     PATH="$PATH:/sbin:/usr/sbin:/usr/local/sbin" nginx -v \
169         || fatal "No nginx. Try: apt-get install nginx"
170     echo -n 'perl: '
171     perl -v | grep version \
172         || fatal "No perl. Try: apt-get install perl"
173     for mod in ExtUtils::MakeMaker JSON LWP Net::SSL; do
174         echo -n "perl $mod: "
175         perl -e "use $mod; print \"\$$mod::VERSION\\n\"" \
176             || fatal "No $mod. Try: apt-get install perl-modules libcrypt-ssleay-perl libjson-perl libwww-perl"
177     done
178     echo -n 'gitolite: '
179     which gitolite \
180         || fatal "No gitolite. Try: apt-get install gitolite3"
181 }
182
183 rotate_logfile() {
184   # i.e.  rotate_logfile "$WORKSPACE/apps/workbench/log/" "test.log"
185   # $BUILD_NUMBER is set by Jenkins if this script is being called as part of a Jenkins run
186   if [[ -f "$1/$2" ]]; then
187     THEDATE=`date +%Y%m%d%H%M%S`
188     mv "$1/$2" "$1/$THEDATE-$BUILD_NUMBER-$2"
189     gzip "$1/$THEDATE-$BUILD_NUMBER-$2"
190   fi
191 }
192
193 declare -a failures
194 declare -A skip
195 declare -A testargs
196 skip[apps/workbench_profile]=1
197
198 while [[ -n "$1" ]]
199 do
200     arg="$1"; shift
201     case "$arg" in
202         --help)
203             echo >&2 "$helpmessage"
204             echo >&2
205             exit 1
206             ;;
207         --skip)
208             skipwhat="$1"; shift
209             skip[$skipwhat]=1
210             ;;
211         --only)
212             only="$1"; skip[$1]=""; shift
213             ;;
214         --skip-install)
215             skip_install=1
216             ;;
217         --only-install)
218             skip_install=1
219             only_install="$1"; shift
220             ;;
221         --temp)
222             temp="$1"; shift
223             temp_preserve=1
224             ;;
225         --leave-temp)
226             temp_preserve=1
227             ;;
228         --retry)
229             retry=1
230             ;;
231         *_test=*)
232             suite="${arg%%_test=*}"
233             args="${arg#*=}"
234             testargs["$suite"]="$args"
235             ;;
236         *=*)
237             eval export $(echo $arg | cut -d= -f1)=\"$(echo $arg | cut -d= -f2-)\"
238             ;;
239         *)
240             echo >&2 "$0: Unrecognized option: '$arg'. Try: $0 --help"
241             exit 1
242             ;;
243     esac
244 done
245
246 start_api() {
247     echo 'Starting API server...'
248     cd "$WORKSPACE" \
249         && eval $(python sdk/python/tests/run_test_server.py start --auth admin) \
250         && export ARVADOS_TEST_API_HOST="$ARVADOS_API_HOST" \
251         && export ARVADOS_TEST_API_INSTALLED="$$" \
252         && (env | egrep ^ARVADOS)
253 }
254
255 start_nginx_proxy_services() {
256     echo 'Starting keepproxy, arv-git-httpd, and nginx ssl proxy...'
257     cd "$WORKSPACE" \
258         && python sdk/python/tests/run_test_server.py start_keep_proxy \
259         && python sdk/python/tests/run_test_server.py start_arv-git-httpd \
260         && python sdk/python/tests/run_test_server.py start_nginx \
261         && export ARVADOS_TEST_PROXY_SERVICES=1
262 }
263
264 stop_services() {
265     if [[ -n "$ARVADOS_TEST_PROXY_SERVICES" ]]; then
266         unset ARVADOS_TEST_PROXY_SERVICES
267         cd "$WORKSPACE" \
268             && python sdk/python/tests/run_test_server.py stop_nginx \
269             && python sdk/python/tests/run_test_server.py stop_arv-git-httpd \
270             && python sdk/python/tests/run_test_server.py stop_keep_proxy
271     fi
272     if [[ -n "$ARVADOS_TEST_API_HOST" ]]; then
273         unset ARVADOS_TEST_API_HOST
274         cd "$WORKSPACE" \
275             && python sdk/python/tests/run_test_server.py stop
276     fi
277 }
278
279 interrupt() {
280     failures+=("($(basename $0) interrupted)")
281     exit_cleanly
282 }
283 trap interrupt INT
284
285 sanity_checks
286
287 echo "WORKSPACE=$WORKSPACE"
288
289 if [[ -z "$CONFIGSRC" ]] && [[ -d "$HOME/arvados-api-server" ]]; then
290     # Jenkins expects us to use this by default.
291     CONFIGSRC="$HOME/arvados-api-server"
292 fi
293
294 # Clean up .pyc files that may exist in the workspace
295 cd "$WORKSPACE"
296 find -name '*.pyc' -delete
297
298 if [[ -z "$temp" ]]; then
299     temp="$(mktemp -d)"
300 fi
301
302 # Set up temporary install dirs (unless existing dirs were supplied)
303 for tmpdir in VENVDIR VENV3DIR GOPATH GEMHOME PERLINSTALLBASE
304 do
305     if [[ -z "${!tmpdir}" ]]; then
306         eval "$tmpdir"="$temp/$tmpdir"
307     fi
308     if ! [[ -d "${!tmpdir}" ]]; then
309         mkdir "${!tmpdir}" || fatal "can't create ${!tmpdir} (does $temp exist?)"
310     fi
311 done
312
313 setup_ruby_environment() {
314     if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then
315       source "$HOME/.rvm/scripts/rvm"
316       using_rvm=true
317     elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then
318       source "/usr/local/rvm/scripts/rvm"
319       using_rvm=true
320     else
321       using_rvm=false
322     fi
323
324     if [[ "$using_rvm" == true ]]; then
325         # If rvm is in use, we can't just put separate "dependencies"
326         # and "gems-under-test" paths to GEM_PATH: passenger resets
327         # the environment to the "current gemset", which would lose
328         # our GEM_PATH and prevent our test suites from running ruby
329         # programs (for example, the Workbench test suite could not
330         # boot an API server or run arv). Instead, we have to make an
331         # rvm gemset and use it for everything.
332
333         [[ `type rvm | head -n1` == "rvm is a function" ]] \
334             || fatal 'rvm check'
335
336         # Put rvm's favorite path back in first place (overriding
337         # virtualenv, which just put itself there). Ignore rvm's
338         # complaint about not being in first place already.
339         rvm use @default 2>/dev/null
340
341         # Create (if needed) and switch to an @arvados-tests
342         # gemset. (Leave the choice of ruby to the caller.)
343         rvm use @arvados-tests --create \
344             || fatal 'rvm gemset setup'
345
346         rvm env
347     else
348         # When our "bundle install"s need to install new gems to
349         # satisfy dependencies, we want them to go where "gem install
350         # --user-install" would put them. (However, if the caller has
351         # already set GEM_HOME, we assume that's where dependencies
352         # should be installed, and we should leave it alone.)
353
354         if [ -z "$GEM_HOME" ]; then
355             user_gempath="$(gem env gempath)"
356             export GEM_HOME="${user_gempath%%:*}"
357         fi
358         PATH="$(gem env gemdir)/bin:$PATH"
359
360         # When we build and install our own gems, we install them in our
361         # $GEMHOME tmpdir, and we want them to be at the front of GEM_PATH and
362         # PATH so integration tests prefer them over other versions that
363         # happen to be installed in $user_gempath, system dirs, etc.
364
365         tmpdir_gem_home="$(env - PATH="$PATH" HOME="$GEMHOME" gem env gempath | cut -f1 -d:)"
366         PATH="$tmpdir_gem_home/bin:$PATH"
367         export GEM_PATH="$tmpdir_gem_home:$(gem env gempath)"
368
369         echo "Will install dependencies to $(gem env gemdir)"
370         echo "Will install arvados gems to $tmpdir_gem_home"
371         echo "Gem search path is GEM_PATH=$GEM_PATH"
372     fi
373 }
374
375 with_test_gemset() {
376     if [[ "$using_rvm" == true ]]; then
377         "$@"
378     else
379         GEM_HOME="$tmpdir_gem_home" GEM_PATH="$tmpdir_gem_home" "$@"
380     fi
381 }
382
383 gem_uninstall_if_exists() {
384     if gem list "$1\$" | egrep '^\w'; then
385         gem uninstall --force --all --executables "$1"
386     fi
387 }
388
389 setup_virtualenv() {
390     local venvdest=$1; shift
391     if ! [[ -e "$venvdest/bin/activate" ]] || ! [[ -e "$venvdest/bin/pip" ]]; then
392         virtualenv --setuptools "$@" "$venvdest" || fatal "virtualenv $venvdest failed"
393     fi
394     "$venvdest/bin/pip" install 'setuptools>=18' 'pip>=7'
395     # ubuntu1404 can't seem to install mock via tests_require, but it can do this.
396     "$venvdest/bin/pip" install 'mock>=1.0'
397 }
398
399 export PERLINSTALLBASE
400 export PERLLIB="$PERLINSTALLBASE/lib/perl5:${PERLLIB:+$PERLLIB}"
401
402 export GOPATH
403 mkdir -p "$GOPATH/src/git.curoverse.com"
404 ln -sfn "$WORKSPACE" "$GOPATH/src/git.curoverse.com/arvados.git" \
405     || fatal "symlink failed"
406
407 setup_virtualenv "$VENVDIR" --python python2.7
408 . "$VENVDIR/bin/activate"
409
410 # Needed for run_test_server.py which is used by certain (non-Python) tests.
411 pip freeze 2>/dev/null | egrep ^PyYAML= \
412     || pip install PyYAML >/dev/null \
413     || fatal "pip install PyYAML failed"
414
415 # Preinstall forked version of libcloud, because nodemanager "pip install"
416 # won't pick it up by default.
417 pip freeze 2>/dev/null | egrep ^apache-libcloud==$LIBCLOUD_PIN \
418     || pip install --pre --ignore-installed https://github.com/curoverse/libcloud/archive/apache-libcloud-$LIBCLOUD_PIN.zip >/dev/null \
419     || fatal "pip install apache-libcloud failed"
420
421 # Deactivate Python 2 virtualenv
422 deactivate
423
424 # If Python 3 is available, set up its virtualenv in $VENV3DIR.
425 # Otherwise, skip dependent tests.
426 PYTHON3=$(which python3)
427 if [ "0" = "$?" ]; then
428     setup_virtualenv "$VENV3DIR" --python python3
429 else
430     PYTHON3=
431     skip[services/dockercleaner]=1
432     cat >&2 <<EOF
433
434 Warning: python3 could not be found
435 services/dockercleaner install and tests will be skipped
436
437 EOF
438 fi
439
440 # Reactivate Python 2 virtualenv
441 . "$VENVDIR/bin/activate"
442
443 # Note: this must be the last time we change PATH, otherwise rvm will
444 # whine a lot.
445 setup_ruby_environment
446
447 echo "PATH is $PATH"
448
449 if ! which bundler >/dev/null
450 then
451     gem install --user-install bundler || fatal 'Could not install bundler'
452 fi
453
454 checkexit() {
455     if [[ "$1" != "0" ]]; then
456         title "!!!!!! $2 FAILED !!!!!!"
457         failures+=("$2 (`timer`)")
458     else
459         successes+=("$2 (`timer`)")
460     fi
461 }
462
463 timer_reset() {
464     t0=$SECONDS
465 }
466
467 timer() {
468     echo -n "$(($SECONDS - $t0))s"
469 }
470
471 do_test() {
472     while ! do_test_once ${@} && [[ "$retry" == 1 ]]
473     do
474         read -p 'Try again? [Y/n] ' x
475         if [[ "$x" != "y" ]] && [[ "$x" != "" ]]
476         then
477             break
478         fi
479     done
480 }
481
482 do_test_once() {
483     unset result
484     if [[ -z "${skip[$1]}" ]] && ( [[ -z "$only" ]] || [[ "$only" == "$1" ]] )
485     then
486         title "Running $1 tests"
487         timer_reset
488         if [[ "$2" == "go" ]]
489         then
490             covername="coverage-$(echo "$1" | sed -e 's/\//_/g')"
491             coverflags=("-covermode=count" "-coverprofile=$WORKSPACE/tmp/.$covername.tmp")
492             # We do "go get -t" here to catch compilation errors
493             # before trying "go test". Otherwise, coverage-reporting
494             # mode makes Go show the wrong line numbers when reporting
495             # compilation errors.
496             if [[ -n "${testargs[$1]}" ]]
497             then
498                 # "go test -check.vv giturl" doesn't work, but this
499                 # does:
500                 cd "$WORKSPACE/$1" && \
501                     go get -t "git.curoverse.com/arvados.git/$1" && \
502                     go test ${coverflags[@]} ${testargs[$1]}
503             else
504                 # The above form gets verbose even when testargs is
505                 # empty, so use this form in such cases:
506                 go get -t "git.curoverse.com/arvados.git/$1" && \
507                     go test ${coverflags[@]} "git.curoverse.com/arvados.git/$1"
508             fi
509             result="$?"
510             go tool cover -html="$WORKSPACE/tmp/.$covername.tmp" -o "$WORKSPACE/tmp/$covername.html"
511             rm "$WORKSPACE/tmp/.$covername.tmp"
512         elif [[ "$2" == "pip" ]]
513         then
514             # $3 can name a path directory for us to use, including trailing
515             # slash; e.g., the bin/ subdirectory of a virtualenv.
516             cd "$WORKSPACE/$1" \
517                 && "${3}python" setup.py test ${testargs[$1]}
518         elif [[ "$2" != "" ]]
519         then
520             "test_$2"
521         else
522             "test_$1"
523         fi
524         result=${result:-$?}
525         checkexit $result "$1 tests"
526         title "End of $1 tests (`timer`)"
527         return $result
528     else
529         title "Skipping $1 tests"
530     fi
531 }
532
533 do_install() {
534     if [[ -z "$skip_install" || (-n "$only_install" && "$only_install" == "$1") ]]
535     then
536         title "Running $1 install"
537         timer_reset
538         if [[ "$2" == "go" ]]
539         then
540             go get -t "git.curoverse.com/arvados.git/$1"
541         elif [[ "$2" == "pip" ]]
542         then
543             # $3 can name a path directory for us to use, including trailing
544             # slash; e.g., the bin/ subdirectory of a virtualenv.
545
546             # Need to change to a different directory after creating
547             # the source dist package to avoid a pip bug.
548             # see https://arvados.org/issues/5766 for details.
549
550             # Also need to install twice, because if it believes the package is
551             # already installed, pip it won't install it.  So the first "pip
552             # install" ensures that the dependencies are met, the second "pip
553             # install" ensures that we've actually installed the local package
554             # we just built.
555             cd "$WORKSPACE/$1" \
556                 && "${3}python" setup.py sdist rotate --keep=1 --match .tar.gz \
557                 && cd "$WORKSPACE" \
558                 && "${3}pip" install --quiet "$WORKSPACE/$1/dist"/*.tar.gz \
559                 && "${3}pip" install --quiet --no-deps --ignore-installed "$WORKSPACE/$1/dist"/*.tar.gz
560         elif [[ "$2" != "" ]]
561         then
562             "install_$2"
563         else
564             "install_$1"
565         fi
566         checkexit $? "$1 install"
567         title "End of $1 install (`timer`)"
568     else
569         title "Skipping $1 install"
570     fi
571 }
572
573 title () {
574     txt="********** $1 **********"
575     printf "\n%*s%s\n\n" $((($COLUMNS-${#txt})/2)) "" "$txt"
576 }
577
578 bundle_install_trylocal() {
579     (
580         set -e
581         echo "(Running bundle install --local. 'could not find package' messages are OK.)"
582         if ! bundle install --local --no-deployment; then
583             echo "(Running bundle install again, without --local.)"
584             bundle install --no-deployment
585         fi
586         bundle package --all
587     )
588 }
589
590 install_doc() {
591     cd "$WORKSPACE/doc" \
592         && bundle_install_trylocal \
593         && rm -rf .site
594 }
595 do_install doc
596
597 install_gem() {
598     gemname=$1
599     srcpath=$2
600     with_test_gemset gem_uninstall_if_exists "$gemname" \
601         && cd "$WORKSPACE/$srcpath" \
602         && bundle_install_trylocal \
603         && gem build "$gemname.gemspec" \
604         && with_test_gemset gem install --no-ri --no-rdoc $(ls -t "$gemname"-*.gem|head -n1)
605 }
606
607 install_ruby_sdk() {
608     install_gem arvados sdk/ruby
609 }
610 do_install sdk/ruby ruby_sdk
611
612 install_perl_sdk() {
613     cd "$WORKSPACE/sdk/perl" \
614         && perl Makefile.PL INSTALL_BASE="$PERLINSTALLBASE" \
615         && make install INSTALLDIRS=perl
616 }
617 do_install sdk/perl perl_sdk
618
619 install_cli() {
620     install_gem arvados-cli sdk/cli
621 }
622 do_install sdk/cli cli
623
624 install_login-sync() {
625     install_gem arvados-login-sync services/login-sync
626 }
627 do_install services/login-sync login-sync
628
629 # Install the Python SDK early. Various other test suites (like
630 # keepproxy) bring up run_test_server.py, which imports the arvados
631 # module. We can't actually *test* the Python SDK yet though, because
632 # its own test suite brings up some of those other programs (like
633 # keepproxy).
634 declare -a pythonstuff
635 pythonstuff=(
636     sdk/pam
637     sdk/python
638     services/fuse
639     services/nodemanager
640     )
641 for p in "${pythonstuff[@]}"
642 do
643     do_install "$p" pip
644 done
645 if [ -n "$PYTHON3" ]; then
646     do_install services/dockercleaner pip "$VENV3DIR/bin/"
647 fi
648
649 install_apiserver() {
650     cd "$WORKSPACE/services/api" \
651         && RAILS_ENV=test bundle_install_trylocal
652
653     rm -f config/environments/test.rb
654     cp config/environments/test.rb.example config/environments/test.rb
655
656     if [ -n "$CONFIGSRC" ]
657     then
658         for f in database.yml application.yml
659         do
660             cp "$CONFIGSRC/$f" config/ || fatal "$f"
661         done
662     fi
663
664     # Fill in a random secret_token and blob_signing_key for testing
665     SECRET_TOKEN=`echo 'puts rand(2**512).to_s(36)' |ruby`
666     BLOB_SIGNING_KEY=`echo 'puts rand(2**512).to_s(36)' |ruby`
667
668     sed -i'' -e "s:SECRET_TOKEN:$SECRET_TOKEN:" config/application.yml
669     sed -i'' -e "s:BLOB_SIGNING_KEY:$BLOB_SIGNING_KEY:" config/application.yml
670
671     # Set up empty git repo (for git tests)
672     GITDIR=$(mktemp -d)
673     sed -i'' -e "s:/var/cache/git:$GITDIR:" config/application.default.yml
674
675     rm -rf $GITDIR
676     mkdir -p $GITDIR/test
677     cd $GITDIR/test \
678         && git init \
679         && git config user.email "jenkins@ci.curoverse.com" \
680         && git config user.name "Jenkins, CI" \
681         && touch tmp \
682         && git add tmp \
683         && git commit -m 'initial commit'
684
685     # Clear out any lingering postgresql connections to the test
686     # database, so that we can drop it. This assumes the current user
687     # is a postgresql superuser.
688     cd "$WORKSPACE/services/api" \
689         && test_database=$(python -c "import yaml; print yaml.load(file('config/database.yml'))['test']['database']") \
690         && 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
691
692     cd "$WORKSPACE/services/api" \
693         && RAILS_ENV=test bundle exec rake db:drop \
694         && RAILS_ENV=test bundle exec rake db:setup \
695         && RAILS_ENV=test bundle exec rake db:fixtures:load
696 }
697 do_install services/api apiserver
698
699 declare -a gostuff
700 gostuff=(
701     sdk/go/arvadosclient
702     sdk/go/streamer
703     services/arv-git-httpd
704     services/crunchstat
705     services/keep-web
706     services/keepstore
707     sdk/go/keepclient
708     services/keepproxy
709     services/datamanager/summary
710     services/datamanager/collection
711     services/datamanager
712     tools/keep-rsync
713     )
714 for g in "${gostuff[@]}"
715 do
716     do_install "$g" go
717 done
718
719 install_workbench() {
720     cd "$WORKSPACE/apps/workbench" \
721         && mkdir -p tmp/cache \
722         && RAILS_ENV=test bundle_install_trylocal
723 }
724 do_install apps/workbench workbench
725
726 test_doclinkchecker() {
727     (
728         set -e
729         cd "$WORKSPACE/doc"
730         ARVADOS_API_HOST=qr1hi.arvadosapi.com
731         # Make sure python-epydoc is installed or the next line won't
732         # do much good!
733         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
734     )
735 }
736 do_test doc doclinkchecker
737
738 stop_services
739
740 test_apiserver() {
741     cd "$WORKSPACE/services/api" \
742         && RAILS_ENV=test bundle exec rake test TESTOPTS=-v ${testargs[services/api]}
743 }
744 do_test services/api apiserver
745
746 # Shortcut for when we're only running apiserver tests. This saves a bit of time,
747 # because we don't need to start up the api server for subsequent tests.
748 if [ ! -z "$only" ] && [ "$only" == "services/api" ]; then
749   rotate_logfile "$WORKSPACE/services/api/log/" "test.log"
750   exit_cleanly
751 fi
752
753 start_api
754
755 test_ruby_sdk() {
756     cd "$WORKSPACE/sdk/ruby" \
757         && bundle exec rake test TESTOPTS=-v ${testargs[sdk/ruby]}
758 }
759 do_test sdk/ruby ruby_sdk
760
761 test_cli() {
762     cd "$WORKSPACE/sdk/cli" \
763         && mkdir -p /tmp/keep \
764         && KEEP_LOCAL_STORE=/tmp/keep bundle exec rake test TESTOPTS=-v ${testargs[sdk/cli]}
765 }
766 do_test sdk/cli cli
767
768 test_login-sync() {
769     cd "$WORKSPACE/services/login-sync" \
770         && bundle exec rake test TESTOPTS=-v ${testargs[services/login-sync]}
771 }
772 do_test services/login-sync login-sync
773
774 for p in "${pythonstuff[@]}"
775 do
776     do_test "$p" pip
777 done
778 do_test services/dockercleaner pip "$VENV3DIR/bin/"
779
780 for g in "${gostuff[@]}"
781 do
782     do_test "$g" go
783 done
784
785 test_workbench() {
786     start_nginx_proxy_services \
787         && cd "$WORKSPACE/apps/workbench" \
788         && RAILS_ENV=test bundle exec rake test TESTOPTS=-v ${testargs[apps/workbench]}
789 }
790 do_test apps/workbench workbench
791
792 test_workbench_benchmark() {
793     start_nginx_proxy_services \
794         && cd "$WORKSPACE/apps/workbench" \
795         && RAILS_ENV=test bundle exec rake test:benchmark ${testargs[apps/workbench_benchmark]}
796 }
797 do_test apps/workbench_benchmark workbench_benchmark
798
799 test_workbench_profile() {
800     start_nginx_proxy_services \
801         && cd "$WORKSPACE/apps/workbench" \
802         && RAILS_ENV=test bundle exec rake test:profile ${testargs[apps/workbench_profile]}
803 }
804 do_test apps/workbench_profile workbench_profile
805
806 exit_cleanly