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 |
| 26 | } |
| 27 | |
| 28 | # Teardown |
| 29 | function after_each_test { |
| 30 | rm -f $LIST_OF_DIRECTORIES |
| 31 | rm -f $LIST_OF_ACTIONS |
| 32 | } |
| 33 | |
| 34 | # Helpers |
| 35 | function given_directory_exists { |
| 36 | echo "$1" >> $LIST_OF_DIRECTORIES |
| 37 | } |
| 38 | |
| 39 | function assert_directory_exists { |
| 40 | grep "$1" $LIST_OF_DIRECTORIES |
| 41 | } |
| 42 | |
| 43 | function assert_previous_command_failed { |
| 44 | [ "$?" != "0" ] || exit 1 |
| 45 | } |
| 46 | |
| 47 | # Tests |
| 48 | function test_plugin_directory_on_xenserver { |
| 49 | given_directory_exists "/etc/xapi.d/plugins/" |
| 50 | |
| 51 | PLUGDIR=$(. mocks && xapi_plugin_location) |
| 52 | |
| 53 | [ "/etc/xapi.d/plugins/" = "$PLUGDIR" ] |
| 54 | } |
| 55 | |
| 56 | function test_plugin_directory_on_xcp { |
| 57 | given_directory_exists "/usr/lib/xcp/plugins/" |
| 58 | |
| 59 | PLUGDIR=$(. mocks && xapi_plugin_location) |
| 60 | |
| 61 | [ "/usr/lib/xcp/plugins/" = "$PLUGDIR" ] |
| 62 | } |
| 63 | |
| 64 | function test_no_plugin_directory_found { |
| 65 | set +e |
| 66 | |
| 67 | local IGNORE |
| 68 | IGNORE=$(. mocks && xapi_plugin_location) |
| 69 | |
| 70 | assert_previous_command_failed |
| 71 | |
| 72 | grep "[ -d /etc/xapi.d/plugins/ ]" $LIST_OF_ACTIONS |
| 73 | grep "[ -d /usr/lib/xcp/plugins/ ]" $LIST_OF_ACTIONS |
| 74 | } |
| 75 | |
| 76 | function test_zip_snapshot_location { |
| 77 | diff \ |
| 78 | <(zip_snapshot_location "https://github.com/openstack/nova.git" "master") \ |
| 79 | <(echo "https://github.com/openstack/nova/zipball/master") |
| 80 | } |
| 81 | |
| 82 | function test_create_directory_for_kernels { |
| 83 | (. mocks && create_directory_for_kernels) |
| 84 | |
| 85 | assert_directory_exists "/boot/guest" |
| 86 | } |
| 87 | |
| 88 | function test_extract_remote_zipball { |
| 89 | local RESULT=$(. mocks && extract_remote_zipball "someurl") |
| 90 | |
| 91 | diff <(cat $LIST_OF_ACTIONS) - << EOF |
| 92 | wget -nv someurl -O tempfile --no-check-certificate |
| 93 | unzip -q -o tempfile -d tempdir |
| 94 | rm -f tempfile |
| 95 | EOF |
| 96 | |
| 97 | [ "$RESULT" = "tempdir" ] |
| 98 | } |
| 99 | |
| 100 | function test_find_nova_plugins { |
| 101 | local tmpdir=$(mktemp -d) |
| 102 | |
| 103 | mkdir -p "$tmpdir/blah/blah/u/xapi.d/plugins" |
| 104 | |
| 105 | [ "$tmpdir/blah/blah/u/xapi.d/plugins" = $(find_xapi_plugins_dir $tmpdir) ] |
| 106 | |
| 107 | rm -rf $tmpdir |
| 108 | } |
| 109 | |
| 110 | # Test runner |
| 111 | [ "$1" = "" ] && { |
| 112 | grep -e "^function *test_" $0 | cut -d" " -f2 |
| 113 | } |
| 114 | |
| 115 | [ "$1" = "run_tests" ] && { |
| 116 | for testname in $($0) |
| 117 | do |
| 118 | echo "$testname" |
| 119 | before_each_test |
| 120 | ( |
| 121 | set -eux |
| 122 | $testname |
| 123 | ) |
| 124 | if [ "$?" != "0" ] |
| 125 | then |
| 126 | echo "FAIL" |
| 127 | exit 1 |
| 128 | else |
| 129 | echo "PASS" |
| 130 | fi |
| 131 | |
| 132 | after_each_test |
| 133 | done |
| 134 | } |