mirror of
https://gitlab.isc.org/isc-projects/kea
synced 2025-08-29 21:18:02 +00:00
[#1188] added unittests
This commit is contained in:
parent
454fd5bd35
commit
abaaa9d869
@ -14,3 +14,17 @@ static const int LIBRARY_NUMBER = 1;
|
||||
#include <config.h>
|
||||
|
||||
#include <dhcp4/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"
|
||||
|
@ -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 @@ dhcp4_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"
|
||||
|
@ -22,6 +22,7 @@
|
||||
#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>
|
||||
@ -52,6 +53,7 @@ using namespace isc::dhcp::test;
|
||||
using namespace isc::hooks;
|
||||
using namespace isc::stats;
|
||||
using namespace isc::test;
|
||||
using namespace isc::util;
|
||||
|
||||
namespace {
|
||||
|
||||
@ -115,6 +117,7 @@ public:
|
||||
socket_path_ = sandbox.join("kea4.sock");
|
||||
}
|
||||
reset();
|
||||
MultiThreadingMgr::instance().setMode(false);
|
||||
}
|
||||
|
||||
/// @brief Destructor
|
||||
@ -127,6 +130,7 @@ public:
|
||||
CommandMgr::instance().setConnectionTimeout(TIMEOUT_DHCP_SERVER_RECEIVE_COMMAND);
|
||||
|
||||
server_.reset();
|
||||
MultiThreadingMgr::instance().setMode(false);
|
||||
};
|
||||
|
||||
/// @brief Returns pointer to the server's IO service.
|
||||
@ -457,6 +461,53 @@ TEST_F(CtrlChannelDhcpv4SrvTest, libreload) {
|
||||
EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "1212"));
|
||||
}
|
||||
|
||||
// Check that the "libreload" command will reload libraries when MT is enabled
|
||||
TEST_F(CtrlChannelDhcpv4SrvTest, 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"));
|
||||
}
|
||||
|
||||
// This test checks which commands are registered by the DHCPv4 server.
|
||||
TEST_F(CtrlChannelDhcpv4SrvTest, commandsRegistration) {
|
||||
|
||||
|
@ -23,6 +23,7 @@
|
||||
#include <dhcp4/tests/dhcp4_client.h>
|
||||
#include <dhcp4/tests/marker_file.h>
|
||||
#include <dhcp4/tests/test_libraries.h>
|
||||
#include <util/multi_threading_mgr.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
@ -33,6 +34,7 @@ using namespace isc::hooks;
|
||||
using namespace isc::config;
|
||||
using namespace isc::dhcp::test;
|
||||
using namespace isc::dhcp;
|
||||
using namespace isc::util;
|
||||
|
||||
// Checks if hooks are registered properly.
|
||||
TEST_F(Dhcpv4SrvTest, Hooks) {
|
||||
@ -860,12 +862,14 @@ public:
|
||||
|
||||
LoadUnloadDhcpv4SrvTest() {
|
||||
reset();
|
||||
MultiThreadingMgr::instance().setMode(false);
|
||||
}
|
||||
|
||||
/// @brief Destructor
|
||||
~LoadUnloadDhcpv4SrvTest() {
|
||||
server_.reset();
|
||||
reset();
|
||||
MultiThreadingMgr::instance().setMode(false);
|
||||
};
|
||||
|
||||
/// @brief Reset hooks data
|
||||
@ -2730,8 +2734,8 @@ TEST_F(LoadUnloadDhcpv4SrvTest, 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
|
||||
@ -2751,6 +2755,53 @@ TEST_F(LoadUnloadDhcpv4SrvTest, unloadLibraries) {
|
||||
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(LoadUnloadDhcpv4SrvTest, failLoadIncompatibleLibraries) {
|
||||
|
||||
ASSERT_NO_THROW(server_.reset(new NakedDhcpv4Srv()));
|
||||
|
||||
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 dhcp4_srv_configured ared indeed called
|
||||
// and all the necessary parameters are passed.
|
||||
TEST_F(LoadUnloadDhcpv4SrvTest, Dhcpv4SrvConfigured) {
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
@ -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.
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user