]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#99,!197] Added merging into the ConfigBase class.
authorMarcin Siodelski <marcin@isc.org>
Wed, 9 Jan 2019 14:41:10 +0000 (15:41 +0100)
committerMarcin Siodelski <marcin@isc.org>
Mon, 14 Jan 2019 12:18:46 +0000 (07:18 -0500)
src/lib/process/config_base.cc
src/lib/process/config_base.h
src/lib/process/config_ctl_info.cc
src/lib/process/config_ctl_info.h
src/lib/process/tests/config_base_unittests.cc

index 74b4ca788f7090a8ae5e610da95d318023f972ae..99f03fb2c823aea4099f2a7d46f59f106ff13e3a 100644 (file)
@@ -82,6 +82,23 @@ ConfigBase::copy(ConfigBase& other) const {
     }
 }
 
+void
+ConfigBase::merge(const ConfigBase& other) {
+    // Merge logging info.
+    if (!other.logging_info_.empty()) {
+        logging_info_ = other.logging_info_;
+    }
+
+    // Merge the config control info
+    if (other.config_ctl_info_) {
+        if (config_ctl_info_) {
+            config_ctl_info_->merge(*other.config_ctl_info_);
+        } else {
+            config_ctl_info_ = other.config_ctl_info_;
+        }
+    }
+}
+
 ElementPtr
 ConfigBase::toElement() const {
     ElementPtr result = Element::createMap();
index bdf8fbc8843631d4c0223fe44f25d2d0184bf0ca..9bb8884bf2bcbbed2b4f98bf13f7ac991fcf9491 100644 (file)
@@ -57,6 +57,19 @@ public:
     /// @param other the other configuration to compare to
     bool equals(const ConfigBase& other) const;
 
+    /// @brief Merges specified configuration into this configuration.
+    ///
+    /// This method merges logging and config control configuration into
+    /// this configuration. The new logging configuration replaces the
+    /// existing configuration if the new logging configuration is
+    /// non-empty. The new config control configuration replaces the
+    /// existing configuration if the new logging configuration is
+    /// non-null and non-empty.
+    ///
+    /// @param other the other configuration to be merged into this
+    /// configuration.
+    virtual void merge(const ConfigBase& other);
+
     /// @brief Converts to Element representation
     ///
     /// This creates a Map element with the following content (expressed
index 9d841346919edc34ef9476f61e4a89fcec0164a9..4fb37d26c8c8572debf799e442ddd20994bc33c6 100644 (file)
@@ -90,6 +90,13 @@ ConfigControlInfo::clear() {
     db_infos_.clear();
 }
 
+void
+ConfigControlInfo::merge(const ConfigControlInfo& other) {
+    if (!other.db_infos_.empty()) {
+        db_infos_ = other.db_infos_;
+    }
+}
+
 ElementPtr
 ConfigControlInfo::toElement() const {
     ElementPtr result = Element::createMap();
index b5c94eff5b185e73a2d257c9aa7e269c05b272ef..19fd63f1291ef000b4f800e4315b93b30996f7ea 100644 (file)
@@ -172,6 +172,15 @@ public:
     /// @brief Empties the contents of the class, including the database list
     void clear();
 
+    /// @brief Merges specified configuration into this configuration.
+    ///
+    /// If the other configuration is non-empty it completely replaces
+    /// this configuration.
+    ///
+    /// @param other the other configuration to be merged into this
+    /// configuration.
+    void merge(const ConfigControlInfo& other);
+
     /// @brief Unparse a configuration object
     ///
     /// @return a pointer to unparsed configuration
index 0b45bd03cc149bbbe254830789cb383ce348f6fa..2c0302cc7dce6e289392712c5941cee71fa79871 100644 (file)
@@ -57,3 +57,69 @@ TEST(ConfigBase, configControlInfoTests) {
     // They should be equal again.
     EXPECT_TRUE(base1.equals(base2));
 }
+
+// Verifies that logging information can be merged to another.
+TEST(ConfigBase, mergeLoggingInfo) {
+    // Create first logging info.
+    LoggingInfo log_info1;
+    log_info1.name_ = "foo";
+
+    // Create second logging info.
+    LoggingInfo log_info2;
+    log_info2.name_ = "bar";
+
+    // Create first config base instance.
+    ConfigBaseImpl base1;
+    base1.addLoggingInfo(log_info1);
+
+    // Copy the first instance to keep it as reference.
+    ConfigBaseImpl base1_copy;
+    base1_copy.copy(base1);
+
+    // Create second config base instance.
+    ConfigBaseImpl base2;
+    ASSERT_NO_THROW(base1.merge(base2));
+    EXPECT_TRUE(base1.equals(base1_copy));
+
+    // Set some data for the second config.
+    base2.addLoggingInfo(log_info2);
+
+    // This time the merge should replace the original config.
+    ASSERT_NO_THROW(base1.merge(base2));
+    EXPECT_TRUE(base1.equals(base2));
+}
+
+// Verifies that config control can be merged to another.
+TEST(ConfigBase, mergeConfigControl) {
+    // Create first config control info.
+    ConfigControlInfoPtr ctl_info1(new ConfigControlInfo());
+    ctl_info1->addConfigDatabase("type=mysql host=example.com");
+    ctl_info1->addConfigDatabase("type=mysql host=example2.com");
+
+    // Create second config control info.
+    ConfigControlInfoPtr ctl_info2(new ConfigControlInfo());
+    ctl_info2->addConfigDatabase("type=pgsql host=example.com");
+    ctl_info2->addConfigDatabase("type=pgsql host=example2.com");
+
+    // Create first config base instance.
+    ConfigBaseImpl base1;
+    base1.setConfigControlInfo(ctl_info1);
+
+    // Copy the first instance to keep it as reference.
+    ConfigBaseImpl base1_copy;
+    base1_copy.copy(base1);
+
+    // Create second config base instance.
+    ConfigBaseImpl base2;
+
+    // Merged base is empty, so the original should be preserved.
+    ASSERT_NO_THROW(base1.merge(base2));
+    EXPECT_TRUE(base1.equals(base1_copy));
+
+    // Set some data for the second config.
+    base2.setConfigControlInfo(ctl_info2);
+
+    // This time the merge should replace the original config.
+    ASSERT_NO_THROW(base1.merge(base2));
+    EXPECT_TRUE(base1.equals(base2));
+}