diff options
author | Razvan Becheriu <razvan@isc.org> | 2020-04-27 17:41:14 +0200 |
---|---|---|
committer | Razvan Becheriu <razvan@isc.org> | 2020-04-30 12:53:48 +0200 |
commit | abaaa9d8696b6e7260c2b1e25713a59d8874b37c (patch) | |
tree | 7753a3b144e37080d2f10ad938e977c353305dff /src/bin/dhcp6/tests | |
parent | [#1188] stop thread pool when applying configuration and use multi threading ... (diff) | |
download | kea-abaaa9d8696b6e7260c2b1e25713a59d8874b37c.tar.xz kea-abaaa9d8696b6e7260c2b1e25713a59d8874b37c.zip |
[#1188] added unittests
Diffstat (limited to 'src/bin/dhcp6/tests')
-rw-r--r-- | src/bin/dhcp6/tests/callout_library_1.cc | 14 | ||||
-rw-r--r-- | src/bin/dhcp6/tests/callout_library_3.cc | 10 | ||||
-rw-r--r-- | src/bin/dhcp6/tests/ctrl_dhcp6_srv_unittest.cc | 56 | ||||
-rw-r--r-- | src/bin/dhcp6/tests/hooks_unittest.cc | 51 |
4 files changed, 127 insertions, 4 deletions
diff --git a/src/bin/dhcp6/tests/callout_library_1.cc b/src/bin/dhcp6/tests/callout_library_1.cc index ffe05eba3f..768f374fe6 100644 --- a/src/bin/dhcp6/tests/callout_library_1.cc +++ b/src/bin/dhcp6/tests/callout_library_1.cc @@ -14,3 +14,17 @@ static const int LIBRARY_NUMBER = 1; #include <config.h> #include <dhcp6/tests/callout_library_common.h> + +// Functions accessed by the hooks framework use C linkage to avoid the name +// mangling that accompanies use of the C++ compiler as well as to avoid +// issues related to namespaces. +extern "C" { + +/// @brief This function is called to retrieve the multi-threading compatibility. +/// +/// @return 1 which means compatible with multi-threading. +int multi_threading_compatible() { + return (1); +} + +} // end extern "C" diff --git a/src/bin/dhcp6/tests/callout_library_3.cc b/src/bin/dhcp6/tests/callout_library_3.cc index 10bb0e6bc7..06a67ff804 100644 --- a/src/bin/dhcp6/tests/callout_library_3.cc +++ b/src/bin/dhcp6/tests/callout_library_3.cc @@ -17,6 +17,9 @@ static const int LIBRARY_NUMBER = 3; using namespace isc::hooks; +// Functions accessed by the hooks framework use C linkage to avoid the name +// mangling that accompanies use of the C++ compiler as well as to avoid +// issues related to namespaces. extern "C" { /// @brief Callout which appends library number and provided arguments to @@ -43,4 +46,11 @@ dhcp6_srv_configured(CalloutHandle& handle) { return (0); } +/// @brief This function is called to retrieve the multi-threading compatibility. +/// +/// @return 0 which means not compatible with multi-threading. +int multi_threading_compatible() { + return (0); } + +} // end extern "C" diff --git a/src/bin/dhcp6/tests/ctrl_dhcp6_srv_unittest.cc b/src/bin/dhcp6/tests/ctrl_dhcp6_srv_unittest.cc index 3a9259e4a5..9cec48651c 100644 --- a/src/bin/dhcp6/tests/ctrl_dhcp6_srv_unittest.cc +++ b/src/bin/dhcp6/tests/ctrl_dhcp6_srv_unittest.cc @@ -18,10 +18,11 @@ #include <hooks/hooks_manager.h> #include <log/logger_support.h> #include <stats/stats_mgr.h> +#include <util/boost_time_utils.h> +#include <util/multi_threading_mgr.h> #include <testutils/io_utils.h> #include <testutils/unix_control_client.h> #include <testutils/sandbox.h> -#include <util/boost_time_utils.h> #include "marker_file.h" #include "test_libraries.h" @@ -31,6 +32,7 @@ #include <iomanip> #include <sstream> +#include <thread> #include <sys/select.h> #include <sys/stat.h> @@ -38,8 +40,6 @@ #include <cstdlib> #include <unistd.h> -#include <thread> - using namespace std; using namespace isc; using namespace isc::asiolink; @@ -50,6 +50,7 @@ using namespace isc::dhcp::test; using namespace isc::hooks; using namespace isc::stats; using namespace isc::test; +using namespace isc::util; namespace { @@ -149,12 +150,14 @@ public: socket_path_ = sandbox.join("/kea6.sock"); } reset(); + MultiThreadingMgr::instance().setMode(false); } /// @brief Destructor ~CtrlChannelDhcpv6SrvTest() { server_.reset(); reset(); + MultiThreadingMgr::instance().setMode(false); }; /// @brief Returns pointer to the server's IO service. @@ -472,6 +475,53 @@ TEST_F(CtrlChannelDhcpv6SrvTest, libreload) { EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "1212")); } +// Check that the "libreload" command will reload libraries when MT is enabled +TEST_F(CtrlChannelDhcpv6SrvTest, libreloadFailMultiThreading) { + createUnixChannelServer(); + + // Ensure no marker files to start with. + ASSERT_FALSE(checkMarkerFileExists(LOAD_MARKER_FILE)); + ASSERT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE)); + + // Load two libraries + HookLibsCollection libraries; + libraries.push_back(make_pair(CALLOUT_LIBRARY_1, ConstElementPtr())); + libraries.push_back(make_pair(CALLOUT_LIBRARY_2, ConstElementPtr())); + HooksManager::loadLibraries(libraries); + + // Check they are loaded. + HookLibsCollection loaded_libraries = + HooksManager::getLibraryInfo(); + ASSERT_TRUE(libraries == loaded_libraries); + + // ... which also included checking that the marker file created by the + // load functions exists and holds the correct value (of "12" - the + // first library appends "1" to the file, the second appends "2"). Also + // check that the unload marker file does not yet exist. + EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "12")); + EXPECT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE)); + + // Enable multi-threading before libreload command which should now fail + // as the second library is not multi-threading compatible. + MultiThreadingMgr::instance().setMode(true); + + // Now execute the "libreload" command. This should cause the libraries + // to unload and to reload. + std::string response; + sendUnixCommand("{ \"command\": \"libreload\" }", response); + EXPECT_EQ("{ \"result\": 1, " + "\"text\": \"Failed to reload hooks libraries.\" }" + , response); + + // Check that the libraries have unloaded and failed to reload. The + // libraries are unloaded in the reverse order to which they are loaded. + // When they load, they should append information to the loading marker + // file. Failing to load the second library will also unload the first + // library. + EXPECT_TRUE(checkMarkerFile(UNLOAD_MARKER_FILE, "211")); + EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "121")); +} + typedef std::map<std::string, isc::data::ConstElementPtr> ElementMap; // This test checks which commands are registered by the DHCPv6 server. diff --git a/src/bin/dhcp6/tests/hooks_unittest.cc b/src/bin/dhcp6/tests/hooks_unittest.cc index fb2d0b5aa5..985007734a 100644 --- a/src/bin/dhcp6/tests/hooks_unittest.cc +++ b/src/bin/dhcp6/tests/hooks_unittest.cc @@ -29,6 +29,7 @@ #include <cc/command_interpreter.h> #include <dhcp6/tests/marker_file.h> #include <dhcp6/tests/test_libraries.h> +#include <util/multi_threading_mgr.h> #include <boost/scoped_ptr.hpp> #include <gtest/gtest.h> @@ -967,12 +968,14 @@ public: LoadUnloadDhcpv6SrvTest() : Dhcpv6SrvTest() { reset(); + MultiThreadingMgr::instance().setMode(false); } /// @brief Destructor ~LoadUnloadDhcpv6SrvTest() { server_.reset(); reset(); + MultiThreadingMgr::instance().setMode(false); }; /// @brief Reset hooks data @@ -4874,8 +4877,8 @@ TEST_F(LoadUnloadDhcpv6SrvTest, unloadLibraries) { libraries.push_back(make_pair(std::string(CALLOUT_LIBRARY_1), ConstElementPtr())); libraries.push_back(make_pair(std::string(CALLOUT_LIBRARY_2), - ConstElementPtr())); + ASSERT_TRUE(HooksManager::loadLibraries(libraries)); // Verify that they load functions created the LOAD_MARKER_FILE @@ -4893,7 +4896,53 @@ TEST_F(LoadUnloadDhcpv6SrvTest, unloadLibraries) { // this should be reflected in the unload file. EXPECT_TRUE(checkMarkerFile(UNLOAD_MARKER_FILE, "21")); EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "12")); +} + +// Verifies that libraries incompatible with multi threading are not loaded by +// the server +// The callout libraries write their library index number to a marker +// file upon load and unload, making it simple to test whether or not +// the load and unload callouts have been invoked. +TEST_F(LoadUnloadDhcpv6SrvTest, failLoadIncompatibleLibraries) { + + ASSERT_NO_THROW(server_.reset(new NakedDhcpv6Srv(0))); + + MultiThreadingMgr::instance().setMode(true); + + // Ensure no marker files to start with. + ASSERT_FALSE(checkMarkerFileExists(LOAD_MARKER_FILE)); + ASSERT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE)); + + // Load the test libraries + HookLibsCollection libraries; + libraries.push_back(make_pair(std::string(CALLOUT_LIBRARY_2), + ConstElementPtr())); + + ASSERT_FALSE(HooksManager::loadLibraries(libraries)); + + // The library is missing multi_threading_compatible function so loading + // should fail + EXPECT_FALSE(checkMarkerFileExists(LOAD_MARKER_FILE)); + EXPECT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE)); + libraries.clear(); + libraries.push_back(make_pair(std::string(CALLOUT_LIBRARY_3), + ConstElementPtr())); + + ASSERT_FALSE(HooksManager::loadLibraries(libraries)); + + // The library is not multi threading compatible so loading should fail + EXPECT_FALSE(checkMarkerFileExists(LOAD_MARKER_FILE)); + EXPECT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE)); + + // Destroy the server, instance which should unload the libraries. + server_.reset(); + + // Check that the libraries were unloaded. The libraries are + // unloaded in the reverse order to which they are loaded, and + // this should be reflected in the unload file. + EXPECT_FALSE(checkMarkerFileExists(LOAD_MARKER_FILE)); + EXPECT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE)); } // Checks if callouts installed on the dhcp6_srv_configured ared indeed called |