Skip to content

Commit addde3e

Browse files
committed
fix(tester): accept empty systemd inventories
1 parent 910b877 commit addde3e

3 files changed

Lines changed: 49 additions & 12 deletions

File tree

‎scripts/install-tester.sh‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ acquire_lifecycle_lock() {
9090
}
9191

9292
reject_other_roles() {
93-
local path inventory_kind inventory
93+
local path inventory_kind inventory_status inventory unit
9494
for path in "$(root_path /etc/ci-fleet)" "$(root_path /var/lib/ci-fleet)" "$(root_path /opt/ci-fleet/current)" \
9595
"$(root_path /etc/ci-fleet-deployer)" "$(root_path /var/lib/ci-fleet-deployer)" "$(root_path /opt/ci-fleet-deployer)" \
9696
"$(root_path /etc/ci-fleet-status)" "$(root_path /var/lib/ci-fleet-status)" \
@@ -100,10 +100,17 @@ reject_other_roles() {
100100
[[ ! -e $path && ! -L $path ]] || die 'another ci-fleet host role is present'
101101
done
102102
for inventory_kind in list-unit-files list-units; do
103-
inventory=$(systemctl "$inventory_kind" --all --plain --no-legend --no-pager \
104-
'ci-fleet-deployer*' 'ci-fleet-health*' 'ci-fleet-cleanup*' 'ci-fleet-reconcile*' \
105-
'ci-fleet-drift*' 'ci-fleet-status-receiver*' 'actions.runner*') || die 'foreign systemd unit inventory could not be inspected'
106-
[[ -z $inventory ]] || die 'another ci-fleet host role is present'
103+
inventory_status=0
104+
inventory=$(SYSTEMD_COLORS=0 systemctl "$inventory_kind" --all --full --plain --no-legend --no-pager 2>&1) || inventory_status=$?
105+
# list-unit-files returns status 1 for an empty inventory without diagnostics.
106+
if ((inventory_status != 0)); then
107+
[[ $inventory_kind == list-unit-files && $inventory_status == 1 && -z $inventory ]] || die 'foreign systemd unit inventory could not be inspected'
108+
fi
109+
while read -r unit _; do
110+
case $unit in
111+
ci-fleet-deployer*|ci-fleet-health*|ci-fleet-cleanup*|ci-fleet-reconcile*|ci-fleet-drift*|ci-fleet-status-receiver*|actions.runner*) die 'another ci-fleet host role is present' ;;
112+
esac
113+
done <<<"$inventory"
107114
done
108115
}
109116

‎scripts/test-install-tester.sh‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ trap cleanup EXIT
99
fail() { printf 'FAIL %s\n' "$*" >&2; exit 1; }
1010
root=$tmp/root
1111
fake_bin=$tmp/bin
12+
real_systemctl=$(command -v systemctl || true)
1213
mkdir -p "$fake_bin" "$root/etc/ci-fleet-tester/environments" "$root/etc/ci-fleet-tester/definitions" "$root/etc/ci-fleet-tester/secrets" "$root/var/lib/ci-fleet-tester/environments" "$root/var/lib/docker" "$root/var/lib/fake-tester-volume" "$root/var/run" "$root/etc/systemd/system"
1314
mkdir -p "$root/run/lock"; chmod 1777 "$root/run/lock"
1415
chmod 700 "$root/etc/ci-fleet-tester" "$root/etc/ci-fleet-tester/environments" "$root/etc/ci-fleet-tester/definitions" "$root/etc/ci-fleet-tester/secrets" "$root/var/lib/ci-fleet-tester" "$root/var/lib/ci-fleet-tester/environments"
@@ -30,7 +31,14 @@ printf '%s\n' "$*" >>"${FAKE_TESTER_SYSTEMCTL_LOG:?}"
3031
if [[ ${FAKE_TESTER_SYSTEMCTL_FAIL_IF_UNITS_MISSING:-0} == 1 && $1 == disable && ! -e ${CI_FLEET_ROOT_PREFIX:?}/etc/systemd/system/ci-fleet-tester-health.timer ]]; then exit 5; fi
3132
if [[ ${FAKE_TESTER_SYSTEMCTL_FAIL_WAIT:-0} == 1 && $1 == start && " $* " == *' ci-fleet-tester-health.service '* && " $* " != *' --no-block '* ]]; then exit 7; fi
3233
if [[ $1 == list-unit-files || $1 == list-units ]]; then
33-
[[ ${FAKE_TESTER_UNIT_INVENTORY_FAIL:-0} != 1 ]] || exit 7
34+
if [[ ${FAKE_TESTER_UNIT_INVENTORY_FAIL:-0} == 1 ]]; then printf 'inventory inspection failed\n' >&2; exit "${FAKE_TESTER_UNIT_INVENTORY_FAILURE_CODE:-7}"; fi
35+
if [[ ${FAKE_TESTER_EMPTY_UNIT_INVENTORY:-0} == 1 ]]; then
36+
[[ $1 != list-unit-files ]] || exit 1
37+
exit 0
38+
fi
39+
if [[ $1 == list-unit-files && " $* " == *'ci-fleet-'* && -z ${FAKE_TESTER_FOREIGN_UNIT:-} ]]; then exit 1; fi
40+
printf 'unrelated.service enabled\n'
41+
printf 'ci-fleet-tester-health.service enabled\n'
3442
if [[ -n ${FAKE_TESTER_FOREIGN_UNIT:-} && ${FAKE_TESTER_FOREIGN_UNIT_KIND:-$1} == "$1" ]]; then printf '%s enabled\n' "$FAKE_TESTER_FOREIGN_UNIT"; fi
3543
exit 0
3644
fi
@@ -111,6 +119,17 @@ write_environment() {
111119
chmod 600 "$root/etc/ci-fleet-tester/environments/$id.env"
112120
}
113121

122+
if [[ -n $real_systemctl ]]; then
123+
offline_root=$tmp/offline-systemd
124+
mkdir -p "$offline_root/etc/systemd/system"
125+
offline_status=0
126+
SYSTEMD_COLORS=0 "$real_systemctl" --root="$offline_root" list-unit-files --all --full --plain --no-legend --no-pager >"$tmp/offline-units.out" 2>&1 || offline_status=$?
127+
[[ $offline_status == 0 || $offline_status == 1 && ! -s $tmp/offline-units.out ]] || fail 'empty systemd inventory returned an inspection error'
128+
[[ ! -s $tmp/offline-units.out ]] || fail 'offline root unexpectedly listed unit files'
129+
printf '[Unit]\nDescription=Example foreign unit\n[Service]\nExecStart=/bin/true\n' >"$offline_root/etc/systemd/system/ci-fleet-deployer.service"
130+
SYSTEMD_COLORS=0 "$real_systemctl" --root="$offline_root" list-unit-files --all --full --plain --no-legend --no-pager >"$tmp/offline-units.out"
131+
grep -q '^ci-fleet-deployer.service ' "$tmp/offline-units.out" || fail 'unfiltered systemd inventory omitted a foreign unit'
132+
fi
114133
clean_root=$tmp/clean-root
115134
mkdir -p "$clean_root/var/run" "$clean_root/var/lib/docker" "$clean_root/run/lock"
116135
chmod 1777 "$clean_root/run/lock"
@@ -121,6 +140,7 @@ for marker in etc/ci-fleet-tester var/lib/ci-fleet-tester opt/ci-fleet-tester; d
121140
done
122141
if FAKE_TESTER_EMPTY_ADDRESS_POOLS=1 "$runtime" --check >/dev/null 2>&1; then fail 'runtime accepted absent explicit Docker address pools'; fi
123142
"$runtime" --check | grep -Fq CHECK_OK || fail 'runtime preflight failed'
143+
FAKE_TESTER_EMPTY_UNIT_INVENTORY=1 "$runtime" --check >/dev/null || fail 'empty foreign unit inventory blocked runtime preflight'
124144
cp "$root/etc/ci-fleet-tester/tester.env" "$tmp/tester.env.valid"
125145
sed 's@https://tester-probe.invalid/health@https://elsewhere.invalid/health@' "$tmp/tester.env.valid" >"$root/etc/ci-fleet-tester/tester.env"
126146
if "$runtime" --check >/dev/null 2>&1; then fail 'check accepted different DNS and HTTPS probe hosts'; fi
@@ -484,8 +504,11 @@ for inventory_kind in list-unit-files list-units; do
484504
grep -Fq 'another ci-fleet host role is present' "$tmp/unit-rejection.out" || fail 'runtime unit failed outside admission check'
485505
done
486506
done
487-
if FAKE_TESTER_UNIT_INVENTORY_FAIL=1 "$installer" --check >/dev/null 2>&1; then fail 'installer accepted unavailable foreign unit inventory'; fi
488-
if FAKE_TESTER_UNIT_INVENTORY_FAIL=1 "$runtime" --health >/dev/null 2>&1; then fail 'runtime accepted unavailable foreign unit inventory'; fi
507+
FAKE_TESTER_EMPTY_UNIT_INVENTORY=1 "$installer" --check >/dev/null || fail 'empty foreign unit inventory blocked installer check'
508+
for inventory_error in 1 7; do
509+
if FAKE_TESTER_UNIT_INVENTORY_FAIL=1 FAKE_TESTER_UNIT_INVENTORY_FAILURE_CODE=$inventory_error "$installer" --check >/dev/null 2>&1; then fail 'installer accepted unavailable foreign unit inventory'; fi
510+
if FAKE_TESTER_UNIT_INVENTORY_FAIL=1 FAKE_TESTER_UNIT_INVENTORY_FAILURE_CODE=$inventory_error "$runtime" --health >/dev/null 2>&1; then fail 'runtime accepted unavailable foreign unit inventory'; fi
511+
done
489512
printf '# drift\n' >>"$installed_unit"
490513
if "$installer" --check --config /etc/ci-fleet-tester/tester.env >/dev/null 2>&1; then fail 'installed unit drift passed check'; fi
491514
cp "$release/host/systemd/ci-fleet-tester-health.service" "$installed_unit"

‎scripts/tester-runtime.sh‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,17 @@ if [[ $action != --remove && $action != --cleanup ]]; then
448448
[[ ! -e $role_unit && ! -L $role_unit ]] || die 'another ci-fleet host role is present'
449449
done
450450
for inventory_kind in list-unit-files list-units; do
451-
inventory=$(systemctl "$inventory_kind" --all --plain --no-legend --no-pager \
452-
'ci-fleet-deployer*' 'ci-fleet-health*' 'ci-fleet-cleanup*' 'ci-fleet-reconcile*' \
453-
'ci-fleet-drift*' 'ci-fleet-status-receiver*' 'actions.runner*') || die 'foreign systemd unit inventory could not be inspected'
454-
[[ -z $inventory ]] || die 'another ci-fleet host role is present'
451+
inventory_status=0
452+
inventory=$(SYSTEMD_COLORS=0 systemctl "$inventory_kind" --all --full --plain --no-legend --no-pager 2>&1) || inventory_status=$?
453+
# list-unit-files returns status 1 for an empty inventory without diagnostics.
454+
if ((inventory_status != 0)); then
455+
[[ $inventory_kind == list-unit-files && $inventory_status == 1 && -z $inventory ]] || die 'foreign systemd unit inventory could not be inspected'
456+
fi
457+
while read -r unit _; do
458+
case $unit in
459+
ci-fleet-deployer*|ci-fleet-health*|ci-fleet-cleanup*|ci-fleet-reconcile*|ci-fleet-drift*|ci-fleet-status-receiver*|actions.runner*) die 'another ci-fleet host role is present' ;;
460+
esac
461+
done <<<"$inventory"
455462
done
456463
fi
457464
docker_socket=$(root_path /var/run/docker.sock)

0 commit comments

Comments
 (0)