| 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 | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 119 | function test_create_directory_for_kernels { | 
| Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 120 |     ( | 
 | 121 |         . mocks | 
| Mate Lakat | 085abd8 | 2013-12-11 12:21:12 +0000 | [diff] [blame] | 122 |         mock_out get_local_sr_path /var/run/sr-mount/uuid1 | 
| Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 123 |         create_directory_for_kernels | 
 | 124 |     ) | 
| Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 125 |  | 
| Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 126 |     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 |  | 
 | 130 | function 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 ] | 
 | 139 | EOF | 
| Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 140 | } | 
 | 141 |  | 
| Bob Ball | 39aeda2 | 2013-06-17 12:51:33 +0100 | [diff] [blame] | 142 | function test_create_directory_for_images { | 
 | 143 |     ( | 
 | 144 |         . mocks | 
| Mate Lakat | 085abd8 | 2013-12-11 12:21:12 +0000 | [diff] [blame] | 145 |         mock_out get_local_sr_path /var/run/sr-mount/uuid1 | 
| Bob Ball | 39aeda2 | 2013-06-17 12:51:33 +0100 | [diff] [blame] | 146 |         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 |  | 
 | 153 | function 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 ] | 
 | 162 | EOF | 
 | 163 | } | 
 | 164 |  | 
| Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 165 | function test_get_local_sr { | 
 | 166 |     setup_xe_response "uuid123" | 
 | 167 |  | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 168 |     local RESULT | 
 | 169 |     RESULT=$(. mocks && get_local_sr) | 
| Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 170 |  | 
 | 171 |     [ "$RESULT" == "uuid123" ] | 
 | 172 |  | 
| Mate Lakat | 085abd8 | 2013-12-11 12:21:12 +0000 | [diff] [blame] | 173 |     assert_xe_param "pool-list" params=default-SR minimal=true | 
| Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 174 | } | 
 | 175 |  | 
 | 176 | function test_get_local_sr_path { | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 177 |     local RESULT | 
 | 178 |     RESULT=$(mock_out get_local_sr "uuid1" && get_local_sr_path) | 
| Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 179 |  | 
 | 180 |     [ "/var/run/sr-mount/uuid1" == "$RESULT" ] | 
 | 181 | } | 
 | 182 |  | 
| Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 183 | # Test runner | 
 | 184 | [ "$1" = "" ] && { | 
 | 185 |     grep -e "^function *test_" $0 | cut -d" " -f2 | 
 | 186 | } | 
 | 187 |  | 
 | 188 | [ "$1" = "run_tests" ] && { | 
| Sean Dague | 16dd8b3 | 2014-02-03 09:10:54 +0900 | [diff] [blame] | 189 |     for testname in $($0); do | 
| Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 190 |         echo "$testname" | 
 | 191 |         before_each_test | 
 | 192 |         ( | 
 | 193 |             set -eux | 
 | 194 |             $testname | 
 | 195 |         ) | 
| Sean Dague | 16dd8b3 | 2014-02-03 09:10:54 +0900 | [diff] [blame] | 196 |         if [ "$?" != "0" ]; then | 
| Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 197 |             echo "FAIL" | 
 | 198 |             exit 1 | 
 | 199 |         else | 
 | 200 |             echo "PASS" | 
 | 201 |         fi | 
 | 202 |  | 
 | 203 |         after_each_test | 
 | 204 |     done | 
 | 205 | } |