From 1e4a81930fafbc710eb9f5cc2e70b5fbabd4c83d Mon Sep 17 00:00:00 2001 From: Michael Carroll Date: Thu, 2 Feb 2023 16:31:40 -0600 Subject: [PATCH 1/5] Support for bazel on garden Signed-off-by: Michael Carroll --- BUILD.bazel | 131 +++++++++++++++++++++ log/BUILD.bazel | 90 +++++++++++++++ log/src/Log_TEST.cc | 7 +- parameters/BUILD.bazel | 72 ++++++++++++ src/CIface_TEST.cc | 3 +- src/Clock_TEST.cc | 1 - src/Discovery_TEST.cc | 14 +-- src/Helpers_TEST.cc | 2 +- src/NodeOptions_TEST.cc | 1 - src/Node_TEST.cc | 3 +- src/SubscribeOptions_TEST.cc | 1 - src/cmd/gz_TEST.cc | 1 - src/cmd/gz_src_TEST.cc | 1 - test/BUILD.bazel | 15 +++ test/test_config.hh.in | 214 ----------------------------------- test/test_utils.hh | 187 ++++++++++++++++++++++++++++++ 16 files changed, 506 insertions(+), 237 deletions(-) create mode 100644 BUILD.bazel create mode 100644 log/BUILD.bazel create mode 100644 parameters/BUILD.bazel create mode 100644 test/BUILD.bazel create mode 100644 test/test_utils.hh diff --git a/BUILD.bazel b/BUILD.bazel new file mode 100644 index 000000000..aa3447b4d --- /dev/null +++ b/BUILD.bazel @@ -0,0 +1,131 @@ +load( + "@gz//bazel/skylark:build_defs.bzl", + "GZ_FEATURES", + "GZ_ROOT", + "GZ_VISIBILITY", + "gz_configure_header", + "gz_export_header", + "gz_include_header", +) +load( + "@gz//bazel/lint:lint.bzl", + "add_lint_tests", +) + +package( + default_visibility = GZ_VISIBILITY, + features = GZ_FEATURES, +) + +licenses(["notice"]) # Apache-2.0 + +exports_files(["LICENSE"]) + +gz_configure_header( + name = "transport_config_hh", + src = "include/gz/transport/config.hh.in", + cmakelists = ["CMakeLists.txt"], + package = "transport", +) + +gz_export_header( + name = "include/gz/transport/Export.hh", + export_base = "GZ_TRANSPORT", + lib_name = "gz-transport", + visibility = ["//visibility:private"], +) + +public_headers_no_gen = glob([ + "include/gz/transport/*.h", + "include/gz/transport/*.hh", + "include/gz/transport/detail/*.hh", +]) + +private_headers = glob(["src/*.hh"]) + +sources = glob( + ["src/*.cc"], + exclude = [ + "src/*_TEST.cc", + ], +) + +gz_include_header( + name = "transport_hh_genrule", + out = "include/gz/transport.hh", + hdrs = public_headers_no_gen + [ + "include/gz/transport/config.hh", + "include/gz/transport/Export.hh", + ], +) + +public_headers = public_headers_no_gen + [ + "include/gz/transport/config.hh", + "include/gz/transport/Export.hh", + "include/gz/transport.hh", +] + +cc_library( + name = "transport", + srcs = sources + private_headers, + hdrs = public_headers, + copts = [ + "-Wno-deprecated-declarations", + ], + includes = ["include"], + deps = [ + GZ_ROOT + "msgs", + "@uuid", + "@zmq", + ], +) + +cc_binary( + name = "topic", + srcs = [ + "src/cmd/gz.cc", + "src/cmd/gz.hh", + "src/cmd/topic_main.cc", + ], + includes = ["src/cmd"], + deps = [ + ":transport", + GZ_ROOT + "utils/cli", + ], +) + +cc_binary( + name = "service", + srcs = [ + "src/cmd/gz.cc", + "src/cmd/gz.hh", + "src/cmd/service_main.cc", + ], + includes = ["src/cmd"], + deps = [ + ":transport", + GZ_ROOT + "utils/cli", + ], +) + +test_sources = glob( + include = ["src/*_TEST.cc"], +) + +[cc_test( + name = src.replace("/", "_").replace(".cc", "").replace("src_", ""), + srcs = [src], + env = { + "GZ_BAZEL": "1", + "GZ_BAZEL_PATH": "transport", + }, + deps = [ + ":transport", + GZ_ROOT + "common/testing", + GZ_ROOT + "transport/test:utils", + "@gtest", + "@gtest//:gtest_main", + ], +) for src in test_sources] + +add_lint_tests() diff --git a/log/BUILD.bazel b/log/BUILD.bazel new file mode 100644 index 000000000..b0760e903 --- /dev/null +++ b/log/BUILD.bazel @@ -0,0 +1,90 @@ +load( + "@gz//bazel/skylark:build_defs.bzl", + "GZ_FEATURES", + "GZ_ROOT", + "GZ_VISIBILITY", + "cmake_configure_file", + "gz_configure_header", + "gz_export_header", + "gz_include_header", +) +load( + "@gz//bazel/lint:lint.bzl", + "add_lint_tests", +) + +cmake_configure_file( + name = "build_config", + src = "src/build_config.hh.in", + out = "include/build_config.hh", + cmakelists = ["src/CMakeLists.txt"], + defines = [ + "SCHEMA_INSTALL_PATH=transport/log/sql", + ], +) + +gz_export_header( + name = "include/gz/transport/log/Export.hh", + export_base = "GZ_TRANSPORT_LOG", + lib_name = "gz-transport-log", + visibility = ["//visibility:private"], +) + +public_headers_no_gen = glob([ + "include/gz/transport/log/*.hh", + "include/gz/transport/log/detail/*.hh", +]) + +private_headers = glob(["src/*.hh"]) + +sources = glob( + ["src/*.cc"], + exclude = [ + "src/*_TEST.cc", + ], +) + +public_headers = public_headers_no_gen + [ + "include/gz/transport/log/Export.hh", +] + +cc_library( + name = "log", + srcs = sources + private_headers + ["include/build_config.hh"], + hdrs = public_headers, + data = ["sql/0.1.0.sql"], + includes = ["include"], + deps = [ + GZ_ROOT + "transport", + "@sqlite3", + ], +) + +test_sources = glob( + include = ["src/*_TEST.cc"], + exclude = ["src/LogCommandAPI_TEST.cc"], +) + +[cc_test( + name = src.replace("/", "_").replace(".cc", "").replace("src_", ""), + srcs = [src], + data = [ + "test/data/state.tlog", + ], + defines = [ + 'GZ_TRANSPORT_LOG_TEST_PATH=\\"transport/log/test\\"', + ], + env = { + "GZ_BAZEL": "1", + "GZ_BAZEL_PATH": "transport", + }, + deps = [ + ":log", + GZ_ROOT + "common/testing", + GZ_ROOT + "transport/test:utils", + "@gtest", + "@gtest//:gtest_main", + ], +) for src in test_sources] + +add_lint_tests() diff --git a/log/src/Log_TEST.cc b/log/src/Log_TEST.cc index f1d683cf8..9500d675f 100644 --- a/log/src/Log_TEST.cc +++ b/log/src/Log_TEST.cc @@ -21,10 +21,10 @@ #include #include "gz/transport/log/Log.hh" -#include "test_config.hh" -#include "log/test_config.hh" #include "gtest/gtest.h" +#include "test_utils.hh" + using namespace gz; using namespace gz::transport; using namespace std::chrono_literals; @@ -254,6 +254,9 @@ TEST(Log, OpenCorruptDatabase) std::string path = testing::portablePathUnion(GZ_TRANSPORT_LOG_TEST_PATH, "data"); path = testing::portablePathUnion(path, "state.tlog"); + + std::cout << path << std::endl; + logFile.Open(path); EXPECT_GT(logFile.EndTime(), 0ns) << "logFile.EndTime() == " << logFile.EndTime().count() << "ns";; diff --git a/parameters/BUILD.bazel b/parameters/BUILD.bazel new file mode 100644 index 000000000..ff305f78e --- /dev/null +++ b/parameters/BUILD.bazel @@ -0,0 +1,72 @@ +load( + "@gz//bazel/skylark:build_defs.bzl", + "GZ_FEATURES", + "GZ_ROOT", + "GZ_VISIBILITY", + "cmake_configure_file", + "gz_configure_header", + "gz_export_header", + "gz_include_header", +) +load( + "@gz//bazel/lint:lint.bzl", + "add_lint_tests", +) + +gz_export_header( + name = "include/gz/transport/parameters/Export.hh", + export_base = "GZ_TRANSPORT_PARAMETERS", + lib_name = "gz-transport-parameters", + visibility = ["//visibility:private"], +) + +public_headers_no_gen = glob([ + "include/gz/transport/parameters/*.hh", + "include/gz/transport/parameters/detail/*.hh", +]) + +private_headers = glob(["src/*.hh"]) + +sources = glob( + ["src/*.cc"], + exclude = [ + "src/*_TEST.cc", + ], +) + +public_headers = public_headers_no_gen + [ + "include/gz/transport/parameters/Export.hh", +] + +cc_library( + name = "parameters", + srcs = sources + private_headers, + hdrs = public_headers, + includes = ["include"], + deps = [ + GZ_ROOT + "transport", + "@sqlite3", + ], + visibility = GZ_VISIBILITY, +) + +test_sources = glob( + include = ["src/*_TEST.cc"], +) + +[cc_test( + name = src.replace("/", "_").replace(".cc", "").replace("src_", ""), + srcs = [src], + env = { + "GZ_BAZEL": "1", + "GZ_BAZEL_PATH": "transport", + }, + deps = [ + ":parameters", + GZ_ROOT + "transport/test:utils", + "@gtest", + "@gtest//:gtest_main", + ], +) for src in test_sources] + +add_lint_tests() diff --git a/src/CIface_TEST.cc b/src/CIface_TEST.cc index a3dc0e765..3e99b43e4 100644 --- a/src/CIface_TEST.cc +++ b/src/CIface_TEST.cc @@ -18,7 +18,8 @@ #include "gtest/gtest.h" #include "gz/transport/CIface.h" -#include "test_config.hh" + +#include "test_utils.hh" static int count; diff --git a/src/Clock_TEST.cc b/src/Clock_TEST.cc index 0c80cc487..22547b2cd 100644 --- a/src/Clock_TEST.cc +++ b/src/Clock_TEST.cc @@ -24,7 +24,6 @@ #include "gz/transport/Clock.hh" #include "gz/transport/Node.hh" #include "gz/transport/TransportTypes.hh" -#include "test_config.hh" #include "gtest/gtest.h" using namespace gz; diff --git a/src/Discovery_TEST.cc b/src/Discovery_TEST.cc index b113ccbff..cc8cf95c5 100644 --- a/src/Discovery_TEST.cc +++ b/src/Discovery_TEST.cc @@ -27,20 +27,8 @@ #include "gz/transport/Publisher.hh" #include "gz/transport/TransportTypes.hh" #include "gz/transport/Uuid.hh" -#include "test_config.hh" -// Temporarily introduce a "DISABLED_ON_LINUX" macro. -// It currently does not exist upstream. -// This can be removed when it is in upstream gz-utils -// or the discovery WrongGzIp test passes on linux -#include -#if defined __linux__ - #define GZ_UTILS_TEST_DISABLED_ON_LINUX(TestName) \ - DETAIL_GZ_UTILS_ADD_DISABLED_PREFIX(TestName) -#else - #define GZ_UTILS_TEST_DISABLED_ON_LINUX(TestName) \ - TestName -#endif // defined __linux__ +#include "test_utils.hh" using namespace gz; using namespace transport; diff --git a/src/Helpers_TEST.cc b/src/Helpers_TEST.cc index c7f79c167..e96358d61 100644 --- a/src/Helpers_TEST.cc +++ b/src/Helpers_TEST.cc @@ -16,7 +16,7 @@ */ #include "gz/transport/Helpers.hh" -#include "test_config.hh" +#include "test_utils.hh" #include "gtest/gtest.h" using namespace gz; diff --git a/src/NodeOptions_TEST.cc b/src/NodeOptions_TEST.cc index b8a78c2fc..9ed82361f 100644 --- a/src/NodeOptions_TEST.cc +++ b/src/NodeOptions_TEST.cc @@ -19,7 +19,6 @@ #include "gz/transport/NetUtils.hh" #include "gz/transport/NodeOptions.hh" -#include "test_config.hh" #include "gtest/gtest.h" using namespace gz; diff --git a/src/Node_TEST.cc b/src/Node_TEST.cc index 6558f226f..befbc2439 100644 --- a/src/Node_TEST.cc +++ b/src/Node_TEST.cc @@ -34,7 +34,8 @@ #include "gz/transport/TopicStatistics.hh" #include "gz/transport/TopicUtils.hh" #include "gz/transport/TransportTypes.hh" -#include "test_config.hh" + +#include "test_utils.hh" using namespace gz; diff --git a/src/SubscribeOptions_TEST.cc b/src/SubscribeOptions_TEST.cc index 8e7ccb780..d6bb9666f 100644 --- a/src/SubscribeOptions_TEST.cc +++ b/src/SubscribeOptions_TEST.cc @@ -17,7 +17,6 @@ #include "gz/transport/Helpers.hh" #include "gz/transport/SubscribeOptions.hh" -#include "test_config.hh" #include "gtest/gtest.h" using namespace gz; diff --git a/src/cmd/gz_TEST.cc b/src/cmd/gz_TEST.cc index 4babd53e0..0a6087eee 100644 --- a/src/cmd/gz_TEST.cc +++ b/src/cmd/gz_TEST.cc @@ -26,7 +26,6 @@ #include "gtest/gtest.h" #include "gz/transport/Node.hh" -#include "test_config.hh" #ifdef _MSC_VER # define popen _popen diff --git a/src/cmd/gz_src_TEST.cc b/src/cmd/gz_src_TEST.cc index 296852e3d..2737fffaa 100644 --- a/src/cmd/gz_src_TEST.cc +++ b/src/cmd/gz_src_TEST.cc @@ -24,7 +24,6 @@ #include "gtest/gtest.h" #include "gz.hh" #include "gz/transport/Node.hh" -#include "test_config.hh" using namespace gz; diff --git a/test/BUILD.bazel b/test/BUILD.bazel new file mode 100644 index 000000000..8700063d5 --- /dev/null +++ b/test/BUILD.bazel @@ -0,0 +1,15 @@ +load( + "@gz//bazel/skylark:build_defs.bzl", + "GZ_ROOT", + "GZ_VISIBILITY", +) + +cc_library( + name = "utils", + hdrs = ["test_utils.hh"], + includes = ["."], + visibility = GZ_VISIBILITY, + deps = [ + GZ_ROOT + "utils", + ], +) diff --git a/test/test_config.hh.in b/test/test_config.hh.in index 4f6461068..059dfbf65 100644 --- a/test/test_config.hh.in +++ b/test/test_config.hh.in @@ -30,218 +30,4 @@ DETAIL_GZ_TRANSPORT_TEST_DIR #endif -#ifndef __APPLE__ - #if __unix__ && __GNUC__ < 8 - #include - #else - #include - #endif -#endif - -#include -#include -#include -#include - -#ifdef _WIN32 - #include -#else - #include - #include - #include - #include -#endif - -#include "gz/transport/Helpers.hh" - -#if (_MSC_VER >= 1400) // Visual Studio 2005 - #include - - /// \brief setenv/unstenv are not present in Windows. Define them to make - /// the code portable. - /// \param[in] _name Variable name. - /// \param[in] _value Value. - /// \param[in] _rewrite If 'name' does exist in the environment, then its - /// value is changed to 'value' if 'rewrite' is nonzero. If overwrite is - /// zero, then the value of 'name' is not changed. - /// /return 0 on success or -1 on error. - int setenv(const char *_name, const char *_value, int /*_rewrite*/) - { - std::stringstream sstr; - std::string name = _name; - std::string value = _value; - sstr << name << '=' << value; - return _putenv(sstr.str().c_str()); - } - - /// \brief Deletes an environment variable. - /// \param[in] _name Variable name. - void unsetenv(const char *_name) - { - std::stringstream sstr; - std::string name = _name; - sstr << name << '='; - _putenv(sstr.str().c_str()); - } -#endif - -namespace testing -{ - /// \brief Join _str1 and _str2 considering both as storing system paths. - /// \param[in] _str1 string containing a path. - /// \param[in] _str2 string containing a path. - /// \return The string representation of the union of two paths. - std::string portablePathUnion(const std::string &_str1, - const std::string &_str2) - { -#ifdef __APPLE__ - // Ugly as hell but trying to avoid boost::filesystem - return _str1 + "/" + _str2; -#else - #if __unix__ && __GNUC__ < 8 - using namespace std::experimental::filesystem; - #else - using namespace std::filesystem; - #endif - return (path(_str1) / path(_str2)).string(); -#endif - } - -#ifdef _WIN32 - using forkHandlerType = PROCESS_INFORMATION; -#else - using forkHandlerType = pid_t; -#endif - - /// \brief create a new process and run command on it. This function is - /// implementing the creation of a new process on both Linux (fork) and - /// Windows (CreateProcess) and the execution of the command provided. - /// \param[in] _command The full system path to the binary to run into the - /// new process. - /// \param[in] _partition Name of the Gazebo partition (GZ_PARTITION) - /// \param[in] _username Username for authentication - /// (GZ_TRANSPORT_USERNAME) - /// \param[in] _password Password for authentication - /// (GZ_TRANSPORT_PASSWORD) - /// \return On success, the PID of the child process is returned in the - /// parent, an 0 is returned in the child. On failure, -1 is returned in the - /// parent and no child process is created. - forkHandlerType forkAndRun(const char *_command, const char *_partition, - const char *_username = nullptr, const char *_password = nullptr) - { -#ifdef _WIN32 - STARTUPINFO info= {sizeof(info)}; - PROCESS_INFORMATION processInfo; - - char cmd[500]; - // We should put quotes around the _command string to make sure we are - // robust to file paths that contain spaces. - gz_strcpy(cmd, "\""); - gz_strcat(cmd, _command); - gz_strcat(cmd, "\""); - gz_strcat(cmd, " "); - gz_strcat(cmd, _partition); - - if (_username && _password) - { - gz_strcat(cmd, " "); - gz_strcat(cmd, _username); - gz_strcat(cmd, " "); - gz_strcat(cmd, _password); - } - - // We set the first argument to NULL, because we want the behavior that - // CreateProcess exhibits when the first argument is NULL: i.e. Windows will - // automatically add the .exe extension onto the filename. When the first - // argument is non-NULL, it will not automatically add the extension, which - // makes more work for us. - // - // It should also be noted that the lookup behavior for the application is - // different when the first argument is non-NULL, so we should take that - // into consideration when determining what to put into the first and second - // arguments of CreateProcess. - if (!CreateProcess(NULL, const_cast(cmd), NULL, NULL, - TRUE, 0, NULL, NULL, &info, &processInfo)) - { - std::cerr << "CreateProcess call failed: " << cmd << std::endl; - } - - return processInfo; -#else - pid_t pid = fork(); - - if (pid == 0) - { - if (_username && _password) - { - if (execl(_command, _command, _partition, _username, _password, - reinterpret_cast(0)) == -1) - { - std::cerr << "Error running execl call: " << _command << std::endl; - } - } - else - { - if (execl(_command, _command, _partition, - reinterpret_cast(0)) == -1) - { - std::cerr << "Error running execl call: " << _command << std::endl; - } - } - } - - return pid; -#endif - } - - /// \brief Wait for the end of a process and handle the termination - /// \param[in] pi Process handler of the process to wait for - /// (PROCESS_INFORMATION in windows or forkHandlerType in UNIX). - void waitAndCleanupFork(const forkHandlerType pi) - { -#ifdef _WIN32 - // Wait until child process exits. - WaitForSingleObject(pi.hProcess, INFINITE); - - // Close process and thread handler. - CloseHandle(pi.hProcess); - CloseHandle(pi.hThread); -#else - // Wait for the child process to return. - int status; - waitpid(pi, &status, 0); - if (status == -1) - std::cerr << "Error while running waitpid" << std::endl; -#endif - } - - /// \brief Send a termination signal to the process handled by pi. - /// \param[in] pi Process handler of the process to stop - /// (PROCESS_INFORMATION in windows or forkHandlerType in UNIX). - void killFork(const forkHandlerType pi) - { -#ifdef _WIN32 - // TerminateProcess return 0 on error - if (TerminateProcess(pi.hProcess, 0) == 0) - std::cerr << "Error running TerminateProcess: " << GetLastError(); -#else - kill(pi, SIGTERM); -#endif - } - - /// \brief Get a random number based on an integer converted to string. - /// \return A random integer converted to string. - std::string getRandomNumber() - { - // Initialize random number generator. - uint32_t seed = std::random_device {}(); - std::mt19937 randGenerator(seed); - - // Create a random number based on an integer converted to string. - std::uniform_int_distribution d(0, INT_MAX); - - return std::to_string(d(randGenerator)); - } -} - #endif // header guard diff --git a/test/test_utils.hh b/test/test_utils.hh new file mode 100644 index 000000000..85e678903 --- /dev/null +++ b/test/test_utils.hh @@ -0,0 +1,187 @@ +/* + * Copyright (C) 2014 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * +*/ + +#ifndef GZ_TRANSPORT_TEST_UTILS_HH_ +#define GZ_TRANSPORT_TEST_UTILS_HH_ + +#include +#include +#include +#include +#include + +#ifdef _WIN32 + #include +#else + #include + #include + #include + #include +#endif + +#include "gz/transport/Helpers.hh" + +namespace testing +{ + /// \brief Join _str1 and _str2 considering both as storing system paths. + /// \param[in] _str1 string containing a path. + /// \param[in] _str2 string containing a path. + /// \return The string representation of the union of two paths. + std::string portablePathUnion(const std::string &_str1, + const std::string &_str2) + { + return (std::filesystem::path(_str1) / std::filesystem::path(_str2)).string(); + } + +#ifdef _WIN32 + using forkHandlerType = PROCESS_INFORMATION; +#else + using forkHandlerType = pid_t; +#endif + + /// \brief create a new process and run command on it. This function is + /// implementing the creation of a new process on both Linux (fork) and + /// Windows (CreateProcess) and the execution of the command provided. + /// \param[in] _command The full system path to the binary to run into the + /// new process. + /// \param[in] _partition Name of the Gazebo partition (GZ_PARTITION) + /// \param[in] _username Username for authentication + /// (GZ_TRANSPORT_USERNAME) + /// \param[in] _password Password for authentication + /// (GZ_TRANSPORT_PASSWORD) + /// \return On success, the PID of the child process is returned in the + /// parent, an 0 is returned in the child. On failure, -1 is returned in the + /// parent and no child process is created. + forkHandlerType forkAndRun(const char *_command, const char *_partition, + const char *_username = nullptr, const char *_password = nullptr) + { +#ifdef _WIN32 + STARTUPINFO info= {sizeof(info)}; + PROCESS_INFORMATION processInfo; + + char cmd[500]; + // We should put quotes around the _command string to make sure we are + // robust to file paths that contain spaces. + gz_strcpy(cmd, "\""); + gz_strcat(cmd, _command); + gz_strcat(cmd, "\""); + gz_strcat(cmd, " "); + gz_strcat(cmd, _partition); + + if (_username && _password) + { + gz_strcat(cmd, " "); + gz_strcat(cmd, _username); + gz_strcat(cmd, " "); + gz_strcat(cmd, _password); + } + + // We set the first argument to NULL, because we want the behavior that + // CreateProcess exhibits when the first argument is NULL: i.e. Windows will + // automatically add the .exe extension onto the filename. When the first + // argument is non-NULL, it will not automatically add the extension, which + // makes more work for us. + // + // It should also be noted that the lookup behavior for the application is + // different when the first argument is non-NULL, so we should take that + // into consideration when determining what to put into the first and second + // arguments of CreateProcess. + if (!CreateProcess(NULL, const_cast(cmd), NULL, NULL, + TRUE, 0, NULL, NULL, &info, &processInfo)) + { + std::cerr << "CreateProcess call failed: " << cmd << std::endl; + } + + return processInfo; +#else + pid_t pid = fork(); + + if (pid == 0) + { + if (_username && _password) + { + if (execl(_command, _command, _partition, _username, _password, + reinterpret_cast(0)) == -1) + { + std::cerr << "Error running execl call: " << _command << std::endl; + } + } + else + { + if (execl(_command, _command, _partition, + reinterpret_cast(0)) == -1) + { + std::cerr << "Error running execl call: " << _command << std::endl; + } + } + } + + return pid; +#endif + } + + /// \brief Wait for the end of a process and handle the termination + /// \param[in] pi Process handler of the process to wait for + /// (PROCESS_INFORMATION in windows or forkHandlerType in UNIX). + void waitAndCleanupFork(const forkHandlerType pi) + { +#ifdef _WIN32 + // Wait until child process exits. + WaitForSingleObject(pi.hProcess, INFINITE); + + // Close process and thread handler. + CloseHandle(pi.hProcess); + CloseHandle(pi.hThread); +#else + // Wait for the child process to return. + int status; + waitpid(pi, &status, 0); + if (status == -1) + std::cerr << "Error while running waitpid" << std::endl; +#endif + } + + /// \brief Send a termination signal to the process handled by pi. + /// \param[in] pi Process handler of the process to stop + /// (PROCESS_INFORMATION in windows or forkHandlerType in UNIX). + void killFork(const forkHandlerType pi) + { +#ifdef _WIN32 + // TerminateProcess return 0 on error + if (TerminateProcess(pi.hProcess, 0) == 0) + std::cerr << "Error running TerminateProcess: " << GetLastError(); +#else + kill(pi, SIGTERM); +#endif + } + + /// \brief Get a random number based on an integer converted to string. + /// \return A random integer converted to string. + std::string getRandomNumber() + { + // Initialize random number generator. + uint32_t seed = std::random_device {}(); + std::mt19937 randGenerator(seed); + + // Create a random number based on an integer converted to string. + std::uniform_int_distribution d(0, INT_MAX); + + return std::to_string(d(randGenerator)); + } +} + +#endif // header guard From fd319f90f9f52ab3a9637d759a342816f3c734cb Mon Sep 17 00:00:00 2001 From: Michael Carroll Date: Wed, 1 Nov 2023 12:11:13 -0500 Subject: [PATCH 2/5] Lint Signed-off-by: Michael Carroll --- parameters/BUILD.bazel | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parameters/BUILD.bazel b/parameters/BUILD.bazel index ff305f78e..8a1842d0c 100644 --- a/parameters/BUILD.bazel +++ b/parameters/BUILD.bazel @@ -43,11 +43,11 @@ cc_library( srcs = sources + private_headers, hdrs = public_headers, includes = ["include"], + visibility = GZ_VISIBILITY, deps = [ GZ_ROOT + "transport", "@sqlite3", ], - visibility = GZ_VISIBILITY, ) test_sources = glob( From b55fc395746995ef13d58c3a9d932c6fee73feea Mon Sep 17 00:00:00 2001 From: Michael Carroll Date: Thu, 7 Dec 2023 15:49:11 +0000 Subject: [PATCH 3/5] Remove conflict marker Signed-off-by: Michael Carroll --- log/src/Log_TEST.cc | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/log/src/Log_TEST.cc b/log/src/Log_TEST.cc index c33d9255f..1faadb310 100644 --- a/log/src/Log_TEST.cc +++ b/log/src/Log_TEST.cc @@ -255,17 +255,7 @@ TEST(Log, NullDescriptorUnopenedLog) TEST(Log, OpenCorruptDatabase) { log::Log logFile; -<<<<<<< HEAD - std::string path = - testing::portablePathUnion(GZ_TRANSPORT_LOG_TEST_PATH, "data"); - path = testing::portablePathUnion(path, "state.tlog"); - - std::cout << path << std::endl; - - logFile.Open(path); -======= logFile.Open(kCorruptDbTestPath); ->>>>>>> gz-transport12 EXPECT_GT(logFile.EndTime(), 0ns) << "logFile.EndTime() == " << logFile.EndTime().count() << "ns";; } From b1b36d002ea37e92e8bebc8ea67c309e8061be50 Mon Sep 17 00:00:00 2001 From: Michael Carroll Date: Thu, 7 Dec 2023 15:54:00 +0000 Subject: [PATCH 4/5] Fix missing define and header Signed-off-by: Michael Carroll --- log/BUILD.bazel | 1 + src/Node_TEST.cc | 1 + 2 files changed, 2 insertions(+) diff --git a/log/BUILD.bazel b/log/BUILD.bazel index b0760e903..2dd8b6109 100644 --- a/log/BUILD.bazel +++ b/log/BUILD.bazel @@ -73,6 +73,7 @@ test_sources = glob( ], defines = [ 'GZ_TRANSPORT_LOG_TEST_PATH=\\"transport/log/test\\"', + 'CORRUPT_DB_TEST_PATH=\\"transport/log/test/data/state.tlog\\"', ], env = { "GZ_BAZEL": "1", diff --git a/src/Node_TEST.cc b/src/Node_TEST.cc index f845fbbcc..cb0d31772 100644 --- a/src/Node_TEST.cc +++ b/src/Node_TEST.cc @@ -29,6 +29,7 @@ #include #include "gz/transport/MessageInfo.hh" +#include "gz/transport/Node.hh" #include "gz/transport/TransportTypes.hh" #include From 91707364903936c85de2dbfab5be027dc55da654 Mon Sep 17 00:00:00 2001 From: Michael Carroll Date: Thu, 7 Dec 2023 16:45:10 +0000 Subject: [PATCH 5/5] Fix test config generation Signed-off-by: Michael Carroll --- log/src/CMakeLists.txt | 2 +- log/src/Log_TEST.cc | 10 ---------- log/test/integration/CMakeLists.txt | 1 + src/CMakeLists.txt | 2 +- src/Node_TEST.cc | 1 + src/cmd/CMakeLists.txt | 7 ++++--- src/cmd/gz_TEST.cc | 2 ++ src/cmd/gz_src_TEST.cc | 2 ++ test/CMakeLists.txt | 12 +++++++----- test/integration/CMakeLists.txt | 3 ++- test/test_config.hh.in | 2 ++ 11 files changed, 23 insertions(+), 21 deletions(-) diff --git a/log/src/CMakeLists.txt b/log/src/CMakeLists.txt index c285c0efb..aab305fbd 100644 --- a/log/src/CMakeLists.txt +++ b/log/src/CMakeLists.txt @@ -19,7 +19,7 @@ endif() gz_build_tests( TYPE "UNIT" SOURCES ${gtest_sources} - LIB_DEPS ${log_lib_target} ${EXTRA_TEST_LIB_DEPS} + LIB_DEPS ${log_lib_target} ${EXTRA_TEST_LIB_DEPS} test_config TEST_LIST logging_tests ) diff --git a/log/src/Log_TEST.cc b/log/src/Log_TEST.cc index c33d9255f..1faadb310 100644 --- a/log/src/Log_TEST.cc +++ b/log/src/Log_TEST.cc @@ -255,17 +255,7 @@ TEST(Log, NullDescriptorUnopenedLog) TEST(Log, OpenCorruptDatabase) { log::Log logFile; -<<<<<<< HEAD - std::string path = - testing::portablePathUnion(GZ_TRANSPORT_LOG_TEST_PATH, "data"); - path = testing::portablePathUnion(path, "state.tlog"); - - std::cout << path << std::endl; - - logFile.Open(path); -======= logFile.Open(kCorruptDbTestPath); ->>>>>>> gz-transport12 EXPECT_GT(logFile.EndTime(), 0ns) << "logFile.EndTime() == " << logFile.EndTime().count() << "ns";; } diff --git a/log/test/integration/CMakeLists.txt b/log/test/integration/CMakeLists.txt index 5537f4511..900575125 100644 --- a/log/test/integration/CMakeLists.txt +++ b/log/test/integration/CMakeLists.txt @@ -20,6 +20,7 @@ gz_build_tests( ChirpParams ${PROJECT_LIBRARY_TARGET_NAME}-log ${EXTRA_TEST_LIB_DEPS} + test_config INCLUDE_DIRS ${CMAKE_BINARY_DIR}/test/ ) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bed76c64d..863e676d0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -31,7 +31,7 @@ endif() # Build the unit tests. gz_build_tests(TYPE UNIT SOURCES ${gtest_sources} TEST_LIST test_list - LIB_DEPS ${EXTRA_TEST_LIB_DEPS}) + LIB_DEPS ${EXTRA_TEST_LIB_DEPS} test_config) foreach(test ${test_list}) diff --git a/src/Node_TEST.cc b/src/Node_TEST.cc index f845fbbcc..cb0d31772 100644 --- a/src/Node_TEST.cc +++ b/src/Node_TEST.cc @@ -29,6 +29,7 @@ #include #include "gz/transport/MessageInfo.hh" +#include "gz/transport/Node.hh" #include "gz/transport/TransportTypes.hh" #include diff --git a/src/cmd/CMakeLists.txt b/src/cmd/CMakeLists.txt index 9a559f1d7..ed165dbd8 100644 --- a/src/cmd/CMakeLists.txt +++ b/src/cmd/CMakeLists.txt @@ -41,11 +41,12 @@ install(TARGETS ${service_executable} DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/gz # Build the unit tests. gz_build_tests(TYPE UNIT SOURCES ${gtest_sources} TEST_LIST test_list - LIB_DEPS ${EXTRA_TEST_LIB_DEPS}) + LIB_DEPS + gz + gz-utils${GZ_UTILS_VER}::gz-utils${GZ_UTILS_VER} + ${EXTRA_TEST_LIB_DEPS} test_config) foreach(test ${test_list}) - target_link_libraries(${test} gz gz-utils${GZ_UTILS_VER}::gz-utils${GZ_UTILS_VER}) - # Inform each test of its output directory so it knows where to call the # auxiliary files from. Using a generator expression here is useful for # multi-configuration generators, like Visual Studio. diff --git a/src/cmd/gz_TEST.cc b/src/cmd/gz_TEST.cc index b3436c29b..5dd861177 100644 --- a/src/cmd/gz_TEST.cc +++ b/src/cmd/gz_TEST.cc @@ -30,6 +30,8 @@ #include "gtest/gtest.h" #include "gz/transport/Node.hh" +#include "test_config.hh" + using namespace gz; static std::string g_partition; // NOLINT(*) diff --git a/src/cmd/gz_src_TEST.cc b/src/cmd/gz_src_TEST.cc index 1973f9f3d..04be10d0d 100644 --- a/src/cmd/gz_src_TEST.cc +++ b/src/cmd/gz_src_TEST.cc @@ -28,6 +28,8 @@ #include +#include "test_utils.hh" + using namespace gz; // Global constants. diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 4166255fa..a260fe231 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,9 +1,11 @@ -configure_file (test_config.hh.in ${PROJECT_BINARY_DIR}/include/test_config.hh) -include_directories ( - ${PROJECT_BINARY_DIR}/include -) - add_subdirectory(gtest_vendor) add_subdirectory(integration) add_subdirectory(performance) add_subdirectory(regression) + +configure_file (test_config.hh.in ${PROJECT_BINARY_DIR}/include/test_config.hh) +add_library(test_config INTERFACE) +target_include_directories(test_config INTERFACE + ${PROJECT_BINARY_DIR}/include + ${PROJECT_SOURCE_DIR}/test +) diff --git a/test/integration/CMakeLists.txt b/test/integration/CMakeLists.txt index a60793e91..3fcbf15ce 100644 --- a/test/integration/CMakeLists.txt +++ b/test/integration/CMakeLists.txt @@ -26,7 +26,7 @@ endif() gz_build_tests(TYPE INTEGRATION SOURCES ${tests} TEST_LIST test_list - LIB_DEPS ${EXTRA_TEST_LIB_DEPS}) + LIB_DEPS ${EXTRA_TEST_LIB_DEPS} test_config) set(auxiliary_files authPubSubSubscriberInvalid_aux @@ -54,6 +54,7 @@ foreach(AUX_EXECUTABLE ${auxiliary_files}) ${PROJECT_LIBRARY_TARGET_NAME} gtest ${EXTRA_TEST_LIB_DEPS} + test_config ) endforeach(AUX_EXECUTABLE) diff --git a/test/test_config.hh.in b/test/test_config.hh.in index dee114b2c..48e0dab43 100644 --- a/test/test_config.hh.in +++ b/test/test_config.hh.in @@ -22,4 +22,6 @@ #define GZ_CONFIG_PATH "@CMAKE_BINARY_DIR@/test/conf" #define GZ_VERSION_FULL "@PROJECT_VERSION_FULL@" +#include "test_utils.hh" + #endif // GZ_TRANSPORT_TEST_CONFIG_HH_