1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
#!/bin/sh
# Check power unit
# Author: Martin Pitt <martin.pitt@ubuntu.com>
set -eu
if [ -d /run/systemd/system ]; then
if [ ! -x /tmp/autopkgtest-reboot ]; then
echo "SKIP: testbed does not support reboot"
exit 0
fi
if [ -n "${ADT_REBOOT_MARK:-}" ]; then
echo "SKIP: Reboot with sysvinit-core still runs systemd; using init= ?"
exit 0
fi
echo "Installing sysvinit-core..."
apt-get install -y sysvinit-core
echo "Rebooting into SysV init..."
/tmp/autopkgtest-reboot b1
fi
CALL_MGR="gdbus call -y -d org.freedesktop.systemd1 -o /org/freedesktop/systemd1 -m org.freedesktop.systemd1.Manager"
# install ephemeral stubs for power management commands
WRAPPED_CMDS="/usr/sbin/pm-suspend /usr/sbin/pm-hibernate /sbin/shutdown /sbin/reboot"
WORKDIR=`mktemp -d`
/bin/echo -e '#!/bin/sh\necho "$0 $@" >> '$WORKDIR'/log' > $WORKDIR/stub
chmod 755 $WORKDIR/stub
for cmd in $WRAPPED_CMDS; do
mount -o bind $WORKDIR/stub $cmd
done
FAKE_SYS_STATE="$WORKDIR/sys_power_state"
touch $FAKE_SYS_STATE
mount -o bind $FAKE_SYS_STATE /sys/power/state
# clean up mounts and temp dir on exit
cleanup() {
echo 'Cleanup...'
set +e
for c in $WRAPPED_CMDS; do umount $c; done
umount /sys/power/state
rm -r $WORKDIR
}
trap cleanup EXIT HUP INT QUIT ABRT PIPE TERM
# flush log and return it in $LOG
flush_log() {
sync
LOG=`cat $WORKDIR/log 2>/dev/null` || LOG=''
rm -f $WORKDIR/log
}
# flush /sys/power/state and return it in $LOG
flush_sys() {
sync
LOG=`cat $FAKE_SYS_STATE 2>/dev/null` || LOG=''
echo > $FAKE_SYS_STATE
}
assert_eq() {
if [ "$1" != "$2" ]; then
echo "FAIL: '$1' not equal to '$2'" >&2
exit 1
fi
}
reset() {
pkill -e -f /usr/lib/*/systemd-shim || true
}
# make sure we start with a clean slate
reset
#
# Tests start here
#
echo "suspend"
${CALL_MGR}.StartUnit 'suspend.target' ''
flush_log
assert_eq "$LOG" "/usr/sbin/pm-suspend "
echo "hibernate"
${CALL_MGR}.StartUnit 'hibernate.target' ''
flush_log
assert_eq "$LOG" "/usr/sbin/pm-hibernate "
echo "shutdown"
${CALL_MGR}.StartUnit 'shutdown.target' ''
flush_log
assert_eq "$LOG" "/sbin/shutdown -h now"
echo "no inactivity timeout after poweroff"
PID=`pidof systemd-shim`
sleep 12
assert_eq "`pidof systemd-shim`" "$PID"
echo "suspend while poweroff is in progress"
${CALL_MGR}.StartUnit 'suspend.target' ''
flush_log
assert_eq "$LOG" ""
reset
echo "reboot"
${CALL_MGR}.StartUnit 'reboot.target' ''
flush_log
assert_eq "$LOG" "/sbin/reboot "
echo "unknown unit"
if ${CALL_MGR}.StartUnit 'unknown.target' '' 2>&1; then
echo "unknown.target unexpectedly succeeded"
exit 1
fi
echo "two suspends in quick succession are just counted once"
reset
${CALL_MGR}.StartUnit 'suspend.target' ''
sleep 0.5
${CALL_MGR}.StartUnit 'suspend.target' ''
flush_log
assert_eq "$LOG" "/usr/sbin/pm-suspend "
echo "suspend falls back to writing /sys if pm-utils is not available"
reset
chmod 0 /usr/sbin/pm-suspend
${CALL_MGR}.StartUnit 'suspend.target' ''
flush_log
assert_eq "$LOG" ""
flush_sys
assert_eq "$LOG" "mem"
echo "hibernate falls back to writing /sys if pm-utils is not available"
reset
chmod 0 /usr/sbin/pm-hibernate
${CALL_MGR}.StartUnit 'hibernate.target' ''
flush_log
assert_eq "$LOG" ""
flush_sys
assert_eq "$LOG" "disk"
echo "all tests passed"
|