]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/TEST-14-MACHINE-ID/test.sh
test: add function to reduce copied setup boilerplate
[thirdparty/systemd.git] / test / TEST-14-MACHINE-ID / test.sh
1 #!/bin/bash
2 set -e
3 TEST_DESCRIPTION="/etc/machine-id testing"
4 TEST_NO_NSPAWN=1
5
6 . $TEST_BASE_DIR/test-functions
7
8 test_setup() {
9 create_empty_image_rootdir
10
11 # Create what will eventually be our root filesystem onto an overlay
12 (
13 LOG_LEVEL=5
14 eval $(udevadm info --export --query=env --name=${LOOPDEV}p2)
15
16 setup_basic_environment
17 mask_supporting_services
18 printf "556f48e837bc4424a710fa2e2c9d3e3c\ne3d\n" >$initdir/etc/machine-id
19 dracut_install mount cmp
20
21 # setup the testsuite service
22 cat >$initdir/etc/systemd/system/testsuite.service <<EOF
23 [Unit]
24 Description=Testsuite service
25
26 [Service]
27 ExecStart=/bin/sh -e -x -c '/test-machine-id-setup.sh; systemctl --state=failed --no-legend --no-pager > /failed ; echo OK > /testok'
28 Type=oneshot
29 EOF
30
31 cat >$initdir/test-machine-id-setup.sh <<'EOF'
32 #!/bin/bash
33
34 set -e
35 set -x
36
37 function setup_root {
38 local _root="$1"
39 mkdir -p "$_root"
40 mount -t tmpfs tmpfs "$_root"
41 mkdir -p "$_root/etc" "$_root/run"
42 }
43
44 function check {
45 printf "Expected\n"
46 cat "$1"
47 printf "\nGot\n"
48 cat "$2"
49 cmp "$1" "$2"
50 }
51
52 r="$(pwd)/overwrite-broken-machine-id"
53 setup_root "$r"
54 systemd-machine-id-setup --print --root "$r"
55 echo abc >>"$r/etc/machine-id"
56 id=$(systemd-machine-id-setup --print --root "$r")
57 echo $id >expected
58 check expected "$r/etc/machine-id"
59
60 r="$(pwd)/transient-machine-id"
61 setup_root "$r"
62 systemd-machine-id-setup --print --root "$r"
63 echo abc >>"$r/etc/machine-id"
64 mount -o remount,ro "$r"
65 mount -t tmpfs tmpfs "$r/run"
66 transient_id=$(systemd-machine-id-setup --print --root "$r")
67 mount -o remount,rw "$r"
68 commited_id=$(systemd-machine-id-setup --print --commit --root "$r")
69 [[ "$transient_id" = "$commited_id" ]]
70 check "$r/etc/machine-id" "$r/run/machine-id"
71 EOF
72 chmod +x $initdir/test-machine-id-setup.sh
73
74 setup_testsuite
75 )
76 }
77
78 do_test "$@"