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 | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | # Teardown |
| 35 | function after_each_test { |
| 36 | rm -f $LIST_OF_DIRECTORIES |
| 37 | rm -f $LIST_OF_ACTIONS |
Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 38 | rm -f $XE_RESPONSE |
| 39 | rm -f $XE_CALLS |
Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | # Helpers |
Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 43 | function setup_xe_response { |
| 44 | echo "$1" > $XE_RESPONSE |
| 45 | } |
| 46 | |
Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 47 | function given_directory_exists { |
| 48 | echo "$1" >> $LIST_OF_DIRECTORIES |
| 49 | } |
| 50 | |
| 51 | function assert_directory_exists { |
| 52 | grep "$1" $LIST_OF_DIRECTORIES |
| 53 | } |
| 54 | |
| 55 | function assert_previous_command_failed { |
| 56 | [ "$?" != "0" ] || exit 1 |
| 57 | } |
| 58 | |
Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 59 | function assert_xe_min { |
| 60 | grep -qe "^--minimal\$" $XE_CALLS |
| 61 | } |
| 62 | |
| 63 | function assert_xe_param { |
| 64 | grep -qe "^$1\$" $XE_CALLS |
| 65 | } |
| 66 | |
| 67 | function mock_out { |
| 68 | local FNNAME="$1" |
| 69 | local OUTPUT="$2" |
| 70 | |
| 71 | . <(cat << EOF |
| 72 | function $FNNAME { |
| 73 | echo "$OUTPUT" |
| 74 | } |
| 75 | EOF |
| 76 | ) |
| 77 | } |
| 78 | |
| 79 | function assert_symlink { |
| 80 | grep -qe "^ln -s $2 $1\$" $LIST_OF_ACTIONS |
| 81 | } |
| 82 | |
Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 83 | # Tests |
| 84 | function 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 | |
| 92 | function 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 | |
| 100 | function 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 | |
| 112 | function 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 | |
| 118 | function test_create_directory_for_kernels { |
Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 119 | ( |
| 120 | . mocks |
| 121 | mock_out get_local_sr uuid1 |
| 122 | create_directory_for_kernels |
| 123 | ) |
Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 124 | |
Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 125 | 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 | |
| 129 | function 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 ] |
| 138 | EOF |
Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Bob Ball | 39aeda2 | 2013-06-17 12:51:33 +0100 | [diff] [blame] | 141 | function test_create_directory_for_images { |
| 142 | ( |
| 143 | . mocks |
| 144 | mock_out get_local_sr uuid1 |
| 145 | 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 | |
| 152 | function 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 ] |
| 161 | EOF |
| 162 | } |
| 163 | |
Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 164 | function test_extract_remote_zipball { |
| 165 | local RESULT=$(. mocks && extract_remote_zipball "someurl") |
| 166 | |
| 167 | diff <(cat $LIST_OF_ACTIONS) - << EOF |
| 168 | wget -nv someurl -O tempfile --no-check-certificate |
| 169 | unzip -q -o tempfile -d tempdir |
| 170 | rm -f tempfile |
| 171 | EOF |
| 172 | |
| 173 | [ "$RESULT" = "tempdir" ] |
| 174 | } |
| 175 | |
Euan Harris | 6f00171 | 2013-07-10 16:30:31 +0100 | [diff] [blame] | 176 | function 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 Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 185 | function 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 Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 195 | function test_get_local_sr { |
| 196 | setup_xe_response "uuid123" |
| 197 | |
| 198 | local RESULT=$(. mocks && get_local_sr) |
| 199 | |
| 200 | [ "$RESULT" == "uuid123" ] |
| 201 | |
| 202 | assert_xe_min |
| 203 | assert_xe_param "sr-list" "name-label=Local storage" |
| 204 | } |
| 205 | |
| 206 | function test_get_local_sr_path { |
| 207 | local RESULT=$(mock_out get_local_sr "uuid1" && get_local_sr_path) |
| 208 | |
| 209 | [ "/var/run/sr-mount/uuid1" == "$RESULT" ] |
| 210 | } |
| 211 | |
Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 212 | # Test runner |
| 213 | [ "$1" = "" ] && { |
| 214 | grep -e "^function *test_" $0 | cut -d" " -f2 |
| 215 | } |
| 216 | |
| 217 | [ "$1" = "run_tests" ] && { |
| 218 | for testname in $($0) |
| 219 | do |
| 220 | echo "$testname" |
| 221 | before_each_test |
| 222 | ( |
| 223 | set -eux |
| 224 | $testname |
| 225 | ) |
| 226 | if [ "$?" != "0" ] |
| 227 | then |
| 228 | echo "FAIL" |
| 229 | exit 1 |
| 230 | else |
| 231 | echo "PASS" |
| 232 | fi |
| 233 | |
| 234 | after_each_test |
| 235 | done |
| 236 | } |