|
@@ -1,4 +1,4 @@
|
|
|
-// Copyright (C) 2013-2016 Internet Systems Consortium, Inc. ("ISC")
|
|
|
+// Copyright (C) 2013-2017 Internet Systems Consortium, Inc. ("ISC")
|
|
|
//
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
@@ -22,6 +22,7 @@ using namespace std;
|
|
|
using namespace isc;
|
|
|
using namespace isc::config;
|
|
|
using namespace isc::process;
|
|
|
+using namespace isc::data;
|
|
|
using namespace boost::posix_time;
|
|
|
|
|
|
namespace {
|
|
@@ -86,6 +87,84 @@ public:
|
|
|
DStubCfgMgrPtr cfg_mgr_;
|
|
|
};
|
|
|
|
|
|
+/// @brief Cfg manager which implements the parseElement() method
|
|
|
+/// This allows testing managers which use the new and/or old parsing
|
|
|
+/// mechanisms to parse configurations. Eventually the latter will
|
|
|
+/// likely be removed.
|
|
|
+class ParseElementMgr : public DStubCfgMgr {
|
|
|
+public:
|
|
|
+
|
|
|
+ /// @brief Constructor
|
|
|
+ ParseElementMgr(){
|
|
|
+ }
|
|
|
+
|
|
|
+ /// @brief Destructor
|
|
|
+ ~ParseElementMgr() {
|
|
|
+ }
|
|
|
+
|
|
|
+ /// @brief Parse the given element if appropriate
|
|
|
+ ///
|
|
|
+ /// Overrides the DCfgMgrBase implementation.
|
|
|
+ /// Looks for the given element by name in the list of "parsable"
|
|
|
+ /// elements. If it is found, it is added to the map of "parsed"
|
|
|
+ /// elements.
|
|
|
+ ///
|
|
|
+ /// @param element_id name of the element as it is expected in the cfg
|
|
|
+ /// @param element value of the element as ElementPtr
|
|
|
+ /// @return true if the element was parsed, false otherwise
|
|
|
+ virtual bool parseElement(const std::string& element_id,
|
|
|
+ isc::data::ConstElementPtr element) {
|
|
|
+ std::string id;
|
|
|
+ BOOST_FOREACH(id, parsable_elements_) {
|
|
|
+ if (element_id == id) {
|
|
|
+ parsed_elements_.set(element_id, element);
|
|
|
+ return (true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return (false);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /// @brief List of element ids which should be parsed by parseElement
|
|
|
+ ElementIdList parsable_elements_;
|
|
|
+
|
|
|
+ /// @brief Map of elements parsed by parseElement
|
|
|
+ MapElement parsed_elements_;
|
|
|
+};
|
|
|
+
|
|
|
+typedef boost::shared_ptr<ParseElementMgr> ParseElementMgrPtr;
|
|
|
+
|
|
|
+/// @brief Test fixture for testing a ParseElementMgr
|
|
|
+class ParseElementMgrTest : public ConfigParseTest {
|
|
|
+public:
|
|
|
+
|
|
|
+ /// @brief Constructor
|
|
|
+ ParseElementMgrTest():cfg_mgr_(new ParseElementMgr()) {
|
|
|
+ }
|
|
|
+
|
|
|
+ /// @brief Destructor
|
|
|
+ ~ParseElementMgrTest() {
|
|
|
+ }
|
|
|
+
|
|
|
+ /// @brief Convenience method which returns a DStubContextPtr to the
|
|
|
+ /// configuration context.
|
|
|
+ ///
|
|
|
+ /// @return returns a DStubContextPtr.
|
|
|
+ DStubContextPtr getStubContext() {
|
|
|
+ return (boost::dynamic_pointer_cast<DStubContext>
|
|
|
+ (cfg_mgr_->getContext()));
|
|
|
+ }
|
|
|
+
|
|
|
+ /// @brief Adds an element id to the list of "parsable" elements
|
|
|
+ void addToParsableElements(const std::string& element_id) {
|
|
|
+ cfg_mgr_->parsable_elements_.push_back(element_id);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// @brief Configuration manager instance.
|
|
|
+ ParseElementMgrPtr cfg_mgr_;
|
|
|
+};
|
|
|
+
|
|
|
///@brief Tests basic construction/destruction of configuration manager.
|
|
|
/// Verifies that:
|
|
|
/// 1. Proper construction succeeds.
|
|
@@ -477,5 +556,69 @@ TEST_F(DStubCfgMgrTest, paramPosition) {
|
|
|
EXPECT_EQ(pos.file_, isc::data::Element::ZERO_POSITION().file_);
|
|
|
}
|
|
|
|
|
|
+// Tests that elements not handled by the parseElement() method are
|
|
|
+// handled by the old parsing mechanisms
|
|
|
+TEST_F(ParseElementMgrTest, basic) {
|
|
|
+ // Create the test config
|
|
|
+ string config = "{ \"bool_test\": true , \n"
|
|
|
+ " \"uint32_test\": 77 , \n"
|
|
|
+ " \"parse_one\": 1, \n"
|
|
|
+ " \"parse_two\": 2, \n"
|
|
|
+ " \"parse_three\": \"3\", \n"
|
|
|
+ " \"string_test\": \"hmmm chewy\" }";
|
|
|
+ ASSERT_TRUE(fromJSON(config));
|
|
|
+
|
|
|
+ // Add two elements to the list of elements handled by parseElement
|
|
|
+ addToParsableElements("parse_one");
|
|
|
+ addToParsableElements("parse_three");
|
|
|
+
|
|
|
+ // Verify that the configuration parses without error.
|
|
|
+ answer_ = cfg_mgr_->parseConfig(config_set_);
|
|
|
+ ASSERT_TRUE(checkAnswer(0));
|
|
|
+ DStubContextPtr context = getStubContext();
|
|
|
+ ASSERT_TRUE(context);
|
|
|
+
|
|
|
+ // Verify that the list of parsed elements is as expected
|
|
|
+ // It should have two entries: "parse_one" and "parse_three"
|
|
|
+ ASSERT_EQ(cfg_mgr_->parsed_elements_.size(), 2);
|
|
|
+ EXPECT_TRUE(cfg_mgr_->parsed_elements_.contains("parse_one"));
|
|
|
+ ConstElementPtr element = cfg_mgr_->parsed_elements_.get("parse_one");
|
|
|
+ EXPECT_EQ(element->intValue(), 1);
|
|
|
+
|
|
|
+ // "parse_two" should not be in the parsed list
|
|
|
+ EXPECT_FALSE(cfg_mgr_->parsed_elements_.contains("parse_two"));
|
|
|
+
|
|
|
+ // "parse_three" should be there
|
|
|
+ EXPECT_TRUE(cfg_mgr_->parsed_elements_.contains("parse_three"));
|
|
|
+ element = cfg_mgr_->parsed_elements_.get("parse_three");
|
|
|
+ EXPECT_EQ(element->stringValue(), "3");
|
|
|
+
|
|
|
+ // Now verify the original mechanism elements were parsed correctly
|
|
|
+ // Verify that the boolean parameter was parsed correctly by retrieving
|
|
|
+ // its value from the context.
|
|
|
+ bool actual_bool = false;
|
|
|
+ isc::data::Element::Position pos;
|
|
|
+ EXPECT_NO_THROW(pos = context->getParam("bool_test", actual_bool));
|
|
|
+ EXPECT_EQ(true, actual_bool);
|
|
|
+ EXPECT_EQ(1, pos.line_);
|
|
|
+
|
|
|
+ // Verify that the uint32 parameter was parsed correctly by retrieving
|
|
|
+ // its value from the context.
|
|
|
+ uint32_t actual_uint32 = 0;
|
|
|
+ EXPECT_NO_THROW(pos = context->getParam("uint32_test", actual_uint32));
|
|
|
+ EXPECT_EQ(77, actual_uint32);
|
|
|
+ EXPECT_EQ(2, pos.line_);
|
|
|
+
|
|
|
+ // Verify that the string parameter was parsed correctly by retrieving
|
|
|
+ // its value from the context.
|
|
|
+ std::string actual_string = "";
|
|
|
+ EXPECT_NO_THROW(pos = context->getParam("string_test", actual_string));
|
|
|
+ EXPECT_EQ("hmmm chewy", actual_string);
|
|
|
+ EXPECT_EQ(6, pos.line_);
|
|
|
+
|
|
|
+ // Verify that "parse_two" wasn't parsed by old parsing either
|
|
|
+ EXPECT_THROW(context->getParam("parse_two", actual_string, false),
|
|
|
+ isc::dhcp::DhcpConfigError);
|
|
|
+}
|
|
|
|
|
|
} // end of anonymous namespace
|