Browse Source

[3113_test] Fix to work on OSX with static link

Initialize logging properly, and work around problem of dynamic
library locations when loading libraries with dlopen().
Stephen Morris 11 years ago
parent
commit
242628300d

+ 6 - 2
src/lib/hooks/hooks.cc

@@ -13,7 +13,9 @@
 // PERFORMANCE OF THIS SOFTWARE.
 // PERFORMANCE OF THIS SOFTWARE.
 
 
 #include <hooks/hooks.h>
 #include <hooks/hooks.h>
-#include <log/message_initializer.h>
+#include <log/logger_support.h>
+
+#include <string>
 
 
 
 
 namespace isc {
 namespace isc {
@@ -23,7 +25,9 @@ namespace hooks {
 
 
 void
 void
 hooksStaticLinkInit() {
 hooksStaticLinkInit() {
-    isc::log::MessageInitializer::loadDictionary();
+    if (!isc::log::isLoggingInitialized()) {
+        isc::log::initLogger(std::string("userlib"));
+    }
 }
 }
 
 
 } // namespace hooks
 } // namespace hooks

+ 15 - 0
src/lib/hooks/hooks_messages.mes

@@ -109,6 +109,14 @@ was called.  The function threw an exception (an error indication)
 during execution, which is an error condition.  The library has been
 during execution, which is an error condition.  The library has been
 unloaded and no callouts from it will be installed.
 unloaded and no callouts from it will be installed.
 
 
+% HOOKS_LOAD_FRAMEWORK_EXCEPTION 'load' function in hook library %1 threw an exception: reason %2
+A "load" function was found in the library named in the message and
+was called.  Either the hooks framework or the function threw an
+exception (an error indication) during execution, which is an error
+condition; the cause of the exception is recorded in the message.
+The library has been unloaded and no callouts from it will be
+installed.
+
 % HOOKS_LOAD_SUCCESS 'load' function in hook library %1 returned success
 % HOOKS_LOAD_SUCCESS 'load' function in hook library %1 returned success
 This is a debug message issued when the "load" function has been found
 This is a debug message issued when the "load" function has been found
 in a hook library and has been successfully called.
 in a hook library and has been successfully called.
@@ -152,6 +160,13 @@ called, but in the process generated an exception (an error indication).
 The unload process continued after this message and the library has
 The unload process continued after this message and the library has
 been unloaded.
 been unloaded.
 
 
+% HOOKS_UNLOAD_FRAMEWORK_EXCEPTION 'unload' function in hook library %1 threw an exception, reason %2
+During the unloading of a library, an "unload" function was found.
+It was called, but in the process either it or the hooks framework
+generated an exception (an error indication); the cause of the error
+is recorded in the message.  The unload process continued after
+this message and the library has been unloaded.
+
 % HOOKS_UNLOAD_SUCCESS 'unload' function in hook library %1 returned success
 % HOOKS_UNLOAD_SUCCESS 'unload' function in hook library %1 returned success
 This is a debug message issued when an "unload" function has been found
 This is a debug message issued when an "unload" function has been found
 in a hook library during the unload process, called, and returned success.
 in a hook library during the unload process, called, and returned success.

+ 9 - 0
src/lib/hooks/library_manager.cc

@@ -12,6 +12,7 @@
 // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 // PERFORMANCE OF THIS SOFTWARE.
 // PERFORMANCE OF THIS SOFTWARE.
 
 
+#include <exceptions/exceptions.h>
 #include <hooks/hooks.h>
 #include <hooks/hooks.h>
 #include <hooks/hooks_log.h>
 #include <hooks/hooks_log.h>
 #include <hooks/callout_manager.h>
 #include <hooks/callout_manager.h>
@@ -179,6 +180,10 @@ LibraryManager::runLoad() {
         try {
         try {
             manager_->setLibraryIndex(index_);
             manager_->setLibraryIndex(index_);
             status = (*pc.loadPtr())(manager_->getLibraryHandle());
             status = (*pc.loadPtr())(manager_->getLibraryHandle());
+        } catch (const isc::Exception& ex) {
+            LOG_ERROR(hooks_logger, HOOKS_LOAD_FRAMEWORK_EXCEPTION)
+                .arg(library_name_).arg(ex.what());
+            return (false);
         } catch (...) {
         } catch (...) {
             LOG_ERROR(hooks_logger, HOOKS_LOAD_EXCEPTION).arg(library_name_);
             LOG_ERROR(hooks_logger, HOOKS_LOAD_EXCEPTION).arg(library_name_);
             return (false);
             return (false);
@@ -217,6 +222,10 @@ LibraryManager::runUnload() {
         int status = -1;
         int status = -1;
         try {
         try {
             status = (*pc.unloadPtr())();
             status = (*pc.unloadPtr())();
+        } catch (const isc::Exception& ex) {
+            LOG_ERROR(hooks_logger, HOOKS_UNLOAD_FRAMEWORK_EXCEPTION)
+                .arg(library_name_).arg(ex.what());
+            return (false);
         } catch (...) {
         } catch (...) {
             // Exception generated.  Note a warning as the unload will occur
             // Exception generated.  Note a warning as the unload will occur
             // anyway.
             // anyway.

+ 29 - 14
src/lib/hooks/tests/Makefile.am

@@ -9,11 +9,14 @@ AM_CPPFLAGS += $(BOOST_INCLUDES) $(MULTITHREADING_FLAG)
 # But older GCC compilers don't have the flag.     
 # But older GCC compilers don't have the flag.     
 AM_CXXFLAGS  = $(WARNING_NO_MISSING_FIELD_INITIALIZERS_CFLAG)
 AM_CXXFLAGS  = $(WARNING_NO_MISSING_FIELD_INITIALIZERS_CFLAG)
 
 
-# Common libraries used in user libraries
-AM_LIBADD  = $(top_builddir)/src/lib/hooks/libb10-hooks.la
-AM_LIBADD += $(top_builddir)/src/lib/log/libb10-log.la
-AM_LIBADD += $(top_builddir)/src/lib/exceptions/libb10-exceptions.la
-AM_LIBADD += $(top_builddir)/src/lib/util/libb10-util.la
+# BIND 10 libraries against which the test user libraries are linked.
+HOOKS_LIB      = $(top_builddir)/src/lib/hooks/libb10-hooks.la
+LOG_LIB        = $(top_builddir)/src/lib/log/libb10-log.la
+EXCEPTIONS_LIB = $(top_builddir)/src/lib/exceptions/libb10-exceptions.la
+UTIL_LIB       = $(top_builddir)/src/lib/util/libb10-util.la
+THREADS_LIB    = $(top_builddir)/src/lib/util/threads/libb10-threads.la
+
+ALL_LIBS       = $(HOOKS_LIB) $(LOG_LIB) $(EXCEPTIONS_LIB) $(UTIL_LIB) $(THREADS_LIB)
 
 
 if USE_CLANGPP
 if USE_CLANGPP
 # see ../Makefile.am
 # see ../Makefile.am
@@ -67,14 +70,14 @@ libbcl_la_SOURCES  = basic_callout_library.cc
 libbcl_la_CXXFLAGS = $(AM_CXXFLAGS)
 libbcl_la_CXXFLAGS = $(AM_CXXFLAGS)
 libbcl_la_CPPFLAGS = $(AM_CPPFLAGS) $(LOG4CPLUS_INCLUDES)
 libbcl_la_CPPFLAGS = $(AM_CPPFLAGS) $(LOG4CPLUS_INCLUDES)
 libbcl_la_LDFLAGS  = -avoid-version -export-dynamic -module
 libbcl_la_LDFLAGS  = -avoid-version -export-dynamic -module
-libbcl_la_LIBADD    = $(AM_LIBADD)
+libbcl_la_LIBADD    = $(ALL_LIBS)
 
 
 # The load callout library - contains a load function
 # The load callout library - contains a load function
 liblcl_la_SOURCES  = load_callout_library.cc
 liblcl_la_SOURCES  = load_callout_library.cc
 liblcl_la_CXXFLAGS = $(AM_CXXFLAGS)
 liblcl_la_CXXFLAGS = $(AM_CXXFLAGS)
 liblcl_la_CPPFLAGS = $(AM_CPPFLAGS) $(LOG4CPLUS_INCLUDES)
 liblcl_la_CPPFLAGS = $(AM_CPPFLAGS) $(LOG4CPLUS_INCLUDES)
 liblcl_la_LDFLAGS  = -avoid-version -export-dynamic -module
 liblcl_la_LDFLAGS  = -avoid-version -export-dynamic -module
-liblcl_la_LIBADD    = $(AM_LIBADD)
+liblcl_la_LIBADD    = $(ALL_LIBS)
 
 
 # The load error callout library - contains a load function that returns
 # The load error callout library - contains a load function that returns
 # an error.
 # an error.
@@ -95,7 +98,7 @@ libfcl_la_SOURCES  = full_callout_library.cc
 libfcl_la_CXXFLAGS = $(AM_CXXFLAGS)
 libfcl_la_CXXFLAGS = $(AM_CXXFLAGS)
 libfcl_la_CPPFLAGS = $(AM_CPPFLAGS) $(LOG4CPLUS_INCLUDES)
 libfcl_la_CPPFLAGS = $(AM_CPPFLAGS) $(LOG4CPLUS_INCLUDES)
 libfcl_la_LDFLAGS  = -avoid-version -export-dynamic -module
 libfcl_la_LDFLAGS  = -avoid-version -export-dynamic -module
-libfcl_la_LIBADD   = $(AM_LIBADD)
+libfcl_la_LIBADD   = $(ALL_LIBS)
 
 
 TESTS += run_unittests
 TESTS += run_unittests
 run_unittests_SOURCES  = run_unittests.cc
 run_unittests_SOURCES  = run_unittests.cc
@@ -112,18 +115,30 @@ nodist_run_unittests_SOURCES  = marker_file.h
 nodist_run_unittests_SOURCES += test_libraries.h
 nodist_run_unittests_SOURCES += test_libraries.h
 
 
 run_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES)
 run_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES)
-
 run_unittests_LDFLAGS  = $(AM_LDFLAGS)  $(GTEST_LDFLAGS)
 run_unittests_LDFLAGS  = $(AM_LDFLAGS)  $(GTEST_LDFLAGS)
 if USE_STATIC_LINK
 if USE_STATIC_LINK
 run_unittests_LDFLAGS += -static
 run_unittests_LDFLAGS += -static
 endif
 endif
 
 
-run_unittests_LDADD    = $(AM_LDADD)    $(GTEST_LDADD)
-run_unittests_LDADD   += $(top_builddir)/src/lib/hooks/libb10-hooks.la
-run_unittests_LDADD   += $(top_builddir)/src/lib/log/libb10-log.la
-run_unittests_LDADD   += $(top_builddir)/src/lib/exceptions/libb10-exceptions.la
-run_unittests_LDADD   += $(top_builddir)/src/lib/util/libb10-util.la
+run_unittests_LDADD    = $(AM_LDADD) $(GTEST_LDADD)
+run_unittests_LDADD   += $(ALL_LIBS)
 run_unittests_LDADD   += $(top_builddir)/src/lib/util/unittests/libutil_unittests.la
 run_unittests_LDADD   += $(top_builddir)/src/lib/util/unittests/libutil_unittests.la
+
+# As noted in configure.ac, libtool doesn't work perfectly with Darwin: it embeds the
+# final install path in dynamic libraries and loadable modules refer to that path even
+# if its loaded within the source tree, so preventing tests from working - but only
+# when linking statically.  The solution used in other Makefiles (setting the path
+# to the dynamic libraries via an environment variable) don't seem to work.  What does
+# work is to run the unit test using libtool and specifying paths via -dlopen switches.
+# So... If running in an environment where we have to set the library path AND if
+# linking statically, override the "check" target and run the unit tests ourselves.
+if USE_STATIC_LINK
+if SET_ENV_LIBRARY_PATH
+check-TESTS:
+	$(LIBTOOL) --mode=execute -dlopen $(HOOKS_LIB)  -dlopen $(LOG_LIB) -dlopen $(EXCEPTIONS_LIB) -dlopen $(UTIL_LIB)  -dlopen $(THREADS_LIB) ./run_unittests
+endif
+endif
+
 endif
 endif
 
 
 noinst_PROGRAMS = $(TESTS)
 noinst_PROGRAMS = $(TESTS)