Provide an error message on bogus config file spec
If local.conf specifies a config file addition in the following way...
[[post-config|$MY_CONF_FILE]]
[xyz]
foo=bar
...and $MY_CONF_FILE points to a file whose directory is not writable by
the user running the script, then stack.sh aborts with the following
obscure message:
2015-09-01 08:20:08.113 | touch: setting times of '/': Permission denied
This patch modifies inc/meta-config to provide a useful error message,
such as:
2015-09-01 08:20:08.114 | could not create config file / ($MY_CONF_FILE)
This patch also modifies inc/meta-config so that it provides an error
message if $MY_CONF_FILE is empty (instead of silently ignoring this local.conf
statement):
2015-09-01 09:38:53.406 | bogus config file specification: $MY_CONF_FILE
is undefined
Change-Id: I9b78407420318548561012a8672762bc7fdd6db6
Closes-Bug: 1490881
diff --git a/inc/meta-config b/inc/meta-config
index e5f902d..b9ab6b2 100644
--- a/inc/meta-config
+++ b/inc/meta-config
@@ -89,9 +89,10 @@
# note, configfile might be a variable (note the iniset, etc
# created in the mega-awk below is "eval"ed too, so we just leave
# it alone.
- local real_configfile=$(eval echo $configfile)
+ local real_configfile
+ real_configfile=$(eval echo $configfile)
if [ ! -f $real_configfile ]; then
- touch $real_configfile
+ touch $real_configfile || die $LINENO "could not create config file $real_configfile ($configfile)"
fi
get_meta_section $file $matchgroup $configfile | \
@@ -177,8 +178,18 @@
local configfile group
for group in $matchgroups; do
for configfile in $(get_meta_section_files $localfile $group); do
- if [[ -d $(dirname $(eval "echo $configfile")) ]]; then
+ local realconfigfile
+ local dir
+
+ realconfigfile=$(eval "echo $configfile")
+ if [[ -z $realconfigfile ]]; then
+ die $LINENO "bogus config file specification: $configfile is undefined"
+ fi
+ dir=$(dirname $realconfigfile)
+ if [[ -d $dir ]]; then
merge_config_file $localfile $group $configfile
+ else
+ die $LINENO "bogus config file specification $configfile ($configfile=$realconfigfile, $dir is not a directory)"
fi
done
done
diff --git a/tests/test_meta_config.sh b/tests/test_meta_config.sh
index a04c081..f3e94af 100755
--- a/tests/test_meta_config.sh
+++ b/tests/test_meta_config.sh
@@ -23,6 +23,12 @@
fi
}
+# mock function-common:die so that it does not
+# interupt our test script
+function die {
+ exit -1
+}
+
TEST_1C_ADD="[eee]
type=new
multi = foo2"
@@ -110,6 +116,15 @@
[DEFAULT]
servers=10.11.12.13:80
+[[test8|/permission-denied.conf]]
+foo=bar
+
+[[test9|\$UNDEF]]
+foo=bar
+
+[[test10|does-not-exist-dir/test.conf]]
+foo=bar
+
[[test-multi-sections|test-multi-sections.conf]]
[sec-1]
cfg_item1 = abcd
@@ -340,6 +355,36 @@
servers = 10.11.12.13:80"
check_result "$VAL" "$EXPECT_VAL"
+echo "merge_config_file test8 non-touchable conf file: "
+set +e
+# function is expected to fail and exit, running it
+# in a subprocess to let this script proceed
+(merge_config_file test.conf test8 /permission-denied.conf)
+VAL=$?
+EXPECT_VAL=255
+check_result "$VAL" "$EXPECT_VAL"
+set -e
+
+echo -n "merge_config_group test9 undefined conf file: "
+set +e
+# function is expected to fail and exit, running it
+# in a subprocess to let this script proceed
+(merge_config_group test.conf test9)
+VAL=$?
+EXPECT_VAL=255
+check_result "$VAL" "$EXPECT_VAL"
+set -e
+
+echo -n "merge_config_group test10 not directory: "
+set +e
+# function is expected to fail and exit, running it
+# in a subprocess to let this script proceed
+(merge_config_group test.conf test10)
+VAL=$?
+EXPECT_VAL=255
+check_result "$VAL" "$EXPECT_VAL"
+set -e
+
rm -f test.conf test1c.conf test2a.conf \
test-space.conf test-equals.conf test-strip.conf \
test-colon.conf test-env.conf test-multiline.conf \