ci: introduce helper to preserve select env variables under sudo

Change-Id: I0217b8fbb005a43dddaf0010aa85130411d20813
Refs: #4610
diff --git a/.jenkins.d/10-build.sh b/.jenkins.d/10-build.sh
index fe2bfa0..875a908 100755
--- a/.jenkins.d/10-build.sh
+++ b/.jenkins.d/10-build.sh
@@ -21,7 +21,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 static library in optimized mode with tests
@@ -29,14 +29,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 static and shared library in optimized mode without tests
   ./waf --color=yes configure --enable-static --enable-shared $OPENSSL
   ./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 shared library in debug mode with tests/examples and without precompiled headers
@@ -47,7 +47,7 @@
 # (tests will be run against debug version)
 
 # Install
-sudo env "PATH=$PATH" ./waf --color=yes install
+sudo_preserve_env PATH -- ./waf --color=yes install
 
 if has Linux $NODE_LABELS; then
     sudo ldconfig
diff --git a/.jenkins.d/20-tests.sh b/.jenkins.d/20-tests.sh
index 00df3e5..ec4461b 100755
--- a/.jenkins.d/20-tests.sh
+++ b/.jenkins.d/20-tests.sh
@@ -40,5 +40,8 @@
 ASAN_OPTIONS+=":strip_path_prefix=${PWD}/"
 export ASAN_OPTIONS
 
+export BOOST_TEST_BUILD_INFO=1
+export BOOST_TEST_COLOR_OUTPUT=1
+
 # Run unit tests
 ./build/unit-tests $(ut_log_args)
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[@]}" "$@"
+}