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