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