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