Skip to content
forked from BOINC/boinc

Commit

Permalink
Tests: add unit testing framework using googletest
Browse files Browse the repository at this point in the history
* enable coverage reports from gcc and disable optimizations via option to configure
* install googletest library into buildcache
* script to compile and run unit tests using cmake
* first set of unit tests originaly contributed by Keith Uplinger

There are several hardcoded paths and assumptions made in order to get this working on Travis CI. New tooling is using cmake for cross platform builds and as such is not easy to use with an autotools based system.
It's not ideal but better than nothing.
  • Loading branch information
ChristianBeer committed Apr 20, 2019
1 parent 99716be commit 6dbf55f
Show file tree
Hide file tree
Showing 16 changed files with 443 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ locale/*/*.flag
.libs/
svn_version.h

## code coverage
*.gcov
*.gcno
*.gcda

# list of executables
apps/1sec
apps/concat
Expand Down
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ matrix:
before_install:
- if [[ "${TRAVIS_OS_NAME}" == "linux" ]]; then ( sudo apt-get -qq update ) fi
- if [[ "${TRAVIS_OS_NAME}" == "linux" ]]; then ( sudo apt-get install -y freeglut3-dev libxmu-dev libxi-dev libfcgi-dev libxss-dev libnotify-dev libxcb-util0-dev libsqlite3-dev libgtk2.0-dev libwebkitgtk-dev mingw-w64 binutils-mingw-w64-i686 binutils-mingw-w64-x86-64 gcc-mingw-w64 gcc-mingw-w64-i686 gcc-mingw-w64-x86-64 g++-mingw-w64 g++-mingw-w64-i686 g++-mingw-w64-x86-64 realpath p7zip-full ) fi
- if [[ "${TRAVIS_OS_NAME}" == "linux" && "${BOINC_TYPE}" == "unit-test" ]]; then ( sudo pip install codecov ) fi
- if [[ "${BOINC_TYPE}" == "integration-test" ]]; then ( sudo apt-get install ansible/trusty-backports; sudo service mysql stop; ) fi

before_script:
Expand All @@ -80,7 +81,7 @@ script:
- if [[ "${BOINC_TYPE}" == "apps-mingw" ]]; then ( cd lib && MINGW=x86_64-w64-mingw32 make -f Makefile.mingw wrapper ) fi
- if [[ "${BOINC_TYPE}" == "manager-osx" ]]; then ( ./3rdParty/buildMacDependencies.sh -q && ./mac_build/buildMacBOINC-CI.sh --no_shared_headers ) fi
- if [[ "${BOINC_TYPE}" == "manager-android" ]]; then ( cd android && ./buildAndroidBOINC-CI.sh --arch arm --silent && ./buildAndroidBOINC-CI.sh --arch arm64 --silent && ./buildAndroidBOINC-CI.sh --arch x86 --silent && ./buildAndroidBOINC-CI.sh --arch x86_64 --silent && cd BOINC && ./gradlew assemble -w && cd ../../ ) fi
- if [[ "${BOINC_TYPE}" == "unit-test" ]]; then ( /bin/true ) fi
- if [[ "${BOINC_TYPE}" == "unit-test" ]]; then ( ./3rdParty/buildLinuxDependencies.sh --gtest-only && ./configure --disable-client --disable-manager --enable-unit-tests CFLAGS="-g -O0" CXXFLAGS="-g -O0" && make && ./tests/executeUnitTests.sh --report-coverage ) fi
- if [[ "${BOINC_TYPE}" == "integration-test" ]]; then ( ./integration_test/executeTestSuite.sh ) fi

after_success:
Expand Down
67 changes: 67 additions & 0 deletions 3rdParty/buildGoogletestLinux.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/bin/bash

# This file is part of BOINC.
# http://boinc.berkeley.edu
# Copyright (C) 2019 University of California
#
# BOINC is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
#
# BOINC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with BOINC. If not, see <http://www.gnu.org/licenses/>.

# Script to build a googletest library for BOINC

# Usage:
# cd [path]/googletest-release-1.8.1/
# source path_to_this_script [--clean] [--prefix PATH]
#
# the --clean argument will force a full rebuild.
# if --prefix is given as absolute path the library is installed into there

doclean=""
debug_flag=""
lprefix=""
cmdline_prefix=""
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-clean|--clean)
doclean="yes"
;;
-prefix|--prefix)
lprefix="$2"
cmdline_prefix="--prefix=${lprefix}"
shift
;;
esac
shift # past argument or value
done

cd googletest
autoreconf -i -f
if [ $? -ne 0 ]; then cd ..; return 1; fi
./configure "${cmdline_prefix}"
if [ $? -ne 0 ]; then cd ..; return 1; fi
if [ "${doclean}" = "yes" ]; then
make clean
fi
make
if [ $? -ne 0 ]; then cd ..; return 1; fi

# make install is not supported so copy files manually
if [ "x${lprefix}" != "x" ]; then
mkdir -p ${lprefix}/lib ${lprefix}/include ${lprefix}/bin
(cp -r lib/.libs/*.a ${lprefix}/lib && cp -r include/gtest ${lprefix}/include && cp scripts/gtest-config ${lprefix}/bin)
if [ $? -ne 0 ]; then cd ..; return 1; fi
fi

cd ..
return 0
12 changes: 10 additions & 2 deletions 3rdParty/buildLinuxDependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# This file is part of BOINC.
# http://boinc.berkeley.edu
# Copyright (C) 2017 University of California
# Copyright (C) 2019 University of California
#
# BOINC is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License
Expand Down Expand Up @@ -51,6 +51,7 @@ ROOTDIR=$(pwd)
cache_dir=""
doclean=""
wxoption=""
gtest_only=""
build_config="Release"
while [[ $# -gt 0 ]]; do
key="$1"
Expand All @@ -69,6 +70,9 @@ while [[ $# -gt 0 ]]; do
--disable-webview)
wxoption="--disable-webview ${wxoption} "
;;
--gtest-only)
gtest_only="yes"
;;
*)
echo "unrecognized option $key"
;;
Expand Down Expand Up @@ -134,7 +138,11 @@ if [ "${doclean}" = "yes" ]; then
fi

#download_and_build $DIRNAME $FILENAME $DOWNLOADURL $BUILDSCRIPT
download_and_build "wxWidgets-3.0.2" "wxWidgets-3.0.2.tar.bz2" "https://sourceforge.net/projects/wxwindows/files/3.0.2/wxWidgets-3.0.2.tar.bz2" "${ROOTDIR}/3rdParty/buildWxLinux.sh ${wxoption}"
if [ "${gtest_only}" = "yes" ]; then
download_and_build "googletest-release-1.8.1" "release-1.8.1.tar.gz" "https://github.com/google/googletest/archive/release-1.8.1.tar.gz" "${ROOTDIR}/3rdParty/buildGoogletestLinux.sh"
else
download_and_build "wxWidgets-3.0.2" "wxWidgets-3.0.2.tar.bz2" "https://sourceforge.net/projects/wxwindows/files/3.0.2/wxWidgets-3.0.2.tar.bz2" "${ROOTDIR}/3rdParty/buildWxLinux.sh ${wxoption}"
fi

# change back to root directory
cd ${ROOTDIR} || exit 1
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ svn_version.h: generate_svn_version.sh
# Add a stage target for staging a distribution

clean-generic:
rm -rf stage
rm -rf stage *.gcov

stage: all
if [ ! -d stage ] ; then mkdir stage ; fi
Expand Down
30 changes: 22 additions & 8 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ AC_ARG_ENABLE(apps,
[enable_apps=${enableval}],
[enable_apps=no])

AC_ARG_ENABLE(unit-tests,
AS_HELP_STRING([--enable-unit-tests],
[enable building the boinc unit tests]),
[enable_unit_tests=${enableval}],
[enable_unit_tests=no])

AC_ARG_ENABLE(pkg-libs,
AS_HELP_STRING([--enable-pkg-libs],
[Builds and installs components that would be present in a
Expand All @@ -139,8 +145,9 @@ AC_ARG_ENABLE(pkg-libs,
enable_manager=no
enable_install_headers=no
enable_static=no
enable_boinczip=yes
enable_apps=no
enable_boinczip=yes
enable_apps=no
enable_unit_tests=no
],
[])

Expand All @@ -156,8 +163,9 @@ AC_ARG_ENABLE(pkg-devel,
enable_client=no
enable_manager=no
enable_install_headers=yes
enable_boinczip=yes
enable_apps=no
enable_boinczip=yes
enable_apps=no
enable_unit_tests=no
],
[])

Expand All @@ -173,8 +181,9 @@ AC_ARG_ENABLE(pkg-client,
enable_client=yes
enable_manager=no
enable_install_headers=no
enable_boinczip=no
enable_apps=no
enable_boinczip=no
enable_apps=no
enable_unit_tests=no
],
[])

Expand All @@ -188,8 +197,9 @@ AC_ARG_ENABLE(pkg-manager,
enable_client=no
enable_manager=yes
enable_install_headers=no
enable_boinczip=no
enable_apps=no
enable_boinczip=no
enable_apps=no
enable_unit_tests=no
],
[])

Expand All @@ -213,6 +223,9 @@ fi
if test x$enable_apps = xyes ; then
configured_to_build="${configured_to_build} apps"
fi
if test x$enable_unit_tests = xyes ; then
configured_to_build="${configured_to_build} unit-tests"
fi

if test -z "${configured_to_build}" ; then
AC_MSG_ERROR([
Expand Down Expand Up @@ -1049,6 +1062,7 @@ AM_CONDITIONAL(ENABLE_MANAGER, [ test "x${ac_cv_have_wxwidgets}" = xyes -a "${en
AM_CONDITIONAL(ENABLE_LIBRARIES, [test "${enable_libraries}" = yes])
AM_CONDITIONAL(ENABLE_BOINCZIP, [test "${enable_boinczip}" = yes])
AM_CONDITIONAL(ENABLE_APPS, [test "${enable_apps}" = yes])
AM_CONDITIONAL(ENABLE_UNIT_TESTS, [test "${enable_unit_tests}" = yes])
AM_CONDITIONAL(ENABLE_BOINCCRYPT, [test "x${enable_server}" = xyes || test "x${enable_client}" = xyes ])
AM_CONDITIONAL(INSTALL_HEADERS, [test "${enable_install_headers}" = yes])
AM_CONDITIONAL(HAVE_CUDA_LIB, [test "${enable_client}" = yes -a -f ./coprocs/CUDA/posix/${boinc_platform}/libcudart.so])
Expand Down
2 changes: 1 addition & 1 deletion lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ $(LIBBOINC_FCGI_STATIC): libboinc_fcgi.la
$(LN) .libs/$(LIBBOINC_FCGI_STATIC) .

clean: clean-am
rm -f $(LIBBOINC_STATIC) $(LIBBOINC_CRYPT_STATIC) $(LIBBOINC_FCGI_STATIC)
rm -f $(LIBBOINC_STATIC) $(LIBBOINC_CRYPT_STATIC) $(LIBBOINC_FCGI_STATIC) *.gcno *.gcda *.gcov

endif
# end of "if ENABLE_LIBRARIES"
Expand Down
7 changes: 7 additions & 0 deletions m4/boinc_set_compile_flags.m4
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,11 @@ if test x${enable_generic_processor} = xyes ; then
;;
esac
fi
if test x${enable_unit_tests} = xyes ; then
BOINC_CHECK_CFLAG(--coverage)
BOINC_CHECK_CXXFLAG(--coverage)
LDFLAGS="$LDFLAGS --coverage"
fi
])
2 changes: 1 addition & 1 deletion sched/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,4 @@ PHONY-start:
status stop: PHONY-start
@test -f $@ || @LN_S@ start $@ && test -f $@

CLEANFILES = status stop
CLEANFILES = status stop *.gcno *.gcda *.gcov
79 changes: 79 additions & 0 deletions tests/executeUnitTests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/bin/bash

# This file is part of BOINC.
# http://boinc.berkeley.edu
# Copyright (C) 2019 University of California
#
# BOINC is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
#
# BOINC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with BOINC. If not, see <http://www.gnu.org/licenses/>.

# Script to execute googletest based unit tests for BOINC

# Usage:
# ./tests/executeUnitTests.sh [--report-coverage]
#
# if --report-coverage is given the test coverage will be reported to codecov.io

# check working directory because the script needs to be called like: ./tests/executeUnitTests.sh
if [ ! -d "tests" ]; then
echo "start this script in the source root directory"
exit 1
fi

ROOTDIR=$(pwd)
CI_RUN="${TRAVIS:-false}"
report=""
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
--report-coverage)
report="yes"
;;
*)
echo "unrecognized option $key"
;;
esac
shift # past argument or value
done

command -v cmake >/dev/null 2>&1 || { echo >&2 "cmake is needed but not installed. Aborting."; exit 1; }

if [ "${report}" = "yes" ]; then
command -v gcov >/dev/null 2>&1 || { echo >&2 "gcov (lcov) is needed but not installed. Aborting."; exit 1; }
command -v codecov >/dev/null 2>&1 || { echo >&2 "codecov (pip install codecov) is needed but not installed. Aborting."; exit 1; }
fi

cd tests || exit 1
if [ -d "gtest" ]; then
rm -rf gtest
if [ $? -ne 0 ]; then cd ..; exit 1; fi
fi
mkdir gtest
if [ $? -ne 0 ]; then cd ..; exit 1; fi
cd gtest
cmake ../unit-tests
if [ $? -ne 0 ]; then cd ../..; exit 1; fi
make
if [ $? -ne 0 ]; then cd ../..; exit 1; fi

for T in lib sched; do
[ -d "${T}" ] && ./${T}/test_${T};
done

cd ../..
if [ "${report}" = "yes" ]; then
for T in lib sched; do
[ -d "${T}" ] && gcov -lp *.o >/dev/null;
done
codecov -X gcov
fi
37 changes: 37 additions & 0 deletions tests/unit-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
cmake_minimum_required(VERSION 2.8)

project(BOINC_unit_testing)
enable_testing()

set (CMAKE_CXX_STANDARD 11)
set (CMAKE_CXX_FLAGS "-g -Wall -Wextra -Werror --coverage")


find_package(Threads REQUIRED)

# There is no easy way to interface with the autotools driven build system in boinc
# so the paths below are hardcoded and are mainly suited for building on Travis CI
# TODO: make paths configurable and generate them from boinc make or switch boinc to cmake

include_directories(BEFORE "${PROJECT_SOURCE_DIR}/../../3rdParty/buildCache/linux/include")
include_directories("${PROJECT_SOURCE_DIR}/../..")
include_directories("${PROJECT_SOURCE_DIR}/../../sched")
include_directories("${PROJECT_SOURCE_DIR}/../../db")
include_directories("${PROJECT_SOURCE_DIR}/../../lib")
include_directories("${PROJECT_SOURCE_DIR}/../../tools")
include_directories("/usr/include/mariadb")
include_directories("/usr/include/mariadb/mysql")


find_library(GTEST_MAIN_LIB libgtest_main.a PATHS ${PROJECT_SOURCE_DIR}/../../3rdParty/buildCache/linux/lib NO_DEFAULT_PATH)
message(STATUS "gtest_main_lib: ${GTEST_MAIN_LIB}")
find_library(GTEST_LIB gtest PATHS ${PROJECT_SOURCE_DIR}/../../3rdParty/buildCache/linux/lib NO_DEFAULT_PATH)
message(STATUS "gtest_lib: ${GTEST_LIB}")
find_library(SCHED_LIB libsched.a PATHS ${PROJECT_SOURCE_DIR}/../../sched NO_DEFAULT_PATH)
find_library(BOINC_CRYPT_LIB libboinc_crypt.a PATHS ${PROJECT_SOURCE_DIR}/../../lib NO_DEFAULT_PATH)
find_library(BOINC_LIB libboinc.a PATHS ${PROJECT_SOURCE_DIR}/../../lib NO_DEFAULT_PATH)
find_library(MYSQL_LIB NAMES mariadb mysql)
message(STATUS "mysql_lib: ${MYSQL_LIB}")

add_subdirectory(lib)
add_subdirectory(sched)
7 changes: 7 additions & 0 deletions tests/unit-tests/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
file(GLOB SRCS *.cpp)

add_executable(test_lib ${SRCS})

TARGET_LINK_LIBRARIES(test_lib "${GTEST_LIB}" "${SCHED_LIB}" "${BOINC_CRYPT_LIB}" "${BOINC_LIB}" "${GTEST_MAIN_LIB}" pthread)

add_test(NAME test_lib COMMAND test_lib)
Loading

0 comments on commit 6dbf55f

Please sign in to comment.