mirror of
https://github.com/servalproject/serval-dna.git
synced 2025-02-06 11:09:13 +00:00
Improve servald 'config' 'set' and 'del' commands
Return exit status 2 if the new config is invalid in any way.
This commit is contained in:
parent
71ed78e058
commit
93c38a764d
@ -948,7 +948,11 @@ int app_config_set(int argc, const char *const *argv, const struct command_line_
|
|||||||
if (cf_om_reload() == -1)
|
if (cf_om_reload() == -1)
|
||||||
return -1;
|
return -1;
|
||||||
// </kludge>
|
// </kludge>
|
||||||
return cf_om_set(&cf_om_root, var, val) == -1 ? -1 : cf_om_save();
|
if (cf_om_set(&cf_om_root, var, val) == -1 || cf_om_save() == -1)
|
||||||
|
return -1;
|
||||||
|
if (cf_reload() == -1) // logs an error if the new config is bad
|
||||||
|
return 2;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int app_config_del(int argc, const char *const *argv, const struct command_line_option *o, void *context)
|
int app_config_del(int argc, const char *const *argv, const struct command_line_option *o, void *context)
|
||||||
@ -963,7 +967,11 @@ int app_config_del(int argc, const char *const *argv, const struct command_line_
|
|||||||
if (cf_om_reload() == -1)
|
if (cf_om_reload() == -1)
|
||||||
return -1;
|
return -1;
|
||||||
// </kludge>
|
// </kludge>
|
||||||
return cf_om_set(&cf_om_root, var, NULL) == -1 ? -1 : cf_om_save();
|
if (cf_om_set(&cf_om_root, var, NULL) == -1 || cf_om_save() == -1)
|
||||||
|
return -1;
|
||||||
|
if (cf_reload() == -1) // logs an error if the new config is bad
|
||||||
|
return 2;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int app_config_get(int argc, const char *const *argv, const struct command_line_option *o, void *context)
|
int app_config_get(int argc, const char *const *argv, const struct command_line_option *o, void *context)
|
||||||
|
2
conf.c
2
conf.c
@ -74,6 +74,8 @@ static int load()
|
|||||||
else if (meta.size > CONFIG_FILE_MAX_SIZE) {
|
else if (meta.size > CONFIG_FILE_MAX_SIZE) {
|
||||||
WHYF("config file %s is too big (%ld bytes exceeds limit %ld)", path, meta.size, CONFIG_FILE_MAX_SIZE);
|
WHYF("config file %s is too big (%ld bytes exceeds limit %ld)", path, meta.size, CONFIG_FILE_MAX_SIZE);
|
||||||
return CFERROR;
|
return CFERROR;
|
||||||
|
} else if (meta.size <= 0) {
|
||||||
|
INFOF("config file %s is zero size", path);
|
||||||
} else {
|
} else {
|
||||||
FILE *f = fopen(path, "r");
|
FILE *f = fopen(path, "r");
|
||||||
if (f == NULL) {
|
if (f == NULL) {
|
||||||
|
81
tests/config
81
tests/config
@ -41,100 +41,83 @@ setup_SetCreateInstanceDir() {
|
|||||||
assert ! [ -d "$SERVALINSTANCE_PATH" ]
|
assert ! [ -d "$SERVALINSTANCE_PATH" ]
|
||||||
}
|
}
|
||||||
test_SetCreateInstanceDir() {
|
test_SetCreateInstanceDir() {
|
||||||
executeOk_servald config set foo bar
|
executeOk_servald config set debug.verbose 0
|
||||||
assert [ -d "$SERVALINSTANCE_PATH" ]
|
assert [ -d "$SERVALINSTANCE_PATH" ]
|
||||||
}
|
}
|
||||||
|
|
||||||
doc_GetNull="Get an unset config item"
|
doc_GetNull="Get an unset config item"
|
||||||
test_GetNull() {
|
test_GetNull() {
|
||||||
executeOk_servald config get foo
|
executeOk_servald config get debug.verbose
|
||||||
assertStdoutLineCount '==' 0
|
assertStdoutLineCount '==' 0
|
||||||
}
|
}
|
||||||
|
|
||||||
doc_SetGet="Set and get a single config item"
|
doc_SetGet="Set and get a single config item"
|
||||||
test_SetGet() {
|
test_SetGet() {
|
||||||
executeOk_servald config set foo bar
|
executeOk_servald config set debug.verbose yes
|
||||||
executeOk_servald config get foo
|
executeOk_servald config get debug.verbose
|
||||||
assertStdoutLineCount '==' 1
|
assertStdoutLineCount '==' 1
|
||||||
assertStdoutGrep --stdout --stderr --matches=1 '^foo=bar$'
|
assertStdoutGrep --stdout --stderr --matches=1 '^debug\.verbose=yes$'
|
||||||
}
|
}
|
||||||
|
|
||||||
doc_GetAll="Get all config items"
|
doc_GetAll="Get all config items"
|
||||||
test_GetAll() {
|
test_GetAll() {
|
||||||
executeOk_servald config set foo bar
|
executeOk_servald config set debug.verbose true
|
||||||
executeOk_servald config set hello world
|
executeOk_servald config set log.show_pid true
|
||||||
|
executeOk_servald config set server.chdir /tmp/nothing
|
||||||
|
executeOk_servald config set rhizome.enable no
|
||||||
executeOk_servald config get
|
executeOk_servald config get
|
||||||
assertStdoutLineCount '==' 2
|
assertStdoutLineCount '==' 4
|
||||||
assertStdoutGrep --stdout --matches=1 '^foo=bar$'
|
assertStdoutGrep --stdout --matches=1 '^debug\.verbose=true$'
|
||||||
assertStdoutGrep --stdout --matches=1 '^hello=world$'
|
assertStdoutGrep --stdout --matches=1 '^log\.show_pid=true$'
|
||||||
|
assertStdoutGrep --stdout --matches=1 '^server\.chdir=/tmp/nothing$'
|
||||||
|
assertStdoutGrep --stdout --matches=1 '^rhizome\.enable=no$'
|
||||||
}
|
}
|
||||||
|
|
||||||
doc_SetTwice="Set a single config item twice"
|
doc_SetTwice="Set a single config item twice"
|
||||||
test_SetTwice() {
|
test_SetTwice() {
|
||||||
executeOk_servald config set foo bar
|
executeOk_servald config set debug.verbose yes
|
||||||
executeOk_servald config get foo
|
executeOk_servald config get debug.verbose
|
||||||
assertStdoutLineCount '==' 1
|
assertStdoutLineCount '==' 1
|
||||||
assertStdoutGrep --stdout --stderr --matches=1 '^foo=bar$'
|
assertStdoutGrep --stdout --stderr --matches=1 '^debug\.verbose=yes$'
|
||||||
executeOk_servald config set foo wah
|
executeOk_servald config set debug.verbose false
|
||||||
executeOk_servald config get foo
|
executeOk_servald config get debug.verbose
|
||||||
assertStdoutLineCount '==' 1
|
assertStdoutLineCount '==' 1
|
||||||
assertStdoutGrep --stdout --stderr --matches=1 '^foo=wah$'
|
assertStdoutGrep --stdout --stderr --matches=1 '^debug\.verbose=false$'
|
||||||
}
|
}
|
||||||
|
|
||||||
doc_DelNull="Delete an unset config item"
|
doc_DelNull="Delete an unset config item"
|
||||||
test_DelNull() {
|
test_DelNull() {
|
||||||
executeOk_servald config del foo
|
executeOk_servald config del debug.verbose
|
||||||
assertStdoutLineCount '==' 0
|
assertStdoutLineCount '==' 0
|
||||||
}
|
}
|
||||||
|
|
||||||
doc_Del="Delete single config item"
|
doc_Del="Delete single config item"
|
||||||
test_Del() {
|
test_Del() {
|
||||||
executeOk_servald config set foo bar
|
executeOk_servald config set debug.verbose yes
|
||||||
executeOk_servald config set hello world
|
executeOk_servald config set log.show_pid true
|
||||||
executeOk_servald config get
|
executeOk_servald config get
|
||||||
assertStdoutLineCount '==' 2
|
assertStdoutLineCount '==' 2
|
||||||
executeOk_servald config del foo
|
executeOk_servald config del debug.verbose
|
||||||
executeOk_servald config get
|
executeOk_servald config get
|
||||||
assertStdoutLineCount '==' 1
|
assertStdoutLineCount '==' 1
|
||||||
executeOk_servald config get foo
|
executeOk_servald config del log.show_pid
|
||||||
assertStdoutLineCount '==' 0
|
assertStdoutLineCount '==' 0
|
||||||
}
|
}
|
||||||
|
|
||||||
doc_CaseSensitive="Config item names are case sensitive"
|
doc_CaseSensitive="Config item names are case sensitive"
|
||||||
test_CaseSensitive() {
|
test_CaseSensitive() {
|
||||||
executeOk_servald config set foo bar
|
execute $servald config set Debug.verbose yes
|
||||||
executeOk_servald config set Foo xxx
|
assertExitStatus --stderr '!=' 0
|
||||||
executeOk_servald config get foo
|
|
||||||
assertStdoutLineCount '==' 1
|
|
||||||
assertStdoutGrep --stdout --stderr --matches=1 '^foo=bar$'
|
|
||||||
executeOk_servald config get Foo
|
|
||||||
assertStdoutLineCount '==' 1
|
|
||||||
assertStdoutGrep --stdout --stderr --matches=1 '^Foo=xxx$'
|
|
||||||
executeOk_servald config set FOO wah
|
|
||||||
executeOk_servald config get foo
|
|
||||||
assertStdoutLineCount '==' 1
|
|
||||||
assertStdoutGrep --stdout --stderr --matches=1 '^foo=bar$'
|
|
||||||
executeOk_servald config get FOO
|
|
||||||
assertStdoutLineCount '==' 1
|
|
||||||
assertStdoutGrep --stdout --stderr --matches=1 '^FOO=wah$'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
doc_DotsInNames="Config item names can have internal dots"
|
doc_OptionNames="Config item names must be well formed"
|
||||||
test_DotsInNames() {
|
test_OptionNames() {
|
||||||
executeOk_servald config set foo.bar yes
|
execute $servald config set debug. yes
|
||||||
executeOk_servald config get foo.bar
|
|
||||||
assertStdoutLineCount '==' 1
|
|
||||||
assertStdoutGrep --stdout --stderr --matches=1 '^foo\.bar=yes$'
|
|
||||||
execute $servald config set foo. yes
|
|
||||||
assertExitStatus --stderr '!=' 0
|
assertExitStatus --stderr '!=' 0
|
||||||
execute $servald config set .foo yes
|
execute $servald config set .verbose yes
|
||||||
assertExitStatus --stderr '!=' 0
|
assertExitStatus --stderr '!=' 0
|
||||||
execute $servald config set foo..bar yes
|
execute $servald config set debug..verbose yes
|
||||||
assertExitStatus --stderr '!=' 0
|
assertExitStatus --stderr '!=' 0
|
||||||
executeOk_servald config set foo.x.bar yes
|
|
||||||
executeOk_servald config get foo.x.bar
|
|
||||||
assertStdoutLineCount --stderr '==' 1
|
|
||||||
assertStdoutGrep --stdout --stderr --matches=1 '^foo\.x\.bar=yes$'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
doc_DebugFlags="Debug config options affect verbosity"
|
doc_DebugFlags="Debug config options affect verbosity"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user