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