blob: 373d996760dc77cd2f892bca845611e8d0b6d5cd [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 Lakat2781f3b2013-12-11 13:41:54 +000032
33 DEAD_MESSAGES=$(mktemp)
34 truncate -s 0 $DEAD_MESSAGES
Mate Lakat57e3da92013-03-22 16:34:05 +000035}
36
37# Teardown
38function after_each_test {
39 rm -f $LIST_OF_DIRECTORIES
40 rm -f $LIST_OF_ACTIONS
Mate Lakatfe586b12013-03-28 15:02:27 +000041 rm -f $XE_RESPONSE
42 rm -f $XE_CALLS
Mate Lakat57e3da92013-03-22 16:34:05 +000043}
44
45# Helpers
Mate Lakatfe586b12013-03-28 15:02:27 +000046function setup_xe_response {
47 echo "$1" > $XE_RESPONSE
48}
49
Mate Lakat57e3da92013-03-22 16:34:05 +000050function given_directory_exists {
51 echo "$1" >> $LIST_OF_DIRECTORIES
52}
53
54function assert_directory_exists {
55 grep "$1" $LIST_OF_DIRECTORIES
56}
57
58function assert_previous_command_failed {
59 [ "$?" != "0" ] || exit 1
60}
61
Mate Lakatfe586b12013-03-28 15:02:27 +000062function assert_xe_min {
63 grep -qe "^--minimal\$" $XE_CALLS
64}
65
66function assert_xe_param {
67 grep -qe "^$1\$" $XE_CALLS
68}
69
Mate Lakat2781f3b2013-12-11 13:41:54 +000070function assert_died_with {
71 diff -u <(echo "$1") $DEAD_MESSAGES
72}
73
Mate Lakatfe586b12013-03-28 15:02:27 +000074function mock_out {
75 local FNNAME="$1"
76 local OUTPUT="$2"
77
78 . <(cat << EOF
79function $FNNAME {
80 echo "$OUTPUT"
81}
82EOF
83)
84}
85
86function assert_symlink {
87 grep -qe "^ln -s $2 $1\$" $LIST_OF_ACTIONS
88}
89
Mate Lakat57e3da92013-03-22 16:34:05 +000090# Tests
91function test_plugin_directory_on_xenserver {
92 given_directory_exists "/etc/xapi.d/plugins/"
93
94 PLUGDIR=$(. mocks && xapi_plugin_location)
95
96 [ "/etc/xapi.d/plugins/" = "$PLUGDIR" ]
97}
98
99function test_plugin_directory_on_xcp {
100 given_directory_exists "/usr/lib/xcp/plugins/"
101
102 PLUGDIR=$(. mocks && xapi_plugin_location)
103
104 [ "/usr/lib/xcp/plugins/" = "$PLUGDIR" ]
105}
106
107function test_no_plugin_directory_found {
108 set +e
109
110 local IGNORE
111 IGNORE=$(. mocks && xapi_plugin_location)
112
113 assert_previous_command_failed
114
115 grep "[ -d /etc/xapi.d/plugins/ ]" $LIST_OF_ACTIONS
116 grep "[ -d /usr/lib/xcp/plugins/ ]" $LIST_OF_ACTIONS
117}
118
Mate Lakat2781f3b2013-12-11 13:41:54 +0000119function test_zip_snapshot_location_http {
Mate Lakat57e3da92013-03-22 16:34:05 +0000120 diff \
Mate Lakat2781f3b2013-12-11 13:41:54 +0000121 <(zip_snapshot_location "http://github.com/openstack/nova.git" "master") \
122 <(echo "http://github.com/openstack/nova/zipball/master")
123}
124
125function test_zip_snapsot_location_git {
126 diff \
127 <(zip_snapshot_location "git://github.com/openstack/nova.git" "master") \
128 <(echo "http://github.com/openstack/nova/zipball/master")
Mate Lakat57e3da92013-03-22 16:34:05 +0000129}
130
131function test_create_directory_for_kernels {
Mate Lakatfe586b12013-03-28 15:02:27 +0000132 (
133 . mocks
Mate Lakat085abd82013-12-11 12:21:12 +0000134 mock_out get_local_sr_path /var/run/sr-mount/uuid1
Mate Lakatfe586b12013-03-28 15:02:27 +0000135 create_directory_for_kernels
136 )
Mate Lakat57e3da92013-03-22 16:34:05 +0000137
Mate Lakatfe586b12013-03-28 15:02:27 +0000138 assert_directory_exists "/var/run/sr-mount/uuid1/os-guest-kernels"
139 assert_symlink "/boot/guest" "/var/run/sr-mount/uuid1/os-guest-kernels"
140}
141
142function test_create_directory_for_kernels_existing_dir {
143 (
144 . mocks
145 given_directory_exists "/boot/guest"
146 create_directory_for_kernels
147 )
148
149 diff -u $LIST_OF_ACTIONS - << EOF
150[ -d /boot/guest ]
151EOF
Mate Lakat57e3da92013-03-22 16:34:05 +0000152}
153
Bob Ball39aeda22013-06-17 12:51:33 +0100154function test_create_directory_for_images {
155 (
156 . mocks
Mate Lakat085abd82013-12-11 12:21:12 +0000157 mock_out get_local_sr_path /var/run/sr-mount/uuid1
Bob Ball39aeda22013-06-17 12:51:33 +0100158 create_directory_for_images
159 )
160
161 assert_directory_exists "/var/run/sr-mount/uuid1/os-images"
162 assert_symlink "/images" "/var/run/sr-mount/uuid1/os-images"
163}
164
165function test_create_directory_for_images_existing_dir {
166 (
167 . mocks
168 given_directory_exists "/images"
169 create_directory_for_images
170 )
171
172 diff -u $LIST_OF_ACTIONS - << EOF
173[ -d /images ]
174EOF
175}
176
Mate Lakat57e3da92013-03-22 16:34:05 +0000177function test_extract_remote_zipball {
178 local RESULT=$(. mocks && extract_remote_zipball "someurl")
179
180 diff <(cat $LIST_OF_ACTIONS) - << EOF
181wget -nv someurl -O tempfile --no-check-certificate
182unzip -q -o tempfile -d tempdir
183rm -f tempfile
184EOF
185
186 [ "$RESULT" = "tempdir" ]
187}
188
Euan Harris6f001712013-07-10 16:30:31 +0100189function test_extract_remote_zipball_wget_fail {
190 set +e
191
192 local IGNORE
193 IGNORE=$(. mocks && extract_remote_zipball "failurl")
194
Mate Lakat2781f3b2013-12-11 13:41:54 +0000195 assert_died_with "Failed to download [failurl]"
Euan Harris6f001712013-07-10 16:30:31 +0100196}
197
Mate Lakat57e3da92013-03-22 16:34:05 +0000198function test_find_nova_plugins {
199 local tmpdir=$(mktemp -d)
200
201 mkdir -p "$tmpdir/blah/blah/u/xapi.d/plugins"
202
203 [ "$tmpdir/blah/blah/u/xapi.d/plugins" = $(find_xapi_plugins_dir $tmpdir) ]
204
205 rm -rf $tmpdir
206}
207
Mate Lakatfe586b12013-03-28 15:02:27 +0000208function test_get_local_sr {
209 setup_xe_response "uuid123"
210
211 local RESULT=$(. mocks && get_local_sr)
212
213 [ "$RESULT" == "uuid123" ]
214
Mate Lakat085abd82013-12-11 12:21:12 +0000215 assert_xe_param "pool-list" params=default-SR minimal=true
Mate Lakatfe586b12013-03-28 15:02:27 +0000216}
217
218function test_get_local_sr_path {
219 local RESULT=$(mock_out get_local_sr "uuid1" && get_local_sr_path)
220
221 [ "/var/run/sr-mount/uuid1" == "$RESULT" ]
222}
223
Mate Lakat57e3da92013-03-22 16:34:05 +0000224# Test runner
225[ "$1" = "" ] && {
226 grep -e "^function *test_" $0 | cut -d" " -f2
227}
228
229[ "$1" = "run_tests" ] && {
230 for testname in $($0)
231 do
232 echo "$testname"
233 before_each_test
234 (
235 set -eux
236 $testname
237 )
238 if [ "$?" != "0" ]
239 then
240 echo "FAIL"
241 exit 1
242 else
243 echo "PASS"
244 fi
245
246 after_each_test
247 done
248}