4bff0a
From 91bd0b915aa17f44625a0c0ce84ed73b6d33968e Mon Sep 17 00:00:00 2001
4bff0a
From: Evgeny Vereshchagin <evvers@ya.ru>
4bff0a
Date: Thu, 30 Aug 2018 07:01:18 +0300
4bff0a
Subject: [PATCH] tests: also run TEST-01-BASIC in an unprivileged container
4bff0a
 (#9957)
4bff0a
4bff0a
This should make it much easier to catch regressions like
4bff0a
https://github.com/systemd/systemd/issues/9914 and
4bff0a
https://github.com/systemd/systemd/issues/8535.
4bff0a
4bff0a
(cherry picked from commit 746fbd9c34af5ed8b6d9aa7a9cbd7cac63a3afce)
4bff0a
---
4bff0a
 test/TEST-01-BASIC/test.sh |  1 +
4bff0a
 test/test-functions        | 23 +++++++++++++++++------
4bff0a
 2 files changed, 18 insertions(+), 6 deletions(-)
4bff0a
4bff0a
diff --git a/test/TEST-01-BASIC/test.sh b/test/TEST-01-BASIC/test.sh
4bff0a
index 8b21ba05d3..1d2f833478 100755
4bff0a
--- a/test/TEST-01-BASIC/test.sh
4bff0a
+++ b/test/TEST-01-BASIC/test.sh
4bff0a
@@ -3,6 +3,7 @@
4bff0a
 # ex: ts=8 sw=4 sts=4 et filetype=sh
4bff0a
 set -e
4bff0a
 TEST_DESCRIPTION="Basic systemd setup"
4bff0a
+RUN_IN_UNPRIVILEGED_CONTAINER=yes
4bff0a
 
4bff0a
 . $TEST_BASE_DIR/test-functions
4bff0a
 
4bff0a
diff --git a/test/test-functions b/test/test-functions
4bff0a
index 8164858c2a..670c2625f7 100644
4bff0a
--- a/test/test-functions
4bff0a
+++ b/test/test-functions
4bff0a
@@ -190,7 +190,7 @@ $KERNEL_APPEND \
4bff0a
 run_nspawn() {
4bff0a
     [[ -d /run/systemd/system ]] || return 1
4bff0a
 
4bff0a
-    local _nspawn_cmd="$BUILD_DIR/systemd-nspawn $NSPAWN_ARGUMENTS --register=no --kill-signal=SIGKILL --directory=$TESTDIR/nspawn-root $PATH_TO_INIT $KERNEL_APPEND"
4bff0a
+    local _nspawn_cmd="$BUILD_DIR/systemd-nspawn $NSPAWN_ARGUMENTS --register=no --kill-signal=SIGKILL --directory=$TESTDIR/$1 $PATH_TO_INIT $KERNEL_APPEND"
4bff0a
     if [[ "$NSPAWN_TIMEOUT" != "infinity" ]]; then
4bff0a
         _nspawn_cmd="timeout --foreground $NSPAWN_TIMEOUT $_nspawn_cmd"
4bff0a
     fi
4bff0a
@@ -450,9 +450,9 @@ EOF
4bff0a
 
4bff0a
 check_result_nspawn() {
4bff0a
     ret=1
4bff0a
-    [[ -e $TESTDIR/nspawn-root/testok ]] && ret=0
4bff0a
-    [[ -f $TESTDIR/nspawn-root/failed ]] && cp -a $TESTDIR/nspawn-root/failed $TESTDIR
4bff0a
-    cp -a $TESTDIR/nspawn-root/var/log/journal $TESTDIR
4bff0a
+    [[ -e $TESTDIR/$1/testok ]] && ret=0
4bff0a
+    [[ -f $TESTDIR/$1/failed ]] && cp -a $TESTDIR/$1/failed $TESTDIR
4bff0a
+    cp -a $TESTDIR/$1/var/log/journal $TESTDIR
4bff0a
     [[ -f $TESTDIR/failed ]] && cat $TESTDIR/failed
4bff0a
     ls -l $TESTDIR/journal/*/*.journal
4bff0a
     test -s $TESTDIR/failed && ret=$(($ret+1))
4bff0a
@@ -662,6 +662,9 @@ setup_nspawn_root() {
4bff0a
     cp -ar $initdir $TESTDIR/nspawn-root
4bff0a
     # we don't mount in the nspawn root
4bff0a
     rm -f $TESTDIR/nspawn-root/etc/fstab
4bff0a
+    if [[ "$RUN_IN_UNPRIVILEGED_CONTAINER" = "yes" ]]; then
4bff0a
+        cp -ar $TESTDIR/nspawn-root $TESTDIR/unprivileged-nspawn-root
4bff0a
+    fi
4bff0a
 }
4bff0a
 
4bff0a
 setup_basic_dirs() {
4bff0a
@@ -1478,11 +1481,19 @@ test_run() {
4bff0a
         fi
4bff0a
     fi
4bff0a
     if [ -z "$TEST_NO_NSPAWN" ]; then
4bff0a
-        if run_nspawn; then
4bff0a
-            check_result_nspawn || return 1
4bff0a
+        if run_nspawn "nspawn-root"; then
4bff0a
+            check_result_nspawn "nspawn-root" || return 1
4bff0a
         else
4bff0a
             dwarn "can't run systemd-nspawn, skipping"
4bff0a
         fi
4bff0a
+
4bff0a
+        if [[ "$RUN_IN_UNPRIVILEGED_CONTAINER" = "yes" ]]; then
4bff0a
+            if NSPAWN_ARGUMENTS="-U --private-network $NSPAWN_ARGUMENTS" run_nspawn "unprivileged-nspawn-root"; then
4bff0a
+                check_result_nspawn "unprivileged-nspawn-root" || return 1
4bff0a
+            else
4bff0a
+                dwarn "can't run systemd-nspawn, skipping"
4bff0a
+            fi
4bff0a
+	fi
4bff0a
     fi
4bff0a
     return 0
4bff0a
 }