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