blob: 62393ca2eb9f637559abeab6bcd7ce71e859e13b [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 \
114 <(zip_snapshot_location "https://github.com/openstack/nova.git" "master") \
115 <(echo "https://github.com/openstack/nova/zipball/master")
116}
117
118function test_create_directory_for_kernels {
Mate Lakatfe586b12013-03-28 15:02:27 +0000119 (
120 . mocks
121 mock_out get_local_sr uuid1
122 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
141function test_extract_remote_zipball {
142 local RESULT=$(. mocks && extract_remote_zipball "someurl")
143
144 diff <(cat $LIST_OF_ACTIONS) - << EOF
145wget -nv someurl -O tempfile --no-check-certificate
146unzip -q -o tempfile -d tempdir
147rm -f tempfile
148EOF
149
150 [ "$RESULT" = "tempdir" ]
151}
152
153function test_find_nova_plugins {
154 local tmpdir=$(mktemp -d)
155
156 mkdir -p "$tmpdir/blah/blah/u/xapi.d/plugins"
157
158 [ "$tmpdir/blah/blah/u/xapi.d/plugins" = $(find_xapi_plugins_dir $tmpdir) ]
159
160 rm -rf $tmpdir
161}
162
Mate Lakatfe586b12013-03-28 15:02:27 +0000163function test_get_local_sr {
164 setup_xe_response "uuid123"
165
166 local RESULT=$(. mocks && get_local_sr)
167
168 [ "$RESULT" == "uuid123" ]
169
170 assert_xe_min
171 assert_xe_param "sr-list" "name-label=Local storage"
172}
173
174function test_get_local_sr_path {
175 local RESULT=$(mock_out get_local_sr "uuid1" && get_local_sr_path)
176
177 [ "/var/run/sr-mount/uuid1" == "$RESULT" ]
178}
179
Mate Lakat57e3da92013-03-22 16:34:05 +0000180# Test runner
181[ "$1" = "" ] && {
182 grep -e "^function *test_" $0 | cut -d" " -f2
183}
184
185[ "$1" = "run_tests" ] && {
186 for testname in $($0)
187 do
188 echo "$testname"
189 before_each_test
190 (
191 set -eux
192 $testname
193 )
194 if [ "$?" != "0" ]
195 then
196 echo "FAIL"
197 exit 1
198 else
199 echo "PASS"
200 fi
201
202 after_each_test
203 done
204}