chiark / gitweb /
systemd-python: check for oom, give nicer error messages
[elogind.git] / test / TEST-03-JOBS / test.sh
1 #!/bin/bash
2 # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3 # ex: ts=8 sw=4 sts=4 et filetype=sh
4 TEST_DESCRIPTION="Job-related tests"
5
6 . $TEST_BASE_DIR/test-functions
7
8 # Uncomment this to debug failures
9 #DEBUGFAIL="systemd.unit=multi-user.target"
10 DEBUGTOOLS="df free ls stty cat ps ln ip route dmesg dhclient mkdir cp ping dhclient strace less grep id tty touch du sort"
11
12 run_qemu() {
13     # TODO: qemu wrapper script: http://www.spinics.net/lists/kvm/msg72389.html
14     qemu-kvm \
15         -hda $TESTDIR/rootdisk.img \
16         -m 512M -nographic \
17         -net none -kernel /boot/vmlinuz-$KERNEL_VER \
18         -append "root=/dev/sda1 systemd.log_level=debug raid=noautodetect loglevel=2 init=/usr/lib/systemd/systemd ro console=ttyS0,115200n81 selinux=0 $DEBUGFAIL" || return 1
19
20     ret=1
21     mkdir -p $TESTDIR/root
22     mount ${LOOPDEV}p1 $TESTDIR/root
23     [[ -e $TESTDIR/root/testok ]] && ret=0
24     cp -a $TESTDIR/root/failed $TESTDIR
25     cp -a $TESTDIR/root/var/log/journal $TESTDIR
26     umount $TESTDIR/root
27     cat $TESTDIR/failed
28     ls -l $TESTDIR/journal/*/*.journal
29     test -s $TESTDIR/failed && ret=$(($ret+1))
30     return $ret
31 }
32
33
34 run_nspawn() {
35     ../../systemd-nspawn -b -D $TESTDIR/nspawn-root /usr/lib/systemd/systemd
36     ret=1
37     [[ -e $TESTDIR/nspawn-root/testok ]] && ret=0
38     cp -a $TESTDIR/nspawn-root/failed $TESTDIR
39     cp -a $TESTDIR/nspawn-root/var/log/journal $TESTDIR
40     cat $TESTDIR/failed
41     ls -l $TESTDIR/journal/*/*.journal
42     test -s $TESTDIR/failed && ret=$(($ret+1))
43     return $ret
44 }
45
46
47 test_run() {
48     if check_qemu ; then
49         run_qemu || return 1
50     else
51         dwarn "can't run qemu-kvm, skipping"
52     fi
53     if check_nspawn; then
54         run_nspawn || return 1
55     else
56         dwarn "can't run systemd-nspawn, skipping"
57     fi
58     return 0
59 }
60
61 test_setup() {
62     rm -f $TESTDIR/rootdisk.img
63     # Create the blank file to use as a root filesystem
64     dd if=/dev/null of=$TESTDIR/rootdisk.img bs=1M seek=200
65     LOOPDEV=$(losetup --show -P -f $TESTDIR/rootdisk.img)
66     [ -b $LOOPDEV ] || return 1
67     echo "LOOPDEV=$LOOPDEV" >> $STATEFILE
68     sfdisk -C 6400 -H 2 -S 32 -L $LOOPDEV <<EOF
69 ,3200
70 ,
71 EOF
72
73     mkfs.ext3 -L systemd ${LOOPDEV}p1
74     echo -n test >$TESTDIR/keyfile
75     mkdir -p $TESTDIR/root
76     mount ${LOOPDEV}p1 $TESTDIR/root
77     mkdir -p $TESTDIR/root/run
78
79     # Create what will eventually be our root filesystem onto an overlay
80     (
81         LOG_LEVEL=5
82         initdir=$TESTDIR/root
83
84         # create the basic filesystem layout
85         setup_basic_dirs
86
87         # install compiled files
88         (cd ../..; make DESTDIR=$initdir install)
89
90         # remove unneeded documentation
91         rm -fr $initdir/usr/share/{man,doc,gtk-doc}
92
93         # install possible missing libraries
94         for i in $initdir/{sbin,bin}/* $initdir/lib/systemd/*; do
95             inst_libs $i
96         done
97
98         # make a journal directory
99         mkdir -p $initdir/var/log/journal
100
101         # install some basic config files
102         inst /etc/sysconfig/init
103         inst /etc/passwd
104         inst /etc/shadow
105         inst /etc/group
106         inst /etc/shells
107         inst /etc/nsswitch.conf
108         inst /etc/pam.conf
109         inst /etc/securetty
110         inst /etc/os-release
111         inst /etc/localtime
112         # we want an empty environment
113         > $initdir/etc/environment
114         > $initdir/etc/machine-id
115
116         # set the hostname
117         echo  systemd-testsuite > $initdir/etc/hostname
118
119         eval $(udevadm info --export --query=env --name=${LOOPDEV}p2)
120
121         cat >$initdir/etc/fstab <<EOF
122 LABEL=systemd           /       ext3    rw 0 1
123 EOF
124
125         # setup the testsuite target and the test ending service
126         cp $TEST_BASE_DIR/{testsuite.target,end.service} $initdir/etc/systemd/system/
127
128         # setup the testsuite service
129         cat >$initdir/etc/systemd/system/testsuite.service <<EOF
130 [Unit]
131 Description=Testsuite service
132 After=multi-user.target
133
134 [Service]
135 ExecStart=/test-jobs.sh
136 Type=oneshot
137 EOF
138
139         # copy the units used by this test
140         cp $TEST_BASE_DIR/{hello.service,sleep.service,hello-after-sleep.target,unstoppable.service} \
141             $initdir/etc/systemd/system
142         cp test-jobs.sh $initdir/
143
144         mkdir -p $initdir/etc/systemd/system/testsuite.target.wants
145         ln -fs ../testsuite.service $initdir/etc/systemd/system/testsuite.target.wants/testsuite.service
146         ln -fs ../end.service $initdir/etc/systemd/system/testsuite.target.wants/end.service
147
148         # make the testsuite the default target
149         ln -fs testsuite.target $initdir/etc/systemd/system/default.target
150         mkdir -p $initdir/etc/rc.d
151         cat >$initdir/etc/rc.d/rc.local <<EOF
152 #!/bin/bash
153 exit 0
154 EOF
155         chmod 0755 $initdir/etc/rc.d/rc.local
156         # install basic tools needed
157         dracut_install sh bash setsid loadkeys setfont \
158             login sushell sulogin gzip sleep echo mount umount cryptsetup date
159         dracut_install dmsetup modprobe
160
161         # install libnss_files for login
162         inst_libdir_file "libnss_files*"
163
164         # install dbus and pam
165         find \
166             /etc/dbus-1 \
167             /etc/pam.d \
168             /etc/security \
169             /lib64/security \
170             /lib/security -xtype f \
171             | while read file; do
172             inst $file
173         done
174
175         # install dbus socket and service file
176         inst /usr/lib/systemd/system/dbus.socket
177         inst /usr/lib/systemd/system/dbus.service
178
179         # install basic keyboard maps and fonts
180         for i in \
181             /usr/lib/kbd/consolefonts/latarcyrheb-sun16* \
182             /usr/lib/kbd/keymaps/include/* \
183             /usr/lib/kbd/keymaps/i386/include/* \
184             /usr/lib/kbd/keymaps/i386/qwerty/us.*; do
185                 [[ -f $i ]] || continue
186                 inst $i
187         done
188
189         # some basic terminfo files
190         for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
191             [ -f ${_terminfodir}/l/linux ] && break
192         done
193         dracut_install -o ${_terminfodir}/l/linux
194
195         # softlink mtab
196         ln -fs /proc/self/mounts $initdir/etc/mtab
197
198         # install any Exec's from the service files
199         egrep -ho '^Exec[^ ]*=[^ ]+' $initdir/lib/systemd/system/*.service \
200             | while read i; do
201             i=${i##Exec*=}; i=${i##-}
202             inst $i
203         done
204
205         # install plymouth, if found... else remove plymouth service files
206         # if [ -x /usr/libexec/plymouth/plymouth-populate-initrd ]; then
207         #     PLYMOUTH_POPULATE_SOURCE_FUNCTIONS="$TEST_BASE_DIR/test-functions" \
208         #         /usr/libexec/plymouth/plymouth-populate-initrd -t $initdir
209         #         dracut_install plymouth plymouthd
210         # else
211         rm -f $initdir/{usr/lib,etc}/systemd/system/plymouth* $initdir/{usr/lib,etc}/systemd/system/*/plymouth*
212         # fi
213
214         # some helper tools for debugging
215         [[ $DEBUGTOOLS ]] && dracut_install $DEBUGTOOLS
216
217         # install ld.so.conf* and run ldconfig
218         cp -a /etc/ld.so.conf* $initdir/etc
219         ldconfig -r "$initdir"
220         ddebug "Strip binaeries"
221         find "$initdir" -perm +111 -type f | xargs strip --strip-unneeded | ddebug
222
223         # copy depmod files
224         inst /lib/modules/$KERNEL_VER/modules.order
225         inst /lib/modules/$KERNEL_VER/modules.builtin
226         # generate module dependencies
227         if [[ -d $initdir/lib/modules/$KERNEL_VER ]] && \
228             ! depmod -a -b "$initdir" $KERNEL_VER; then
229                 dfatal "\"depmod -a $KERNEL_VER\" failed."
230                 exit 1
231         fi
232     )
233     rm -fr $TESTDIR/nspawn-root
234     ddebug "cp -ar $TESTDIR/root $TESTDIR/nspawn-root"
235     cp -ar $TESTDIR/root $TESTDIR/nspawn-root
236     # we don't mount in the nspawn root
237     rm -fr $TESTDIR/nspawn-root/etc/fstab
238
239     ddebug "umount $TESTDIR/root"
240     umount $TESTDIR/root
241 }
242
243 test_cleanup() {
244     umount $TESTDIR/root 2>/dev/null
245     [[ $LOOPDEV ]] && losetup -d $LOOPDEV
246     return 0
247 }
248
249 do_test "$@"