From 642c5089a48f9d0e09a7d13245473b0b76332a53 Mon Sep 17 00:00:00 2001
From: "lintao@ihep.ac.cn" <lintao@ihep.ac.cn>
Date: Sat, 15 Jun 2024 11:34:39 +0000
Subject: [PATCH] Enable ctest in CI

---
 .build.ci.sh                                  | 62 ++++++++++++-------
 .gitlab-ci.yml                                | 34 +++++-----
 .test.ci.sh                                   | 46 ++++++++++++++
 Detector/DetCRD/CMakeLists.txt                | 16 +++++
 .../DetCRD/scripts/TDR_o1_v01/tracking.py     |  3 +-
 build.sh                                      |  4 +-
 6 files changed, 124 insertions(+), 41 deletions(-)
 mode change 100644 => 100755 .build.ci.sh
 create mode 100755 .test.ci.sh

diff --git a/.build.ci.sh b/.build.ci.sh
old mode 100644
new mode 100755
index 6e4c3265..3710b9a6
--- a/.build.ci.sh
+++ b/.build.ci.sh
@@ -1,31 +1,51 @@
 #!/bin/bash
 # This is wrapper to run the build.sh on CI
 
-echo "LCG_RELEASE: ${LCG_RELEASE}"
+echo "CEPCSW_LCG_RELEASE: ${CEPCSW_LCG_RELEASE}"
+echo "CEPCSW_LCG_PLATFORM: ${CEPCSW_LCG_PLATFORM}"
+echo "CEPCSW_LCG_VERSION: ${CEPCSW_LCG_VERSION}"
 echo "CEPCSW_BLDTOOL: ${CEPCSW_BLDTOOL}"
-buildpid=
-logfile=mylog.txt
 
-if [ "$LCG_RELEASE" = "KEY4HEP_STACK" ]; then
-    logfile=mylog-k4.sh
-    ./build-k4.sh >& ${logfile} &
-    buildpid=$!
-else
-    source setup.sh
-    ./build.sh >& ${logfile} &
-    buildpid=$!
-fi
+function build-with-log() {
+    buildpid=
+    logfile=mylog.txt
+
+    if [ "$CEPCSW_LCG_RELEASE" = "KEY4HEP_STACK" ]; then
+        logfile=mylog-k4.sh
+        ./build-k4.sh >& ${logfile} &
+        buildpid=$!
+    else
+        source setup.sh
+        ./build.sh >& ${logfile} &
+        buildpid=$!
+    fi
+
+    while ps -p $buildpid 2>/dev/null ; do
+        sleep 60
+    done &
+    echoer=$!
 
-while ps -p $buildpid 2>/dev/null ; do
-    sleep 60
-done &
-echoer=$!
+    trap 'kill $echoer' 0
 
-trap 'kill $echoer' 0
+    wait $buildpid
+    statuspid=$?
 
-wait $buildpid
-statuspid=$?
+    tail -n100 ${logfile}
 
-tail -n100 ${logfile}
+    exit $statuspid
+}
 
-exit $statuspid
+function build-with-stdout() {
+    if [ "$CEPCSW_LCG_RELEASE" = "KEY4HEP_STACK" ]; then
+        ./build-k4.sh
+    else
+        source setup.sh
+        ./build.sh
+    fi
+}
+
+if [ -n "${GITHUB_ACTION}" ]; then
+    build-with-log
+else
+    build-with-stdout
+fi
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index efd28435..9064c2ac 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -26,19 +26,23 @@ workflow:
 
 stages:
   - build
-  # - test
 
 ##############################################################################
-# Build Template
+# Template for Build and Test
 ##############################################################################
+# Due to cmake/ctest will hardcode the path in build directory,
+# the test job will be failed if it is executed on a different nodes.
+# Therefore, put the build script and test script together.
+
 .build_template:
   stage: build
   variables:
-    LCG_RELEASE:
-    CEPCSW_BLDTOOL: ninja
+    CEPCSW_LCG_RELEASE:
+    CEPCSW_LCG_PLATFORM:
+    CEPCSW_LCG_VERSION:
   script:
     - bash ./.build.ci.sh
-
+    - bash ./.test.ci.sh
 
 ##############################################################################
 # Build CentOS 7 (LCG)
@@ -46,17 +50,13 @@ stages:
 build:lcg:el7:
   extends: .build_template
   variables:
-    LCG_RELEASE: LCG
+    CEPCSW_LCG_RELEASE: LCG
+    CEPCSW_LCG_PLATFORM: x86_64-centos7-gcc11-opt
+    CEPCSW_LCG_VERSION: 103.0.2
   tags:
     - centos7
-
-##############################################################################
-# Build CentOS 7 (KEY4HEP)
-##############################################################################
-# build:k4:el7:
-#   extends: .build_template
-#   variables:
-#     LCG_RELEASE: KEY4HEP_STACK
-#   tags:
-#     - centos7
-
+  artifacts:
+    paths:
+      - InstallArea
+    reports:
+      junit: build.${CEPCSW_LCG_VERSION}.${CEPCSW_LCG_PLATFORM}/cepcsw-ctest-result.xml
diff --git a/.test.ci.sh b/.test.ci.sh
new file mode 100755
index 00000000..45f07baf
--- /dev/null
+++ b/.test.ci.sh
@@ -0,0 +1,46 @@
+#!/bin/bash
+# Description:
+#   Run the tests using ctest
+#
+# Author:
+#   Tao Lin <lintao AT ihep.ac.cn>
+
+##############################################################################
+# Utilities
+##############################################################################
+function build-dir() {
+    local blddir=build
+
+    if [ -n "${CEPCSW_BLDTOOL}" ]; then
+        blddir=${blddir}.${CEPCSW_BLDTOOL}
+    fi
+
+    # If detect the extra env var, append it to the build dir
+    if [ -n "${CEPCSW_LCG_VERSION}" ]; then
+        blddir=${blddir}.${CEPCSW_LCG_VERSION}
+    fi
+    if [ -n "${CEPCSW_LCG_PLATFORM}" ]; then
+        blddir=${blddir}.${CEPCSW_LCG_PLATFORM}
+    fi
+
+    echo $blddir
+}
+
+function junit-output() {
+    local default=cepcsw-ctest-result.xml
+    echo ${CEPCSW_JUNIT_OUTPUT:-$default}
+}
+
+##############################################################################
+# Main
+##############################################################################
+
+
+echo "CEPCSW_LCG_RELEASE: ${CEPCSW_LCG_RELEASE}"
+echo "CEPCSW_LCG_PLATFORM: ${CEPCSW_LCG_PLATFORM}"
+echo "CEPCSW_LCG_VERSION: ${CEPCSW_LCG_VERSION}"
+echo "CEPCSW_BLDTOOL: ${CEPCSW_BLDTOOL}"
+
+source setup.sh
+
+ctest --output-junit $(junit-output) --test-dir $(build-dir)
diff --git a/Detector/DetCRD/CMakeLists.txt b/Detector/DetCRD/CMakeLists.txt
index 836cf2bf..f7628431 100644
--- a/Detector/DetCRD/CMakeLists.txt
+++ b/Detector/DetCRD/CMakeLists.txt
@@ -42,3 +42,19 @@ install(TARGETS DetCRD
   RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT bin
   LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT shlib
   COMPONENT dev)
+
+################################################################################
+# Add tests
+################################################################################
+
+add_test(
+  NAME Test_TDR_o1_v01_Sim
+  COMMAND gaudirun.py Detector/DetCRD/scripts/TDR_o1_v01/sim.py
+  WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
+)
+
+add_test(
+  NAME Test_TDR_o1_v01_Rec
+  COMMAND gaudirun.py Detector/DetCRD/scripts/TDR_o1_v01/tracking.py
+  WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
+)
diff --git a/Detector/DetCRD/scripts/TDR_o1_v01/tracking.py b/Detector/DetCRD/scripts/TDR_o1_v01/tracking.py
index 5ce28166..2e76ba55 100644
--- a/Detector/DetCRD/scripts/TDR_o1_v01/tracking.py
+++ b/Detector/DetCRD/scripts/TDR_o1_v01/tracking.py
@@ -41,7 +41,8 @@ tracksystemsvc = TrackSystemSvc("TrackSystemSvc")
 
 from Configurables import SimplePIDSvc
 pidsvc = SimplePIDSvc("SimplePIDSvc")
-pidsvc.ParFile = "/cefs/higgs/zhaog/dndx_files/dNdx_TPC.root"
+cepcswdatatop = "/cvmfs/cepcsw.ihep.ac.cn/prototype/releases/data/latest"
+pidsvc.ParFile = os.path.join(cepcswdatatop, "CEPCSWData/offline-data/Service/SimplePIDSvc/data/dNdx_TPC.root")
 
 from Configurables import PodioInput
 podioinput = PodioInput("PodioReader", collections=[
diff --git a/build.sh b/build.sh
index 5c7393d1..85934189 100755
--- a/build.sh
+++ b/build.sh
@@ -96,8 +96,8 @@ function run-install() {
 ##############################################################################
 
 # The current default platform
-lcg_platform=x86_64-centos7-gcc11-opt
-lcg_version=103.0.2
+lcg_platform=${CEPCSW_LCG_PLATFORM:-x86_64-centos7-gcc11-opt}
+lcg_version=${CEPCSW_LCG_VERSION:-103.0.2}
 
 bldtool=${CEPCSW_BLDTOOL} # make, ninja # set in env var
 
-- 
GitLab