From 1dc4ab367e56c0ae69831f1737386bf51797c052 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Wed, 7 Sep 2016 08:20:51 +0200 Subject: [PATCH] Fix: Unit tests randomly crash after the tests have completed fixes #12610 --- test/CMakeLists.txt | 6 ++- test/base-array.cpp | 2 +- test/base-convert.cpp | 2 +- test/base-dictionary.cpp | 2 +- test/base-fifo.cpp | 2 +- test/base-json.cpp | 2 +- test/base-match.cpp | 2 +- test/base-netstring.cpp | 2 +- test/base-object.cpp | 2 +- test/base-serialize.cpp | 2 +- test/base-shellescape.cpp | 2 +- test/base-stacktrace.cpp | 2 +- test/base-stream.cpp | 2 +- test/base-string.cpp | 2 +- test/base-test.cpp | 48 ------------------- test/base-timer.cpp | 2 +- test/base-type.cpp | 2 +- test/base-value.cpp | 2 +- test/config-ops.cpp | 2 +- test/icinga-checkresult.cpp | 2 +- test/icinga-macros.cpp | 2 +- test/icinga-perfdata.cpp | 2 +- ...status-test.cpp => livestatus-fixture.cpp} | 20 +------- test/livestatus.cpp | 2 +- test/remote-base64.cpp | 2 +- test/remote-url.cpp | 2 +- test/test-runner.cpp | 45 +++++++++++++++++ third-party/cmake/BoostTestTargets.cmake | 4 +- 28 files changed, 75 insertions(+), 94 deletions(-) delete mode 100644 test/base-test.cpp rename test/{livestatus-test.cpp => livestatus-fixture.cpp} (85%) create mode 100644 test/test-runner.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 918fe5b8c..e577d0358 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -15,6 +15,8 @@ # along with this program; if not, write to the Free Software Foundation # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. +set(Boost_USE_STATIC_LIBS OFF) + include(BoostTestTargets) set(base_test_SOURCES @@ -31,7 +33,7 @@ if(ICINGA2_UNITY_BUILD) endif() add_boost_test(base - SOURCES base-test.cpp ${base_test_SOURCES} + SOURCES test-runner.cpp ${base_test_SOURCES} LIBRARIES base config icinga TESTS base_array/construct base_array/getset @@ -123,7 +125,7 @@ if(ICINGA2_WITH_LIVESTATUS) endif() add_boost_test(livestatus - SOURCES livestatus-test.cpp ${livestatus_test_SOURCES} + SOURCES test-runner.cpp livestatus-fixture.cpp ${livestatus_test_SOURCES} LIBRARIES base config icinga livestatus DEPENDENCIES methods TESTS livestatus/hosts livestatus/services diff --git a/test/base-array.cpp b/test/base-array.cpp index 61b2043df..a748b3eeb 100644 --- a/test/base-array.cpp +++ b/test/base-array.cpp @@ -20,7 +20,7 @@ #include "base/array.hpp" #include "base/objectlock.hpp" #include "base/json.hpp" -#include +#include using namespace icinga; diff --git a/test/base-convert.cpp b/test/base-convert.cpp index 16e84da86..0c330daa0 100644 --- a/test/base-convert.cpp +++ b/test/base-convert.cpp @@ -19,7 +19,7 @@ #include "base/convert.hpp" #include "base/object.hpp" -#include +#include #include using namespace icinga; diff --git a/test/base-dictionary.cpp b/test/base-dictionary.cpp index bd336b4c2..903d9c185 100644 --- a/test/base-dictionary.cpp +++ b/test/base-dictionary.cpp @@ -20,7 +20,7 @@ #include "base/dictionary.hpp" #include "base/objectlock.hpp" #include "base/json.hpp" -#include +#include #include using namespace icinga; diff --git a/test/base-fifo.cpp b/test/base-fifo.cpp index 06385f1b6..51b7391ed 100644 --- a/test/base-fifo.cpp +++ b/test/base-fifo.cpp @@ -19,7 +19,7 @@ #include "base/fifo.hpp" #include "base/objectlock.hpp" -#include +#include using namespace icinga; diff --git a/test/base-json.cpp b/test/base-json.cpp index 3bb70153f..bbe89c733 100644 --- a/test/base-json.cpp +++ b/test/base-json.cpp @@ -21,7 +21,7 @@ #include "base/dictionary.hpp" #include "base/objectlock.hpp" #include "base/json.hpp" -#include +#include #include using namespace icinga; diff --git a/test/base-match.cpp b/test/base-match.cpp index c150e579e..0b00f63ee 100644 --- a/test/base-match.cpp +++ b/test/base-match.cpp @@ -18,7 +18,7 @@ ******************************************************************************/ #include "base/utility.hpp" -#include +#include using namespace icinga; diff --git a/test/base-netstring.cpp b/test/base-netstring.cpp index 2e4d0adee..9c29229b6 100644 --- a/test/base-netstring.cpp +++ b/test/base-netstring.cpp @@ -19,7 +19,7 @@ #include "base/netstring.hpp" #include "base/fifo.hpp" -#include +#include using namespace icinga; diff --git a/test/base-object.cpp b/test/base-object.cpp index 5fac6f716..004aa9e8e 100644 --- a/test/base-object.cpp +++ b/test/base-object.cpp @@ -19,7 +19,7 @@ #include "base/object.hpp" #include "base/value.hpp" -#include +#include using namespace icinga; diff --git a/test/base-serialize.cpp b/test/base-serialize.cpp index 4373b2ec0..7f5669c72 100644 --- a/test/base-serialize.cpp +++ b/test/base-serialize.cpp @@ -23,7 +23,7 @@ #include "base/serializer.hpp" #include "base/array.hpp" #include "base/dictionary.hpp" -#include +#include #include using namespace icinga; diff --git a/test/base-shellescape.cpp b/test/base-shellescape.cpp index c6317dbbd..6f89cd349 100644 --- a/test/base-shellescape.cpp +++ b/test/base-shellescape.cpp @@ -18,7 +18,7 @@ ******************************************************************************/ #include "base/utility.hpp" -#include +#include #include using namespace icinga; diff --git a/test/base-stacktrace.cpp b/test/base-stacktrace.cpp index 745338cff..2fceecd88 100644 --- a/test/base-stacktrace.cpp +++ b/test/base-stacktrace.cpp @@ -18,7 +18,7 @@ ******************************************************************************/ #include "base/stacktrace.hpp" -#include +#include using namespace icinga; diff --git a/test/base-stream.cpp b/test/base-stream.cpp index c093b06e9..dfe3909dc 100644 --- a/test/base-stream.cpp +++ b/test/base-stream.cpp @@ -19,7 +19,7 @@ #include "base/stdiostream.hpp" #include "base/string.hpp" -#include +#include #include using namespace icinga; diff --git a/test/base-string.cpp b/test/base-string.cpp index f61a9382f..841f78776 100644 --- a/test/base-string.cpp +++ b/test/base-string.cpp @@ -18,7 +18,7 @@ ******************************************************************************/ #include "base/string.hpp" -#include +#include using namespace icinga; diff --git a/test/base-test.cpp b/test/base-test.cpp deleted file mode 100644 index 0ed1d89a3..000000000 --- a/test/base-test.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/****************************************************************************** - * Icinga 2 * - * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) * - * * - * This program is free software; you can redistribute it and/or * - * modify it under the terms of the GNU General Public License * - * as published by the Free Software Foundation; either version 2 * - * of the License, or (at your option) any later version. * - * * - * This program 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 General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the Free Software Foundation * - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * - ******************************************************************************/ - -#define BOOST_TEST_MAIN -#define BOOST_TEST_MODULE icinga2_test - -#include "icinga/icingaapplication.hpp" -#include "base/application.hpp" -#include "base/timer.hpp" -#include - -using namespace icinga; - -struct InitLibBase -{ - IcingaApplication::Ptr appInst; - - InitLibBase(void) - { - Application::InitializeBase(); - - appInst = new IcingaApplication(); - static_pointer_cast(appInst)->OnConfigLoaded(); - } - - ~InitLibBase(void) - { - Application::UninitializeBase(); - } -}; - -BOOST_GLOBAL_FIXTURE(InitLibBase); diff --git a/test/base-timer.cpp b/test/base-timer.cpp index 14a39b86f..cd4404fc4 100644 --- a/test/base-timer.cpp +++ b/test/base-timer.cpp @@ -20,7 +20,7 @@ #include "base/timer.hpp" #include "base/utility.hpp" #include "base/application.hpp" -#include +#include using namespace icinga; diff --git a/test/base-type.cpp b/test/base-type.cpp index 0c726f4cf..0b83f9a71 100644 --- a/test/base-type.cpp +++ b/test/base-type.cpp @@ -22,7 +22,7 @@ #include "base/objectlock.hpp" #include "base/application.hpp" #include "base/type.hpp" -#include +#include #include using namespace icinga; diff --git a/test/base-value.cpp b/test/base-value.cpp index a8170d1f9..f79f5f83f 100644 --- a/test/base-value.cpp +++ b/test/base-value.cpp @@ -18,7 +18,7 @@ ******************************************************************************/ #include "base/value.hpp" -#include +#include using namespace icinga; diff --git a/test/config-ops.cpp b/test/config-ops.cpp index ffa813bec..77a384cad 100644 --- a/test/config-ops.cpp +++ b/test/config-ops.cpp @@ -19,7 +19,7 @@ #include "config/configcompiler.hpp" #include "base/exception.hpp" -#include +#include using namespace icinga; diff --git a/test/icinga-checkresult.cpp b/test/icinga-checkresult.cpp index 5241d43e2..703602d99 100644 --- a/test/icinga-checkresult.cpp +++ b/test/icinga-checkresult.cpp @@ -18,7 +18,7 @@ ******************************************************************************/ #include "icinga/host.hpp" -#include +#include using namespace icinga; diff --git a/test/icinga-macros.cpp b/test/icinga-macros.cpp index ef15cfba7..43f8f7910 100644 --- a/test/icinga-macros.cpp +++ b/test/icinga-macros.cpp @@ -18,7 +18,7 @@ ******************************************************************************/ #include "icinga/macroprocessor.hpp" -#include +#include using namespace icinga; diff --git a/test/icinga-perfdata.cpp b/test/icinga-perfdata.cpp index 7372159af..4ca535ee4 100644 --- a/test/icinga-perfdata.cpp +++ b/test/icinga-perfdata.cpp @@ -19,7 +19,7 @@ #include "icinga/perfdatavalue.hpp" #include "icinga/pluginutility.hpp" -#include +#include using namespace icinga; diff --git a/test/livestatus-test.cpp b/test/livestatus-fixture.cpp similarity index 85% rename from test/livestatus-test.cpp rename to test/livestatus-fixture.cpp index 18aa93207..e034188e8 100644 --- a/test/livestatus-test.cpp +++ b/test/livestatus-fixture.cpp @@ -17,14 +17,10 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * ******************************************************************************/ -#define BOOST_TEST_MAIN -#define BOOST_TEST_MODULE icinga2_test - #include "config/configcompiler.hpp" #include "config/configitem.hpp" #include "base/application.hpp" #include "base/loader.hpp" -#include #include using namespace icinga; @@ -33,22 +29,11 @@ struct LivestatusFixture { LivestatusFixture(void) { - BOOST_TEST_MESSAGE("setup global config fixture"); - - Application::InitializeBase(); - - BOOST_TEST_MESSAGE( "Preparing config objects..."); + BOOST_TEST_MESSAGE("Preparing config objects..."); ConfigItem::RunWithActivationContext(new Function("CreateTestObjects", WrapFunction(CreateTestObjects))); } - ~LivestatusFixture(void) - { - BOOST_TEST_MESSAGE("cleanup global config fixture"); - - Application::UninitializeBase(); - } - static void CreateTestObjects(void) { String config = R"CONFIG( @@ -78,5 +63,4 @@ apply Service "livestatus" { } }; -BOOST_GLOBAL_FIXTURE(LivestatusFixture); - +BOOST_GLOBAL_FIXTURE(LivestatusFixture); \ No newline at end of file diff --git a/test/livestatus.cpp b/test/livestatus.cpp index a065bf1d0..e6fb37edc 100644 --- a/test/livestatus.cpp +++ b/test/livestatus.cpp @@ -21,7 +21,7 @@ #include "base/application.hpp" #include "base/stdiostream.hpp" #include "base/json.hpp" -#include +#include using namespace icinga; diff --git a/test/remote-base64.cpp b/test/remote-base64.cpp index b0c78cc09..3dd014369 100644 --- a/test/remote-base64.cpp +++ b/test/remote-base64.cpp @@ -18,7 +18,7 @@ ******************************************************************************/ #include "remote/base64.hpp" -#include +#include using namespace icinga; diff --git a/test/remote-url.cpp b/test/remote-url.cpp index 7ba9640d4..b8d7c5fb7 100644 --- a/test/remote-url.cpp +++ b/test/remote-url.cpp @@ -19,7 +19,7 @@ #include "base/array.hpp" #include "remote/url.hpp" -#include +#include #include using namespace icinga; diff --git a/test/test-runner.cpp b/test/test-runner.cpp new file mode 100644 index 000000000..3cc7bc8bd --- /dev/null +++ b/test/test-runner.cpp @@ -0,0 +1,45 @@ +/****************************************************************************** +* Icinga 2 * +* Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) * +* * +* This program is free software; you can redistribute it and/or * +* modify it under the terms of the GNU General Public License * +* as published by the Free Software Foundation; either version 2 * +* of the License, or (at your option) any later version. * +* * +* This program 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 General Public License for more details. * +* * +* You should have received a copy of the GNU General Public License * +* along with this program; if not, write to the Free Software Foundation * +* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * +******************************************************************************/ + +#include "icinga/icingaapplication.hpp" +#include "base/application.hpp" +#include + +using namespace icinga; + +static bool init_unit_test(void) +{ + return true; +} + +int main(int argc, char *argv[]) +{ + Application::InitializeBase(); + + IcingaApplication::Ptr appInst; + + appInst = new IcingaApplication(); + static_pointer_cast(appInst)->OnConfigLoaded(); + + int rc = boost::unit_test::unit_test_main(&init_unit_test, argc, argv); + + appInst.reset(); + + Application::Exit(rc); +} \ No newline at end of file diff --git a/third-party/cmake/BoostTestTargets.cmake b/third-party/cmake/BoostTestTargets.cmake index e2f664ee2..8c2632485 100644 --- a/third-party/cmake/BoostTestTargets.cmake +++ b/third-party/cmake/BoostTestTargets.cmake @@ -78,9 +78,7 @@ if(Boost_FOUND AND NOT "${Boost_VERSION}0" LESS "1034000") if(Boost_USE_STATIC_LIBS) set(_boostConfig "BoostTestTargetsStatic.h") else() - if(NOT APPLE) - set(_boostConfig "BoostTestTargetsDynamic.h") - endif() + set(_boostConfig "BoostTestTargetsDynamic.h") endif() endif() get_filename_component(_moddir ${CMAKE_CURRENT_LIST_FILE} PATH)