| Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 1 | #!/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 | 
|  | 20 | function 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 Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 26 |  | 
|  | 27 | XE_RESPONSE=$(mktemp) | 
|  | 28 | truncate -s 0 $XE_RESPONSE | 
|  | 29 |  | 
|  | 30 | XE_CALLS=$(mktemp) | 
|  | 31 | truncate -s 0 $XE_CALLS | 
| Mate Lakat | 2781f3b | 2013-12-11 13:41:54 +0000 | [diff] [blame] | 32 |  | 
|  | 33 | DEAD_MESSAGES=$(mktemp) | 
|  | 34 | truncate -s 0 $DEAD_MESSAGES | 
| Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 35 | } | 
|  | 36 |  | 
|  | 37 | # Teardown | 
|  | 38 | function after_each_test { | 
|  | 39 | rm -f $LIST_OF_DIRECTORIES | 
|  | 40 | rm -f $LIST_OF_ACTIONS | 
| Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 41 | rm -f $XE_RESPONSE | 
|  | 42 | rm -f $XE_CALLS | 
| Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 43 | } | 
|  | 44 |  | 
|  | 45 | # Helpers | 
| Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 46 | function setup_xe_response { | 
|  | 47 | echo "$1" > $XE_RESPONSE | 
|  | 48 | } | 
|  | 49 |  | 
| Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 50 | function given_directory_exists { | 
|  | 51 | echo "$1" >> $LIST_OF_DIRECTORIES | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | function assert_directory_exists { | 
|  | 55 | grep "$1" $LIST_OF_DIRECTORIES | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | function assert_previous_command_failed { | 
|  | 59 | [ "$?" != "0" ] || exit 1 | 
|  | 60 | } | 
|  | 61 |  | 
| Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 62 | function assert_xe_min { | 
|  | 63 | grep -qe "^--minimal\$" $XE_CALLS | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | function assert_xe_param { | 
|  | 67 | grep -qe "^$1\$" $XE_CALLS | 
|  | 68 | } | 
|  | 69 |  | 
| Mate Lakat | 2781f3b | 2013-12-11 13:41:54 +0000 | [diff] [blame] | 70 | function assert_died_with { | 
|  | 71 | diff -u <(echo "$1") $DEAD_MESSAGES | 
|  | 72 | } | 
|  | 73 |  | 
| Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 74 | function mock_out { | 
|  | 75 | local FNNAME="$1" | 
|  | 76 | local OUTPUT="$2" | 
|  | 77 |  | 
|  | 78 | . <(cat << EOF | 
|  | 79 | function $FNNAME { | 
|  | 80 | echo "$OUTPUT" | 
|  | 81 | } | 
|  | 82 | EOF | 
|  | 83 | ) | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | function assert_symlink { | 
|  | 87 | grep -qe "^ln -s $2 $1\$" $LIST_OF_ACTIONS | 
|  | 88 | } | 
|  | 89 |  | 
| Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 90 | # Tests | 
|  | 91 | function 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 |  | 
|  | 99 | function 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 |  | 
|  | 107 | function 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 Lakat | 2781f3b | 2013-12-11 13:41:54 +0000 | [diff] [blame] | 119 | function test_zip_snapshot_location_http { | 
| Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 120 | diff \ | 
| Mate Lakat | 2781f3b | 2013-12-11 13:41:54 +0000 | [diff] [blame] | 121 | <(zip_snapshot_location "http://github.com/openstack/nova.git" "master") \ | 
|  | 122 | <(echo "http://github.com/openstack/nova/zipball/master") | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | function 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 Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 129 | } | 
|  | 130 |  | 
|  | 131 | function test_create_directory_for_kernels { | 
| Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 132 | ( | 
|  | 133 | . mocks | 
| Mate Lakat | 085abd8 | 2013-12-11 12:21:12 +0000 | [diff] [blame] | 134 | mock_out get_local_sr_path /var/run/sr-mount/uuid1 | 
| Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 135 | create_directory_for_kernels | 
|  | 136 | ) | 
| Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 137 |  | 
| Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 138 | 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 |  | 
|  | 142 | function 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 ] | 
|  | 151 | EOF | 
| Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 152 | } | 
|  | 153 |  | 
| Bob Ball | 39aeda2 | 2013-06-17 12:51:33 +0100 | [diff] [blame] | 154 | function test_create_directory_for_images { | 
|  | 155 | ( | 
|  | 156 | . mocks | 
| Mate Lakat | 085abd8 | 2013-12-11 12:21:12 +0000 | [diff] [blame] | 157 | mock_out get_local_sr_path /var/run/sr-mount/uuid1 | 
| Bob Ball | 39aeda2 | 2013-06-17 12:51:33 +0100 | [diff] [blame] | 158 | 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 |  | 
|  | 165 | function 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 ] | 
|  | 174 | EOF | 
|  | 175 | } | 
|  | 176 |  | 
| Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 177 | function test_extract_remote_zipball { | 
|  | 178 | local RESULT=$(. mocks && extract_remote_zipball "someurl") | 
|  | 179 |  | 
|  | 180 | diff <(cat $LIST_OF_ACTIONS) - << EOF | 
|  | 181 | wget -nv someurl -O tempfile --no-check-certificate | 
|  | 182 | unzip -q -o tempfile -d tempdir | 
|  | 183 | rm -f tempfile | 
|  | 184 | EOF | 
|  | 185 |  | 
|  | 186 | [ "$RESULT" = "tempdir" ] | 
|  | 187 | } | 
|  | 188 |  | 
| Euan Harris | 6f00171 | 2013-07-10 16:30:31 +0100 | [diff] [blame] | 189 | function test_extract_remote_zipball_wget_fail { | 
|  | 190 | set +e | 
|  | 191 |  | 
|  | 192 | local IGNORE | 
|  | 193 | IGNORE=$(. mocks && extract_remote_zipball "failurl") | 
|  | 194 |  | 
| Mate Lakat | 2781f3b | 2013-12-11 13:41:54 +0000 | [diff] [blame] | 195 | assert_died_with "Failed to download [failurl]" | 
| Euan Harris | 6f00171 | 2013-07-10 16:30:31 +0100 | [diff] [blame] | 196 | } | 
|  | 197 |  | 
| Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 198 | function 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 Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 208 | function test_get_local_sr { | 
|  | 209 | setup_xe_response "uuid123" | 
|  | 210 |  | 
|  | 211 | local RESULT=$(. mocks && get_local_sr) | 
|  | 212 |  | 
|  | 213 | [ "$RESULT" == "uuid123" ] | 
|  | 214 |  | 
| Mate Lakat | 085abd8 | 2013-12-11 12:21:12 +0000 | [diff] [blame] | 215 | assert_xe_param "pool-list" params=default-SR minimal=true | 
| Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 216 | } | 
|  | 217 |  | 
|  | 218 | function 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 Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 224 | # Test runner | 
|  | 225 | [ "$1" = "" ] && { | 
|  | 226 | grep -e "^function *test_" $0 | cut -d" " -f2 | 
|  | 227 | } | 
|  | 228 |  | 
|  | 229 | [ "$1" = "run_tests" ] && { | 
| Sean Dague | 16dd8b3 | 2014-02-03 09:10:54 +0900 | [diff] [blame] | 230 | for testname in $($0); do | 
| Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 231 | echo "$testname" | 
|  | 232 | before_each_test | 
|  | 233 | ( | 
|  | 234 | set -eux | 
|  | 235 | $testname | 
|  | 236 | ) | 
| Sean Dague | 16dd8b3 | 2014-02-03 09:10:54 +0900 | [diff] [blame] | 237 | if [ "$?" != "0" ]; then | 
| Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 238 | echo "FAIL" | 
|  | 239 | exit 1 | 
|  | 240 | else | 
|  | 241 | echo "PASS" | 
|  | 242 | fi | 
|  | 243 |  | 
|  | 244 | after_each_test | 
|  | 245 | done | 
|  | 246 | } |