Subsequent configuration keys now override previous ones

instead of merging.

The exception is final values, for example, values like
default-log-dir that may be set on the command line.
pull/678/merge
Jason Ish 12 years ago committed by Victor Julien
parent c981a16579
commit b033acfb0c

@ -237,9 +237,14 @@ ConfYamlParse(yaml_parser_t *parser, ConfNode *parent, int inseq)
Mangle(parent->val);
}
}
ConfNode *n0 = ConfNodeLookupChild(parent, value);
if (n0 != NULL) {
node = n0;
ConfNode *existing = ConfNodeLookupChild(parent, value);
if (existing != NULL) {
if (!existing->final) {
SCLogInfo("Configuration node '%s' redefined.",
existing->name);
ConfNodePrune(existing);
}
node = existing;
}
else {
node = ConfNodeNew();
@ -733,6 +738,88 @@ cleanup:
return ret;
}
/**
* Test that a configuration section is overridden but subsequent
* occurrences.
*/
static int
ConfYamlOverrideTest(void)
{
char config[] =
"%YAML 1.1\n"
"---\n"
"some-log-dir: /var/log\n"
"some-log-dir: /tmp\n"
"\n"
"parent:\n"
" child0:\n"
" key: value\n"
"parent:\n"
" child1:\n"
" key: value\n"
;
char *value;
ConfCreateContextBackup();
ConfInit();
if (ConfYamlLoadString(config, strlen(config)) != 0)
return 0;
if (!ConfGet("some-log-dir", &value))
return 0;
if (strcmp(value, "/tmp") != 0)
return 0;
/* Test that parent.child0 does not exist, but child1 does. */
if (ConfGetNode("parent.child0") != NULL)
return 0;
if (!ConfGet("parent.child1.key", &value))
return 0;
if (strcmp(value, "value") != 0)
return 0;
ConfDeInit();
ConfRestoreContextBackup();
return 1;
}
/**
* Test that a configuration parameter loaded from YAML doesn't
* override a 'final' value that may be set on the command line.
*/
static int
ConfYamlOverrideFinalTest(void)
{
ConfCreateContextBackup();
ConfInit();
char config[] =
"%YAML 1.1\n"
"---\n"
"default-log-dir: /var/log\n";
/* Set the log directory as if it was set on the command line. */
if (!ConfSetFinal("default-log-dir", "/tmp"))
return 0;
if (ConfYamlLoadString(config, strlen(config)) != 0)
return 0;
char *default_log_dir;
if (!ConfGet("default-log-dir", &default_log_dir))
return 0;
if (strcmp(default_log_dir, "/tmp") != 0) {
fprintf(stderr, "final value was reassigned\n");
return 0;
}
ConfDeInit();
ConfRestoreContextBackup();
return 1;
}
#endif /* UNITTESTS */
void
@ -746,5 +833,7 @@ ConfYamlRegisterTests(void)
UtRegisterTest("ConfYamlSecondLevelSequenceTest",
ConfYamlSecondLevelSequenceTest, 1);
UtRegisterTest("ConfYamlFileIncludeTest", ConfYamlFileIncludeTest, 1);
UtRegisterTest("ConfYamlOverrideTest", ConfYamlOverrideTest, 1);
UtRegisterTest("ConfYamlOverrideFinalTest", ConfYamlOverrideFinalTest, 1);
#endif /* UNITTESTS */
}

Loading…
Cancel
Save