Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # Tests for DevStack INI functions |
| 4 | |
| 5 | TOP=$(cd $(dirname "$0")/.. && pwd) |
| 6 | |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 7 | # Import config functions |
| 8 | source $TOP/inc/ini-config |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 9 | |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 10 | source $TOP/tests/unittest.sh |
| 11 | |
| 12 | set -e |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 13 | |
| 14 | echo "Testing INI functions" |
| 15 | |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 16 | INI_TMP_DIR=$(mktemp -d) |
| 17 | INI_TMP_ETC_DIR=$INI_TMP_DIR/etc |
| 18 | TEST_INI=${INI_TMP_ETC_DIR}/test.ini |
| 19 | mkdir ${INI_TMP_ETC_DIR} |
| 20 | |
| 21 | echo "Creating $TEST_INI" |
| 22 | cat >${TEST_INI} <<EOF |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 23 | [default] |
| 24 | # comment an option |
| 25 | #log_file=./log.conf |
| 26 | log_file=/etc/log.conf |
| 27 | handlers=do not disturb |
| 28 | |
| 29 | [aaa] |
| 30 | # the commented option should not change |
| 31 | #handlers=cc,dd |
| 32 | handlers = aa, bb |
| 33 | |
| 34 | [bbb] |
| 35 | handlers=ee,ff |
| 36 | |
| 37 | [ ccc ] |
| 38 | spaces = yes |
| 39 | |
| 40 | [ddd] |
| 41 | empty = |
| 42 | |
| 43 | [eee] |
| 44 | multi = foo1 |
| 45 | multi = foo2 |
Doug Wiegley | 1f65fd6 | 2014-12-13 11:56:16 -0700 | [diff] [blame] | 46 | |
| 47 | # inidelete(a) |
| 48 | [del_separate_options] |
| 49 | a=b |
| 50 | b=c |
| 51 | |
| 52 | # inidelete(a) |
| 53 | [del_same_option] |
| 54 | a=b |
| 55 | a=c |
| 56 | |
| 57 | # inidelete(a) |
| 58 | [del_missing_option] |
| 59 | b=c |
| 60 | |
| 61 | # inidelete(a) |
| 62 | [del_missing_option_multi] |
| 63 | b=c |
| 64 | b=d |
| 65 | |
| 66 | # inidelete(a) |
| 67 | [del_no_options] |
| 68 | |
| 69 | # inidelete(a) |
| 70 | # no section - del_no_section |
| 71 | |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 72 | EOF |
| 73 | |
| 74 | # Test with missing arguments |
| 75 | |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 76 | BEFORE=$(cat ${TEST_INI}) |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 77 | |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 78 | iniset ${TEST_INI} aaa |
| 79 | NO_ATTRIBUTE=$(cat ${TEST_INI}) |
| 80 | assert_equal "$BEFORE" "$NO_ATTRIBUTE" "test missing attribute argument" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 81 | |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 82 | iniset ${TEST_INI} |
| 83 | NO_SECTION=$(cat ${TEST_INI}) |
| 84 | assert_equal "$BEFORE" "$NO_SECTION" "missing section argument" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 85 | |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 86 | # Test with spaces in values |
| 87 | VAL=$(iniget ${TEST_INI} aaa handlers) |
| 88 | assert_equal "$VAL" "aa, bb" "iniget spaces in option" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 89 | |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 90 | iniset ${TEST_INI} aaa handlers "11, 22" |
| 91 | VAL=$(iniget ${TEST_INI} aaa handlers) |
| 92 | assert_equal "$VAL" "11, 22" "iniset spaces in option" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 93 | |
| 94 | # Test with spaces in section header |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 95 | VAL=$(iniget ${TEST_INI} " ccc " spaces) |
| 96 | assert_equal "$VAL" "yes" "iniget with section header space" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 97 | |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 98 | iniset ${TEST_INI} "b b" opt_ion 42 |
| 99 | VAL=$(iniget ${TEST_INI} "b b" opt_ion) |
| 100 | assert_equal "$VAL" "42" "iniset with section header space" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 101 | |
| 102 | # Test without spaces, end of file |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 103 | VAL=$(iniget ${TEST_INI} bbb handlers) |
| 104 | assert_equal "$VAL" "ee,ff" "iniget at EOF" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 105 | |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 106 | iniset ${TEST_INI} bbb handlers "33,44" |
| 107 | VAL=$(iniget ${TEST_INI} bbb handlers) |
| 108 | assert_equal "$VAL" "33,44" "inset at EOF" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 109 | |
| 110 | # test empty option |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 111 | if ini_has_option ${TEST_INI} ddd empty; then |
| 112 | passed "ini_has_option: ddd.empty present" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 113 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 114 | failed "ini_has_option failed: ddd.empty not found" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 115 | fi |
| 116 | |
| 117 | # test non-empty option |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 118 | if ini_has_option ${TEST_INI} bbb handlers; then |
| 119 | passed "ini_has_option: bbb.handlers present" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 120 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 121 | failed "ini_has_option failed: bbb.handlers not found" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 122 | fi |
| 123 | |
| 124 | # test changing empty option |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 125 | iniset ${TEST_INI} ddd empty "42" |
| 126 | VAL=$(iniget ${TEST_INI} ddd empty) |
| 127 | assert_equal "$VAL" "42" "change empty option" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 128 | |
Andrea Frittoli | cd7d956 | 2013-12-05 08:09:12 +0000 | [diff] [blame] | 129 | # test pipe in option |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 130 | iniset ${TEST_INI} aaa handlers "a|b" |
| 131 | VAL=$(iniget ${TEST_INI} aaa handlers) |
| 132 | assert_equal "$VAL" "a|b" "pipe in option" |
Andrea Frittoli | cd7d956 | 2013-12-05 08:09:12 +0000 | [diff] [blame] | 133 | |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 134 | # Test section not exist |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 135 | VAL=$(iniget ${TEST_INI} zzz handlers) |
| 136 | assert_empty VAL "section does not exist" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 137 | |
| 138 | # Test option not exist |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 139 | VAL=$(iniget ${TEST_INI} aaa debug) |
| 140 | assert_empty VAL "option does not exist" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 141 | |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 142 | if ! ini_has_option ${TEST_INI} aaa debug; then |
| 143 | passed "ini_has_option: aaa.debug not present" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 144 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 145 | failed "ini_has_option failed: aaa.debug" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 146 | fi |
| 147 | |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 148 | # Test comments |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 149 | inicomment ${TEST_INI} aaa handlers |
| 150 | VAL=$(iniget ${TEST_INI} aaa handlers) |
| 151 | assert_empty VAL "test inicomment" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 152 | |
| 153 | # Test multiple line iniset/iniget |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 154 | iniset_multiline ${TEST_INI} eee multi bar1 bar2 |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 155 | |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 156 | VAL=$(iniget_multiline ${TEST_INI} eee multi) |
| 157 | assert_equal "$VAL" "bar1 bar2" "iniget_multiline" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 158 | |
| 159 | # Test iniadd with exiting values |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 160 | iniadd ${TEST_INI} eee multi bar3 |
| 161 | VAL=$(iniget_multiline ${TEST_INI} eee multi) |
| 162 | assert_equal "$VAL" "bar1 bar2 bar3" "iniadd with existing values" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 163 | |
| 164 | # Test iniadd with non-exiting values |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 165 | iniadd ${TEST_INI} eee non-multi foobar1 foobar2 |
| 166 | VAL=$(iniget_multiline ${TEST_INI} eee non-multi) |
| 167 | assert_equal "$VAL" "foobar1 foobar2" "iniadd non-existing values" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 168 | |
Doug Wiegley | 1f65fd6 | 2014-12-13 11:56:16 -0700 | [diff] [blame] | 169 | # Test inidelete |
| 170 | del_cases=" |
| 171 | del_separate_options |
| 172 | del_same_option |
| 173 | del_missing_option |
| 174 | del_missing_option_multi |
| 175 | del_no_options |
| 176 | del_no_section" |
| 177 | |
| 178 | for x in $del_cases; do |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 179 | inidelete ${TEST_INI} $x a |
| 180 | VAL=$(iniget_multiline ${TEST_INI} $x a) |
| 181 | assert_empty VAL "inidelete $x" |
Doug Wiegley | 1f65fd6 | 2014-12-13 11:56:16 -0700 | [diff] [blame] | 182 | if [ "$x" = "del_separate_options" -o \ |
| 183 | "$x" = "del_missing_option" -o \ |
| 184 | "$x" = "del_missing_option_multi" ]; then |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 185 | VAL=$(iniget_multiline ${TEST_INI} $x b) |
Doug Wiegley | 1f65fd6 | 2014-12-13 11:56:16 -0700 | [diff] [blame] | 186 | if [ "$VAL" = "c" -o "$VAL" = "c d" ]; then |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 187 | passed "inidelete other_options $x" |
Doug Wiegley | 1f65fd6 | 2014-12-13 11:56:16 -0700 | [diff] [blame] | 188 | else |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 189 | failed "inidelete other_option $x: $VAL" |
Doug Wiegley | 1f65fd6 | 2014-12-13 11:56:16 -0700 | [diff] [blame] | 190 | fi |
| 191 | fi |
| 192 | done |
| 193 | |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame^] | 194 | rm -rf ${INI_TMP_DIR} |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 195 | |
| 196 | report_results |