af2b3e2db8fd99e435b7e18cee9c5dfb9e02a15a
[arvados.git] / jenkins / run-tests.sh
1 #!/bin/bash
2
3 # Install and test Arvados components.
4 #
5 # Exit non-zero if any tests fail.
6 #
7 # Arguments:
8 # --skip FOO     Do not test the FOO component.
9 # --only FOO     Do not test anything except the FOO component.
10 # envvar=value   Set $envvar to value
11 #
12 # Regardless of which components are tested, install all components in
13 # the usual sequence. (Many test suites depend on other components
14 # being installed.)
15 #
16 # To run a specific Ruby test, set $workbench_test, $apiserver_test or
17 # $cli_test on the command line:
18 #
19 # $ run-tests.sh --only workbench workbench_test=TEST=test/integration/pipeline_instances_test.rb
20 #
21 #
22 # To run a specific Python test set $python_sdk_test or $fuse_test.
23 #
24 # $ run-tests.sh --only python_sdk python_sdk_test="--test-suite tests.test_keep_locator"
25 #
26 #
27 # You can also pass "export ARVADOS_DEBUG=1" to enable additional debugging output:
28 #
29 # $ run-tests.sh "export ARVADOS_DEBUG=1"
30 #
31 #
32 # Finally, you can skip the installation steps on subsequent runs this way:
33 #
34 ## First run
35 # $ run-tests.sh --leave-temp
36 #
37 ## Subsequent runs: record the values of VENVDIR and GOPATH from the first run, and
38 # provide them on the command line in subsequent runs:
39 #
40 # $ run-tests.sh --skip-install VENVDIR="/tmp/tmp.y3tsTmigio" GOPATH="/tmp/tmp.3r4sSA9F3l"
41
42
43 # First make sure to remove any ARVADOS_ variables from the calling environment
44 # that could interfer with the tests.
45 unset $(env | cut -d= -f1 | grep \^ARVADOS_)
46
47 COLUMNS=80
48
49 export GOPATH=$(mktemp -d)
50 VENVDIR=$(mktemp -d)
51 cli_test=
52 workbench_test=
53 apiserver_test=
54 python_sdk_test=
55 ruby_sdk_test=
56 fuse_test=
57 leave_temp=
58 skip_install=
59
60 if [[ -f /etc/profile.d/rvm.sh ]]
61 then
62     source /etc/profile.d/rvm.sh
63 fi
64
65 fatal() {
66     clear_temp
67     echo >&2 "Fatal: $* in ${FUNCNAME[1]} at ${BASH_SOURCE[1]} line ${BASH_LINENO[0]}"
68     exit 1
69 }
70
71 # Sanity check
72 echo "WORKSPACE=$WORKSPACE"
73 [[ -n "$WORKSPACE" ]] || fatal "WORKSPACE not set"
74
75 # Set up temporary install dirs
76 mkdir -p "$GOPATH/src/git.curoverse.com"
77 ln -sfn "$WORKSPACE" "$GOPATH/src/git.curoverse.com/arvados.git" \
78     || fatal "symlink failed"
79
80 virtualenv --setuptools "$VENVDIR" || fatal "virtualenv $VENVDIR failed"
81 PATH="$VENVDIR/bin:$PATH"
82
83 declare -a failures
84 declare -A skip
85
86 # Always skip CLI tests. They don't know how to use run_test_server.py.
87 skip[cli]=1
88
89 while [[ -n "$1" ]]
90 do
91     arg="$1"; shift
92     case "$arg" in
93         --skip)
94             skipwhat="$1"; shift
95             skip[$skipwhat]=1
96             ;;
97         --only)
98             only="$1"; shift
99             ;;
100         --skip-install)
101             skip_install=1
102             ;;
103         --leave-temp)
104             leave_temp=1
105             ;;
106         *=*)
107             eval $(echo $arg | cut -d= -f1)=\"$(echo $arg | cut -d= -f2-)\"
108             ;;
109         *)
110             echo >&2 "$0: Unrecognized option: '$arg'"
111             exit 1
112             ;;
113     esac
114 done
115
116 checkexit() {
117     if [[ "$?" != "0" ]]; then
118         title "!!!!!! $1 FAILED !!!!!!"
119         failures+=("$1 (`timer`)")
120     else
121         successes+=("$1 (`timer`)")
122     fi
123 }
124
125 timer_reset() {
126     t0=$SECONDS
127 }
128
129 timer() {
130     echo -n "$(($SECONDS - $t0))s"
131 }
132
133 do_test() {
134     if [[ -z "${skip[$1]}" ]] && ( [[ -z "$only" ]] || [[ "$only" == "$1" ]] )
135     then
136         title "Running $1 tests"
137         timer_reset
138         if [[ "$2" == "go" ]]
139         then
140             go test -timeout 20s "git.curoverse.com/arvados.git/$1"
141         else
142             "test_$1"
143         fi
144         checkexit "$1 tests"
145         title "End of $1 tests (`timer`)"
146     else
147         title "Skipping $1 tests"
148     fi
149 }
150
151 do_install() {
152     if [[ -z "$skip_install" ]]
153     then
154         title "Running $1 install"
155         timer_reset
156         if [[ "$2" == "go" ]]
157         then
158             pip install pyyaml
159             go get -t "git.curoverse.com/arvados.git/$1"
160         else
161             "install_$1"
162         fi
163         checkexit "$1 install"
164         title "End of $1 install (`timer`)"
165     else
166         title "Skipping $1 install"
167     fi
168 }
169
170 title () {
171     txt="********** $1 **********"
172     printf "\n%*s%s\n\n" $((($COLUMNS-${#txt})/2)) "" "$txt"
173 }
174
175 clear_temp() {
176     if [[ -z "$leave_temp" ]]
177     then
178         for t in "$VENVDIR" "$GOPATH"
179         do
180             if [[ -n "$t" ]]
181             then
182                 rm -rf "$t"
183             fi
184         done
185     else
186         echo "Leaving VENVDIR=\"$VENVDIR\""
187         echo "Leaving GOPATH=\"$GOPATH\""
188     fi
189 }
190
191 test_docs() {
192     cd "$WORKSPACE/doc"
193     bundle install --no-deployment
194     rm -rf .site
195     # Make sure python-epydoc is installed or the next line won't do much good!
196     ARVADOS_API_HOST=qr1hi.arvadosapi.com
197     PYTHONPATH=$WORKSPACE/sdk/python/ bundle exec rake generate baseurl=file://$WORKSPACE/doc/.site/ arvados_workbench_host=workbench.$ARVADOS_API_HOST arvados_api_host=$ARVADOS_API_HOST
198     unset ARVADOS_API_HOST
199 }
200 do_test docs
201
202 test_doclinkchecker() {
203     cd "$WORKSPACE/doc"
204     bundle exec rake linkchecker baseurl=file://$WORKSPACE/doc/.site/
205 }
206 do_test doclinkchecker
207
208 test_ruby_sdk() {
209     cd "$WORKSPACE/sdk/ruby" \
210         && bundle install --no-deployment \
211         && bundle exec rake test
212 }
213 do_test ruby_sdk
214
215 install_ruby_sdk() {
216     cd "$WORKSPACE/sdk/ruby" \
217         && gem build arvados.gemspec \
218         && gem install --no-ri --no-rdoc `ls -t arvados-*.gem|head -n1`
219 }
220 do_install ruby_sdk
221
222 install_cli() {
223     cd "$WORKSPACE/sdk/cli" \
224         && gem build arvados-cli.gemspec \
225         && gem install --no-ri --no-rdoc `ls -t arvados-cli-*.gem|head -n1`
226 }
227 do_install cli
228
229 test_cli() {
230     title "Starting SDK CLI tests"
231     cd "$WORKSPACE/sdk/cli" \
232         && bundle install --no-deployment \
233         && mkdir -p /tmp/keep \
234         && KEEP_LOCAL_STORE=/tmp/keep bundle exec rake test $cli_test
235 }
236 do_test cli
237
238 install_apiserver() {
239     cd "$WORKSPACE/services/api"
240     bundle install --no-deployment
241
242     rm -f config/environments/test.rb
243     cp config/environments/test.rb.example config/environments/test.rb
244
245     cp $HOME/arvados-api-server/database.yml config/ || fatal "database.yml"
246     cp $HOME/arvados-api-server/application.yml config/ || fatal "application.yml"
247
248     # Fill in a random secret_token and blob_signing_key for testing
249     SECRET_TOKEN=`echo 'puts rand(2**512).to_s(36)' |ruby`
250     BLOB_SIGNING_KEY=`echo 'puts rand(2**512).to_s(36)' |ruby`
251
252     sed -i'' -e "s:SECRET_TOKEN:$SECRET_TOKEN:" config/application.yml
253     sed -i'' -e "s:BLOB_SIGNING_KEY:$BLOB_SIGNING_KEY:" config/application.yml
254
255     export RAILS_ENV=test
256
257     # Set up empty git repo (for git tests)
258     GITDIR="$WORKSPACE/tmpgit"
259     sed -i'' -e "s:/var/cache/git:$GITDIR:" config/application.default.yml
260
261     rm -rf $GITDIR
262     mkdir -p $GITDIR/test
263     cd $GITDIR/test \
264         && git init \
265         && git config user.email "jenkins@ci.curoverse.com" \
266         && git config user.name "Jenkins, CI" \
267         && touch tmp \
268         && git add tmp \
269         && git commit -m 'initial commit'
270
271     # Clear out any lingering postgresql connections to arvados_test, so that we can drop it
272     # This assumes the current user is a postgresql superuser
273     psql arvados_test -c "SELECT pg_terminate_backend (pg_stat_activity.procpid::int) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'arvados_test';" 2>/dev/null
274
275     cd "$WORKSPACE/services/api" \
276         && bundle exec rake db:drop \
277         && bundle exec rake db:create \
278         && bundle exec rake db:setup
279 }
280 do_install apiserver
281
282 test_apiserver() {
283     cd "$WORKSPACE/services/api"
284     bundle exec rake test $apiserver_test
285 }
286 do_test apiserver
287
288 declare -a gostuff
289 gostuff=(
290     services/keepstore
291     services/keepproxy
292     sdk/go/arvadosclient
293     sdk/go/keepclient
294     sdk/go/streamer
295     )
296 for g in "${gostuff[@]}"
297 do
298     do_install "$g" go
299 done
300
301 install_python_sdk() {
302     # Install the Python SDK early. Various other test suites (like
303     # keepproxy) bring up run_test_server.py, which imports the arvados
304     # module. We can't actually *test* the Python SDK yet though, because
305     # its own test suite brings up some of those other programs (like
306     # keepproxy).
307
308     cd "$WORKSPACE/sdk/python" \
309         && python setup.py egg_info -b ".$(git log --format=format:%ct.%h -n1 .)" sdist rotate --keep=1 --match .tar.gz \
310         && pip install dist/arvados-python-client-0.1.*.tar.gz
311 }
312 do_install python_sdk
313
314 install_fuse() {
315     cd "$WORKSPACE/services/fuse" \
316         && python setup.py egg_info -b ".$(git log --format=format:%ct.%h -n1 .)" sdist rotate --keep=1 --match .tar.gz \
317         && pip install dist/arvados_fuse-0.1.*.tar.gz
318 }
319 do_install fuse
320
321 test_python_sdk() {
322     # Python SDK. We test this before testing keepproxy: keepproxy runs
323     # run_test_server.py, which depends on the yaml package, which is in
324     # tests_require but not install_requires, and therefore does not get
325     # installed by setuptools until we run "setup.py test" *and* install
326     # the .egg files that setup.py downloads.
327
328     cd "$WORKSPACE/sdk/python" \
329         && python setup.py test $python_sdk_test
330     r=$?
331     easy_install *.egg
332     return $r
333 }
334 do_test python_sdk
335
336 test_fuse() {
337     # Install test dependencies here too, in case run_test_server needs them.
338     cd "$WORKSPACE/services/fuse" \
339         && python setup.py test $fuse_test
340     r=$?
341     easy_install *.egg
342     return $r
343 }
344 do_test fuse
345
346 for g in "${gostuff[@]}"
347 do
348     do_test "$g" go
349 done
350
351 test_workbench() {
352     cd "$WORKSPACE/apps/workbench" \
353         && bundle install --no-deployment \
354         && bundle exec rake test $workbench_test
355 }
356 do_test workbench
357
358 clear_temp
359
360 for x in "${successes[@]}"
361 do
362     echo "Pass: $x"
363 done
364
365 if [[ ${#failures[@]} == 0 ]]
366 then
367     echo "All test suites passed."
368 else
369     echo "Failures (${#failures[@]}):"
370     for x in "${failures[@]}"
371     do
372         echo "Fail: $x"
373     done
374 fi
375 exit ${#failures}