From: Michal Nowikowski Date: Tue, 5 Mar 2019 05:40:15 +0000 (+0100) Subject: Changed location of unit socket in unit tests X-Git-Tag: 465-add-subnet4-update-and-subnet6-update-commands-to-subnet-cmds-hook_base2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a45e2f68d7d1848adb0cf755954a3d76c9dff338;p=thirdparty%2Fkea.git Changed location of unit socket in unit tests Previously it was stored in TEST_DATA_BUILDDIR which resides inside repo sources. Due to the fact that when repo was located in deep patch creating socket was failing as max socket patch is about 100 characters. Now it is located in temp folder managed by Sandbox class. The sandbox directory is created in test constructor and deleted in destructor. As the temp directory is in form /tmp/kea-XXXXXX the lnegth is always lower than 1000, so running the unit tests never fails. --- diff --git a/src/bin/agent/tests/ca_command_mgr_unittests.cc b/src/bin/agent/tests/ca_command_mgr_unittests.cc index f8a7833a71..bf50f61148 100644 --- a/src/bin/agent/tests/ca_command_mgr_unittests.cc +++ b/src/bin/agent/tests/ca_command_mgr_unittests.cc @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -30,9 +31,6 @@ using namespace isc::process; namespace { -/// @brief Test unix socket file name. -const std::string TEST_SOCKET = "test-socket"; - /// @brief Test timeout in ms. const long TEST_TIMEOUT = 10000; @@ -43,6 +41,7 @@ const long TEST_TIMEOUT = 10000; /// Meanwhile, this is just a placeholder for the tests. class CtrlAgentCommandMgrTest : public DControllerTest { public: + isc::test::Sandbox sandbox; /// @brief Constructor. /// @@ -111,17 +110,15 @@ public: /// If the KEA_SOCKET_TEST_DIR environment variable is specified, the /// socket file is created in the location pointed to by this variable. /// Otherwise, it is created in the build directory. - static std::string unixSocketFilePath() { - std::ostringstream s; + std::string unixSocketFilePath() { + std::string socket_path; const char* env = getenv("KEA_SOCKET_TEST_DIR"); if (env) { - s << std::string(env); + socket_path = std::string(env) + "/test-socket"; } else { - s << TEST_DATA_BUILDDIR; + socket_path = sandbox.join("test-socket"); } - - s << "/" << TEST_SOCKET; - return (s.str()); + return (socket_path); } /// @brief Removes unix socket descriptor. diff --git a/src/bin/d2/tests/d2_command_unittest.cc b/src/bin/d2/tests/d2_command_unittest.cc index 0f88dfc680..29d4b17457 100644 --- a/src/bin/d2/tests/d2_command_unittest.cc +++ b/src/bin/d2/tests/d2_command_unittest.cc @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -98,6 +99,7 @@ private: /// @brief Fixture class intended for testin control channel in D2. class CtrlChannelD2Test : public ::testing::Test { public: + isc::test::Sandbox sandbox; /// @brief Path to the UNIX socket being used to communicate with the server. string socket_path_; @@ -122,7 +124,7 @@ public: if (env) { socket_path_ = string(env) + "/d2.sock"; } else { - socket_path_ = string(TEST_DATA_BUILDDIR) + "/d2.sock"; + socket_path_ = sandbox.join("d2.sock"); } ::remove(socket_path_.c_str()); } diff --git a/src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc b/src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc index 1331dd1298..4803e7d912 100644 --- a/src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc +++ b/src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc @@ -22,6 +22,7 @@ #include #include #include +#include #include "marker_file.h" #include "test_libraries.h" @@ -93,6 +94,7 @@ public: /// @brief Fixture class intended for testing control channel in the DHCPv4Srv class CtrlChannelDhcpv4SrvTest : public ::testing::Test { public: + isc::test::Sandbox sandbox; /// @brief Path to the UNIX socket being used to communicate with the server std::string socket_path_; @@ -108,7 +110,7 @@ public: if (env) { socket_path_ = string(env) + "/kea4.sock"; } else { - socket_path_ = string(TEST_DATA_BUILDDIR) + "/kea4.sock"; + socket_path_ = sandbox.join("kea4.sock"); } reset(); } diff --git a/src/bin/dhcp6/tests/ctrl_dhcp6_srv_unittest.cc b/src/bin/dhcp6/tests/ctrl_dhcp6_srv_unittest.cc index 765a9905d8..9a9990fd51 100644 --- a/src/bin/dhcp6/tests/ctrl_dhcp6_srv_unittest.cc +++ b/src/bin/dhcp6/tests/ctrl_dhcp6_srv_unittest.cc @@ -19,6 +19,7 @@ #include #include #include +#include #include "marker_file.h" #include "test_libraries.h" @@ -125,6 +126,7 @@ public: class CtrlChannelDhcpv6SrvTest : public CtrlDhcpv6SrvTest { public: + isc::test::Sandbox sandbox; /// @brief Path to the UNIX socket being used to communicate with the server std::string socket_path_; @@ -140,7 +142,7 @@ public: if (env) { socket_path_ = string(env) + "/kea6.sock"; } else { - socket_path_ = string(TEST_DATA_BUILDDIR) + "/kea6.sock"; + socket_path_ = sandbox.join("/kea6.sock"); } reset(); } diff --git a/src/bin/netconf/tests/control_socket_unittests.cc b/src/bin/netconf/tests/control_socket_unittests.cc index dec968aba0..0d55e25be5 100644 --- a/src/bin/netconf/tests/control_socket_unittests.cc +++ b/src/bin/netconf/tests/control_socket_unittests.cc @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -131,15 +132,14 @@ TEST(StdoutControlSocketTest, configSet) { //////////////////////////////// UNIX //////////////////////////////// -/// @brief Test unix socket file name. -const string TEST_SOCKET = "test-socket"; - /// @brief Test timeout in ms. const long TEST_TIMEOUT = 10000; /// @brief Test fixture class for unix control sockets. class UnixControlSocketTest : public ThreadedTest { public: + isc::test::Sandbox sandbox; + /// @brief Constructor. UnixControlSocketTest() : ThreadedTest(), io_service_() { @@ -164,17 +164,15 @@ public: /// If the KEA_SOCKET_TEST_DIR environment variable is specified, the /// socket file is created in the location pointed to by this variable. /// Otherwise, it is created in the build directory. - static string unixSocketFilePath() { - ostringstream s; + string unixSocketFilePath() { + std::string socket_path; const char* env = getenv("KEA_SOCKET_TEST_DIR"); if (env) { - s << string(env); + socket_path = std::string(env) + "/test-socket"; } else { - s << TEST_DATA_BUILDDIR; + socket_path = sandbox.join("test-socket"); } - - s << "/" << TEST_SOCKET; - return (s.str()); + return (socket_path); } /// @brief Removes unix socket descriptor. diff --git a/src/bin/netconf/tests/netconf_unittests.cc b/src/bin/netconf/tests/netconf_unittests.cc index d1f24a58b5..b85a4589ee 100644 --- a/src/bin/netconf/tests/netconf_unittests.cc +++ b/src/bin/netconf/tests/netconf_unittests.cc @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -89,6 +90,8 @@ void clearYang(NakedNetconfAgentPtr agent) { /// @brief Test fixture class for netconf agent. class NetconfAgentTest : public ThreadedTest { public: + isc::test::Sandbox sandbox; + /// @brief Constructor. NetconfAgentTest() : ThreadedTest(), @@ -127,17 +130,15 @@ public: /// If the KEA_SOCKET_TEST_DIR environment variable is specified, the /// socket file is created in the location pointed to by this variable. /// Otherwise, it is created in the build directory. - static string unixSocketFilePath() { - ostringstream s; + string unixSocketFilePath() { + std::string socket_path; const char* env = getenv("KEA_SOCKET_TEST_DIR"); if (env) { - s << string(env); + socket_path = std::string(env) + "/test-socket"; } else { - s << TEST_DATA_BUILDDIR; + socket_path = sandbox.join("test-socket"); } - - s << "/" << TEST_SOCKET; - return (s.str()); + return (socket_path); } /// @brief Removes unix socket descriptor. diff --git a/src/lib/asiolink/tests/unix_domain_socket_unittest.cc b/src/lib/asiolink/tests/unix_domain_socket_unittest.cc index 916e8dccea..544948a582 100644 --- a/src/lib/asiolink/tests/unix_domain_socket_unittest.cc +++ b/src/lib/asiolink/tests/unix_domain_socket_unittest.cc @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -20,15 +21,13 @@ using namespace isc::asiolink; namespace { -/// @brief Test unix socket file name. -const std::string TEST_SOCKET = "test-socket"; - /// @brief Test timeout in ms. const long TEST_TIMEOUT = 10000; /// @brief Test fixture class for @ref UnixDomainSocket class. class UnixDomainSocketTest : public ::testing::Test { public: + isc::test::Sandbox sandbox; /// @brief Constructor. /// @@ -55,17 +54,15 @@ public: /// If the KEA_SOCKET_TEST_DIR environment variable is specified, the /// socket file is created in the location pointed to by this variable. /// Otherwise, it is created in the build directory. - static std::string unixSocketFilePath() { - std::ostringstream s; + std::string unixSocketFilePath() { + std::string socket_path; const char* env = getenv("KEA_SOCKET_TEST_DIR"); if (env) { - s << std::string(env); + socket_path = std::string(env) + "/test-socket"; } else { - s << TEST_DATA_BUILDDIR; + socket_path = sandbox.join("test-socket"); } - - s << "/" << TEST_SOCKET; - return (s.str()); + return (socket_path); } /// @brief Removes unix socket descriptor. diff --git a/src/lib/config/tests/client_connection_unittests.cc b/src/lib/config/tests/client_connection_unittests.cc index 0b27cbce37..c741b6cae7 100644 --- a/src/lib/config/tests/client_connection_unittests.cc +++ b/src/lib/config/tests/client_connection_unittests.cc @@ -5,6 +5,7 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. #include +#include #include #include #include @@ -20,15 +21,13 @@ using namespace isc::config; namespace { -/// @brief Test unix socket file name. -const std::string TEST_SOCKET = "test-socket"; - /// @brief Test timeout in ms. const long TEST_TIMEOUT = 10000; /// Test fixture class for @ref ClientConnection. class ClientConnectionTest : public ::testing::Test { public: + isc::test::Sandbox sandbox; /// @brief Constructor. /// @@ -60,17 +59,15 @@ public: /// the KEA_SOCKET_TEST_DIR environmental variable to point to an alternative /// location, e.g. /tmp, with an absolute path length being within the /// allowed range. - static std::string unixSocketFilePath() { - std::ostringstream s; + std::string unixSocketFilePath() { + std::string socket_path; const char* env = getenv("KEA_SOCKET_TEST_DIR"); if (env) { - s << std::string(env); + socket_path = std::string(env) + "/test-socket"; } else { - s << TEST_DATA_BUILDDIR; + socket_path = sandbox.join("test-socket"); } - - s << "/" << TEST_SOCKET; - return (s.str()); + return (socket_path); } /// @brief Removes unix socket descriptor. diff --git a/src/lib/config/tests/command_mgr_unittests.cc b/src/lib/config/tests/command_mgr_unittests.cc index 3d2b0ac29a..a2101c11c6 100644 --- a/src/lib/config/tests/command_mgr_unittests.cc +++ b/src/lib/config/tests/command_mgr_unittests.cc @@ -8,6 +8,7 @@ #include +#include #include #include #include @@ -28,6 +29,7 @@ using namespace std; // Test class for Command Manager class CommandMgrTest : public ::testing::Test { public: + isc::test::Sandbox sandbox; /// Default constructor CommandMgrTest() @@ -58,13 +60,12 @@ public: /// @brief Returns socket path (using either hardcoded path or env variable) /// @return path to the unix socket std::string getSocketPath() { - std::string socket_path; const char* env = getenv("KEA_SOCKET_TEST_DIR"); if (env) { socket_path = std::string(env) + "/test-socket"; } else { - socket_path = std::string(TEST_DATA_BUILDDIR) + "/test-socket"; + socket_path = sandbox.join("test-socket"); } return (socket_path); } diff --git a/src/lib/testutils/Makefile.am b/src/lib/testutils/Makefile.am index bc3c4a23a0..4eeb3c7aa6 100644 --- a/src/lib/testutils/Makefile.am +++ b/src/lib/testutils/Makefile.am @@ -8,6 +8,7 @@ if HAVE_GTEST noinst_LTLIBRARIES = libkea-testutils.la libkea_testutils_la_SOURCES = io_utils.cc io_utils.h +libkea_testutils_la_SOURCES += sandbox.h libkea_testutils_la_SOURCES += log_utils.cc log_utils.h libkea_testutils_la_SOURCES += test_to_element.cc test_to_element.h libkea_testutils_la_SOURCES += threaded_test.cc threaded_test.h diff --git a/src/lib/testutils/sandbox.h b/src/lib/testutils/sandbox.h new file mode 100644 index 0000000000..115108e350 --- /dev/null +++ b/src/lib/testutils/sandbox.h @@ -0,0 +1,64 @@ +// Copyright (C) 2019 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/. + +#ifndef SANDBOX_H +#define SANDBOX_H + +#include + +#include +#include +#include +#include +#include + +namespace isc { +namespace test { + +/// @brief A Sandbox class that provides access to unit test unique +/// temporary folder. +/// +/// The sandbox's temporary folder is created in constructor ie. +/// in unit test setup phase, and then it is deleted with its content +/// in destructor ie. in unit test tear down phase. +class Sandbox { + /// Path to temporary folder + std::string path_; + + /// @brief Method for deleting files and folders, used in nftw traversal function. + static int rmFile(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) { + (void)sb; + (void)typeflag; + (void)ftwbuf; + return(remove(fpath)); + } + +public: + /// @brief Sandbox constructor. + Sandbox() { + char tmpl[] = {P_tmpdir "/kea-XXXXXX"}; + path_ = mkdtemp(tmpl); + } + + /// @brief Destructor, it deletes temporary folder with its content. + ~Sandbox() { + // Delete content of path_ recursively. + if (nftw(path_.c_str(), Sandbox::rmFile, 10, FTW_DEPTH | FTW_MOUNT | FTW_PHYS) < 0) { + isc_throw(isc::Unexpected, "Some issue occured while deleting unit test sandbox."); + } + } + + // @brief Join sandbox path with indicated file subpath. + std::string join(std::string file) { + return path_ + "/" + file; + } +}; + + +}; // end of isc::test namespace +}; // end of isc namespace + +#endif // SANDBOX_H diff --git a/src/lib/util/tests/socketsession_unittest.cc b/src/lib/util/tests/socketsession_unittest.cc index 2c81e29de6..4df14ee6ae 100644 --- a/src/lib/util/tests/socketsession_unittest.cc +++ b/src/lib/util/tests/socketsession_unittest.cc @@ -28,6 +28,7 @@ #include #include +#include #include @@ -147,17 +148,17 @@ private: class ForwardTest : public ::testing::Test { protected: + isc::test::Sandbox sandbox; /// @brief Returns socket path (using either hardcoded path or env variable) /// @return path to the unix socket std::string getSocketPath() { - std::string socket_path; const char* env = getenv("KEA_SOCKET_TEST_DIR"); if (env) { socket_path = string(env) + "/test.unix"; } else { - socket_path = string(TEST_DATA_BUILDDIR) + "/test.unix"; + socket_path = sandbox.join("test.unix"); } return (socket_path); }