]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
Changed location of unit socket in unit tests
authorMichal Nowikowski <godfryd@isc.org>
Tue, 5 Mar 2019 05:40:15 +0000 (06:40 +0100)
committerMichal Nowikowski <godfryd@isc.org>
Thu, 7 Mar 2019 09:57:33 +0000 (10:57 +0100)
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.

12 files changed:
src/bin/agent/tests/ca_command_mgr_unittests.cc
src/bin/d2/tests/d2_command_unittest.cc
src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc
src/bin/dhcp6/tests/ctrl_dhcp6_srv_unittest.cc
src/bin/netconf/tests/control_socket_unittests.cc
src/bin/netconf/tests/netconf_unittests.cc
src/lib/asiolink/tests/unix_domain_socket_unittest.cc
src/lib/config/tests/client_connection_unittests.cc
src/lib/config/tests/command_mgr_unittests.cc
src/lib/testutils/Makefile.am
src/lib/testutils/sandbox.h [new file with mode: 0644]
src/lib/util/tests/socketsession_unittest.cc

index f8a7833a7142f270d12db229619fb1aa275837ba..bf50f61148c5e88cc04bf18cd8a5746045f1ab3d 100644 (file)
@@ -20,6 +20,7 @@
 #include <boost/bind.hpp>
 #include <boost/pointer_cast.hpp>
 #include <gtest/gtest.h>
+#include <testutils/sandbox.h>
 #include <cstdlib>
 #include <vector>
 
@@ -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.
index 0f88dfc6807693ef2ef0da7d3ca939b9b7ce278c..29d4b17457ce6e30af2de3801a10df055c572488 100644 (file)
@@ -17,6 +17,7 @@
 #include <d2/d2_process.h>
 #include <d2/parser_context.h>
 #include <gtest/gtest.h>
+#include <testutils/sandbox.h>
 #include <boost/pointer_cast.hpp>
 #include <fstream>
 #include <iostream>
@@ -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());
     }
index 1331dd1298ad485a71d4c4bcf884b6ea7123164c..4803e7d9123312768c2e2350e128035b3864d637 100644 (file)
@@ -22,6 +22,7 @@
 #include <stats/stats_mgr.h>
 #include <testutils/io_utils.h>
 #include <testutils/unix_control_client.h>
+#include <testutils/sandbox.h>
 
 #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();
     }
index 765a9905d84bac3674d22cd4020940fd43b9edd4..9a9990fd51a0679f9bffd8076868ecb2672fad9f 100644 (file)
@@ -19,6 +19,7 @@
 #include <stats/stats_mgr.h>
 #include <testutils/unix_control_client.h>
 #include <testutils/io_utils.h>
+#include <testutils/sandbox.h>
 
 #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();
     }
index dec968aba0dd4ef9deb3dfc9b8a2e52207f1ab0f..0d55e25be5d325b20e7d6b42eb36147b51ce1738 100644 (file)
@@ -19,6 +19,7 @@
 #include <http/response_json.h>
 #include <http/tests/response_test.h>
 #include <testutils/threaded_test.h>
+#include <testutils/sandbox.h>
 #include <util/threads/thread.h>
 #include <util/threads/sync.h>
 #include <gtest/gtest.h>
@@ -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.
index d1f24a58b5aab9d2e5b40f22f72ae9f8cc837d9e..b85a4589eea280472a0cac10597acabbe7af1030 100644 (file)
@@ -21,6 +21,7 @@
 #include <yang/testutils/translator_test.h>
 #include <testutils/log_utils.h>
 #include <testutils/threaded_test.h>
+#include <testutils/sandbox.h>
 #include <gtest/gtest.h>
 #include <sstream>
 
@@ -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.
index 916e8dcceace934eefced288f166948d4df00707..544948a582752913bf0aca41d7d382f7d04ceeeb 100644 (file)
@@ -10,6 +10,7 @@
 #include <asiolink/unix_domain_socket.h>
 #include <asiolink/testutils/test_server_unix_socket.h>
 #include <gtest/gtest.h>
+#include <testutils/sandbox.h>
 #include <array>
 #include <cstdio>
 #include <cstdlib>
@@ -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.
index 0b27cbce37c2cabec8692cafa7f91c0d707525bc..c741b6cae7bf97640efde2ecb51d39c9970d35de 100644 (file)
@@ -5,6 +5,7 @@
 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 #include <config.h>
+#include <testutils/sandbox.h>
 #include <asiolink/asio_wrapper.h>
 #include <asiolink/io_service.h>
 #include <asiolink/testutils/test_server_unix_socket.h>
@@ -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.
index 3d2b0ac29afee9505c31170ee690449dd3900e23..a2101c11c608e27df40addc8007c32631b8838f4 100644 (file)
@@ -8,6 +8,7 @@
 
 #include <gtest/gtest.h>
 
+#include <testutils/sandbox.h>
 #include <asiolink/io_service.h>
 #include <config/base_command_mgr.h>
 #include <config/command_mgr.h>
@@ -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);
     }
index bc3c4a23a0f350249f34bab0e49eb6e7e56395b6..4eeb3c7aa6c20e63e3ff5452f746b97f94b73082 100644 (file)
@@ -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 (file)
index 0000000..115108e
--- /dev/null
@@ -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 <exceptions/exceptions.h>
+
+#include <iostream>
+#include <string>
+#include <stdlib.h>
+#include <stdio.h>
+#include <ftw.h>
+
+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
index 2c81e29de6a97d4f1b79a17927eb671223ed0bf9..4df14ee6aeab6be7364de8ce84ce955bb75d275e 100644 (file)
@@ -28,6 +28,7 @@
 #include <boost/scoped_ptr.hpp>
 
 #include <gtest/gtest.h>
+#include <testutils/sandbox.h>
 
 #include <exceptions/exceptions.h>
 
@@ -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);
     }