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