tests/test_ini_config.sh
2ac8b3f3
 #!/usr/bin/env bash
 
 # Tests for DevStack INI functions
 
 TOP=$(cd $(dirname "$0")/.. && pwd)
 
bf2ad701
 # Import config functions
 source $TOP/inc/ini-config
2ac8b3f3
 
fcdca05d
 source $TOP/tests/unittest.sh
 
 set -e
2ac8b3f3
 
 echo "Testing INI functions"
 
b997db60
 INI_TMP_DIR=$(mktemp -d)
 INI_TMP_ETC_DIR=$INI_TMP_DIR/etc
 TEST_INI=${INI_TMP_ETC_DIR}/test.ini
 mkdir ${INI_TMP_ETC_DIR}
 
 echo "Creating $TEST_INI"
 cat >${TEST_INI} <<EOF
2ac8b3f3
 [default]
 # comment an option
 #log_file=./log.conf
 log_file=/etc/log.conf
 handlers=do not disturb
 
 [aaa]
 # the commented option should not change
 #handlers=cc,dd
 handlers = aa, bb
 
 [bbb]
 handlers=ee,ff
 
 [ ccc ]
 spaces  =  yes
 
 [ddd]
 empty =
 
 [eee]
 multi = foo1
 multi = foo2
1f65fd64
 
 # inidelete(a)
 [del_separate_options]
 a=b
 b=c
 
 # inidelete(a)
 [del_same_option]
 a=b
 a=c
 
 # inidelete(a)
 [del_missing_option]
 b=c
 
 # inidelete(a)
 [del_missing_option_multi]
 b=c
 b=d
 
 # inidelete(a)
 [del_no_options]
 
 # inidelete(a)
 # no section - del_no_section
 
2ac8b3f3
 EOF
 
f44a024f
 # set TEST_SUDO to test writing to root-owned files
 SUDO_ARG=""
 SUDO=""
 if [ -n "$TEST_SUDO" ]; then
     SUDO="sudo "
     SUDO_ARG="-sudo "
     sudo chown -R root:root ${INI_TMP_ETC_DIR}
 fi
2ac8b3f3
 
135bd484
 # test iniget_sections
 VAL=$(iniget_sections "${TEST_INI}")
 assert_equal "$VAL" "default aaa bbb ccc ddd eee del_separate_options \
 del_same_option del_missing_option del_missing_option_multi del_no_options"
 
f44a024f
 # Test with missing arguments
b997db60
 BEFORE=$(cat ${TEST_INI})
2ac8b3f3
 
f44a024f
 iniset ${SUDO_ARG} ${TEST_INI} aaa
b997db60
 NO_ATTRIBUTE=$(cat ${TEST_INI})
 assert_equal "$BEFORE" "$NO_ATTRIBUTE" "test missing attribute argument"
2ac8b3f3
 
f44a024f
 iniset ${SUDO_ARG} ${TEST_INI}
b997db60
 NO_SECTION=$(cat ${TEST_INI})
 assert_equal "$BEFORE" "$NO_SECTION" "missing section argument"
2ac8b3f3
 
b997db60
 # Test with spaces in values
 VAL=$(iniget ${TEST_INI} aaa handlers)
 assert_equal "$VAL" "aa, bb" "iniget spaces in option"
2ac8b3f3
 
f44a024f
 iniset ${SUDO_ARG} ${TEST_INI} aaa handlers "11, 22"
b997db60
 VAL=$(iniget ${TEST_INI} aaa handlers)
 assert_equal "$VAL" "11, 22" "iniset spaces in option"
2ac8b3f3
 
 # Test with spaces in section header
b997db60
 VAL=$(iniget ${TEST_INI} " ccc " spaces)
 assert_equal "$VAL" "yes" "iniget with section header space"
2ac8b3f3
 
f44a024f
 iniset ${SUDO_ARG} ${TEST_INI} "b b" opt_ion 42
b997db60
 VAL=$(iniget ${TEST_INI} "b b" opt_ion)
 assert_equal "$VAL" "42" "iniset with section header space"
2ac8b3f3
 
 # Test without spaces, end of file
b997db60
 VAL=$(iniget ${TEST_INI} bbb handlers)
 assert_equal "$VAL" "ee,ff" "iniget at EOF"
2ac8b3f3
 
f44a024f
 iniset ${SUDO_ARG} ${TEST_INI} bbb handlers "33,44"
b997db60
 VAL=$(iniget ${TEST_INI} bbb handlers)
 assert_equal "$VAL" "33,44" "inset at EOF"
2ac8b3f3
 
 # test empty option
b997db60
 if ini_has_option ${TEST_INI} ddd empty; then
     passed "ini_has_option: ddd.empty present"
2ac8b3f3
 else
fcdca05d
     failed "ini_has_option failed: ddd.empty not found"
2ac8b3f3
 fi
 
 # test non-empty option
b997db60
 if ini_has_option ${TEST_INI} bbb handlers; then
     passed "ini_has_option: bbb.handlers present"
2ac8b3f3
 else
fcdca05d
     failed "ini_has_option failed: bbb.handlers not found"
2ac8b3f3
 fi
 
 # test changing empty option
f44a024f
 iniset ${SUDO_ARG} ${TEST_INI} ddd empty "42"
b997db60
 VAL=$(iniget ${TEST_INI} ddd empty)
 assert_equal "$VAL" "42" "change empty option"
2ac8b3f3
 
cd7d956f
 # test pipe in option
f44a024f
 iniset ${SUDO_ARG} ${TEST_INI} aaa handlers "a|b"
b997db60
 VAL=$(iniget ${TEST_INI} aaa handlers)
 assert_equal "$VAL" "a|b" "pipe in option"
cd7d956f
 
2ac8b3f3
 # Test section not exist
b997db60
 VAL=$(iniget ${TEST_INI} zzz handlers)
 assert_empty VAL "section does not exist"
2ac8b3f3
 
 # Test option not exist
b997db60
 VAL=$(iniget ${TEST_INI} aaa debug)
 assert_empty VAL "option does not exist"
2ac8b3f3
 
b997db60
 if ! ini_has_option ${TEST_INI} aaa debug; then
     passed "ini_has_option: aaa.debug not present"
2ac8b3f3
 else
fcdca05d
     failed "ini_has_option failed: aaa.debug"
2ac8b3f3
 fi
 
 # Test comments
f44a024f
 inicomment ${SUDO_ARG} ${TEST_INI} aaa handlers
b997db60
 VAL=$(iniget ${TEST_INI} aaa handlers)
 assert_empty VAL "test inicomment"
2ac8b3f3
 
 # Test multiple line iniset/iniget
f44a024f
 iniset_multiline ${SUDO_ARG} ${TEST_INI} eee multi bar1 bar2
2ac8b3f3
 
b997db60
 VAL=$(iniget_multiline ${TEST_INI} eee multi)
 assert_equal "$VAL" "bar1 bar2" "iniget_multiline"
2ac8b3f3
 
 # Test iniadd with exiting values
f44a024f
 iniadd ${SUDO_ARG} ${TEST_INI} eee multi bar3
b997db60
 VAL=$(iniget_multiline ${TEST_INI} eee multi)
 assert_equal "$VAL" "bar1 bar2 bar3" "iniadd with existing values"
2ac8b3f3
 
 # Test iniadd with non-exiting values
f44a024f
 iniadd ${SUDO_ARG} ${TEST_INI} eee non-multi foobar1 foobar2
b997db60
 VAL=$(iniget_multiline ${TEST_INI} eee non-multi)
 assert_equal "$VAL" "foobar1 foobar2" "iniadd non-existing values"
2ac8b3f3
 
1f65fd64
 # Test inidelete
 del_cases="
     del_separate_options
     del_same_option
     del_missing_option
     del_missing_option_multi
     del_no_options
     del_no_section"
 
 for x in $del_cases; do
f44a024f
     inidelete ${SUDO_ARG} ${TEST_INI} $x a
b997db60
     VAL=$(iniget_multiline ${TEST_INI} $x a)
     assert_empty VAL "inidelete $x"
1f65fd64
     if [ "$x" = "del_separate_options" -o \
         "$x" = "del_missing_option" -o \
         "$x" = "del_missing_option_multi" ]; then
b997db60
         VAL=$(iniget_multiline ${TEST_INI} $x b)
1f65fd64
         if [ "$VAL" = "c" -o "$VAL" = "c d" ]; then
b997db60
             passed "inidelete other_options $x"
1f65fd64
         else
b997db60
             failed "inidelete other_option $x: $VAL"
1f65fd64
         fi
     fi
 done
 
cede7874
 # test file-creation
 iniset $SUDO_ARG ${INI_TMP_ETC_DIR}/test.new.ini test foo bar
 VAL=$(iniget ${INI_TMP_ETC_DIR}/test.new.ini test foo)
 assert_equal "$VAL" "bar" "iniset created file"
 
f44a024f
 $SUDO rm -rf ${INI_TMP_DIR}
fcdca05d
 
 report_results