]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#3594] accept output-options or output_options in log parser
authorRazvan Becheriu <razvan@isc.org>
Mon, 7 Oct 2024 18:31:26 +0000 (21:31 +0300)
committerRazvan Becheriu <razvan@isc.org>
Mon, 7 Oct 2024 18:31:26 +0000 (21:31 +0300)
src/lib/process/log_parser.cc
src/lib/process/tests/log_parser_unittests.cc

index dc067f04f2cbc07013d0f580cac47ce75e569140..7e06ac32c3b515cd3e3a73b54cdbfbbfa7a298c5 100644 (file)
@@ -104,6 +104,18 @@ void LogConfigParser::parseConfigEntry(isc::data::ConstElementPtr entry) {
     }
 
     isc::data::ConstElementPtr output_options = entry->get("output-options");
+    isc::data::ConstElementPtr deprecated_output_options = entry->get("output_options");
+
+    if (output_options && deprecated_output_options) {
+        isc_throw(BadValue, "Only one of 'output-options' and 'output_options' may be specified.");
+    }
+
+    if (deprecated_output_options) {
+        output_options = deprecated_output_options;
+        ElementPtr mutable_element = boost::const_pointer_cast<Element>(entry);
+        mutable_element->remove("output_options");
+        mutable_element->set("output-options", output_options);
+    }
 
     if (output_options) {
         parseOutputOptions(info.destinations_, output_options);
index 43c30541d7ae4c5298cf3e7d0ec047da650d07f2..448c471551abebf99625c6a0e0ee75494a4a67dc 100644 (file)
@@ -132,6 +132,99 @@ TEST_F(LoggingTest, parsingConsoleOutput) {
     EXPECT_TRUE(storage->getLoggingInfo()[0].destinations_[0].flush_);
 }
 
+// This test checks that deprecated parameter output_options is accepted.
+TEST_F(LoggingTest, parsingDeprecatedOutputOptions) {
+
+    const char* config_txt =
+    "{ \"loggers\": ["
+    "    {"
+    "        \"name\": \"kea\","
+    "        \"output_options\": ["
+    "            {"
+    "                \"output\": \"stdout\","
+    "                \"flush\": true"
+    "            }"
+    "        ],"
+    "        \"debuglevel\": 99,"
+    "        \"severity\": \"DEBUG\""
+    "    }"
+    "]}";
+
+    ConfigPtr storage(new ConfigBase());
+
+    LogConfigParser parser(storage);
+
+    // We need to parse properly formed JSON and then extract
+    // "loggers" element from it. For some reason fromJSON is
+    // throwing at opening square bracket
+    ConstElementPtr config = Element::fromJSON(config_txt);
+    config = config->get("loggers");
+
+    ASSERT_EQ(1, config->size());
+    ASSERT_TRUE(config->get(0)->get("output_options"));
+    ASSERT_FALSE(config->get(0)->get("output-options"));
+
+    EXPECT_NO_THROW(parser.parseConfiguration(config));
+
+    ASSERT_EQ(1, storage->getLoggingInfo().size());
+
+    EXPECT_EQ("kea", storage->getLoggingInfo()[0].name_);
+    EXPECT_EQ(99, storage->getLoggingInfo()[0].debuglevel_);
+    EXPECT_EQ(isc::log::DEBUG, storage->getLoggingInfo()[0].severity_);
+
+    ASSERT_EQ(1, storage->getLoggingInfo()[0].destinations_.size());
+    EXPECT_EQ("stdout" , storage->getLoggingInfo()[0].destinations_[0].output_);
+    EXPECT_TRUE(storage->getLoggingInfo()[0].destinations_[0].flush_);
+
+    ASSERT_TRUE(config->get(0)->get("output-options"));
+    ASSERT_FALSE(config->get(0)->get("output_options"));
+}
+
+// This test checks that specifying both output-options and deprecated parameter
+// output_options is not accepted.
+TEST_F(LoggingTest, parsingErrorOutputOptions) {
+
+    const char* config_txt =
+    "{ \"loggers\": ["
+    "    {"
+    "        \"name\": \"kea\","
+    "        \"output_options\": ["
+    "            {"
+    "                \"output\": \"stdout\","
+    "                \"flush\": true"
+    "            }"
+    "        ],"
+    "        \"output-options\": ["
+    "            {"
+    "                \"output\": \"stdout\","
+    "                \"flush\": true"
+    "            }"
+    "        ],"
+    "        \"debuglevel\": 99,"
+    "        \"severity\": \"DEBUG\""
+    "    }"
+    "]}";
+
+    ConfigPtr storage(new ConfigBase());
+
+    LogConfigParser parser(storage);
+
+    // We need to parse properly formed JSON and then extract
+    // "loggers" element from it. For some reason fromJSON is
+    // throwing at opening square bracket
+    ConstElementPtr config = Element::fromJSON(config_txt);
+    config = config->get("loggers");
+
+    ASSERT_EQ(1, config->size());
+    ASSERT_TRUE(config->get(0)->get("output_options"));
+    ASSERT_TRUE(config->get(0)->get("output-options"));
+
+    ASSERT_THROW_MSG(parser.parseConfiguration(config), BadValue, "Only one of "
+                     "'output-options' and 'output_options' may be specified.");
+
+    ASSERT_EQ(0, storage->getLoggingInfo().size());
+}
+
 // Check that LogConfigParser can parse configuration that
 // lacks a severity entry.
 TEST_F(LoggingTest, parsingNoSeverity) {