Commit e67d6501 authored by rem31's avatar rem31 Committed by Gres Remi
Browse files

enh(test): Migrate centreon-clib tests to google-test (#51)

enh(test): Migrate centreon-clib tests to google-test
REFS : MON-10725
parent bdc1575a
build/*
inc/com/centreon/clib/version.hh
script/centreon-clib.pc
......@@ -38,7 +38,7 @@ set(CLIB_VERSION "${CLIB_MAJOR}.${CLIB_MINOR}.${CLIB_PATCH}")
# Include module to check existing libraries.
include(CheckLibraryExists)
include(${CMAKE_BINARY_DIR}/conan_paths.cmake)
# Include module CTest if necessary.
if (WITH_TESTING)
include(CTest)
......
#!/bin/bash
# Usage info
show_help() {
cat << EOF
Usage: ${0##*/} -n=[yes|no] -v
......@@ -8,11 +8,22 @@ This program build Centreon-clib
-f|--force : force rebuild
-r|--release : Build on release mode
-fcr|--force-conan-rebuild : rebuild conan data
-h|--help : help
EOF
}
force=0
BUILD_TYPE="Debug"
CONAN_REBUILD="0"
for i in $(cat conanfile.txt) ; do
if [[ $i =~ / ]] ; then
if [ ! -d ~/.conan/data/$i ] ; then
echo "The package '$i' is missing"
CONAN_REBUILD="1"
break
fi
fi
done
for i in "$@"
do
case $i in
......@@ -22,6 +33,10 @@ do
;;
-r|--release)
BUILD_TYPE="Release"
shift
;;
-fcr|--force-conan-rebuild)
CONAN_REBUILD="1"
;;
-h|--help)
show_help
......@@ -43,19 +58,38 @@ if [ -r /etc/centos-release ] ; then
cmake='cmake'
else
if rpm -q cmake3 ; then
cmake='cmake3'
rm -f /usr/bin/cmake
ln -s /usr/bin/cmake3 /usr/bin/cmake
cmake='cmake'
elif [ $maj = "centos7" ] ; then
yum -y install epel-release
yum -y install cmake3
cmake='cmake3'
yum -y install epel-release cmake3
mv /usr/bin/cmake /usr/bin/cmake2
ln -s /usr/bin/cmake3 /usr/bin/cmake
cmake='cmake'
else
dnf -y install cmake
cmake='cmake'
fi
fi
if [[ ! -x /usr/bin/python3 ]] ; then
yum -y install python3
else
echo "python3 already installed"
fi
if ! rpm -q python3-pip ; then
yum -y install python3-pip
else
echo "pip3 already installed"
fi
good=$(gcc --version | awk '/gcc/ && ($3+0)>5.0{print 1}')
if ! rpm -q gcc-c++ ; then
yum -y install gcc-c++
if [ ! $good ] ; then
echo "Your compiler is too old. Trying to used devtoolset-9."
yum -y install centos-release-scl
yum-config-manager --enable rhel-server-rhscl-7-rpms
yum -y install devtoolset-9
source /opt/rh/devtoolset-9/enable
fi
pkgs=(
......@@ -73,16 +107,29 @@ if [ -r /etc/centos-release ] ; then
elif [ -r /etc/issue ] ; then
maj=$(cat /etc/issue | awk '{print $1}')
version=$(cat /etc/issue | awk '{print $3}')
if [ $version = "9" ] ; then
dpkg='dpkg'
else
dpkg='dpkg --no-pager'
fi
v=$(cmake --version)
if [[ $v =~ "version 3" ]] ; then
cmake='cmake'
elif [ $maj = "Debian" ] ; then
if [ $version = "9" ] ; then
dpkg="dpkg"
if $dpkg -l cmake ; then
echo "Bad version of cmake..."
exit 1
else
dpkg="dpkg --no-pager"
if [ $my_id -eq 0 ] ; then
apt install -y cmake
cmake='cmake'
else
echo -e "cmake is not installed, you could enter, as root:\n\tapt install -y cmake\n\n"
exit 1
fi
fi
if $dpkg -l --no-pager cmake ; then
elif [ $maj = "Raspbian" ] ; then
if $dpkg -l cmake ; then
echo "Bad version of cmake..."
exit 1
else
......@@ -94,14 +141,21 @@ elif [ -r /etc/issue ] ; then
exit 1
fi
fi
else
echo "Bad version of cmake..."
exit 1
fi
if [ $maj = "Debian" ] ; then
pkgs=(
gcc
g++
ninja-build
pkg-config
ninja-build
python3
python3-pip
)
for i in "${pkgs[@]}"; do
if ! $dpkg -l --no-pager $i | grep "^ii" ; then
if ! $dpkg -l $i | grep "^ii" ; then
if [ $my_id -eq 0 ] ; then
apt install -y $i
else
......@@ -110,32 +164,18 @@ elif [ -r /etc/issue ] ; then
fi
fi
done
elif [ $maj = "Raspbian" ] ; then
if [ $version = "9" ] ; then
dpkg="dpkg"
else
dpkg="dpkg --no-pager"
fi
if $dpkg -l --no-pager cmake ; then
echo "Bad version of cmake..."
exit 1
else
if [ $my_id -eq 0 ] ; then
apt install -y cmake
cmake='cmake'
else
echo -e "cmake is not installed, you could enter, as root:\n\tapt install -y cmake\n\n"
exit 1
fi
fi
fi
if [ $maj = "Raspbian" ] ; then
pkgs=(
gcc
g++
ninja-build
pkg-config
ninja-build
python3
python3-pip
)
for i in "${pkgs[@]}"; do
if ! $dpkg -l --no-pager $i | grep "^ii" ; then
if ! $dpkg -l $i | grep "^ii" ; then
if [ $my_id -eq 0 ] ; then
apt install -y $i
else
......@@ -144,14 +184,23 @@ elif [ -r /etc/issue ] ; then
fi
fi
done
else
echo "Bad version of cmake..."
exit 1
fi
fi
pip3 install conan --upgrade
if [ $my_id -eq 0 ] ; then
conan='/usr/local/bin/conan'
elif which conan ; then
conan=$(which conan)
else
conan="$HOME/.local/bin/conan"
fi
if [ ! -d build ] ; then
mkdir build
else
echo "'build' directory already there"
fi
if [ "$force" = "1" ] ; then
......@@ -159,12 +208,24 @@ if [ "$force" = "1" ] ; then
mkdir build
fi
cd build
if [ $maj = "centos7" ] ; then
rm -rf ~/.conan/profiles/default
if [ "$CONAN_REBUILD" = "1" ] ; then
$conan install .. -s compiler.libcxx=libstdc++11 --build="*"
else
$conan install .. -s compiler.libcxx=libstdc++11 --build=missing
fi
else
$conan install .. -s compiler.libcxx=libstdc++11 --build=missing
fi
if [ $maj = "Raspbian" ] ; then
CXXFLAGS="-Wall -Wextra" $cmake -DWITH_PREFIX=/usr -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DWITH_PREFIX_LIB=/usr/lib -DWITH_TESTING=On -DUSE_CXX11_ABI=1 $* ..
elif [ $maj = "Debian" ] ; then
CXXFLAGS="-Wall -Wextra" $cmake -DWITH_PREFIX=/usr -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DWITH_PREFIX_LIB=/usr/lib64 -DWITH_TESTING=On -DUSE_CXX11_ABI=1 $* ..
else
CXXFLAGS="-Wall -Wextra" $cmake -DWITH_PREFIX=/usr -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DWITH_PREFIX_LIB=/usr/lib64 -DWITH_TESTING=On -DUSE_CXX11_ABI=0 $* ..
CXXFLAGS="-Wall -Wextra" $cmake -DWITH_PREFIX=/usr -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DWITH_PREFIX_LIB=/usr/lib64 -DWITH_TESTING=On -DUSE_CXX11_ABI=1 $* ..
fi
#CXX=/usr/bin/clang++ CC=/usr/bin/clang CXXFLAGS="-Wall -Wextra" cmake -DWITH_PREFIX=/usr -DCMAKE_BUILD_TYPE=Debug -DWITH_PREFIX_LIB=/usr/lib64 -DWITH_TESTING=On $* ..
......
[requires]
gtest/cci.20210126
[generators]
cmake_paths
cmake_find_package
\ No newline at end of file
##
## Copyright 2011-2013 Centreon
##
## 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.
##
## For more information : contact@centreon.com
##
# Set directories.
set(TEST_DIR "${PROJECT_SOURCE_DIR}/test")
# Add subdirectories.
add_subdirectory("clib")
add_subdirectory("exceptions")
add_subdirectory("io")
add_subdirectory("logging")
add_subdirectory("misc")
# bin_test_process_output
set(TEST_NAME "bin_test_process_output")
add_executable("${TEST_NAME}" "${TEST_DIR}/${TEST_NAME}.cc")
# shared_testing_library
set(LIB_NAME "shared_testing_library")
add_library("${LIB_NAME}" SHARED "${TEST_DIR}/${LIB_NAME}.cc")
set(CLIB_TESTS
handle_manager_add
handle_manager_constructor
handle_manager_remove_by_handle
handle_manager_remove_by_handle_listener
handle_manager_multiplex
library_resolve
library_load
process_env
process_kill
process_return
process_terminate
process_timeout
process_wait_timeout
)
foreach (TEST_NAME ${CLIB_TESTS})
add_executable("${TEST_NAME}" "${TEST_DIR}/${TEST_NAME}.cc")
target_link_libraries("${TEST_NAME}" "${DEFAULT_LINK_NAME}")
add_test("${TEST_NAME}" "${TEST_NAME}")
endforeach ()
# process_output
set(TEST_NAME "process_output")
add_executable("${TEST_NAME}" "${TEST_DIR}/${TEST_NAME}.cc")
target_link_libraries("${TEST_NAME}" "${DEFAULT_LINK_NAME}")
add_test("${TEST_NAME}_stderr" "${TEST_NAME}" "err")
add_test("${TEST_NAME}_stdout" "${TEST_NAME}" "out")
# task_manager tests
set(TASK_MANAGER_LIST
task_manager_add
task_manager_add_recurring
task_manager_execute
task_manager_execute_recurring
task_manager_next_execution_time
task_manager_remove_by_id
task_manager_remove_by_task
task_manager_remove_self
)
foreach(name ${TASK_MANAGER_LIST})
add_executable("${name}" "${TEST_DIR}/${name}.cc")
target_link_libraries("${name}" "${DEFAULT_LINK_NAME}")
add_test("${name}" "${name}")
endforeach(name)
# task_manager tests
set(TIMESTAMP_LIST
timestamp_add
timestamp_add_mseconds
timestamp_add_seconds
timestamp_add_useconds
timestamp_clear
timestamp_constructor
timestamp_equal
timestamp_not_equal
timestamp_copy
timestamp_greater
timestamp_greater_or_equal
timestamp_less_or_equal
timestamp_less
timestamp_sub
timestamp_sub_mseconds
timestamp_sub_seconds
timestamp_sub_useconds
timestamp_to_mseconds
timestamp_to_seconds
timestamp_to_useconds
)
foreach(name ${TIMESTAMP_LIST})
add_executable("${name}" "${TEST_DIR}/${name}.cc")
target_link_libraries("${name}" "${DEFAULT_LINK_NAME}")
add_test("${name}" "${name}")
endforeach(name)
if (WITH_COVERAGE)
include("${PROJECT_SOURCE_DIR}/cmake/CodeCoverage.cmake")
APPEND_COVERAGE_COMPILER_FLAGS()
set(COVERAGE_EXCLUDES '${PROJECT_BINARY_DIR}/*' '${PROJECT_SOURCE_DIR}/test/*' '/usr/include/*')
SETUP_TARGET_FOR_COVERAGE(
NAME test-coverage
EXECUTABLE ctest # Executable in PROJECT_BINARY_DIR
DEPENDENCIES
${CLIB_TESTS}
${TASK_MANAGER_LIST}
${TIMESTAMP_LIST}
)
endif ()
find_package(GTest REQUIRED)
include_directories(${GTest_INCLUDE_DIRS})
link_directories(${GTest_LIB_DIRS})
add_executable(bin_test_process_output ${CMAKE_SOURCE_DIR}/test/bin_test_process_output.cc)
add_library(shared_testing_library
SHARED
${CMAKE_SOURCE_DIR}/test/shared_testing_library.cc
)
include_directories(${CMAKE_SOURCE_DIR}/test/)
add_executable(ut-clib
${CMAKE_SOURCE_DIR}/test/process.cc
${CMAKE_SOURCE_DIR}/test/exceptions.cc
${CMAKE_SOURCE_DIR}/test/handle_manager.cc
${CMAKE_SOURCE_DIR}/test/io.cc
${CMAKE_SOURCE_DIR}/test/library.cc
${CMAKE_SOURCE_DIR}/test/logging.cc
${CMAKE_SOURCE_DIR}/test/main.cc
${CMAKE_SOURCE_DIR}/test/misc.cc
${CMAKE_SOURCE_DIR}/test/task_manager.cc
${CMAKE_SOURCE_DIR}/test/timestamp.cc
${CMAKE_SOURCE_DIR}/test/version.cc)
if (WITH_COVERAGE)
set(COVERAGE_EXCLUDES '${PROJECT_BINARY_DIR}/*' '${PROJECT_SOURCE_DIR}/tests/*' '/usr/include/* ')
SETUP_TARGET_FOR_COVERAGE(
NAME test-coverage
EXECUTABLE ut
DEPENDENCIES ut
)
set(GCOV gcov)
endif ()
add_test(NAME tests COMMAND ut-clib)
target_link_libraries(ut-clib ${GTest_LIBS} pthread centreon_clib)
/*
** Copyright 2011-2013 Centreon
**
** 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.
**
** For more information : contact@centreon.com
*/
#ifndef CC_TEST_LOGGING_BACKEND_TEST_HH
#define CC_TEST_LOGGING_BACKEND_TEST_HH
#include "com/centreon/logging/backend.hh"
CC_BEGIN()
namespace logging {
/**
* @class backend_test
* @brief litle implementation of backend to test logging engine.
*/
class backend_test : public backend {
public:
backend_test(bool is_sync = false,
bool show_pid = false,
time_precision show_timestamp = none,
bool show_thread_id = false)
: backend(is_sync, show_pid, show_timestamp, show_thread_id),
_nb_call(0) {}
~backend_test() noexcept {}
void close() noexcept {}
std::string const& data() const noexcept { return _buffer; }
void log(uint64_t types,
uint32_t verbose,
char const* msg,
uint32_t size) noexcept {
std::lock_guard<std::mutex> lock(_lock);
(void)types;
(void)verbose;
misc::stringifier header;
_build_header(header);
_buffer.append(header.data(), header.size());
_buffer.append(msg, size);
++_nb_call;
}
uint32_t get_nb_call() const noexcept { return _nb_call; }
void open() {}
void reopen() {}
void reset() noexcept { _buffer.clear(); }
private:
std::string _buffer;
uint32_t _nb_call;
};
}
CC_END()
#endif // !CC_TEST_LOGGING_BACKEND_TEST_HH
/*
** Copyright 2011-2013 Centreon
**
** 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.
**
** For more information : contact@centreon.com
*/
#ifndef CC_TEST_LOGGING_BACKEND_TEST_HH
#define CC_TEST_LOGGING_BACKEND_TEST_HH
#include "com/centreon/logging/backend.hh"
#include "com/centreon/misc/stringifier.hh"
CC_BEGIN()
namespace logging {
/**
* @class backend_test
* @brief litle implementation of backend to test logging engine.
*/
class backend_test : public backend {
public:
backend_test(bool is_sync = false,
bool show_pid = false,
time_precision show_timestamp = none,
bool show_thread_id = false)
: backend(is_sync, show_pid, show_timestamp, show_thread_id),
_nb_call(0) {}
~backend_test() noexcept {}
void close() noexcept {}
std::string const& data() const noexcept { return _buffer; }
void log(uint64_t types,
uint32_t verbose,
char const* msg,
uint32_t size) noexcept {
std::lock_guard<std::mutex> lock(_lock);
(void)types;
(void)verbose;
misc::stringifier header;
_build_header(header);
_buffer.append(header.data(), header.size());
_buffer.append(msg, size);
++_nb_call;
}
uint32_t get_nb_call() const noexcept { return _nb_call; }
void open() {}
void reopen() {}
void reset() noexcept { _buffer.clear(); }
private:
std::string _buffer;
uint32_t _nb_call;
};
}
CC_END()
#endif // !CC_TEST_LOGGING_BACKEND_TEST_HH
/*
** Copyright 2011-2013 Centreon
**
** 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.
**
** For more information : contact@centreon.com
*/
#include <cstdlib>
#include <cstring>
#include <exception>
#include <iostream>
#include <unistd.h>
/**
* Find line into the environement.
*
* @param[in] data line on form key=value.
* @param[in] env The environment.
*
* @return True if data is find into env, otherwise false.
*/
static bool find(char const* data, char** env) {
for (unsigned int i(0); env[i]; ++i)
if (!strcmp(data, env[i]))
return (true);
return (false);
}
/**
* Check if the content of ref is in env array.
*
* @param[in] ref The content reference.
* @param[in] env The content to check.
*
* @return EXIT_SUCCESS on success, otherwise EXIT_FAILURE.
*/
static int check_env(char** ref, char** env) {
for (unsigned int i(0); ref[i]; ++i)
if (!find(ref[i], env))
return (EXIT_FAILURE);
return (EXIT_SUCCESS);
}
/**
* Read stdin and write into the stdout.
*
* @param[in] type If type if "err" write data on stderr,
* otherwise write on stdout.
*
* @return The total bytes written.
*/
static int check_output(char const* type) {
int output(strcmp(type, "err") ? 1 : 2);
int total(0);
int size(0);
char buffer[1024];
while ((size = read(0, buffer, sizeof(buffer))) > 0)
total += write(output, buffer, size);
return (EXIT_SUCCESS);
}
/**
* Usage of the application.
*