tests: Capture the output of netopeer2-server

Change-Id: I78c097db22ab360718a9d128d81ce9ad32353071
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 92ab37f..be7f96e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -270,6 +270,7 @@
 
         file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test_repositories)
         file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test_sockets)
+        file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test_netopeer_outputs)
     endif()
 
     configure_file(${CMAKE_CURRENT_SOURCE_DIR}/tests/yang_access_test_vars.hpp.in ${CMAKE_CURRENT_BINARY_DIR}/yang_access_test_vars.hpp @ONLY)
diff --git a/ci/build.sh b/ci/build.sh
index d6b75f4..3dacb5f 100755
--- a/ci/build.sh
+++ b/ci/build.sh
@@ -91,7 +91,10 @@
     rm ${PREFIX}/bin/sysrepo*
 fi
 
+mkdir ~/zuul-output/logs/test_netopeer_outputs
+
 cd ${BUILD_DIR}
+ln -s ~/zuul-output/logs/test_netopeer_outputs test_netopeer_outputs
 cmake -GNinja -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE:-Debug} -DCMAKE_INSTALL_PREFIX=${PREFIX} ${CMAKE_OPTIONS} ${ZUUL_PROJECT_SRC_DIR}
 ninja-build
 ctest -j${CI_PARALLEL_JOBS} --output-on-failure
diff --git a/tests/init_datastore.bash.in b/tests/init_datastore.bash.in
index 5aec0c0..2664a4b 100755
--- a/tests/init_datastore.bash.in
+++ b/tests/init_datastore.bash.in
@@ -37,7 +37,13 @@
         "$SYSREPOCFG" --import="@CMAKE_CURRENT_SOURCE_DIR@/tests/disable-nacm.xml" --datastore="$datastore" --format=xml --module=ietf-netconf-acm
     done
 
-    # Run netopeer. Use exec -a, so that each process has a recognizable name for `pkill`.
-    (exec -a "${SYSREPO_SHM_PREFIX}_netopeer2-server" "$NETOPEER2" -v2 "-U$NETOPEER_SOCKET")
+    # I need to run netopeer in the foreground, because I want to get its standard outputs.  However, ctest doesn't like
+    # when child processes don't exit and always waits fot them.  The `setsid` command runs a program in a separate
+    # session and also forks, which means it becomes invisible to ctest.  I want the name of the process to be
+    # recognizable. For that `exec -a` can be used. Unfortunately `exec` is shell-builtin, so I need to run bash instead
+    # of running `exec` directly. Finally, I redirect its stdout and stderr to a file.
+    setsid -f \
+        bash -c "exec -a '${SYSREPO_SHM_PREFIX}_netopeer2-server' '$NETOPEER2' -d -v2 '-U$NETOPEER_SOCKET'" \
+        < /dev/null &> "@CMAKE_CURRENT_BINARY_DIR@/test_netopeer_outputs/$SYSREPO_SHM_PREFIX.out"
     sleep 5
 fi