Declare empty variables up top to avoid bash parsing errors. no issue #
[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 # Useful environment variables include $workbench_test, $apiserver_test and
17 # $cli_test.  To run a specific test, use a command line such as:
18 #
19 # $ ./run-tests.sh --only workbench workbench_test=TEST=test/integration/pipeline_instances_test.rb
20
21
22 # First make sure to remove any ARVADOS_ variables from the calling environment
23 # that could interfer with the tests.
24 unset $(env | cut -d= -f1 | grep \^ARVADOS_)
25
26 COLUMNS=80
27
28 export GOPATH=$(mktemp -d)
29 VENVDIR=$(mktemp -d)
30 cli_test=
31 workbench_test=
32 apiserver_test=
33
34 source /etc/profile.d/rvm.sh
35
36 fatal() {
37     clear_temp
38     echo >&2 "Fatal: $* in ${FUNCNAME[1]} at ${BASH_SOURCE[1]} line ${BASH_LINENO[0]}"
39     exit 1
40 }
41
42 # Sanity check
43 echo "WORKSPACE=$WORKSPACE"
44 [[ -n "$WORKSPACE" ]] || fatal "WORKSPACE not set"
45
46 # Set up temporary install dirs
47 mkdir -p "$GOPATH/src/git.curoverse.com"
48 ln -sfn "$WORKSPACE" "$GOPATH/src/git.curoverse.com/arvados.git" \
49     || fatal "symlink failed"
50
51 virtualenv --setuptools "$VENVDIR" || fatal "virtualenv $VENVDIR failed"
52 PATH="$VENVDIR/bin:$PATH"
53
54 declare -a failures
55 declare -A skip
56
57 # Always skip CLI tests. They don't know how to use run_test_server.py.
58 skip[cli]=1
59
60 while [[ -n "$1" ]]
61 do
62     arg="$1"; shift
63     case "$arg" in
64         --skip)
65             skipwhat="$1"; shift
66             skip[$skipwhat]=1
67             ;;
68         --only)
69             only="$1"; shift
70             ;;
71         --skip-install)
72             skip_install=1
73             ;;
74         --leave-temp)
75             leave_temp=1
76             ;;
77         *=*)
78             eval $arg
79             ;;
80         *)
81             echo >&2 "$0: Unrecognized option: '$arg'"
82             exit 1
83             ;;
84     esac
85 done
86
87 checkexit() {
88     if [[ "$?" != "0" ]]; then
89         title "!!!!!! $1 FAILED !!!!!!"
90         failures+=("$1 (`timer`)")
91     else
92         successes+=("$1 (`timer`)")
93     fi
94 }
95
96 timer_reset() {
97     t0=$SECONDS
98 }
99
100 timer() {
101     echo -n "$(($SECONDS - $t0))s"
102 }
103
104 do_test() {
105     if [[ -z "${skip[$1]}" ]] && ( [[ -z "$only" ]] || [[ "$only" == "$1" ]] )
106     then
107         title "Running $1 tests"
108         timer_reset
109         if [[ "$2" == "go" ]]
110         then
111             go test "git.curoverse.com/arvados.git/$1"
112         else
113             "test_$1"
114         fi
115         checkexit "$1 tests"
116         title "End of $1 tests (`timer`)"
117     else
118         title "Skipping $1 tests"
119     fi
120 }
121
122 do_install() {
123     if [[ -z "$skip_install" ]]
124     then
125         title "Running $1 install"
126         timer_reset
127         if [[ "$2" == "go" ]]
128         then
129             go get -t "git.curoverse.com/arvados.git/$1"
130         else
131             "install_$1"
132         fi
133         checkexit "$1 install"
134         title "End of $1 install (`timer`)"
135     else
136         title "Skipping $1 install"
137     fi
138 }
139
140 title () {
141     txt="********** $1 **********"
142     printf "\n%*s%s\n\n" $((($COLUMNS-${#txt})/2)) "" "$txt"
143 }
144
145 clear_temp() {
146     for t in "$VENVDIR" "$GOPATH"
147     do
148         if [[ -n "$t" && -z "$leave-temp" ]]
149         then
150             rm -rf "$t"
151         else
152             echo "Leaving $t"
153         fi
154     done
155 }
156
157 test_docs() {
158     cd "$WORKSPACE/doc"
159     bundle install --deployment
160     rm -rf .site
161     # Make sure python-epydoc is installed or the next line won't do much good!
162     ARVADOS_API_HOST=qr1hi.arvadosapi.com
163     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
164     unset ARVADOS_API_HOST
165 }
166 do_test docs
167
168 test_doclinkchecker() {
169     cd "$WORKSPACE/doc"
170     bundle exec rake linkchecker baseurl=file://$WORKSPACE/doc/.site/
171 }
172 do_test doclinkchecker
173
174 install_apiserver() {
175     cd "$WORKSPACE/services/api"
176     bundle install --deployment
177
178     rm -f config/environments/test.rb
179     cp config/environments/test.rb.example config/environments/test.rb
180
181     cp $HOME/arvados-api-server/database.yml config/ || fatal "database.yml"
182     cp $HOME/arvados-api-server/application.yml config/ || fatal "application.yml"
183
184     # Fill in a random secret_token and blob_signing_key for testing
185     SECRET_TOKEN=`echo 'puts rand(2**512).to_s(36)' |ruby`
186     BLOB_SIGNING_KEY=`echo 'puts rand(2**512).to_s(36)' |ruby`
187
188     sed -i'' -e "s:SECRET_TOKEN:$SECRET_TOKEN:" config/application.yml
189     sed -i'' -e "s:BLOB_SIGNING_KEY:$BLOB_SIGNING_KEY:" config/application.yml
190
191     export RAILS_ENV=test
192
193     # Set up empty git repo (for git tests)
194     GITDIR="$WORKSPACE/tmpgit"
195     sed -i'' -e "s:/var/cache/git:$GITDIR:" config/application.default.yml
196
197     rm -rf $GITDIR
198     mkdir -p $GITDIR/test
199     cd $GITDIR/test \
200         && git init \
201         && git config user.email "jenkins@ci.curoverse.com" \
202         && git config user.name "Jenkins, CI" \
203         && touch tmp \
204         && git add tmp \
205         && git commit -m 'initial commit'
206
207     cd "$WORKSPACE/services/api" \
208         && bundle exec rake db:drop \
209         && bundle exec rake db:create \
210         && bundle exec rake db:setup
211 }
212 do_install apiserver
213
214 test_apiserver() {
215     cd "$WORKSPACE/services/api"
216     bundle exec rake test $apiserver_test
217 }
218 do_test apiserver
219
220 install_cli() {
221     cd "$WORKSPACE/sdk/cli"
222     bundle install --deployment
223 }
224 do_install cli
225
226 declare -a gostuff
227 gostuff=(
228     services/keepstore
229     services/keepproxy
230     sdk/go/arvadosclient
231     sdk/go/keepclient
232     sdk/go/streamer
233     )
234 for g in "${gostuff[@]}"
235 do
236     do_install "$g" go
237 done
238
239 install_python_sdk() {
240     # Install the Python SDK early. Various other test suites (like
241     # keepproxy) bring up run_test_server.py, which imports the arvados
242     # module. We can't actually *test* the Python SDK yet though, because
243     # its own test suite brings up some of those other programs (like
244     # keepproxy).
245
246     cd "$WORKSPACE/sdk/python" \
247         && python setup.py egg_info -b ".$(git log --format=format:%ct.%h -n1 .)" sdist rotate --keep=1 --match .tar.gz \
248         && pip install dist/arvados-python-client-0.1.*.tar.gz
249 }
250 do_install python_sdk
251
252 install_fuse() {
253     cd "$WORKSPACE/services/fuse" \
254         && python setup.py egg_info -b ".$(git log --format=format:%ct.%h -n1 .)" sdist rotate --keep=1 --match .tar.gz \
255         && pip install dist/arvados_fuse-0.1.*.tar.gz
256 }
257 do_install fuse
258
259 test_python_sdk() {
260     # Python SDK. We test this before testing keepproxy: keepproxy runs
261     # run_test_server.py, which depends on the yaml package, which is in
262     # tests_require but not install_requires, and therefore does not get
263     # installed by setuptools until we run "setup.py test" *and* install
264     # the .egg files that setup.py downloads.
265
266     cd "$WORKSPACE/sdk/python" \
267         && python setup.py test
268     r=$?
269     easy_install *.egg
270     return $r
271 }
272 do_test python_sdk
273
274 test_fuse() {
275     # Install test dependencies here too, in case run_test_server needs them.
276     cd "$WORKSPACE/services/fuse" \
277         && python setup.py test
278     r=$?
279     easy_install *.egg
280     return $r
281 }
282 do_test fuse
283
284 for g in "${gostuff[@]}"
285 do
286     do_test "$g" go
287 done
288
289 test_workbench() {
290     cd "$WORKSPACE/apps/workbench" \
291         && bundle install --deployment \
292         && bundle exec rake test $workbench_test
293 }
294 do_test workbench
295
296 test_cli() {
297     title "Starting SDK CLI tests"
298     cd "$WORKSPACE/sdk/cli" \
299         && bundle install --deployment \
300         && mkdir -p /tmp/keep \
301         && KEEP_LOCAL_STORE=/tmp/keep bundle exec rake test $cli_test
302 }
303 do_test cli
304
305 clear_temp
306
307 for x in "${successes[@]}"
308 do
309     echo "Pass: $x"
310 done
311
312 if [[ ${#failures[@]} == 0 ]]
313 then
314     echo "All test suites passed."
315 else
316     echo "Failures (${#failures[@]}):"
317     for x in "${failures[@]}"
318     do
319         echo "Fail: $x"
320     done
321 fi
322 exit ${#failures}