]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - test/test-functions
systemctl: don't use get_process_comm() on non-local PIDs (#7518)
[thirdparty/systemd.git] / test / test-functions
index 61787b6d3398ad973762a77480d963f0b7eb7ff4..a2f82725d129a64ca364ab2b6ac44a411fb745c6 100644 (file)
@@ -4,25 +4,30 @@
 PATH=/sbin:/bin:/usr/sbin:/usr/bin
 export PATH
 
-LOOKS_LIKE_DEBIAN=$(source /etc/os-release && [[ "$ID" = "debian" || "$ID_LIKE" = "debian" ]] && echo yes)
-LOOKS_LIKE_ARCH=$(source /etc/os-release && [[ "$ID" = "arch" ]] && echo yes)
-LOOKS_LIKE_SUSE=$(source /etc/os-release && [[ "$ID_LIKE" = "suse" ]] && echo yes)
+LOOKS_LIKE_DEBIAN=$(source /etc/os-release && [[ "$ID" = "debian" || "$ID_LIKE" = "debian" ]] && echo yes || true)
+LOOKS_LIKE_ARCH=$(source /etc/os-release && [[ "$ID" = "arch" ]] && echo yes || true)
+LOOKS_LIKE_SUSE=$(source /etc/os-release && [[ "$ID_LIKE" = "suse" ]] && echo yes || true)
 KERNEL_VER=${KERNEL_VER-$(uname -r)}
 KERNEL_MODS="/lib/modules/$KERNEL_VER/"
 QEMU_TIMEOUT="${QEMU_TIMEOUT:-infinity}"
 NSPAWN_TIMEOUT="${NSPAWN_TIMEOUT:-infinity}"
 TIMED_OUT=  # will be 1 after run_* if *_TIMEOUT is set and test timed out
-[[ "$LOOKS_LIKE_SUSE" ]] && FSTYPE="${FSTYPE:-btrfs}" || FSTYPE="${FSTYPE:-ext3}"
+[[ "$LOOKS_LIKE_SUSE" ]] && FSTYPE="${FSTYPE:-btrfs}" || FSTYPE="${FSTYPE:-ext4}"
 UNIFIED_CGROUP_HIERARCHY="${UNIFIED_CGROUP_HIERARCHY:-default}"
+EFI_MOUNT="$(bootctl -p 2>/dev/null || echo /boot)"
 
 if ! ROOTLIBDIR=$(pkg-config --variable=systemdutildir systemd); then
     echo "WARNING! Cannot determine rootlibdir from pkg-config, assuming /usr/lib/systemd" >&2
     ROOTLIBDIR=/usr/lib/systemd
 fi
 
-BASICTOOLS="sh bash setsid loadkeys setfont login sulogin gzip sleep echo mount umount cryptsetup date dmsetup modprobe sed cmp tee rm"
+BASICTOOLS="test sh bash setsid loadkeys setfont login sulogin gzip sleep echo mount umount cryptsetup date dmsetup modprobe sed cmp tee rm true false"
 DEBUGTOOLS="df free ls stty cat ps ln ip route dmesg dhclient mkdir cp ping dhclient strace less grep id tty touch du sort hostname find"
 
+STATEDIR="${BUILD_DIR:-.}/test/$(basename $(dirname $(realpath $0)))"
+STATEFILE="$STATEDIR/.testdir"
+TESTLOG="$STATEDIR/test.log"
+
 function find_qemu_bin() {
     # SUSE and Red Hat call the binary qemu-kvm
     # Debian and Gentoo call it kvm
@@ -57,10 +62,10 @@ function find_qemu_bin() {
 run_qemu() {
     if [ -f /etc/machine-id ]; then
         read MACHINE_ID < /etc/machine-id
-        [ -z "$INITRD" ] && [ -e "/boot/$MACHINE_ID/$KERNEL_VER/initrd" ] \
-            && INITRD="/boot/$MACHINE_ID/$KERNEL_VER/initrd"
-        [ -z "$KERNEL_BIN" ] && [ -e "/boot/$MACHINE_ID/$KERNEL_VER/linux" ] \
-            && KERNEL_BIN="/boot/$MACHINE_ID/$KERNEL_VER/linux"
+        [ -z "$INITRD" ] && [ -e "$EFI_MOUNT/$MACHINE_ID/$KERNEL_VER/initrd" ] \
+            && INITRD="$EFI_MOUNT/$MACHINE_ID/$KERNEL_VER/initrd"
+        [ -z "$KERNEL_BIN" ] && [ -e "$EFI_MOUNT/$MACHINE_ID/$KERNEL_VER/linux" ] \
+            && KERNEL_BIN="$EFI_MOUNT/$MACHINE_ID/$KERNEL_VER/linux"
     fi
 
     if [[ ! "$KERNEL_BIN" ]]; then
@@ -422,8 +427,9 @@ install_execs() {
     systemduserunitdir=$(pkg-config --variable=systemduserunitdir systemd)
     egrep -ho '^Exec[^ ]*=[^ ]+' $initdir/{$systemdsystemunitdir,$systemduserunitdir}/*.service \
          | while read i; do
-         i=${i##Exec*=}; i=${i##-}
-         inst $i
+         i=${i##Exec*=}; i=${i##[@+\!-]}; i=${i##\!}
+         # some {rc,halt}.local scripts and programs are okay to not exist, the rest should
+         inst $i || [ "${i%.local}" != "$i" ] || [ "${i%systemd-update-done}" != "$i" ]
      done
     )
 }
@@ -458,15 +464,15 @@ install_ld_so_conf() {
 }
 
 install_config_files() {
-    inst /etc/sysconfig/init
+    inst /etc/sysconfig/init || true
     inst /etc/passwd
     inst /etc/shadow
     inst /etc/login.defs
     inst /etc/group
     inst /etc/shells
     inst /etc/nsswitch.conf
-    inst /etc/pam.conf
-    inst /etc/securetty
+    inst /etc/pam.conf || true
+    inst /etc/securetty || true
     inst /etc/os-release
     inst /etc/localtime
     # we want an empty environment
@@ -516,12 +522,12 @@ install_dbus() {
 
 install_pam() {
     (
-    [[ "$LOOKS_LIKE_DEBIAN" ]] && type -p dpkg-architecture &>/dev/null && find "/lib/$(dpkg-architecture -qDEB_HOST_MULTIARCH)/security" -xtype f
-    find \
-        /etc/pam.d \
-        /etc/security \
-        /lib64/security \
-        /lib/security -xtype f \
+    if [[ "$LOOKS_LIKE_DEBIAN" ]] && type -p dpkg-architecture &>/dev/null; then
+        find "/lib/$(dpkg-architecture -qDEB_HOST_MULTIARCH)/security" -xtype f
+    else
+        find /lib*/security -xtype f
+    fi
+    find /etc/pam.d /etc/security -xtype f
     ) | while read file; do
         inst $file
     done
@@ -626,7 +632,6 @@ inst_libs() {
 }
 
 import_testdir() {
-    STATEFILE=".testdir"
     [[ -e $STATEFILE ]] && . $STATEFILE
     if [[ -z "$TESTDIR" ]] || [[ ! -d "$TESTDIR" ]]; then
         TESTDIR=$(mktemp --tmpdir=/var/tmp -d -t systemd-test.XXXXXX)
@@ -920,7 +925,7 @@ inst_library() {
 
     # Create additional symlinks.  See rev_symlinks description.
     for _symlink in $(rev_lib_symlinks $_src) $(rev_lib_symlinks $_reallib); do
-        [[ ! -e $initdir/$_symlink ]] && {
+        [[ -e $initdir/$_symlink ]] || {
             ddebug "Creating extra symlink: $_symlink"
             inst_symlink $_symlink
         }
@@ -1384,7 +1389,7 @@ setup_suse() {
 # can be overridden in specific test
 test_cleanup() {
     umount $TESTDIR/root 2>/dev/null || true
-    [[ $LOOPDEV ]] && losetup -d $LOOPDEV
+    [[ $LOOPDEV ]] && losetup -d $LOOPDEV || true
     return 0
 }
 
@@ -1421,6 +1426,8 @@ do_test() {
         [[ -d $usrlibdir ]] && libdirs+=" $usrlibdir" && break
     done
 
+    mkdir -p "$STATEDIR"
+
     import_testdir
     import_initdir
 
@@ -1428,9 +1435,7 @@ do_test() {
         case $1 in
             --run)
                 echo "TEST RUN: $TEST_DESCRIPTION"
-                test_run
-                ret=$?
-                if [ $ret -eq 0 ]; then
+                if test_run; then
                     echo "TEST RUN: $TEST_DESCRIPTION [OK]"
                 else
                     echo "TEST RUN: $TEST_DESCRIPTION [FAILED]"
@@ -1439,30 +1444,30 @@ do_test() {
             --setup)
                 echo "TEST SETUP: $TEST_DESCRIPTION"
                 test_setup
-                exit $?;;
+                ;;
             --clean)
                 echo "TEST CLEANUP: $TEST_DESCRIPTION"
                 test_cleanup
                 rm -fr "$TESTDIR"
-                rm -f .testdir
-                exit $?;;
+                rm -f "$STATEFILE"
+                ;;
             --all)
+                ret=0
                 echo -n "TEST: $TEST_DESCRIPTION ";
                 (
                     test_setup && test_run
                     ret=$?
                     test_cleanup
                     rm -fr "$TESTDIR"
-                    rm -f .testdir
+                    rm -f "$STATEFILE"
                     exit $ret
-                ) </dev/null >test.log 2>&1
-                ret=$?
+                ) </dev/null >"$TESTLOG" 2>&1 || ret=$?
                 if [ $ret -eq 0 ]; then
-                    rm test.log
+                    rm "$TESTLOG"
                     echo "[OK]"
                 else
                     echo "[FAILED]"
-                    echo "see $(pwd)/test.log"
+                    echo "see $TESTLOG"
                 fi
                 exit $ret;;
             *) break ;;