test-functions revision 3f5c306d3810673364f8eae662f3735ffd645a1d
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# ex: ts=8 sw=4 sts=4 et filetype=sh
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart PoetteringLOOKS_LIKE_DEBIAN=$(source /etc/os-release && [[ "$ID" = "debian" || "$ID_LIKE" = "debian" ]] && echo yes)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringif ! ROOTLIBDIR=$(pkg-config --variable=systemdutildir systemd); then
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering echo "WARNING! Cannot determine rootlibdir from pkg-config, assuming /usr/lib/systemd" >&2
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart PoetteringBASICTOOLS="sh bash setsid loadkeys setfont login sulogin gzip sleep echo mount umount cryptsetup date dmsetup modprobe"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart PoetteringDEBUGTOOLS="df free ls stty cat ps ln ip route dmesg dhclient mkdir cp ping dhclient strace less grep id tty touch du sort hostname"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering # SUSE and Red Hat call the binary qemu-kvm
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a kvm qemu-kvm 2>/dev/null | grep '^/' -m1)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering # QEMU's own build system calls it qemu-system-x86_64
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu-system-x86_64 2>/dev/null | grep '^/' -m1)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu-system-i386 2>/dev/null | grep '^/' -m1)
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu 2>/dev/null | grep '^/' -m1)
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering echo "Could not find a suitable QEMU binary" >&2
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering if [ -f /etc/machine-id ]; then
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering [ -z "$INITRD" ] && [ -e "/boot/$MACHINE_ID/$KERNEL_VER/initrd" ] \
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering && INITRD="/boot/$MACHINE_ID/$KERNEL_VER/initrd"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering [ -z "$KERNEL_BIN" ] && [ -e "/boot/$MACHINE_ID/$KERNEL_VER/linux" ] \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering && KERNEL_BIN="/boot/$MACHINE_ID/$KERNEL_VER/linux"
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering [ "$KERNEL_BIN" ] || KERNEL_BIN=/boot/vmlinuz-$KERNEL_VER
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering [ "$INITRD" ] || INITRD=/boot/initramfs-${KERNEL_VER}.img
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringraid=noautodetect \
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringconsole=ttyS0 \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering QEMU_OPTIONS="$QEMU_OPTIONS -initrd $INITRD"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if [ -c /dev/kvm ]; then
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering QEMU_OPTIONS="$QEMU_OPTIONS -machine accel=kvm -enable-kvm -cpu host"
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering $QEMU_BIN $QEMU_OPTIONS -append "$KERNEL_APPEND" $TESTDIR/rootdisk.img ) || return 1
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering ../../systemd-nspawn --boot --directory=$TESTDIR/nspawn-root $ROOTLIBDIR/systemd $KERNEL_APPEND
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering ln -fs /proc/self/mounts $initdir/etc/mtab
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering type -P dmeventd >/dev/null && dracut_install dmeventd
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering inst_libdir_file "libdevmapper-event.so*"
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering inst_rules 10-dm.rules 13-dm-disk.rules 95-dm-notify.rules
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering (cd $TEST_BASE_DIR/..; set -x; make DESTDIR=$initdir install)
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering # we strip binaries since debug symbols increase binaries size a lot
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering for i in $initdir/{sbin,bin}/* $initdir/lib/systemd/*; do
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering # Create the blank file to use as a root filesystem
962225baa862c2023790b3c7f140d22bac9e801fThomas Hindoe Paaboel Andersen dd if=/dev/null of="$TESTDIR/rootdisk.img" bs=1M seek=400
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering LOOPDEV=$(losetup --show -P -f $TESTDIR/rootdisk.img)
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering [[ -e $TESTDIR/nspawn-root/testok ]] && ret=0
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering [[ -f $TESTDIR/nspawn-root/failed ]] && cp -a $TESTDIR/nspawn-root/failed $TESTDIR
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering cp -a $TESTDIR/nspawn-root/var/log/journal $TESTDIR
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering [[ -f $TESTDIR/failed ]] && cat $TESTDIR/failed
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering test -s $TESTDIR/failed && ret=$(($ret+1))
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering find "$initdir" -executable -not -path '*/lib/modules/*.ko' -type f | xargs strip --strip-unneeded | ddebug
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering ddebug "install any Execs from the service files"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering export PKG_CONFIG_PATH=$TEST_BASE_DIR/../src/core/
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering systemdsystemunitdir=$(pkg-config --variable=systemdsystemunitdir systemd)
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering systemduserunitdir=$(pkg-config --variable=systemduserunitdir systemd)
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering egrep -ho '^Exec[^ ]*=[^ ]+' $initdir/{$systemdsystemunitdir,$systemduserunitdir}/*.service \
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering | while read i; do
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering if [[ -d $initdir/lib/modules/$KERNEL_VER ]] && \
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering ! depmod -a -b "$initdir" $KERNEL_VER; then
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering dfatal "\"depmod -a $KERNEL_VER\" failed."
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering inst /lib/modules/$KERNEL_VER/modules.order
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering inst /lib/modules/$KERNEL_VER/modules.builtin
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering # install plymouth, if found... else remove plymouth service files
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering # if [ -x /usr/libexec/plymouth/plymouth-populate-initrd ]; then
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering # PLYMOUTH_POPULATE_SOURCE_FUNCTIONS="$TEST_BASE_DIR/test-functions" \
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering # /usr/libexec/plymouth/plymouth-populate-initrd -t $initdir
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering rm -f $initdir/{usr/lib,etc}/systemd/system/plymouth* $initdir/{usr/lib,etc}/systemd/system/*/plymouth*
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering echo systemd-testsuite > $initdir/etc/hostname
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart PoetteringLABEL=systemd / ext3 rw 0 1
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering [[ $BASICTOOLS ]] && dracut_install $BASICTOOLS
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering # in Debian ldconfig is just a shell script wrapper around ldconfig.real
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering [[ $DEBUGTOOLS ]] && dracut_install $DEBUGTOOLS
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering NSS_LIBS=$(LD_DEBUG=files getent passwd 2>&1 >/dev/null |sed -n '/calling init: .*libnss_/ {s!^.* /!/!; p}')
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering | while read file; do
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering [[ "$LOOKS_LIKE_DEBIAN" ]] && type -p dpkg-architecture &>/dev/null && find "/lib/$(dpkg-architecture -qDEB_HOST_MULTIARCH)/security" -xtype f
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering cp /etc/pam.d/systemd-user $initdir/etc/pam.d/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /usr/lib/kbd/keymaps/i386/qwerty/us.*; do
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering [[ -f $i ]] || continue
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /usr/lib/kbd/consolefonts/latarcyrheb-sun16*; do
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering [[ -f $i ]] || continue
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering [ -f ${_terminfodir}/l/linux ] && break
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering dracut_install -o ${_terminfodir}/l/linux
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering cp $TEST_BASE_DIR/testsuite.target $initdir/etc/systemd/system/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering cp $TEST_BASE_DIR/end.service $initdir/etc/systemd/system/
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering mkdir -p $initdir/etc/systemd/system/testsuite.target.wants
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering ln -fs $TEST_BASE_DIR/testsuite.service $initdir/etc/systemd/system/testsuite.target.wants/testsuite.service
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering ln -fs $TEST_BASE_DIR/end.service $initdir/etc/systemd/system/testsuite.target.wants/end.service
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering ln -fs testsuite.target $initdir/etc/systemd/system/default.target
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering ddebug "cp -ar $initdir $TESTDIR/nspawn-root"
962225baa862c2023790b3c7f140d22bac9e801fThomas Hindoe Paaboel Andersen rm -f $TESTDIR/nspawn-root/etc/fstab
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering for d in usr/bin usr/sbin bin etc lib "$libdir" sbin tmp usr var var/log dev proc sys sysroot root run run/lock run/initramfs; do
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering [[ $_line = 'not a dynamic executable' ]] && break
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering if [[ $_line =~ $_so_regex ]]; then
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering [[ -e ${initdir}/$_file ]] && continue
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering if [[ $_line =~ not\ found ]]; then
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering dfatal "Missing a shared library required by $_bin."
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering dfatal "Run \"ldd $_bin\" to find out what it is."
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if [[ -z "$TESTDIR" ]] || [[ ! -d "$TESTDIR" ]]; then
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering TESTDIR=$(mktemp --tmpdir=/var/tmp -d -t systemd-test.XXXXXX)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering## @brief Converts numeric logging level to the first letter of level name.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# @param lvl Numeric logging level in range from 1 to 6.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# @retval 1 if @a lvl is out of range.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# @retval 0 if @a lvl is correct.
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering# @result Echoes first letter of level name.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering## @brief Internal helper function for _do_dlog()
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# @param lvl Numeric logging level.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# @param msg Message.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# @retval 0 It's always returned, even if logging failed.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# @note This function is not supposed to be called manually. Please use
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# dtrace(), ddebug(), or others instead which wrap this one.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# This function calls _do_dlog() either with parameter msg, or if
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# none is given, it will read standard input and will use every line as
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# This enables:
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# dwarn "This is a warning"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# echo "This is a warning" | dwarn
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering [ $1 -le $LOG_LEVEL ] || return 0
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering local lvlc=$(_lvl2char "$lvl") || return 0
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if [ $# -ge 1 ]; then
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering while read line; do
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering## @brief Logs message at TRACE level (6)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# @param msg Message.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# @retval 0 It's always returned, even if logging failed.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering## @brief Logs message at DEBUG level (5)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# @param msg Message.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# @retval 0 It's always returned, even if logging failed.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# [ -n "$debug" ] && set -x || :
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering## @brief Logs message at INFO level (4)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# @param msg Message.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# @retval 0 It's always returned, even if logging failed.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering## @brief Logs message at WARN level (3)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# @param msg Message.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# @retval 0 It's always returned, even if logging failed.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering## @brief Logs message at ERROR level (2)
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering# @param msg Message.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# @retval 0 It's always returned, even if logging failed.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# [ -n "$debug" ] && set -x || :
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering## @brief Logs message at FATAL level (1)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# @param msg Message.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# @retval 0 It's always returned, even if logging failed.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering# Generic substring function. If $2 is in $1, return 0.
while [[ ${__absolute[__level]} == ${__current[__level]} ]]
if [[ -n $__newpath ]]
if [[ -L $_file ]]; then
# rev_lib_symlinks libfoo.so.8.1
if [[ -L $_src ]]; then
[[ -L $_bin ]] && inst_symlink $_bin $_target && return 0
if [[ $_line =~ $_so_regex ]]; then
_file=${BASH_REMATCH[1]}
local _bin
local _line _shebang_regex
[[ $_line =~ $_shebang_regex ]] || return 1
if [[ -d $_realsrc ]]; then
if [[ -f $r/$_rule ]]; then
if [[ -f ${r}$_rule ]]; then
local _x
case $# in
[[ $initdir = $2 ]] && set $1;;
for _x in inst_symlink inst_script inst_binary inst_simple; do
local to f
if [[ $_optional = yes ]]; then
if [[ $omit_drivers ]]; then
_found=1
local _modname _filtercmd
if ! [[ $hostonly ]]; then
if ! [[ $hostonly ]]; then
[[ $no_kernel = yes ]] && return
local _fderr=9
| instmods
| instmods
local _moddirname=${KERNEL_MODS%%/lib/modules/*}
--set-version $KERNEL_VER ${_moddirname} $_mpargs
return $_ret
while read _mod; do
set -o pipefail
| while read line; do [[ "$line" =~ $_filter_not_found ]] && echo $line || echo $line >&2 ;done | derror
_ret=$?
set +o pipefail
return $_ret
local _pattern=$1
[[ -e /lib/systemd/systemd-machined ]]
case $1 in
--run)
ret=$?
exit $ret;;
--setup)
--clean)
--all)
ret=$?
exit $ret
ret=$?
exit $ret;;