Skip to content

Commit 4dc6497

Browse files
committed
Check that org.freedesktop.resolve1 is available
and short-circuit with an error message about requiring systemd >= 229 if it is unavailable. Incidental changes: 1. Ensure that the test runner sources the "update-systemd-resolved" script that lives in this repository, rather than one that might live somewhere in PATH, and 2. Update the test runner's "logger" function to support reading messages from stdin.
1 parent 86434ed commit 4dc6497

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

run-tests

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,35 @@ RUNTEST_CALLED=0
3131

3232
AUTOMATED_TESTING=1
3333

34+
# Used for determining which "update-systemd-resolved" we are supposed to
35+
# source
36+
if ! TOPLEVEL="$(git rev-parse --show-toplevel 2>/dev/null)"; then
37+
if ! [[ "${BASH_SOURCE[0]}" == */* ]]; then
38+
if ! TOPLEVEL="${PWD:-$(pwd 2>/dev/null)}"; then
39+
TOPLEVEL=.
40+
fi
41+
else
42+
TOPLEVEL="${BASH_SOURCE[0]%/*}"
43+
fi
44+
fi
45+
3446
function busctl {
47+
# Short-circuit if we got "busctl status <...>". If TEST_BUSCTL_STATUS_RC is
48+
# unset or empty, assume this is the call from the "main" function in
49+
# update-systemd-resolved, rather than an automated test checking the
50+
# behaviour of "busctl status <...>".
51+
case "${1:-}" in
52+
status)
53+
if [[ -n "${TEST_BUSCTL_STATUS_RC:-}" ]]; then
54+
busctl_called=1
55+
_log "busctl status: returning ${TEST_BUSCTL_STATUS_RC}"
56+
return "$TEST_BUSCTL_STATUS_RC"
57+
else
58+
return 0
59+
fi
60+
;;
61+
esac
62+
3563
shift 4
3664
_log "busctl called with: ${@}"
3765
# Set that busctl has been called
@@ -96,9 +124,17 @@ function resolvectl {
96124
}
97125

98126
function logger {
99-
# Remove standard options
100-
local message="$*"
101-
_log "-- ${message##* -- }"
127+
local message
128+
129+
if [[ "$*" == *' --' ]] && ! [[ -t 0 ]]; then
130+
while read -r message; do
131+
_log "$message"
132+
done
133+
else
134+
# Remove standard options
135+
message="$*"
136+
_log "-- ${message##* -- }"
137+
fi
102138
}
103139

104140
function exit {
@@ -130,7 +166,7 @@ function runtest {
130166

131167
# Source, don't run, so we don't need to export and internal functions override
132168
# external calls out to system commands
133-
source update-systemd-resolved
169+
source "${TOPLEVEL?}/update-systemd-resolved"
134170
exit_status="$?"
135171
exit_message="script exited with a ${exit_status} exit status"
136172

@@ -164,6 +200,7 @@ for TEST in tests/*.sh; do
164200
TEST_BUSCTL_DNS=""
165201
TEST_BUSCTL_DOMAINS=""
166202
TEST_BUSCTL_DNSSEC=""
203+
TEST_BUSCTL_STATUS_RC=""
167204
# Keep this random, as we will never know the ifindex up-front
168205
ip_ifindex=$((RANDOM%=64))
169206
# Clear foreign_option_*

tests/23_resolve1_dbus_presence.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
script_type="up"
2+
dev="tun23"
3+
4+
TEST_TITLE='Error if "busctl status org.freedesktop.resolve1" fails'
5+
TEST_BUSCTL_CALLED=1
6+
7+
# Mocked-up busctl function will return exit code 1 upon "busctl status <...>"
8+
TEST_BUSCTL_STATUS_RC=1
9+
EXPECT_FAILURE=1

update-systemd-resolved

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ usage() {
4444
err "${1:?${1}. }. Usage: ${SCRIPT_NAME} up|down device_name."
4545
}
4646

47+
busctl_status() {
48+
busctl status "$DBUS_DEST"
49+
}
50+
4751
busctl_call() {
4852
# Preserve busctl's exit status
4953
busctl call "$DBUS_DEST" "$DBUS_NODE" "${DBUS_DEST}.Manager" "$@" || {
@@ -404,6 +408,15 @@ main() {
404408
local dev="${1:-$dev}"
405409
shift
406410

411+
busctl_status &>/dev/null || {
412+
local -i status=$?
413+
emerg <<ERR
414+
systemd-resolved DBus interface (${DBUS_DEST}) is not available.
415+
$SCRIPT_NAME requires systemd version 229 or above.
416+
ERR
417+
return $status
418+
}
419+
407420
if [[ -z "$script_type" ]]; then
408421
usage 'No script type specified'
409422
return 1

0 commit comments

Comments
 (0)