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