ci: introduce helper to preserve select env variables under sudo

Change-Id: Ia5db4b50e07a40c2eb256182a721c990c5b53382
Refs: #4610
diff --git a/.jenkins.d/01-ndn-cxx.sh b/.jenkins.d/01-ndn-cxx.sh
index c9e2a63..29a41ea 100755
--- a/.jenkins.d/01-ndn-cxx.sh
+++ b/.jenkins.d/01-ndn-cxx.sh
@@ -44,7 +44,7 @@
 
 ./waf configure --color=yes --enable-shared --disable-static --without-osx-keychain
 ./waf build --color=yes -j${WAF_JOBS:-1}
-sudo env "PATH=$PATH" ./waf install --color=yes
+sudo_preserve_env PATH -- ./waf install --color=yes
 
 popd >/dev/null
 popd >/dev/null
diff --git a/.jenkins.d/10-build.sh b/.jenkins.d/10-build.sh
index d6f9e37..cfa32f8 100755
--- a/.jenkins.d/10-build.sh
+++ b/.jenkins.d/10-build.sh
@@ -17,7 +17,7 @@
 fi
 
 # Cleanup
-sudo env "PATH=$PATH" ./waf --color=yes distclean
+sudo_preserve_env PATH -- ./waf --color=yes distclean
 
 if [[ $JOB_NAME != *"code-coverage" && $JOB_NAME != *"limited-build" ]]; then
   # Configure/build in optimized mode with tests
@@ -25,14 +25,14 @@
   ./waf --color=yes build -j${WAF_JOBS:-1}
 
   # Cleanup
-  sudo env "PATH=$PATH" ./waf --color=yes distclean
+  sudo_preserve_env PATH -- ./waf --color=yes distclean
 
   # Configure/build in optimized mode without tests, but with "other tests"
   ./waf --color=yes configure --with-other-tests
   ./waf --color=yes build -j${WAF_JOBS:-1}
 
   # Cleanup
-  sudo env "PATH=$PATH" ./waf --color=yes distclean
+  sudo_preserve_env PATH -- ./waf --color=yes distclean
 fi
 
 # Configure/build in debug mode with tests and without precompiled headers
@@ -42,4 +42,4 @@
 # (tests will be run against debug version)
 
 # Install
-sudo env "PATH=$PATH" ./waf --color=yes install
+sudo_preserve_env PATH -- ./waf --color=yes install
diff --git a/.jenkins.d/20-tests.sh b/.jenkins.d/20-tests.sh
index 76d0d34..151172f 100755
--- a/.jenkins.d/20-tests.sh
+++ b/.jenkins.d/20-tests.sh
@@ -41,6 +41,9 @@
 ASAN_OPTIONS+=":strip_path_prefix=${PWD}/"
 export ASAN_OPTIONS
 
+export BOOST_TEST_BUILD_INFO=1
+export BOOST_TEST_COLOR_OUTPUT=1
+
 # First run all tests as unprivileged user
 ./build/unit-tests-core $(ut_log_args core)
 ./build/unit-tests-daemon $(ut_log_args daemon)
@@ -48,7 +51,11 @@
 ./build/unit-tests-tools $(ut_log_args tools)
 
 # Then use sudo to run those tests that need superuser powers
-sudo -E ./build/unit-tests-core -t TestPrivilegeHelper $(ut_log_args core-privilege)
-sudo -E ./build/unit-tests-daemon -t Face/*Ethernet* $(ut_log_args daemon-ethernet)
-sudo -E ./build/unit-tests-daemon -t Face/TestUdpFactory $(ut_log_args daemon-udp-factory)
-sudo -E ./build/unit-tests-daemon -t Mgmt/TestGeneralConfigSection/UserAndGroupConfig,NoUserConfig $(ut_log_args daemon-user-config)
+sudo_preserve_env ASAN_OPTIONS BOOST_TEST_COLOR_OUTPUT -- \
+    ./build/unit-tests-core -t TestPrivilegeHelper $(ut_log_args core-privilege)
+sudo_preserve_env ASAN_OPTIONS BOOST_TEST_COLOR_OUTPUT -- \
+    ./build/unit-tests-daemon -t Face/*Ethernet* $(ut_log_args daemon-ethernet)
+sudo_preserve_env ASAN_OPTIONS BOOST_TEST_COLOR_OUTPUT -- \
+    ./build/unit-tests-daemon -t Face/TestUdpFactory $(ut_log_args daemon-udp-factory)
+sudo_preserve_env ASAN_OPTIONS BOOST_TEST_COLOR_OUTPUT -- \
+    ./build/unit-tests-daemon -t Mgmt/TestGeneralConfigSection/UserAndGroupConfig,NoUserConfig $(ut_log_args daemon-user-config)
diff --git a/.jenkins.d/util.sh b/.jenkins.d/util.sh
index a89bc27..8ddc4ba 100644
--- a/.jenkins.d/util.sh
+++ b/.jenkins.d/util.sh
@@ -16,3 +16,22 @@
     set ${saved_xtrace}
     return ${ret}
 }
+
+sudo_preserve_env() {
+    local saved_xtrace
+    [[ $- == *x* ]] && saved_xtrace=-x || saved_xtrace=+x
+    set +x
+
+    local vars=()
+    while [[ $# -gt 0 ]]; do
+        local arg=$1
+        shift
+        case ${arg} in
+            --) break ;;
+            *)  vars+=("${arg}=${!arg}") ;;
+        esac
+    done
+
+    set ${saved_xtrace}
+    sudo env "${vars[@]}" "$@"
+}