]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/TEST-14-MACHINE-ID/test.sh
machine-id-setup: `--print --commit` respects the --root option
[thirdparty/systemd.git] / test / TEST-14-MACHINE-ID / 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 SKIP_INITRD=yes
6 . $TEST_BASE_DIR/test-functions
7
8 check_result_qemu() {
9 ret=1
10 mkdir -p $TESTDIR/root
11 mount ${LOOPDEV}p1 $TESTDIR/root
12 [[ -e $TESTDIR/root/testok ]] && ret=0
13 [[ -f $TESTDIR/root/failed ]] && cp -a $TESTDIR/root/failed $TESTDIR
14 cp -a $TESTDIR/root/var/log/journal $TESTDIR
15 umount $TESTDIR/root
16 [[ -f $TESTDIR/failed ]] && cat $TESTDIR/failed
17 ls -l $TESTDIR/journal/*/*.journal
18 test -s $TESTDIR/failed && ret=$(($ret+1))
19 return $ret
20 }
21
22 test_run() {
23 if run_qemu; then
24 check_result_qemu || return 1
25 else
26 dwarn "can't run QEMU, skipping"
27 fi
28 return 0
29 }
30
31 test_setup() {
32 create_empty_image
33 mkdir -p $TESTDIR/root
34 mount ${LOOPDEV}p1 $TESTDIR/root
35
36 # Create what will eventually be our root filesystem onto an overlay
37 (
38 LOG_LEVEL=5
39 eval $(udevadm info --export --query=env --name=${LOOPDEV}p2)
40
41 setup_basic_environment
42 dracut_install mount cmp
43
44 # setup the testsuite service
45 cat >$initdir/etc/systemd/system/testsuite.service <<EOF
46 [Unit]
47 Description=Testsuite service
48 After=multi-user.target
49
50 [Service]
51 ExecStart=/bin/sh -e -x -c '/test-machine-id-setup.sh; systemctl --state=failed --no-legend --no-pager > /failed ; echo OK > /testok'
52 Type=oneshot
53 EOF
54
55 cat >$initdir/test-machine-id-setup.sh <<'EOF'
56 #!/bin/bash
57
58 set -e
59 set -x
60
61 function setup_root {
62 local _root="$1"
63 mkdir -p "$_root"
64 mount -t tmpfs tmpfs "$_root"
65 mkdir -p "$_root/etc" "$_root/run"
66 }
67
68 function check {
69 printf "Expected\n"
70 cat "$1"
71 printf "\nGot\n"
72 cat "$2"
73 cmp "$1" "$2"
74 }
75
76 r="$(pwd)/overwrite-broken-machine-id"
77 setup_root "$r"
78 systemd-machine-id-setup --print --root "$r"
79 echo abc >>"$r/etc/machine-id"
80 id=$(systemd-machine-id-setup --print --root "$r")
81 echo $id >expected
82 check expected "$r/etc/machine-id"
83
84 r="$(pwd)/transient-machine-id"
85 setup_root "$r"
86 touch "$r/etc/machine-id"
87 mount -o remount,ro "$r"
88 mount -t tmpfs tmpfs "$r/run"
89 transient_id=$(systemd-machine-id-setup --print --root "$r")
90 mount -o remount,rw "$r"
91 commited_id=$(systemd-machine-id-setup --print --commit --root "$r")
92 [[ "$transient_id" = "$commited_id" ]]
93 check "$r/etc/machine-id" "$r/run/machine-id"
94 EOF
95 chmod +x $initdir/test-machine-id-setup.sh
96
97 setup_testsuite
98 ) || return 1
99
100 # mask some services that we do not want to run in these tests
101 ln -s /dev/null $initdir/etc/systemd/system/systemd-hwdb-update.service
102 ln -s /dev/null $initdir/etc/systemd/system/systemd-journal-catalog-update.service
103 ln -s /dev/null $initdir/etc/systemd/system/systemd-networkd.service
104 ln -s /dev/null $initdir/etc/systemd/system/systemd-networkd.socket
105 ln -s /dev/null $initdir/etc/systemd/system/systemd-resolved.service
106
107 ddebug "umount $TESTDIR/root"
108 umount $TESTDIR/root
109 }
110
111 test_cleanup() {
112 umount $TESTDIR/root 2>/dev/null
113 [[ $LOOPDEV ]] && losetup -d $LOOPDEV
114 return 0
115 }
116
117 do_test "$@"