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 | |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 74 | # set TEST_SUDO to test writing to root-owned files |
| 75 | SUDO_ARG="" |
| 76 | SUDO="" |
| 77 | if [ -n "$TEST_SUDO" ]; then |
| 78 | SUDO="sudo " |
| 79 | SUDO_ARG="-sudo " |
| 80 | sudo chown -R root:root ${INI_TMP_ETC_DIR} |
| 81 | fi |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 82 | |
vsaienko | 135bd48 | 2015-12-11 11:03:52 +0200 | [diff] [blame^] | 83 | # test iniget_sections |
| 84 | VAL=$(iniget_sections "${TEST_INI}") |
| 85 | assert_equal "$VAL" "default aaa bbb ccc ddd eee del_separate_options \ |
| 86 | del_same_option del_missing_option del_missing_option_multi del_no_options" |
| 87 | |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 88 | # Test with missing arguments |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 89 | BEFORE=$(cat ${TEST_INI}) |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 90 | |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 91 | iniset ${SUDO_ARG} ${TEST_INI} aaa |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 92 | NO_ATTRIBUTE=$(cat ${TEST_INI}) |
| 93 | assert_equal "$BEFORE" "$NO_ATTRIBUTE" "test missing attribute argument" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 94 | |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 95 | iniset ${SUDO_ARG} ${TEST_INI} |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 96 | NO_SECTION=$(cat ${TEST_INI}) |
| 97 | assert_equal "$BEFORE" "$NO_SECTION" "missing section argument" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 98 | |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 99 | # Test with spaces in values |
| 100 | VAL=$(iniget ${TEST_INI} aaa handlers) |
| 101 | assert_equal "$VAL" "aa, bb" "iniget spaces in option" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 102 | |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 103 | iniset ${SUDO_ARG} ${TEST_INI} aaa handlers "11, 22" |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 104 | VAL=$(iniget ${TEST_INI} aaa handlers) |
| 105 | assert_equal "$VAL" "11, 22" "iniset spaces in option" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 106 | |
| 107 | # Test with spaces in section header |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 108 | VAL=$(iniget ${TEST_INI} " ccc " spaces) |
| 109 | assert_equal "$VAL" "yes" "iniget with section header space" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 110 | |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 111 | iniset ${SUDO_ARG} ${TEST_INI} "b b" opt_ion 42 |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 112 | VAL=$(iniget ${TEST_INI} "b b" opt_ion) |
| 113 | assert_equal "$VAL" "42" "iniset with section header space" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 114 | |
| 115 | # Test without spaces, end of file |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 116 | VAL=$(iniget ${TEST_INI} bbb handlers) |
| 117 | assert_equal "$VAL" "ee,ff" "iniget at EOF" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 118 | |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 119 | iniset ${SUDO_ARG} ${TEST_INI} bbb handlers "33,44" |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 120 | VAL=$(iniget ${TEST_INI} bbb handlers) |
| 121 | assert_equal "$VAL" "33,44" "inset at EOF" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 122 | |
| 123 | # test empty option |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 124 | if ini_has_option ${TEST_INI} ddd empty; then |
| 125 | passed "ini_has_option: ddd.empty present" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 126 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 127 | failed "ini_has_option failed: ddd.empty not found" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 128 | fi |
| 129 | |
| 130 | # test non-empty option |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 131 | if ini_has_option ${TEST_INI} bbb handlers; then |
| 132 | passed "ini_has_option: bbb.handlers present" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 133 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 134 | failed "ini_has_option failed: bbb.handlers not found" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 135 | fi |
| 136 | |
| 137 | # test changing empty option |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 138 | iniset ${SUDO_ARG} ${TEST_INI} ddd empty "42" |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 139 | VAL=$(iniget ${TEST_INI} ddd empty) |
| 140 | assert_equal "$VAL" "42" "change empty option" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 141 | |
Andrea Frittoli | cd7d956 | 2013-12-05 08:09:12 +0000 | [diff] [blame] | 142 | # test pipe in option |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 143 | iniset ${SUDO_ARG} ${TEST_INI} aaa handlers "a|b" |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 144 | VAL=$(iniget ${TEST_INI} aaa handlers) |
| 145 | assert_equal "$VAL" "a|b" "pipe in option" |
Andrea Frittoli | cd7d956 | 2013-12-05 08:09:12 +0000 | [diff] [blame] | 146 | |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 147 | # Test section not exist |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 148 | VAL=$(iniget ${TEST_INI} zzz handlers) |
| 149 | assert_empty VAL "section does not exist" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 150 | |
| 151 | # Test option not exist |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 152 | VAL=$(iniget ${TEST_INI} aaa debug) |
| 153 | assert_empty VAL "option does not exist" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 154 | |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 155 | if ! ini_has_option ${TEST_INI} aaa debug; then |
| 156 | passed "ini_has_option: aaa.debug not present" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 157 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 158 | failed "ini_has_option failed: aaa.debug" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 159 | fi |
| 160 | |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 161 | # Test comments |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 162 | inicomment ${SUDO_ARG} ${TEST_INI} aaa handlers |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 163 | VAL=$(iniget ${TEST_INI} aaa handlers) |
| 164 | assert_empty VAL "test inicomment" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 165 | |
| 166 | # Test multiple line iniset/iniget |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 167 | iniset_multiline ${SUDO_ARG} ${TEST_INI} eee multi bar1 bar2 |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 168 | |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 169 | VAL=$(iniget_multiline ${TEST_INI} eee multi) |
| 170 | assert_equal "$VAL" "bar1 bar2" "iniget_multiline" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 171 | |
| 172 | # Test iniadd with exiting values |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 173 | iniadd ${SUDO_ARG} ${TEST_INI} eee multi bar3 |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 174 | VAL=$(iniget_multiline ${TEST_INI} eee multi) |
| 175 | assert_equal "$VAL" "bar1 bar2 bar3" "iniadd with existing values" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 176 | |
| 177 | # Test iniadd with non-exiting values |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 178 | iniadd ${SUDO_ARG} ${TEST_INI} eee non-multi foobar1 foobar2 |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 179 | VAL=$(iniget_multiline ${TEST_INI} eee non-multi) |
| 180 | assert_equal "$VAL" "foobar1 foobar2" "iniadd non-existing values" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 181 | |
Doug Wiegley | 1f65fd6 | 2014-12-13 11:56:16 -0700 | [diff] [blame] | 182 | # Test inidelete |
| 183 | del_cases=" |
| 184 | del_separate_options |
| 185 | del_same_option |
| 186 | del_missing_option |
| 187 | del_missing_option_multi |
| 188 | del_no_options |
| 189 | del_no_section" |
| 190 | |
| 191 | for x in $del_cases; do |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 192 | inidelete ${SUDO_ARG} ${TEST_INI} $x a |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 193 | VAL=$(iniget_multiline ${TEST_INI} $x a) |
| 194 | assert_empty VAL "inidelete $x" |
Doug Wiegley | 1f65fd6 | 2014-12-13 11:56:16 -0700 | [diff] [blame] | 195 | if [ "$x" = "del_separate_options" -o \ |
| 196 | "$x" = "del_missing_option" -o \ |
| 197 | "$x" = "del_missing_option_multi" ]; then |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 198 | VAL=$(iniget_multiline ${TEST_INI} $x b) |
Doug Wiegley | 1f65fd6 | 2014-12-13 11:56:16 -0700 | [diff] [blame] | 199 | if [ "$VAL" = "c" -o "$VAL" = "c d" ]; then |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 200 | passed "inidelete other_options $x" |
Doug Wiegley | 1f65fd6 | 2014-12-13 11:56:16 -0700 | [diff] [blame] | 201 | else |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 202 | failed "inidelete other_option $x: $VAL" |
Doug Wiegley | 1f65fd6 | 2014-12-13 11:56:16 -0700 | [diff] [blame] | 203 | fi |
| 204 | fi |
| 205 | done |
| 206 | |
Ian Wienand | cede787 | 2015-07-22 13:36:12 +1000 | [diff] [blame] | 207 | # test file-creation |
| 208 | iniset $SUDO_ARG ${INI_TMP_ETC_DIR}/test.new.ini test foo bar |
| 209 | VAL=$(iniget ${INI_TMP_ETC_DIR}/test.new.ini test foo) |
| 210 | assert_equal "$VAL" "bar" "iniset created file" |
| 211 | |
Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 212 | $SUDO rm -rf ${INI_TMP_DIR} |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 213 | |
| 214 | report_results |