chiark / gitweb /
6e7e97ef431c6067261f72784572f1e8f023a2d2
[elogind.git] / test / TEST-01-BASIC / 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="Basic systemd setup"
5
6 KVERSION=${KVERSION-$(uname -r)}
7
8 # Uncomment this to debug failures
9 #DEBUGFAIL="systemd.unit=multi-user.target"
10
11 run_qemu() {
12     qemu-kvm \
13         -hda $TESTDIR/rootdisk.img \
14         -m 256M -nographic \
15         -net none -kernel /boot/vmlinuz-$KVERSION \
16         -append "root=/dev/sda1 systemd.log_level=debug raid=noautodetect loglevel=2 init=/usr/lib/systemd/systemd rw console=ttyS0,115200n81 selinux=0 $DEBUGFAIL"
17     ret=1
18     mkdir -p $TESTDIR/root
19     mount ${LOOPDEV}p1 $TESTDIR/root
20     [[ -e $TESTDIR/root/testok ]] && ret=0
21     cp -a $TESTDIR/root/var/log/journal $TESTDIR
22     cp -a $TESTDIR/root/failed $TESTDIR
23     umount $TESTDIR/root
24     cat $TESTDIR/failed
25     ls -l $TESTDIR/journal/*/*.journal
26     test -s $TESTDIR/failed && ret=$(($ret+1))
27     return $ret
28 }
29
30
31 run_nspawn() {
32     systemd-nspawn -b -D $TESTDIR/nspawn-root /usr/lib/systemd/systemd
33     ret=1
34     [[ -e $TESTDIR/nspawn-root/testok ]] && ret=0
35     cp -a $TESTDIR/nspawn-root/var/log/journal $TESTDIR
36     cp -a $TESTDIR/nspawn-root/failed $TESTDIR
37     cat $TESTDIR/failed
38     ls -l $TESTDIR/journal/*/*.journal
39     test -s $TESTDIR/failed && ret=$(($ret+1))
40     return $ret
41 }
42
43
44 test_run() {
45     run_qemu || return 1
46     if [[ -d /sys/fs/cgroup/systemd ]]; then
47         run_nspawn || return 1
48     fi
49     return 0
50 }
51
52 test_setup() {
53     rm -f $TESTDIR/rootdisk.img
54     # Create the blank file to use as a root filesystem
55     dd if=/dev/null of=$TESTDIR/rootdisk.img bs=1M seek=100
56     LOOPDEV=$(losetup --show -P -f $TESTDIR/rootdisk.img)
57     [ -b $LOOPDEV ] || return 1
58     echo "LOOPDEV=$LOOPDEV" >> $STATEFILE
59     sfdisk -C 3200 -H 2 -S 32 -L $LOOPDEV <<EOF
60 ,
61 EOF
62
63     mkfs.ext3 -L systemd ${LOOPDEV}p1
64     mkdir -p $TESTDIR/root
65     mount ${LOOPDEV}p1 $TESTDIR/root
66     mkdir -p $TESTDIR/root/run
67
68     kernel=$KVERSION
69     # Create what will eventually be our root filesystem onto an overlay
70     (
71         LOG_LEVEL=5
72         initdir=$TESTDIR/root
73
74         # create the basic filesystem layout
75         setup_basic_dirs
76
77         # install compiled files
78         (cd ../..; make DESTDIR=$initdir install)
79
80         # install possible missing libraries
81         for i in $initdir/{sbin,bin}/* $initdir/lib/systemd/*; do
82             inst_libs $i
83         done
84
85         # activate kmsg import
86         echo 'ImportKernel=yes' >> $initdir/etc/systemd/journald.conf
87
88         # make a journal directory
89         mkdir -p $initdir/var/log/journal
90
91         # install some basic config files
92         inst /etc/sysconfig/init
93         inst /etc/passwd
94         inst /etc/shadow
95         inst /etc/group
96         inst /etc/shells
97         inst /etc/nsswitch.conf
98         inst /etc/pam.conf
99         inst /etc/securetty
100         inst /etc/os-release
101         inst /etc/localtime
102         # we want an empty environment
103         > $initdir/etc/environment
104
105         # set the hostname
106         echo  systemd-testsuite > $initdir/etc/hostname
107
108         # setup the testsuite target
109         cat >$initdir/etc/systemd/system/testsuite.target <<EOF
110 [Unit]
111 Description=Testsuite target
112 Requires=multi-user.target
113 After=multi-user.target
114 Conflicts=rescue.target
115 AllowIsolate=yes
116 EOF
117
118         # setup the testsuite service
119         cat >$initdir/etc/systemd/system/testsuite.service <<EOF
120 [Unit]
121 Description=Testsuite service
122 After=multi-user.target
123
124 [Service]
125 ExecStart=/bin/sh -c 'systemctl --failed --no-legend --no-pager > /failed ; echo OK > /testok'
126 ExecStartPost=/usr/sbin/poweroff
127 Type=oneshot
128
129 EOF
130         mkdir -p $initdir/etc/systemd/system/testsuite.target.wants
131         ln -fs ../testsuite.service $initdir/etc/systemd/system/testsuite.target.wants/testsuite.service
132
133         # make the testsuite the default target
134         ln -fs testsuite.target $initdir/etc/systemd/system/default.target
135         mkdir -p $initdir/etc/rc.d
136         cat >$initdir/etc/rc.d/rc.local <<EOF
137 #!/bin/bash
138 exit 0
139 EOF
140         chmod 0755 $initdir/etc/rc.d/rc.local
141         # install basic tools needed
142         dracut_install sh bash setsid loadkeys setfont \
143             login sushell sulogin gzip sleep echo
144
145         # install libnss_files for login
146         inst_libdir_file "libnss_files*"
147
148         # install dbus and pam
149         find \
150             /etc/dbus-1 \
151             /etc/pam.d \
152             /etc/security \
153             /lib64/security \
154             /lib/security -xtype f \
155             | while read file; do
156             inst $file
157         done
158
159         # install dbus socket and service file
160         inst /usr/lib/systemd/system/dbus.socket
161         inst /usr/lib/systemd/system/dbus.service
162
163         # install basic keyboard maps and fonts
164         for i in \
165             /usr/lib/kbd/consolefonts/latarcyrheb-sun16* \
166             /usr/lib/kbd/keymaps/include/* \
167             /usr/lib/kbd/keymaps/i386/include/* \
168             /usr/lib/kbd/keymaps/i386/qwerty/us.*; do
169                 [[ -f $i ]] || continue
170                 inst $i
171         done
172
173         # some basic terminfo files
174         for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
175             [ -f ${_terminfodir}/l/linux ] && break
176         done
177         dracut_install -o ${_terminfodir}/l/linux
178
179         # softlink mtab
180         ln -fs /proc/self/mounts $initdir/etc/mtab
181
182         # install any Exec's from the service files
183         egrep -ho '^Exec[^ ]*=[^ ]+' $initdir/lib/systemd/system/*.service \
184             | while read i; do
185             i=${i##Exec*=}; i=${i##-}
186             inst $i
187         done
188
189         # install plymouth, if found... else remove plymouth service files
190         if [ -x /usr/libexec/plymouth/plymouth-populate-initrd ]; then
191             PLYMOUTH_POPULATE_SOURCE_FUNCTIONS="$TEST_BASE_DIR/test-functions" \
192                 /usr/libexec/plymouth/plymouth-populate-initrd -t $initdir
193                 dracut_install plymouth plymouthd
194         else
195                 rm -f $initdir/usr/lib/systemd/system/plymouth* $initdir/usr/lib/systemd/system/*/plymouth*
196         fi
197
198         # some helper tools for debugging
199         dracut_install sh df free ls shutdown poweroff \
200             stty cat ps ln ip route \
201             mount dmesg dhclient mkdir cp ping dhclient \
202             umount strace less grep id tty touch
203
204         # install ld.so.conf* and run ldconfig
205         cp -a /etc/ld.so.conf* $initdir/etc
206         ldconfig -r "$initdir"
207
208     )
209     rm -fr $TESTDIR/nspawn-root
210     cp -avr $TESTDIR/root $TESTDIR/nspawn-root
211
212     umount $TESTDIR/root
213 }
214
215 test_cleanup() {
216     umount $TESTDIR/root 2>/dev/null
217     [[ $LOOPDEV ]] && losetup -d $LOOPDEV
218     return 0
219 }
220
221 . $TEST_BASE_DIR/test-functions
222 do_test "$@"