ソースを参照

make unit tests with valgrind optional

Julius Pfrommer 8 年 前
コミット
1f6c35b4f7
共有4 個のファイルを変更した42 個の追加17 個の削除を含む
  1. 3 0
      CMakeLists.txt
  2. 19 16
      tests/CMakeLists.txt
  3. 18 0
      tools/cmake/FindValgrind.cmake
  4. 2 1
      tools/travis/travis_linux_script.sh

+ 3 - 0
CMakeLists.txt

@@ -72,6 +72,9 @@ mark_as_advanced(UA_ENABLE_GENERATE_NAMESPACE0)
 option(UA_ENABLE_EXTERNAL_NAMESPACES "Enable namespace handling by an external component (experimental)" OFF)
 mark_as_advanced(UA_ENABLE_EXTERNAL_NAMESPACES)
 
+option(UA_ENABLE_VALGRIND_UNIT_TESTS "Use Valgrind to detect memory leaks when running the unit tests" OFF)
+mark_as_advanced(UA_ENABLE_VALGRIND_UNIT_TESTS)
+
 option(UA_ENABLE_NONSTANDARD_STATELESS "Enable stateless extension (non-standard)" OFF)
 mark_as_advanced(UA_ENABLE_NONSTANDARD_STATELESS)
 

+ 19 - 16
tests/CMakeLists.txt

@@ -7,11 +7,14 @@ include_directories(${CHECK_INCLUDE_DIRS})
 
 find_package(Check REQUIRED)
 find_package(Threads REQUIRED)
+if(UA_ENABLE_VALGRIND_UNIT_TESTS)
+    find_package(Valgrind REQUIRED)
+endif()
 
 set(LIBS ${CHECK_LIBRARIES} ${open62541_LIBRARIES})
 if(NOT WIN32)
   list(APPEND LIBS pthread m)
-  if (NOT APPLE)
+  if(NOT APPLE)
     list(APPEND LIBS rt subunit)
   endif()
 else()
@@ -25,20 +28,16 @@ if(CMAKE_COMPILER_IS_GNUCC OR "x${CMAKE_C_COMPILER_ID}" STREQUAL "xClang")
     add_definitions(-Wno-sign-conversion)
 endif()
 
-# Valgrind definition
-set(UA_TEST_WITH_VALGRIND ON)
-SET(VALGRIND_FLAGS --quiet --trace-children=yes --leak-check=full)
+# Unit Test Definition Macro
+set(VALGRIND_FLAGS --quiet --trace-children=yes --leak-check=full)
 macro(add_test_valgrind TEST_NAME)
-    IF(UA_TEST_WITH_VALGRIND)
-        add_test(${TEST_NAME}
-                valgrind --error-exitcode=1 ${VALGRIND_FLAGS} ${ARGN} )
-    ELSE()
+    if(UA_ENABLE_VALGRIND_UNIT_TESTS)
+        add_test(${TEST_NAME} valgrind --error-exitcode=1 ${VALGRIND_FLAGS} ${ARGN})
+    else()
         add_test(${TEST_NAME} ${ARGN})
-    ENDIF()
+    endif()
 endmacro()
 
-
-
 # the unit test are built directly on the open62541 object files. so they can
 # access symbols that are hidden/not exported to the shared library
 
@@ -58,6 +57,12 @@ add_executable(check_chunking check_chunking.c $<TARGET_OBJECTS:open62541-object
 target_link_libraries(check_chunking ${LIBS})
 add_test_valgrind(chunking ${CMAKE_CURRENT_BINARY_DIR}/check_chunking)
 
+add_executable(check_utils check_utils.c $<TARGET_OBJECTS:open62541-object>)
+target_link_libraries(check_utils ${LIBS})
+add_test_valgrind(check_utils ${CMAKE_CURRENT_BINARY_DIR}/check_utils)
+
+# Test Server
+
 add_executable(check_services_view check_services_view.c $<TARGET_OBJECTS:open62541-object>)
 target_link_libraries(check_services_view ${LIBS})
 add_test_valgrind(services_view ${CMAKE_CURRENT_BINARY_DIR}/check_services_view)
@@ -90,7 +95,7 @@ add_executable(check_server_userspace check_server_userspace.c $<TARGET_OBJECTS:
 target_link_libraries(check_server_userspace ${LIBS})
 add_test_valgrind(check_server_userspace ${CMAKE_CURRENT_BINARY_DIR}/check_server_userspace)
 
-# test with canned interactions from files
+# Test server with network dumps from files
 
 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/client_HELOPN.bin
                           ${CMAKE_CURRENT_BINARY_DIR}/client_CLO.bin
@@ -139,6 +144,8 @@ add_test_valgrind(check_server_binary_messages_write ${CMAKE_CURRENT_BINARY_DIR}
                                            ${CMAKE_CURRENT_BINARY_DIR}/client_Write.bin
                                            ${CMAKE_CURRENT_BINARY_DIR}/client_CLO.bin)
 
+# Test Client
+
 add_executable(check_client check_client.c $<TARGET_OBJECTS:open62541-object>)
 target_link_libraries(check_client ${LIBS})
 add_test_valgrind(check_client ${CMAKE_CURRENT_BINARY_DIR}/check_client)
@@ -146,7 +153,3 @@ add_test_valgrind(check_client ${CMAKE_CURRENT_BINARY_DIR}/check_client)
 add_executable(check_client_subscriptions check_client_subscriptions.c $<TARGET_OBJECTS:open62541-object>)
 target_link_libraries(check_client_subscriptions ${LIBS})
 add_test_valgrind(check_client_subscriptions ${CMAKE_CURRENT_BINARY_DIR}/check_client_subscriptions)
-
-add_executable(check_utils check_utils.c $<TARGET_OBJECTS:open62541-object>)
-target_link_libraries(check_utils ${LIBS})
-add_test_valgrind(check_utils ${CMAKE_CURRENT_BINARY_DIR}/check_utils)

+ 18 - 0
tools/cmake/FindValgrind.cmake

@@ -0,0 +1,18 @@
+# Find Valgrind.
+#
+# This module defines:
+#  VALGRIND_INCLUDE_DIR, where to find valgrind/memcheck.h, etc.
+#  VALGRIND_PROGRAM, the valgrind executable.
+#  VALGRIND_FOUND, If false, do not try to use valgrind.
+#
+# If you have valgrind installed in a non-standard place, you can define
+# VALGRIND_PREFIX to tell cmake where it is.
+
+find_path(VALGRIND_INCLUDE_DIR memcheck.h
+  /usr/include /usr/include/valgrind /usr/local/include /usr/local/include/valgrind
+  ${VALGRIND_PREFIX}/include ${VALGRIND_PREFIX}/include/valgrind)
+find_program(VALGRIND_PROGRAM NAMES valgrind PATH /usr/bin /usr/local/bin ${VALGRIND_PREFIX}/bin)
+
+find_package_handle_standard_args(VALGRIND DEFAULT_MSG VALGRIND_INCLUDE_DIR VALGRIND_PROGRAM)
+
+mark_as_advanced(VALGRIND_INCLUDE_DIR VALGRIND_PROGRAM)

+ 2 - 1
tools/travis/travis_linux_script.sh

@@ -110,11 +110,12 @@ else
     make -j8
     cd .. && rm build -rf
 
-    #this run inclides full examples and methodcalls
     echo "Debug build and unit tests (64 bit)"
     mkdir -p build && cd build
     cmake -DCMAKE_BUILD_TYPE=Debug -DUA_BUILD_EXAMPLES=ON -DUA_BUILD_UNIT_TESTS=ON -DUA_ENABLE_COVERAGE=ON ..
     make -j8 && make test ARGS="-V"
+    cmake -DCMAKE_BUILD_TYPE=Debug -DUA_BUILD_EXAMPLES=ON -DUA_BUILD_UNIT_TESTS=ON -DUA_ENABLE_COVERAGE=ON -DUA_ENABLE_VALGRIND_UNIT_TESTS=ON ..
+    make -j8 && make test ARGS="-V"
     echo "Run valgrind to see if the server leaks memory (just starting up and closing..)"
     (valgrind --leak-check=yes --error-exitcode=3 ./examples/server & export pid=$!; sleep 2; kill -INT $pid; wait $pid);
     # only run coveralls on main repo, otherwise it fails uploading the files