Commit a45e2f68 authored by Michal Nowikowski's avatar Michal Nowikowski

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.
parent 41f40b7d
......@@ -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.
......
......@@ -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());
}
......
......@@ -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();
}
......
......@@ -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();
}
......
......@@ -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.
......
......@@ -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.
......
......@@ -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.
......
......@@ -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.
......
......@@ -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);
}
......
......@@ -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
......
// 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
......@@ -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);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment