From df3997142414b4a8543808e12572786e5834eb68 Mon Sep 17 00:00:00 2001
From: Eric Cano <Eric.Cano@cern.ch>
Date: Thu, 31 May 2018 11:25:53 +0200
Subject: [PATCH] Parallelized execution of valgrind tests.

---
 CMakeLists.txt                    | 42 ++++++++++++++++++++++++++
 cta.spec.in                       |  1 +
 tests/CMakeLists.txt              |  3 +-
 tests/cta-valgrindUnitTests.sh.in | 16 +---------
 tests/parallelTestsMakefile.in    | 49 +++++++++++++++++++++++++++++++
 5 files changed, 95 insertions(+), 16 deletions(-)
 create mode 100644 tests/parallelTestsMakefile.in

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3aa490729a..abc2ea9f00 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -225,3 +225,45 @@ add_custom_target(shortunittests
   
   DEPENDS tests/cta-unitTests tests/cta-unitTests-multiProcess
   COMMENT "Running unit tests" VERBATIM)
+
+add_custom_target(helgrindBase
+  echo ${VALGRIND} --tool=helgrind ${HELGRIND_OPTS_W_SUPPR} -q tests/cta-unitTests --gtest_filter=-OStoreDBPlusMockSchedulerTestVFS*:OStoreTestVFS*:OStoreDBPlusMockSchedulerTestVFS*:InMemory*
+  COMMAND ${VALGRIND} --tool=helgrind ${HELGRIND_OPTS_W_SUPPR} -q tests/cta-unitTests --gtest_filter=-OStoreDBPlusMockSchedulerTestVFS*:OStoreTestVFS*:OStoreDBPlusMockSchedulerTestVFS*:InMemory*
+  COMMAND echo ${VALGRIND} --tool=helgrind ${HELGRIND_OPTS_W_SUPPR} -q tests/cta-unitTests-multiProcess
+  COMMAND ${VALGRIND} --tool=helgrind ${HELGRIND_OPTS_W_SUPPR} -q tests/cta-unitTests-multiProcess
+  
+  DEPENDS tests/cta-unitTests tests/cta-unitTests-multiProcess tests/helgrind.suppr
+  COMMENT "Running unit tests with race conditions detection (except slow sections)" VERBATIM)
+
+add_custom_target(helgrindScheduler
+  echo ${VALGRIND} --tool=helgrind ${HELGRIND_OPTS_W_SUPPR} -q tests/cta-unitTests --gtest_filter=OStoreDBPlusMockSchedulerTestVFS/Scheduler*
+  COMMAND ${VALGRIND} --tool=helgrind ${HELGRIND_OPTS_W_SUPPR} -q tests/cta-unitTests --gtest_filter=OStoreDBPlusMockSchedulerTestVFS/Scheduler*
+  
+  DEPENDS tests/cta-unitTests tests/cta-unitTests-multiProcess tests/helgrind.suppr
+  COMMENT "Running unit tests with race conditions detection (Mock scheduler section)" VERBATIM)
+
+add_custom_target(helgrindOStoreDB
+  echo ${VALGRIND} --tool=helgrind ${HELGRIND_OPTS_W_SUPPR} -q tests/cta-unitTests --gtest_filter=OStoreTestVFS*
+  COMMAND ${VALGRIND} --tool=helgrind ${HELGRIND_OPTS_W_SUPPR} -q tests/cta-unitTests --gtest_filter=OStoreTestVFS*
+  
+  DEPENDS tests/cta-unitTests tests/cta-unitTests-multiProcess tests/helgrind.suppr
+  COMMENT "Running unit tests with race conditions detection (Object store DB section)" VERBATIM)
+
+add_custom_target(helgrindDataTransfer
+  echo ${VALGRIND} --tool=helgrind ${HELGRIND_OPTS_W_SUPPR} -q tests/cta-unitTests --gtest_filter=OStoreDBPlusMockSchedulerTestVFS/DataTransferSessionTest*
+  COMMAND ${VALGRIND} --tool=helgrind ${HELGRIND_OPTS_W_SUPPR} -q tests/cta-unitTests --gtest_filter=OStoreDBPlusMockSchedulerTestVFS/DataTransferSessionTest*
+  
+  DEPENDS tests/cta-unitTests tests/cta-unitTests-multiProcess tests/helgrind.suppr
+  COMMENT "Running unit tests with race conditions detection (Data transfer section)" VERBATIM)
+
+add_custom_target(helgrindInMemoryCatalogue
+  echo ${VALGRIND} --tool=helgrind ${HELGRIND_OPTS_W_SUPPR} -q tests/cta-unitTests --gtest_filter=InMemory/*
+  COMMAND ${VALGRIND} --tool=helgrind ${HELGRIND_OPTS_W_SUPPR} -q tests/cta-unitTests --gtest_filter=InMemory/*
+  
+  DEPENDS tests/cta-unitTests tests/cta-unitTests-multiProcess tests/helgrind.suppr
+  COMMENT "Running unit tests with race conditions detection (In memory catalogue section)" VERBATIM)
+
+add_custom_target(parallelHelgrind
+  echo
+  
+  DEPENDS helgrindBase helgrindMockSechduler helgrindOStoreDB helgrindDataTransfer helgrindInMemoryCatalogue)
diff --git a/cta.spec.in b/cta.spec.in
index aebb2a794a..fd6c6e198a 100644
--- a/cta.spec.in
+++ b/cta.spec.in
@@ -242,6 +242,7 @@ Unit tests and system tests with virtual tape drives
 %{_bindir}/cta-systemTests
 %{_libdir}/libctadaemonunittests-multiprocess.so*
 %attr(0644,root,root) %{_datadir}/%{name}-%{ctaVersion}/unittest/*.suppr
+%attr(0644,root,root) %{_datadir}/%{name}-%{ctaVersion}/unittest/parallelTestsMakefile
 
 %package -n cta-objectstore-tools
 Summary: CERN Tape Archive: object store tools
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 636959829f..60f6314790 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -108,6 +108,7 @@ install(TARGETS cta-catalogueUnitTests cta-unitTests cta-unitTests-multiProcess
 install(TARGETS systemTestHelperTests DESTINATION usr/${CMAKE_INSTALL_LIBDIR})
 
 configure_file(cta-valgrindUnitTests.sh.in ${CMAKE_CURRENT_BINARY_DIR}/cta-valgrindUnitTests.sh)
+configure_file(parallelTestsMakefile.in ${CMAKE_CURRENT_BINARY_DIR}/parallelTestsMakefile)
 
 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/cta-valgrindUnitTests.sh cta-unitPlusSystemTests.sh DESTINATION usr/bin)
-install(FILES valgrind.suppr helgrind.suppr DESTINATION usr/share/cta-${CTA_VERSION}/unittest/)
+install(FILES valgrind.suppr helgrind.suppr ${CMAKE_CURRENT_BINARY_DIR}/parallelTestsMakefile DESTINATION usr/share/cta-${CTA_VERSION}/unittest/)
diff --git a/tests/cta-valgrindUnitTests.sh.in b/tests/cta-valgrindUnitTests.sh.in
index 5464f62dc7..6fafb06382 100644
--- a/tests/cta-valgrindUnitTests.sh.in
+++ b/tests/cta-valgrindUnitTests.sh.in
@@ -1,20 +1,6 @@
 #!/bin/bash
 # exit from the script on any error.
 set -e
-/usr/bin/cta-unitTests
 
-/usr/bin/cta-unitTests-multiProcess
+make -f /usr/share/cta-@CTA_VERSION@/parallelTestsMakefile -j 4
 
-valgrind @VALGRIND_OPTS_STR@ --suppressions=/usr/share/cta-@CTA_VERSION@/unittest/valgrind.suppr     \
-  /usr/bin/cta-unitTests
-
-valgrind --tool=helgrind @HELGRIND_OPTS_STR@                                                         \
-  --suppressions=/usr/share/cta-@CTA_VERSION@/unittest/helgrind.suppr                                \
-  /usr/bin/cta-unitTests
-
-valgrind @VALGRIND_OPTS_STR@ --suppressions=/usr/share/cta-@CTA_VERSION@/unittest/valgrind.suppr     \
-  --child-silent-after-fork=yes /usr/bin/cta-unitTests-multiProcess
-
-valgrind --tool=helgrind @HELGRIND_OPTS_STR@                                                         \
-  --suppressions=/usr/share/cta-@CTA_VERSION@/unittest/helgrind.suppr                                \
-  /usr/bin/cta-unitTests-multiProcess
diff --git a/tests/parallelTestsMakefile.in b/tests/parallelTestsMakefile.in
new file mode 100644
index 0000000000..0cd82fa9a8
--- /dev/null
+++ b/tests/parallelTestsMakefile.in
@@ -0,0 +1,49 @@
+.phony: all
+
+all: valgrind valgrindMultiprocess helgrindBase helgrindScheduler helgrindOStoreDB helgrindDataTransfer helgrindInMemoryCatalogue helgrindMultiprocess
+	
+	
+valgrind:
+	valgrind -q @VALGRIND_OPTS_STR@                                               \
+	--suppressions=/usr/share/cta-@CTA_VERSION@/unittest/valgrind.suppr           \
+	/usr/bin/cta-unitTests
+
+valgrindMultiprocess:
+	valgrind -q @VALGRIND_OPTS_STR@ --child-silent-after-fork=yes                 \
+	--suppressions=/usr/share/cta-@CTA_VERSION@/unittest/valgrind.suppr           \
+	/usr/bin/cta-unitTests-multiProcess
+
+helgrindBase:
+	valgrind -q --tool=helgrind @HELGRIND_OPTS_STR@                               \
+	--suppressions=/usr/share/cta-@CTA_VERSION@/unittest/helgrind.suppr           \
+	/usr/bin/cta-unitTests                                                        \
+	--gtest_filter=-OStoreDBPlusMockSchedulerTestVFS*:OStoreTestVFS*:OStoreDBPlusMockSchedulerTestVFS*:InMemory*
+
+helgrindScheduler:
+	valgrind -q --tool=helgrind @HELGRIND_OPTS_STR@                               \
+	--suppressions=/usr/share/cta-@CTA_VERSION@/unittest/helgrind.suppr           \
+	/usr/bin/cta-unitTests                                                        \
+	--gtest_filter=OStoreDBPlusMockSchedulerTestVFS/Scheduler*
+
+helgrindOStoreDB:
+	valgrind -q --tool=helgrind @HELGRIND_OPTS_STR@                               \
+	--suppressions=/usr/share/cta-@CTA_VERSION@/unittest/helgrind.suppr           \
+	/usr/bin/cta-unitTests                                                        \
+	--gtest_filter=OStoreTestVFS*
+
+helgrindDataTransfer:
+	valgrind -q --tool=helgrind @HELGRIND_OPTS_STR@                               \
+	--suppressions=/usr/share/cta-@CTA_VERSION@/unittest/helgrind.suppr           \
+	/usr/bin/cta-unitTests                                                        \
+	--gtest_filter=OStoreDBPlusMockSchedulerTestVFS/DataTransferSessionTest*
+
+helgrindInMemoryCatalogue:
+	valgrind -q --tool=helgrind @HELGRIND_OPTS_STR@                               \
+	--suppressions=/usr/share/cta-@CTA_VERSION@/unittest/helgrind.suppr           \
+	/usr/bin/cta-unitTests                                                        \
+	--gtest_filter=InMemory/*
+
+helgrindMultiprocess:
+	valgrind -q --tool=helgrind @HELGRIND_OPTS_STR@                               \
+	--suppressions=/usr/share/cta-@CTA_VERSION@/unittest/helgrind.suppr           \
+	/usr/bin/cta-unitTests-multiProcess
\ No newline at end of file
-- 
GitLab