| 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 |  | 
|  | 141 | function test_extract_remote_zipball { | 
|  | 142 | local RESULT=$(. mocks && extract_remote_zipball "someurl") | 
|  | 143 |  | 
|  | 144 | diff <(cat $LIST_OF_ACTIONS) - << EOF | 
|  | 145 | wget -nv someurl -O tempfile --no-check-certificate | 
|  | 146 | unzip -q -o tempfile -d tempdir | 
|  | 147 | rm -f tempfile | 
|  | 148 | EOF | 
|  | 149 |  | 
|  | 150 | [ "$RESULT" = "tempdir" ] | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | function 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 Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 163 | function 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 |  | 
|  | 174 | function 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 Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 180 | # 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 | } |