]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: allow overriding PID1's environment for test purposes
authorFrantisek Sumsal <frantisek@sumsal.cz>
Fri, 14 Apr 2023 10:58:16 +0000 (12:58 +0200)
committerFrantisek Sumsal <frantisek@sumsal.cz>
Fri, 14 Apr 2023 10:58:16 +0000 (12:58 +0200)
test/units/generator-utils.sh

index 8f502d00ccc30c48ea16c3879f1126fb34ad19c8..035fd55b0d0f05446e65142c97cf189b54f10de3 100644 (file)
@@ -54,8 +54,26 @@ opt_filter_consumed() {(
 run_and_list() {
     local generator="${1:?}"
     local out_dir="${2:?}"
+    local environ
+
+    # If $PID1_ENVIRON is set temporarily overmount /proc/1/environ with
+    # a temporary file that contains contents of $PID1_ENVIRON. This is
+    # necessary in cases where the generator reads the environment through
+    # getenv_for_pid(1, ...) or similar like getty-generator does.
+    #
+    # Note: $PID1_ENVIRON should be a NUL separated list of env assignments
+    if [[ -n "${PID1_ENVIRON:-}" ]]; then
+        environ="$(mktemp)"
+        echo -ne "${PID1_ENVIRON}\0" >"${environ:?}"
+        mount -v --bind "$environ" /proc/1/environ
+    fi
 
     rm -fr "${out_dir:?}"/*
     SYSTEMD_LOG_LEVEL="${SYSTEMD_LOG_LEVEL:-debug}" "$generator" "$out_dir"
     ls -lR "$out_dir"
+
+    if [[ -n "${environ:-}" ]]; then
+        umount /proc/1/environ
+        rm -f "$environ"
+    fi
 }