blob: 14551868e14836097739bd80f0735e0954617e88 [file] [log] [blame]
Mate Lakat57e3da92013-03-22 16:34:05 +00001#!/bin/bash
2
3# Tests for functions.
4#
5# The tests are sourcing the mocks file to mock out various functions. The
6# mocking-out always happens in a sub-shell, thus it does not have impact on
7# the functions defined here.
8
9# To run the tests, please run:
10#
11# ./test_functions.sh run_tests
12#
13# To only print out the discovered test functions, run:
14#
15# ./test_functions.sh
16
17. functions
18
19# Setup
20function before_each_test {
21 LIST_OF_DIRECTORIES=$(mktemp)
22 truncate -s 0 $LIST_OF_DIRECTORIES
23
24 LIST_OF_ACTIONS=$(mktemp)
25 truncate -s 0 $LIST_OF_ACTIONS
Mate Lakatfe586b12013-03-28 15:02:27 +000026
27 XE_RESPONSE=$(mktemp)
28 truncate -s 0 $XE_RESPONSE
29
30 XE_CALLS=$(mktemp)
31 truncate -s 0 $XE_CALLS
Mate Lakat57e3da92013-03-22 16:34:05 +000032}
33
34# Teardown
35function after_each_test {
36 rm -f $LIST_OF_DIRECTORIES
37 rm -f $LIST_OF_ACTIONS
Mate Lakatfe586b12013-03-28 15:02:27 +000038 rm -f $XE_RESPONSE
39 rm -f $XE_CALLS
Mate Lakat57e3da92013-03-22 16:34:05 +000040}
41
42# Helpers
Mate Lakatfe586b12013-03-28 15:02:27 +000043function setup_xe_response {
44 echo "$1" > $XE_RESPONSE
45}
46
Mate Lakat57e3da92013-03-22 16:34:05 +000047function given_directory_exists {
48 echo "$1" >> $LIST_OF_DIRECTORIES
49}
50
51function assert_directory_exists {
52 grep "$1" $LIST_OF_DIRECTORIES
53}
54
55function assert_previous_command_failed {
56 [ "$?" != "0" ] || exit 1
57}
58
Mate Lakatfe586b12013-03-28 15:02:27 +000059function assert_xe_min {
60 grep -qe "^--minimal\$" $XE_CALLS
61}
62
63function assert_xe_param {
64 grep -qe "^$1\$" $XE_CALLS
65}
66
67function mock_out {
68 local FNNAME="$1"
69 local OUTPUT="$2"
70
71 . <(cat << EOF
72function $FNNAME {
73 echo "$OUTPUT"
74}
75EOF
76)
77}
78
79function assert_symlink {
80 grep -qe "^ln -s $2 $1\$" $LIST_OF_ACTIONS
81}
82
Mate Lakat57e3da92013-03-22 16:34:05 +000083# Tests
84function test_plugin_directory_on_xenserver {
85 given_directory_exists "/etc/xapi.d/plugins/"
86
87 PLUGDIR=$(. mocks && xapi_plugin_location)
88
89 [ "/etc/xapi.d/plugins/" = "$PLUGDIR" ]
90}
91
92function test_plugin_directory_on_xcp {
93 given_directory_exists "/usr/lib/xcp/plugins/"
94
95 PLUGDIR=$(. mocks && xapi_plugin_location)
96
97 [ "/usr/lib/xcp/plugins/" = "$PLUGDIR" ]
98}
99
100function test_no_plugin_directory_found {
101 set +e
102
103 local IGNORE
104 IGNORE=$(. mocks && xapi_plugin_location)
105
106 assert_previous_command_failed
107
108 grep "[ -d /etc/xapi.d/plugins/ ]" $LIST_OF_ACTIONS
109 grep "[ -d /usr/lib/xcp/plugins/ ]" $LIST_OF_ACTIONS
110}
111
112function test_zip_snapshot_location {
113 diff \
Steve Kowalik047cac52013-11-07 22:36:10 +1100114 <(zip_snapshot_location "git://git.openstack.org/openstack/nova.git" "master") \
115 <(echo "git://git.openstack.org/openstack/nova/zipball/master")
Mate Lakat57e3da92013-03-22 16:34:05 +0000116}
117
118function test_create_directory_for_kernels {
Mate Lakatfe586b12013-03-28 15:02:27 +0000119 (
120 . mocks
Mate Lakat085abd82013-12-11 12:21:12 +0000121 mock_out get_local_sr_path /var/run/sr-mount/uuid1
Mate Lakatfe586b12013-03-28 15:02:27 +0000122 create_directory_for_kernels
123 )
Mate Lakat57e3da92013-03-22 16:34:05 +0000124
Mate Lakatfe586b12013-03-28 15:02:27 +0000125 assert_directory_exists "/var/run/sr-mount/uuid1/os-guest-kernels"
126 assert_symlink "/boot/guest" "/var/run/sr-mount/uuid1/os-guest-kernels"
127}
128
129function test_create_directory_for_kernels_existing_dir {
130 (
131 . mocks
132 given_directory_exists "/boot/guest"
133 create_directory_for_kernels
134 )
135
136 diff -u $LIST_OF_ACTIONS - << EOF
137[ -d /boot/guest ]
138EOF
Mate Lakat57e3da92013-03-22 16:34:05 +0000139}
140
Bob Ball39aeda22013-06-17 12:51:33 +0100141function test_create_directory_for_images {
142 (
143 . mocks
Mate Lakat085abd82013-12-11 12:21:12 +0000144 mock_out get_local_sr_path /var/run/sr-mount/uuid1
Bob Ball39aeda22013-06-17 12:51:33 +0100145 create_directory_for_images
146 )
147
148 assert_directory_exists "/var/run/sr-mount/uuid1/os-images"
149 assert_symlink "/images" "/var/run/sr-mount/uuid1/os-images"
150}
151
152function test_create_directory_for_images_existing_dir {
153 (
154 . mocks
155 given_directory_exists "/images"
156 create_directory_for_images
157 )
158
159 diff -u $LIST_OF_ACTIONS - << EOF
160[ -d /images ]
161EOF
162}
163
Mate Lakat57e3da92013-03-22 16:34:05 +0000164function test_extract_remote_zipball {
165 local RESULT=$(. mocks && extract_remote_zipball "someurl")
166
167 diff <(cat $LIST_OF_ACTIONS) - << EOF
168wget -nv someurl -O tempfile --no-check-certificate
169unzip -q -o tempfile -d tempdir
170rm -f tempfile
171EOF
172
173 [ "$RESULT" = "tempdir" ]
174}
175
Euan Harris6f001712013-07-10 16:30:31 +0100176function test_extract_remote_zipball_wget_fail {
177 set +e
178
179 local IGNORE
180 IGNORE=$(. mocks && extract_remote_zipball "failurl")
181
182 assert_previous_command_failed
183}
184
Mate Lakat57e3da92013-03-22 16:34:05 +0000185function test_find_nova_plugins {
186 local tmpdir=$(mktemp -d)
187
188 mkdir -p "$tmpdir/blah/blah/u/xapi.d/plugins"
189
190 [ "$tmpdir/blah/blah/u/xapi.d/plugins" = $(find_xapi_plugins_dir $tmpdir) ]
191
192 rm -rf $tmpdir
193}
194
Mate Lakatfe586b12013-03-28 15:02:27 +0000195function test_get_local_sr {
196 setup_xe_response "uuid123"
197
198 local RESULT=$(. mocks && get_local_sr)
199
200 [ "$RESULT" == "uuid123" ]
201
Mate Lakat085abd82013-12-11 12:21:12 +0000202 assert_xe_param "pool-list" params=default-SR minimal=true
Mate Lakatfe586b12013-03-28 15:02:27 +0000203}
204
205function test_get_local_sr_path {
206 local RESULT=$(mock_out get_local_sr "uuid1" && get_local_sr_path)
207
208 [ "/var/run/sr-mount/uuid1" == "$RESULT" ]
209}
210
Mate Lakat57e3da92013-03-22 16:34:05 +0000211# Test runner
212[ "$1" = "" ] && {
213 grep -e "^function *test_" $0 | cut -d" " -f2
214}
215
216[ "$1" = "run_tests" ] && {
217 for testname in $($0)
218 do
219 echo "$testname"
220 before_each_test
221 (
222 set -eux
223 $testname
224 )
225 if [ "$?" != "0" ]
226 then
227 echo "FAIL"
228 exit 1
229 else
230 echo "PASS"
231 fi
232
233 after_each_test
234 done
235}