]> git.ipfire.org Git - thirdparty/systemd.git/blame - test/TEST-21-SYSUSERS/test.sh
Merge pull request #16556 from keszybz/test-terminal-colors
[thirdparty/systemd.git] / test / TEST-21-SYSUSERS / test.sh
CommitLineData
ff12a795 1#!/usr/bin/env bash
1e589ed2
MV
2set -e
3TEST_DESCRIPTION="Sysuser-related tests"
3be6f501 4IMAGE_NAME="sysusers"
1e589ed2
MV
5. $TEST_BASE_DIR/test-functions
6
7test_setup() {
cc5549ca 8 mkdir -p $TESTDIR/etc/sysusers.d $TESTDIR/usr/lib/sysusers.d $TESTDIR/tmp
1e589ed2
MV
9}
10
97e34e94 11prepare_testdir() {
cc5549ca
ZJS
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
97e34e94
FB
17}
18
b9ee05c2
MV
19preprocess() {
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
ef8b52c6
ER
25 [[ -e /etc/login.defs ]] && login_defs_file="/etc/login.defs" || login_defs_file="/usr/etc/login.defs"
26 SYSTEM_UID_MAX=$(awk 'BEGIN { uid=999 } /^\s*SYS_UID_MAX\s+/ { uid=$2 } END { print uid }' $login_defs_file)
27 SYSTEM_GID_MAX=$(awk 'BEGIN { gid=999 } /^\s*SYS_GID_MAX\s+/ { gid=$2 } END { print gid }' $login_defs_file)
6db90462
MB
28
29 # we can't rely on config.h to get the nologin path, as autopkgtest
30 # uses pre-compiled binaries, so extract it from the systemd-sysusers
31 # binary which we are about to execute
32 NOLOGIN=$(strings $(type -p systemd-sysusers) | grep nologin)
33
34 sed -e "s/SYSTEM_UID_MAX/${SYSTEM_UID_MAX}/g" \
649916d3 35 -e "s/SYSTEM_GID_MAX/${SYSTEM_GID_MAX}/g" \
6db90462 36 -e "s#NOLOGIN#${NOLOGIN}#g" "$in"
b9ee05c2
MV
37}
38
4e9fe38d
ZJS
39compare() {
40 if ! diff -u $TESTDIR/etc/passwd <(preprocess ${1%.*}.expected-passwd); then
cc5549ca
ZJS
41 echo "**** Unexpected output for $f"
42 exit 1
4e9fe38d
ZJS
43 fi
44
45 if ! diff -u $TESTDIR/etc/group <(preprocess ${1%.*}.expected-group); then
cc5549ca
ZJS
46 echo "**** Unexpected output for $f $2"
47 exit 1
4e9fe38d
ZJS
48 fi
49}
50
1e589ed2 51test_run() {
cc5549ca
ZJS
52 # ensure our build of systemd-sysusers is run
53 PATH=${BUILD_DIR}:$PATH
54
55 rm -f $TESTDIR/etc/sysusers.d/* $TESTDIR/usr/lib/sysusers.d/*
56
57 # happy tests
58 for f in test-*.input; do
59 echo "*** Running $f"
60 prepare_testdir ${f%.input}
61 cp $f $TESTDIR/usr/lib/sysusers.d/test.conf
62 systemd-sysusers --root=$TESTDIR
63
64 compare $f ""
65 done
66
67 for f in test-*.input; do
68 echo "*** Running $f on stdin"
69 prepare_testdir ${f%.input}
70 touch $TESTDIR/etc/sysusers.d/test.conf
71 cat $f | systemd-sysusers --root=$TESTDIR -
72
73 compare $f "on stdin"
74 done
75
76 for f in test-*.input; do
77 echo "*** Running $f on stdin with --replace"
78 prepare_testdir ${f%.input}
79 touch $TESTDIR/etc/sysusers.d/test.conf
80 # this overrides test.conf which is masked on disk
81 cat $f | systemd-sysusers --root=$TESTDIR --replace=/etc/sysusers.d/test.conf -
82 # this should be ignored
83 cat test-1.input | systemd-sysusers --root=$TESTDIR --replace=/usr/lib/sysusers.d/test.conf -
84
85 compare $f "on stdin with --replace"
86 done
87
88 # test --inline
89 echo "*** Testing --inline"
90 prepare_testdir
91 # copy a random file to make sure it is ignored
92 cp $f $TESTDIR/etc/sysusers.d/confuse.conf
93 systemd-sysusers --root=$TESTDIR --inline \
94 "u u1 222 - - /bin/zsh" \
95 "g g1 111"
96
97 compare inline "(--inline)"
98
99 # test --replace
100 echo "*** Testing --inline with --replace"
101 prepare_testdir
102 # copy a random file to make sure it is ignored
103 cp $f $TESTDIR/etc/sysusers.d/confuse.conf
104 systemd-sysusers --root=$TESTDIR \
105 --inline \
106 --replace=/etc/sysusers.d/confuse.conf \
107 "u u1 222 - - /bin/zsh" \
108 "g g1 111"
109
110 compare inline "(--inline --replace=…)"
111
112 rm -f $TESTDIR/etc/sysusers.d/* $TESTDIR/usr/lib/sysusers.d/*
113
114 # tests for error conditions
115 for f in unhappy-*.input; do
116 echo "*** Running test $f"
117 prepare_testdir ${f%.input}
118 cp $f $TESTDIR/usr/lib/sysusers.d/test.conf
bc694c06 119 systemd-sysusers --root=$TESTDIR 2>&1 | tail -n1 > $TESTDIR/tmp/err
cc5549ca
ZJS
120 if ! diff -u $TESTDIR/tmp/err ${f%.*}.expected-err; then
121 echo "**** Unexpected error output for $f"
122 cat $TESTDIR/tmp/err
123 exit 1
124 fi
125 done
1e589ed2
MV
126}
127
128do_test "$@"