]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/TEST-21-SYSUSERS/test.sh
scripts: use 4 space indentation
[thirdparty/systemd.git] / test / TEST-21-SYSUSERS / test.sh
1 #!/bin/bash
2 set -e
3 TEST_DESCRIPTION="Sysuser-related tests"
4
5 . $TEST_BASE_DIR/test-functions
6
7 test_setup() {
8 mkdir -p $TESTDIR/etc/sysusers.d $TESTDIR/usr/lib/sysusers.d $TESTDIR/tmp
9 }
10
11 prepare_testdir() {
12 rm -f $TESTDIR/etc/*{passwd,group,shadow}
13 for i in $1.initial-{passwd,group,shadow}; do
14 test -f $i && cp $i $TESTDIR/etc/${i#*.initial-}
15 done
16 return 0
17 }
18
19 preprocess() {
20 in="$1"
21
22 # see meson.build how to extract this. gcc -E was used before to
23 # get this value from config.h, however the autopkgtest fails with
24 # it
25 SYSTEM_UID_MAX=$(awk 'BEGIN { uid=999 } /^\s*SYS_UID_MAX\s+/ { uid=$2 } END { print uid }' /etc/login.defs)
26 sed "s/SYSTEM_UID_MAX/${SYSTEM_UID_MAX}/g" "$in"
27 }
28
29 compare() {
30 if ! diff -u $TESTDIR/etc/passwd <(preprocess ${1%.*}.expected-passwd); then
31 echo "**** Unexpected output for $f"
32 exit 1
33 fi
34
35 if ! diff -u $TESTDIR/etc/group <(preprocess ${1%.*}.expected-group); then
36 echo "**** Unexpected output for $f $2"
37 exit 1
38 fi
39 }
40
41 test_run() {
42 # ensure our build of systemd-sysusers is run
43 PATH=${BUILD_DIR}:$PATH
44
45 rm -f $TESTDIR/etc/sysusers.d/* $TESTDIR/usr/lib/sysusers.d/*
46
47 # happy tests
48 for f in test-*.input; do
49 echo "*** Running $f"
50 prepare_testdir ${f%.input}
51 cp $f $TESTDIR/usr/lib/sysusers.d/test.conf
52 systemd-sysusers --root=$TESTDIR
53
54 compare $f ""
55 done
56
57 for f in test-*.input; do
58 echo "*** Running $f on stdin"
59 prepare_testdir ${f%.input}
60 touch $TESTDIR/etc/sysusers.d/test.conf
61 cat $f | systemd-sysusers --root=$TESTDIR -
62
63 compare $f "on stdin"
64 done
65
66 for f in test-*.input; do
67 echo "*** Running $f on stdin with --replace"
68 prepare_testdir ${f%.input}
69 touch $TESTDIR/etc/sysusers.d/test.conf
70 # this overrides test.conf which is masked on disk
71 cat $f | systemd-sysusers --root=$TESTDIR --replace=/etc/sysusers.d/test.conf -
72 # this should be ignored
73 cat test-1.input | systemd-sysusers --root=$TESTDIR --replace=/usr/lib/sysusers.d/test.conf -
74
75 compare $f "on stdin with --replace"
76 done
77
78 # test --inline
79 echo "*** Testing --inline"
80 prepare_testdir
81 # copy a random file to make sure it is ignored
82 cp $f $TESTDIR/etc/sysusers.d/confuse.conf
83 systemd-sysusers --root=$TESTDIR --inline \
84 "u u1 222 - - /bin/zsh" \
85 "g g1 111"
86
87 compare inline "(--inline)"
88
89 # test --replace
90 echo "*** Testing --inline with --replace"
91 prepare_testdir
92 # copy a random file to make sure it is ignored
93 cp $f $TESTDIR/etc/sysusers.d/confuse.conf
94 systemd-sysusers --root=$TESTDIR \
95 --inline \
96 --replace=/etc/sysusers.d/confuse.conf \
97 "u u1 222 - - /bin/zsh" \
98 "g g1 111"
99
100 compare inline "(--inline --replace=…)"
101
102 rm -f $TESTDIR/etc/sysusers.d/* $TESTDIR/usr/lib/sysusers.d/*
103
104 # tests for error conditions
105 for f in unhappy-*.input; do
106 echo "*** Running test $f"
107 prepare_testdir ${f%.input}
108 cp $f $TESTDIR/usr/lib/sysusers.d/test.conf
109 systemd-sysusers --root=$TESTDIR 2> /dev/null
110 journalctl -t systemd-sysusers -o cat | tail -n1 > $TESTDIR/tmp/err
111 if ! diff -u $TESTDIR/tmp/err ${f%.*}.expected-err; then
112 echo "**** Unexpected error output for $f"
113 cat $TESTDIR/tmp/err
114 exit 1
115 fi
116 done
117 }
118
119 do_test "$@"