From: Tomek Mrugalski Date: Fri, 31 Aug 2018 19:10:32 +0000 (+0200) Subject: [#25,!14] libprocess code now compiles and unit-tests pass X-Git-Tag: gitlab116_base~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1c29e58e0a2768a72d393fe6acc8d138912bdfc0;p=thirdparty%2Fkea.git [#25,!14] libprocess code now compiles and unit-tests pass --- diff --git a/src/lib/process/config_base.h b/src/lib/process/config_base.h index 71ba21534e..c9e3671384 100644 --- a/src/lib/process/config_base.h +++ b/src/lib/process/config_base.h @@ -54,7 +54,7 @@ public: /// @brief Compares two configuration. /// /// @param other the other configuration to compare to - virtual bool equals(const ConfigBase& other) const; + bool equals(const ConfigBase& other) const; /// @brief Converts to Element representation /// @@ -70,7 +70,7 @@ protected: /// by descendant classes. /// /// @param new_config this configuration will be copied to new_config - virtual void copy(ConfigBase& new_config) const; + void copy(ConfigBase& new_config) const; private: /// @brief Logging specific information. diff --git a/src/lib/process/d_cfg_mgr.h b/src/lib/process/d_cfg_mgr.h index d9ff933ea0..e789643473 100644 --- a/src/lib/process/d_cfg_mgr.h +++ b/src/lib/process/d_cfg_mgr.h @@ -60,7 +60,7 @@ typedef boost::shared_ptr DCfgContextBasePtr; /// // Restore from backup /// context_ = backup_copy; /// -class DCfgContextBase : public isc::dhcp::UserContext, public isc::data::CfgToElement { +class DCfgContextBase : public isc::data::UserContext, public isc::data::CfgToElement { public: /// @brief Indicator that a configuration parameter is optional. static const bool OPTIONAL = true; diff --git a/src/lib/process/d_controller.cc b/src/lib/process/d_controller.cc index 10eb43574e..c43c0588bd 100644 --- a/src/lib/process/d_controller.cc +++ b/src/lib/process/d_controller.cc @@ -100,7 +100,7 @@ DControllerBase::launch(int argc, char* argv[], const bool test_mode) { try { createPIDFile(); - } catch (const dhcp::DaemonPIDExists& ex) { + } catch (const DaemonPIDExists& ex) { LOG_FATAL(dctl_logger, DCTL_ALREADY_RUNNING) .arg(bin_name_).arg(ex.what()); isc_throw (LaunchError, "Launch Failed: " << ex.what()); diff --git a/src/lib/process/d_controller.h b/src/lib/process/d_controller.h index e5de1aa7c5..a9e4391013 100644 --- a/src/lib/process/d_controller.h +++ b/src/lib/process/d_controller.h @@ -9,9 +9,9 @@ #include #include -#include #include #include +#include #include #include #include @@ -101,7 +101,7 @@ typedef boost::shared_ptr DControllerBasePtr; /// NOTE: Derivations must supply their own static singleton instance method(s) /// for creating and fetching the instance. The base class declares the instance /// member in order for it to be available for static callback functions. -class DControllerBase : public dhcp::Daemon { +class DControllerBase : public Daemon { public: /// @brief Constructor /// diff --git a/src/lib/process/daemon.cc b/src/lib/process/daemon.cc index fd06e8b732..d167530e72 100644 --- a/src/lib/process/daemon.cc +++ b/src/lib/process/daemon.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2015,2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2018 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 @@ -6,12 +6,12 @@ #include #include -#include -#include +#include +#include #include #include #include -#include +#include #include #include @@ -25,11 +25,15 @@ /// This file provides stub implementations that are expected to be redefined /// in derived classes (e.g. ControlledDhcpv6Srv) namespace isc { -namespace dhcp { +namespace process { + +bool Daemon::verbose_ = false; + +std::string Daemon::default_logger_name_("kea"); Daemon::Daemon() : signal_set_(), signal_handler_(), config_file_(""), proc_name_(""), - pid_file_dir_(DHCP_DATA_DIR), pid_file_(), am_file_author_(false) { + pid_file_dir_(DATA_DIR), pid_file_(), am_file_author_(false) { // The pid_file_dir can be overridden via environment variable // This is primarily intended to simplify testing @@ -60,25 +64,25 @@ void Daemon::handleSignal() { } void Daemon::configureLogger(const isc::data::ConstElementPtr& log_config, - const SrvConfigPtr& storage) { + const ConfigPtr& storage) { if (log_config) { isc::data::ConstElementPtr loggers = log_config->get("loggers"); if (loggers) { LogConfigParser parser(storage); - parser.parseConfiguration(loggers, CfgMgr::instance().isVerbose()); + parser.parseConfiguration(loggers, verbose_); } } } void Daemon::setVerbose(bool verbose) { - CfgMgr::instance().setVerbose(verbose); + verbose_ = verbose; } bool -Daemon::getVerbose() const { - return (CfgMgr::instance().isVerbose()); +Daemon::getVerbose() { + return (verbose_); } void Daemon::loggerInit(const char* name, bool verbose) { @@ -209,10 +213,6 @@ Daemon::createPIDFile(int pid) { size_t Daemon::writeConfigFile(const std::string& config_file, isc::data::ConstElementPtr cfg) const { - if (!cfg) { - cfg = CfgMgr::instance().getCurrentCfg()->toElement(); - } - if (!cfg) { isc_throw(Unexpected, "Can't write configuration: conversion to JSON failed"); } diff --git a/src/lib/process/daemon.h b/src/lib/process/daemon.h index df895ddd42..2570ba9b67 100644 --- a/src/lib/process/daemon.h +++ b/src/lib/process/daemon.h @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2015,2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2018 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 @@ -8,14 +8,14 @@ #define DAEMON_H #include -#include +#include #include #include #include #include namespace isc { -namespace dhcp { +namespace process { /// @brief Exception thrown when a the PID file points to a live PID class DaemonPIDExists : public Exception { @@ -96,7 +96,7 @@ public: /// @param log_config JSON structures that describe logging /// @param storage configuration will be stored here static void configureLogger(const isc::data::ConstElementPtr& log_config, - const isc::dhcp::SrvConfigPtr& storage); + const isc::process::ConfigPtr& storage); /// @brief Sets or clears verbose mode /// @@ -105,12 +105,12 @@ public: /// config file are ignored. /// /// @param verbose specifies if verbose should be set or not - void setVerbose(const bool verbose); + static void setVerbose(const bool verbose); /// @brief Returns if running in verbose mode /// /// @return verbose mode - bool getVerbose() const; + static bool getVerbose(); /// @brief returns Kea version on stdout and exits. /// @@ -200,6 +200,19 @@ public: /// PID of the current process is used. void createPIDFile(int pid = 0); + /// @brief Returns default logger name. + static std::string getDefaultLoggerName() { + return (default_logger_name_); + } + + /// @brief Sets the default logger name. + /// + /// This name is used in cases when a user doesn't provide a configuration + /// for logger in the Kea configuration file. + static void setDefaultLoggerName(const std::string& logger) { + default_logger_name_ = logger; + } + protected: /// @brief Invokes handler for the next received signal. @@ -245,6 +258,12 @@ private: /// @brief Pointer to the PID file for this process isc::util::PIDFilePtr pid_file_; + /// @brief Indicates whether verbose mode is turned on or not. + static bool verbose_; + + /// @brief Stores default logger name + static std::string default_logger_name_; + /// @brief Flag indicating if this instance created the file bool am_file_author_; }; diff --git a/src/lib/process/log_parser.cc b/src/lib/process/log_parser.cc index 6fad9354b5..47f340b679 100644 --- a/src/lib/process/log_parser.cc +++ b/src/lib/process/log_parser.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2018 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 @@ -6,7 +6,7 @@ #include #include -#include +#include #include #include #include @@ -18,9 +18,9 @@ using namespace isc::data; using namespace isc::log; namespace isc { -namespace dhcp { +namespace process { -LogConfigParser::LogConfigParser(const SrvConfigPtr& storage) +LogConfigParser::LogConfigParser(const ConfigPtr& storage) :config_(storage), verbose_(false) { if (!storage) { isc_throw(BadValue, "LogConfigParser needs a pointer to the " diff --git a/src/lib/process/log_parser.h b/src/lib/process/log_parser.h index 77a197f5c5..f7cd8c5fc8 100644 --- a/src/lib/process/log_parser.h +++ b/src/lib/process/log_parser.h @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2018 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 @@ -8,11 +8,12 @@ #define DHCPSRV_LOGGING_H #include -#include +#include +#include #include namespace isc { -namespace dhcp { +namespace process { /// @brief Configures log4cplus by translating Kea configuration structures /// @@ -45,7 +46,7 @@ public: /// @brief Constructor /// /// @param storage parsed logging configuration will be stored here - LogConfigParser(const SrvConfigPtr& storage); + LogConfigParser(const ConfigPtr& storage); /// @brief Parses specified configuration /// @@ -77,7 +78,7 @@ private: /// @brief Configuration is stored here /// /// LogConfigParser class uses only config_->logging_info_ field. - SrvConfigPtr config_; + ConfigPtr config_; /// @brief Verbose mode /// diff --git a/src/lib/process/logging_info.cc b/src/lib/process/logging_info.cc index ff39f3a886..8bb42c146b 100644 --- a/src/lib/process/logging_info.cc +++ b/src/lib/process/logging_info.cc @@ -1,19 +1,19 @@ -// Copyright (C) 2014-2015,2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2018 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 // file, You can obtain one at http://mozilla.org/MPL/2.0/. #include -#include -#include +#include +#include #include using namespace isc::log; using namespace isc::data; namespace isc { -namespace dhcp { +namespace process { bool LoggingDestination::equals(const LoggingDestination& other) const { @@ -43,16 +43,16 @@ LoggingInfo::LoggingInfo() : name_("kea"), severity_(isc::log::INFO), debuglevel_(0) { // If configuration Manager is in the verbose mode, we need to modify the // default settings. - if (CfgMgr::instance().isVerbose()) { + if (Daemon::getVerbose()) { severity_ = isc::log::DEBUG; debuglevel_ = 99; } // If the process has set the non-empty name for the default logger, // let's use this name. - std::string logger_name = CfgMgr::instance().getDefaultLoggerName(); - if (!logger_name.empty()) { - name_ = logger_name; + std::string default_logger = Daemon::getDefaultLoggerName(); + if (!default_logger.empty()) { + name_ = default_logger; } // Add a default logging destination in case use hasn't provided a diff --git a/src/lib/process/logging_info.h b/src/lib/process/logging_info.h index d74fb4f16d..91ca81181b 100644 --- a/src/lib/process/logging_info.h +++ b/src/lib/process/logging_info.h @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2015,2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2018 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 @@ -15,7 +15,7 @@ #include namespace isc { -namespace dhcp { +namespace process { /// @brief Defines single logging destination /// @@ -73,7 +73,7 @@ public: /// "severity": "WARN", /// "debuglevel": 99 /// }, -class LoggingInfo : public UserContext, public isc::data::CfgToElement { +class LoggingInfo : public isc::data::UserContext, public isc::data::CfgToElement { public: /// @brief logging name @@ -133,7 +133,7 @@ public: }; /// @brief storage for logging information in log4cplus format -typedef std::vector LoggingInfoStorage; +typedef std::vector LoggingInfoStorage; } } diff --git a/src/lib/process/tests/Makefile.am b/src/lib/process/tests/Makefile.am index 28833698e1..5fe2f0227b 100644 --- a/src/lib/process/tests/Makefile.am +++ b/src/lib/process/tests/Makefile.am @@ -3,6 +3,7 @@ SUBDIRS = . AM_CPPFLAGS = -I$(top_builddir)/src/lib -I$(top_srcdir)/src/lib AM_CPPFLAGS += $(BOOST_INCLUDES) AM_CPPFLAGS += -DTEST_DATA_BUILDDIR=\"$(abs_top_builddir)/src/lib/process/tests\" +AM_CPPFLAGS += -DDATA_DIR="\"$(dhcp_data_dir)\"" AM_CPPFLAGS += -DINSTALL_PROG=\"$(abs_top_srcdir)/install-sh\" AM_CXXFLAGS = $(KEA_CXXFLAGS) @@ -24,6 +25,8 @@ libprocess_unittests_SOURCES = d_cfg_mgr_unittests.cc libprocess_unittests_SOURCES += d_controller_unittests.cc libprocess_unittests_SOURCES += daemon_unittest.cc libprocess_unittests_SOURCES += io_service_signal_unittests.cc +libprocess_unittests_SOURCES += log_parser_unittests.cc +libprocess_unittests_SOURCES += logging_info_unittests.cc libprocess_unittests_SOURCES += run_unittests.cc libprocess_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES) @@ -35,6 +38,7 @@ libprocess_unittests_LDFLAGS = $(AM_LDFLAGS) $(GTEST_LDFLAGS) libprocess_unittests_LDADD = $(top_builddir)/src/lib/process/testutils/libprocesstest.la libprocess_unittests_LDADD += $(top_builddir)/src/lib/process/libkea-process.la libprocess_unittests_LDADD += $(top_builddir)/src/lib/cfgrpt/libcfgrpt.la +libprocess_unittests_LDADD += $(top_builddir)/src/lib/testutils/libkea-testutils.la libprocess_unittests_LDADD += $(top_builddir)/src/lib/dhcpsrv/libkea-dhcpsrv.la libprocess_unittests_LDADD += $(top_builddir)/src/lib/config/libkea-cfgclient.la libprocess_unittests_LDADD += $(top_builddir)/src/lib/dhcp/libkea-dhcp++.la diff --git a/src/lib/process/tests/daemon_unittest.cc b/src/lib/process/tests/daemon_unittest.cc index 80765a4ea1..3cf3151d45 100644 --- a/src/lib/process/tests/daemon_unittest.cc +++ b/src/lib/process/tests/daemon_unittest.cc @@ -9,8 +9,9 @@ #include #include #include -#include -#include +#include +#include +#include #include #include @@ -18,11 +19,11 @@ #include using namespace isc; -using namespace isc::dhcp; +using namespace isc::process; using namespace isc::data; namespace isc { -namespace dhcp { +namespace process { // @brief Derived Daemon class class DaemonImpl : public Daemon { @@ -92,7 +93,7 @@ TEST_F(DaemonTest, constructor) { EXPECT_TRUE(instance2.getConfigFile().empty()); EXPECT_TRUE(instance2.getProcName().empty()); - EXPECT_EQ(CfgMgr::instance().getDataDir(),instance2.getPIDFileDir()); + EXPECT_EQ(std::string(DATA_DIR), instance2.getPIDFileDir()); EXPECT_TRUE(instance2.getPIDFileName().empty()); } @@ -265,10 +266,10 @@ TEST_F(DaemonTest, PIDFileCleanup) { // More dedicated tests are available for LogConfigParser class. // See logger_unittest.cc TEST_F(DaemonTest, parsingConsoleOutput) { - CfgMgr::instance().setVerbose(false); + Daemon::setVerbose(false); // Storage - parsed configuration will be stored here - SrvConfigPtr storage(new SrvConfig()); + ConfigPtr storage(new ConfigBase()); const char* config_txt = "{ \"loggers\": [" diff --git a/src/lib/process/tests/log_parser_unittests.cc b/src/lib/process/tests/log_parser_unittests.cc index 30046f28bb..d66f05903c 100644 --- a/src/lib/process/tests/log_parser_unittests.cc +++ b/src/lib/process/tests/log_parser_unittests.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2018 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 @@ -6,16 +6,17 @@ #include #include -#include -#include +#include +#include #include #include +#include #include #include using namespace isc; -using namespace isc::dhcp; +using namespace isc::process; using namespace isc::data; namespace { @@ -78,10 +79,10 @@ const int LoggingTest::TEST_MAX_VERS = 2; // More than the default of 1 // Checks that the constructor is able to process specified storage properly. TEST_F(LoggingTest, constructor) { - SrvConfigPtr null_ptr; + ConfigPtr null_ptr; EXPECT_THROW(LogConfigParser parser(null_ptr), BadValue); - SrvConfigPtr nonnull(new SrvConfig()); + ConfigPtr nonnull(new ConfigBase()); EXPECT_NO_THROW(LogConfigParser parser(nonnull)); } @@ -106,7 +107,7 @@ TEST_F(LoggingTest, parsingConsoleOutput) { " }" "]}"; - SrvConfigPtr storage(new SrvConfig()); + ConfigPtr storage(new ConfigBase()); LogConfigParser parser(storage); @@ -147,7 +148,7 @@ TEST_F(LoggingTest, parsingFile) { " }" "]}"; - SrvConfigPtr storage(new SrvConfig()); + ConfigPtr storage(new ConfigBase()); LogConfigParser parser(storage); @@ -201,7 +202,7 @@ TEST_F(LoggingTest, multipleLoggers) { " }" "]}"; - SrvConfigPtr storage(new SrvConfig()); + ConfigPtr storage(new ConfigBase()); LogConfigParser parser(storage); @@ -251,7 +252,7 @@ TEST_F(LoggingTest, multipleLoggingDestinations) { " }" "]}"; - SrvConfigPtr storage(new SrvConfig()); + ConfigPtr storage(new ConfigBase()); LogConfigParser parser(storage); @@ -305,7 +306,7 @@ TEST_F(LoggingTest, logRotate) { "]}"; // Create our server config container. - SrvConfigPtr server_cfg(new SrvConfig()); + ConfigPtr server_cfg(new ConfigBase()); // LogConfigParser expects a list of loggers, so parse // the JSON text and extract the "loggers" element from it @@ -330,7 +331,7 @@ TEST_F(LoggingTest, logRotate) { for (int i = 1; i < TEST_MAX_VERS + 1; i++) { // Output the big log and make sure we get the expected rotation file. - LOG_INFO(logger, DHCPSRV_CFGMGR_ADD_IFACE).arg(big_arg); + LOG_INFO(logger, DCTL_CONFIG_COMPLETE).arg(big_arg); EXPECT_TRUE(isc::test::fileExists(logName(i).c_str())); } diff --git a/src/lib/process/tests/logging_info_unittests.cc b/src/lib/process/tests/logging_info_unittests.cc index f778765066..7768b77c16 100644 --- a/src/lib/process/tests/logging_info_unittests.cc +++ b/src/lib/process/tests/logging_info_unittests.cc @@ -1,16 +1,16 @@ -// Copyright (C) 2014-2015,2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2018 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 // file, You can obtain one at http://mozilla.org/MPL/2.0/. #include -#include -#include +#include +#include #include #include -using namespace isc::dhcp; +using namespace isc::process; using namespace isc::test; using namespace isc::data; @@ -53,12 +53,12 @@ public: /// @brief Setup the test. virtual void SetUp() { - CfgMgr::instance().setVerbose(false); + Daemon::setVerbose(false); } /// @brief Clear after the test. virtual void TearDown() { - CfgMgr::instance().setVerbose(false); + Daemon::setVerbose(false); } }; @@ -94,7 +94,7 @@ TEST_F(LoggingInfoTest, defaults) { expected = header + comment + ",\n" + begin + "INFO" + dbglvl + "0" + trailer; runToElementTest(expected, info_non_verbose); - CfgMgr::instance().setVerbose(true); + Daemon::setVerbose(true); LoggingInfo info_verbose; EXPECT_EQ("kea", info_verbose.name_); EXPECT_EQ(isc::log::DEBUG, info_verbose.severity_);