1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-22 17:35:35 +03:00

Merge pull request #27707 from mrc0mmand/tests

test: provide coverage for a couple of recent-ish issues
This commit is contained in:
Yu Watanabe 2023-05-20 08:40:01 +09:00 committed by GitHub
commit b3bd6ccd1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 151 additions and 15 deletions

View File

@ -154,8 +154,12 @@ sleep 3
[[ ! -f "/tmp/i-lose-my-logs" ]]
systemctl stop forever-print-hola
set +o pipefail
# https://github.com/systemd/systemd/issues/15528
journalctl --follow --file=/var/log/journal/*/* | head -n1 || [[ $? -eq 1 ]]
journalctl --follow --file=/var/log/journal/*/* | head -n1 | grep .
# https://github.com/systemd/systemd/issues/24565
journalctl --follow --merge | head -n1 | grep .
set -o pipefail
add_logs_filtering_override() {
local unit="${1:?}"

View File

@ -0,0 +1,70 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: LGPL-2.1-or-later
set -eux
set -o pipefail
# Don't send invalid characters over dbus if a mount contains them
at_exit() {
mountpoint -q /proc/1/mountinfo && umount /proc/1/mountinfo
[[ -e /tmp/fstab.bak ]] && mv -f /tmp/fstab /etc/fstab
rm -f /run/systemd/system/foo-*.mount
systemctl daemon-reload
}
trap at_exit EXIT
# Check invalid characters directly in /proc/mountinfo
#
# This is a bit tricky (and hacky), since we have to temporarily replace
# PID 1's /proc/mountinfo, but we have to keep the original mounts intact,
# otherwise systemd would unmount them on reload
TMP_MOUNTINFO="$(mktemp)"
cp /proc/1/mountinfo "$TMP_MOUNTINFO"
# Add a mount entry with a "Unicode non-character" in it
echo -ne '69 1 252:2 / /foo/mountinfo rw,relatime shared:1 - cifs //foo\ufffebar rw,seclabel\n' >>"$TMP_MOUNTINFO"
mount --bind "$TMP_MOUNTINFO" /proc/1/mountinfo
systemctl daemon-reload
# On affected versions this would throw an error:
# Failed to get properties: Bad message
systemctl status foo-mountinfo.mount
umount /proc/1/mountinfo
systemctl daemon-reload
rm -f "$TMP_MOUNTINFO"
# Check invalid characters in a mount unit
#
# systemd already handles this and refuses to load the invalid string, e.g.:
# foo-fstab.mount:9: String is not UTF-8 clean, ignoring assignment: What=//localhost/foo<6F><6F><EFBFBD>bar
#
# a) Unit generated from /etc/fstab
[[ -e /etc/fstab ]] && cp -f /etc/fstab /tmp/fstab.bak
echo -ne '//localhost/foo\ufffebar /foo/fstab cifs defaults 0 0\n' >/etc/fstab
systemctl daemon-reload
[[ "$(systemctl show -P UnitFileState foo-fstab.mount)" == bad ]]
# b) Unit generated from /etc/fstab (but the invalid character is in options)
echo -ne '//localhost/foobar /foo/fstab/opt cifs nosuid,a\ufffeb,noexec 0 0\n' >/etc/fstab
systemctl daemon-reload
[[ "$(systemctl show -P UnitFileState foo-fstab-opt.mount)" == bad ]]
rm -f /etc/fstab
[[ -e /tmp/fstab.bak ]] && mv -f /tmp/fstab /etc/fstab
systemctl daemon-reload
# c) Mount unit
mkdir -p /run/systemd/system
echo -ne '[Mount]\nWhat=//localhost/foo\ufffebar\nWhere=/foo/unit\nType=cifs\nOptions=noexec\n' >/run/systemd/system/foo-unit.mount
systemctl daemon-reload
[[ "$(systemctl show -P UnitFileState foo-unit.mount)" == bad ]]
rm -f /run/systemd/system/foo-unit.mount
# d) Mount unit (but the invalid character is in Options=)
mkdir -p /run/systemd/system
echo -ne '[Mount]\nWhat=//localhost/foobar\nWhere=/foo/unit/opt\nType=cifs\nOptions=noexec,a\ufffeb,nosuid\n' >/run/systemd/system/foo-unit-opt.mount
systemctl daemon-reload
[[ "$(systemctl show -P UnitFileState foo-unit-opt.mount)" == bad ]]
rm -f /run/systemd/system/foo-unit-opt.mount

View File

@ -76,6 +76,7 @@ cat >"$OCI/config.json" <<EOF
"type" : "tmpfs",
"source" : "tmpfs"
},
${COVERAGE_BUILD_DIR:+"{ \"destination\" : \"$COVERAGE_BUILD_DIR\" },"}
{
"destination" : "/var",
"type" : "none",

View File

@ -18,8 +18,13 @@
# - also a note - if /etc/systemd/nspawn/cont-name.nspawn exists, it takes
# precedence and /run/systemd/nspawn/cont-name.nspawn won't be read even
# if it exists
# - also a note 2 - --bind= overrides any Bind= from a config file
# - in some cases we don't create a test container using create_dummy_container(),
# so in that case an explicit call to coverage_create_nspawn_dropin() is needed
#
# However, even after jumping through all these hooks, there still might (and is)
# some "incorrectly" missing coverage, especially in the window between spawning
# the inner child process and bind-mounting the coverage $BUILD_DIR
set -eux
set -o pipefail
@ -101,7 +106,6 @@ testcase_sanity() {
systemd-nspawn --directory="$root" --ephemeral bash -xec 'touch /ephemeral'
test ! -e "$root/ephemeral"
(! systemd-nspawn --directory="$root" \
--bind="${COVERAGE_BUILD_DIR:-$tmpdir}" \
--read-only \
bash -xec 'touch /nope')
test ! -e "$root/nope"
@ -122,7 +126,6 @@ testcase_sanity() {
test ! -e "$root/usr/read-only"
# volatile=state: rootfs is read-only, /var/ is tmpfs
systemd-nspawn --directory="$root" \
--bind="${COVERAGE_BUILD_DIR:-$tmpdir}" \
--volatile=state \
bash -xec 'test -e /usr/has-usr; mountpoint /var; touch /read-only && exit 1; touch /var/nope'
test ! -e "$root/read-only"
@ -206,13 +209,16 @@ EOF
touch "$tmpdir/foo"
# --bind=
systemd-nspawn --directory="$root" \
${COVERAGE_BUILD_DIR:+--bind="$COVERAGE_BUILD_DIR"} \
--bind="$tmpdir:/foo" \
bash -xec 'test -e /foo/foo; touch /foo/bar'
--bind="$tmpdir:/also-foo:noidmap,norbind" \
bash -xec 'test -e /foo/foo; touch /foo/bar; test -e /also-foo/bar'
test -e "$tmpdir/bar"
# --bind-ro=
systemd-nspawn --directory="$root" \
--bind-ro="$tmpdir:/foo" \
bash -xec 'test -e /foo/foo; touch /foo/baz && exit 1; true'
--bind-ro="$tmpdir:/bar:noidmap,norbind" \
bash -xec 'test -e /foo/foo; touch /foo/baz && exit 1; touch /bar && exit 1; true'
# --inaccessible=
systemd-nspawn --directory="$root" \
--inaccessible=/var \
@ -247,6 +253,22 @@ EOF
(! systemd-nspawn --network-veth --directory="$root" --port=icmp:80:8080 true)
(! systemd-nspawn --network-veth --directory="$root" --port=tcp::8080 true)
(! systemd-nspawn --network-veth --directory="$root" --port=8080: true)
# Exercise adding/removing ports from an interface
systemd-nspawn --directory="$root" \
--network-veth \
--port=6667 \
--port=80:8080 \
--port=udp:53 \
--port=tcp:22:2222 \
bash -xec 'ip addr add dev host0 10.0.0.10/24; ip a; ip addr del dev host0 10.0.0.10/24'
# --load-credential=, --set-credential=
echo "foo bar" >/tmp/cred.path
systemd-nspawn --directory="$root" \
--load-credential=cred.path:/tmp/cred.path \
--set-credential="cred.set:hello world" \
bash -xec '[[ "$(</run/host/credentials/cred.path)" == "foo bar" ]]; [[ "$(</run/host/credentials/cred.set)" == "hello world" ]]'
rm -f /tmp/cred.path
# Assorted tests
systemd-nspawn --directory="$root" --suppress-sync=yes bash -xec 'echo hello'

View File

@ -51,7 +51,7 @@ restore_locale() {
fi
}
test_locale() {
testcase_locale() {
local i output
if [[ -f /etc/locale.conf ]]; then
@ -222,7 +222,7 @@ wait_vconsole_setup() {
return 1
}
test_vc_keymap() {
testcase_vc_keymap() {
local i output vc
if [[ -z "$(localectl list-keymaps)" ]]; then
@ -292,7 +292,7 @@ test_vc_keymap() {
assert_in "VC Keymap: .unset." "$(localectl)"
}
test_x11_keymap() {
testcase_x11_keymap() {
local output
if [[ -z "$(localectl list-x11-keymap-layouts)" ]]; then
@ -431,7 +431,7 @@ XKBMODEL=pc105+inet"
assert_not_in "X11 Options:" "$output"
}
test_convert() {
testcase_convert() {
if [[ -z "$(localectl list-keymaps)" ]]; then
echo "No vconsole keymap installed, skipping test."
return
@ -552,7 +552,7 @@ test_convert() {
assert_not_in "X11 Options:" "$output"
}
test_validate() {
testcase_validate() {
if [[ -z "$(localectl list-keymaps)" ]]; then
echo "No vconsole keymap installed, skipping test."
return
@ -648,6 +648,38 @@ EOF
assert_in "XKBLAYOUT=us" "$output"
}
locale_gen_cleanup() {
# Some running apps might keep the mount point busy, hence the lazy unmount
mountpoint -q /usr/lib/locale && umount --lazy /usr/lib/locale
[[ -e /tmp/locale.gen.bak ]] && mv -f /tmp/locale.gen.bak /etc/locale.gen
return 0
}
# Issue: https://github.com/systemd/systemd/pull/27179
testcase_locale_gen_leading_space() {
if ! command -v locale-gen >/dev/null; then
echo "No locale-gen support, skipping test."
return 0
fi
[[ -e /etc/locale.gen ]] && cp -f /etc/locale.gen /tmp/locale.gen.bak
trap locale_gen_cleanup RETURN
# Overmount the existing locale-gen database with an empty directory
# to force it to regenerate locales
mount -t tmpfs tmpfs /usr/lib/locale
{
echo -e "en_US.UTF-8 UTF-8"
echo -e " en_US.UTF-8 UTF-8"
echo -e "\ten_US.UTF-8 UTF-8"
echo -e " \t en_US.UTF-8 UTF-8 \t"
} >/etc/locale.gen
localectl set-locale de_DE.UTF-8
localectl set-locale en_US.UTF-8
}
: >/failed
# Make sure the content of kbd-model-map is the one that the tests expect
@ -656,11 +688,18 @@ EOF
export SYSTEMD_KBD_MODEL_MAP=/usr/lib/systemd/tests/testdata/test-keymap-util/kbd-model-map
enable_debug
test_locale
test_vc_keymap
test_x11_keymap
test_convert
test_validate
# Create a list of all functions prefixed with testcase_
mapfile -t TESTCASES < <(declare -F | awk '$3 ~ /^testcase_/ {print $3;}')
if [[ "${#TESTCASES[@]}" -eq 0 ]]; then
echo >&2 "No test cases found, this is most likely an error"
exit 1
fi
for testcase in "${TESTCASES[@]}"; do
"$testcase"
done
touch /testok
rm /failed