blob: 324e6a1a1e5b00a352cc4781d6ecf380166addc4 [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 Lakat57e3da92013-03-22 16:34:05 +0000119function test_create_directory_for_kernels {
Mate Lakatfe586b12013-03-28 15:02:27 +0000120 (
121 . mocks
Mate Lakat085abd82013-12-11 12:21:12 +0000122 mock_out get_local_sr_path /var/run/sr-mount/uuid1
Mate Lakatfe586b12013-03-28 15:02:27 +0000123 create_directory_for_kernels
124 )
Mate Lakat57e3da92013-03-22 16:34:05 +0000125
Mate Lakatfe586b12013-03-28 15:02:27 +0000126 assert_directory_exists "/var/run/sr-mount/uuid1/os-guest-kernels"
127 assert_symlink "/boot/guest" "/var/run/sr-mount/uuid1/os-guest-kernels"
128}
129
130function test_create_directory_for_kernels_existing_dir {
131 (
132 . mocks
133 given_directory_exists "/boot/guest"
134 create_directory_for_kernels
135 )
136
137 diff -u $LIST_OF_ACTIONS - << EOF
138[ -d /boot/guest ]
139EOF
Mate Lakat57e3da92013-03-22 16:34:05 +0000140}
141
Bob Ball39aeda22013-06-17 12:51:33 +0100142function test_create_directory_for_images {
143 (
144 . mocks
Mate Lakat085abd82013-12-11 12:21:12 +0000145 mock_out get_local_sr_path /var/run/sr-mount/uuid1
Bob Ball39aeda22013-06-17 12:51:33 +0100146 create_directory_for_images
147 )
148
149 assert_directory_exists "/var/run/sr-mount/uuid1/os-images"
150 assert_symlink "/images" "/var/run/sr-mount/uuid1/os-images"
151}
152
153function test_create_directory_for_images_existing_dir {
154 (
155 . mocks
156 given_directory_exists "/images"
157 create_directory_for_images
158 )
159
160 diff -u $LIST_OF_ACTIONS - << EOF
161[ -d /images ]
162EOF
163}
164
Mate Lakatfe586b12013-03-28 15:02:27 +0000165function test_get_local_sr {
166 setup_xe_response "uuid123"
167
Ian Wienandada886d2015-10-07 14:06:26 +1100168 local RESULT
169 RESULT=$(. mocks && get_local_sr)
Mate Lakatfe586b12013-03-28 15:02:27 +0000170
171 [ "$RESULT" == "uuid123" ]
172
Mate Lakat085abd82013-12-11 12:21:12 +0000173 assert_xe_param "pool-list" params=default-SR minimal=true
Mate Lakatfe586b12013-03-28 15:02:27 +0000174}
175
176function test_get_local_sr_path {
Ian Wienandada886d2015-10-07 14:06:26 +1100177 local RESULT
178 RESULT=$(mock_out get_local_sr "uuid1" && get_local_sr_path)
Mate Lakatfe586b12013-03-28 15:02:27 +0000179
180 [ "/var/run/sr-mount/uuid1" == "$RESULT" ]
181}
182
Mate Lakat57e3da92013-03-22 16:34:05 +0000183# Test runner
184[ "$1" = "" ] && {
185 grep -e "^function *test_" $0 | cut -d" " -f2
186}
187
188[ "$1" = "run_tests" ] && {
Sean Dague16dd8b32014-02-03 09:10:54 +0900189 for testname in $($0); do
Mate Lakat57e3da92013-03-22 16:34:05 +0000190 echo "$testname"
191 before_each_test
192 (
193 set -eux
194 $testname
195 )
Sean Dague16dd8b32014-02-03 09:10:54 +0900196 if [ "$?" != "0" ]; then
Mate Lakat57e3da92013-03-22 16:34:05 +0000197 echo "FAIL"
198 exit 1
199 else
200 echo "PASS"
201 fi
202
203 after_each_test
204 done
205}