| 1 |
#!/bin/sh |
| 2 |
set -e |
| 3 |
|
| 4 |
# Exercise the systemd helpers added by the Debian systemd integration |
| 5 |
# patch (debian/patches/systemd-receive-wal-integration.patch). These are |
| 6 |
# pure functions, so they can be checked deterministically without a running |
| 7 |
# systemd or PostgreSQL. |
| 8 |
|
| 9 |
echo "Testing barman.process systemd helpers..." |
| 10 |
|
| 11 |
python3 -c ' |
| 12 |
import os |
| 13 |
from barman.process import ( |
| 14 |
SYSTEMD_RECEIVE_WAL_UNIT_PREFIX, |
| 15 |
server_name_is_unit_safe, |
| 16 |
systemd_receive_wal_unit, |
| 17 |
systemd_unit_for_pid, |
| 18 |
_systemd_unit_from_cgroup, |
| 19 |
) |
| 20 |
|
| 21 |
unit = systemd_receive_wal_unit("main") |
| 22 |
assert unit == "[email protected]", unit |
| 23 |
assert systemd_receive_wal_unit("pg-12").startswith(SYSTEMD_RECEIVE_WAL_UNIT_PREFIX) |
| 24 |
|
| 25 |
# cgroup parser: positive matches (cgroup v2 and the v1 named hierarchy) |
| 26 |
v2 = "0::/system.slice/[email protected]\n" |
| 27 |
assert _systemd_unit_from_cgroup(v2) == "[email protected]" |
| 28 |
v1 = "5:name=systemd:/system.slice/[email protected]\n" |
| 29 |
assert _systemd_unit_from_cgroup(v1) == "[email protected]" |
| 30 |
# a server name containing dots is matched as a whole (up to .service) |
| 31 |
dotted = "0::/system.slice/[email protected]\n" |
| 32 |
assert _systemd_unit_from_cgroup(dotted) == "[email protected]" |
| 33 |
# a cgroup v2 line with a trailing sub-cgroup still resolves to the bare unit |
| 34 |
v2sub = "0::/system.slice/[email protected]/payload\n" |
| 35 |
assert _systemd_unit_from_cgroup(v2sub) == "[email protected]" |
| 36 |
|
| 37 |
# cgroup parser: negative matches (unrelated unit, no unit, empty) |
| 38 |
assert _systemd_unit_from_cgroup("0::/system.slice/barman.service\n") is None |
| 39 |
assert _systemd_unit_from_cgroup("0::/user.slice/user-1000.slice\n") is None |
| 40 |
assert _systemd_unit_from_cgroup("") is None |
| 41 |
|
| 42 |
# This very process is not supervised by a barman-receive-wal@ unit, and a |
| 43 |
# non-existent PID must be handled gracefully. |
| 44 |
assert systemd_unit_for_pid(os.getpid()) is None |
| 45 |
assert systemd_unit_for_pid(2 ** 30) is None |
| 46 |
|
| 47 |
# server-name unit-safety: conventional names are safe; anything that would need |
| 48 |
# systemd escaping (slash, whitespace, leading dot, empty) is not. |
| 49 |
for safe in ("main", "pg-12", "pg.prod", "testdb"): |
| 50 |
assert server_name_is_unit_safe(safe), safe |
| 51 |
for unsafe in ("pg/prod", "has space", ".hidden", "", ":x"): |
| 52 |
assert not server_name_is_unit_safe(unsafe), unsafe |
| 53 |
|
| 54 |
print("All systemd helper tests passed.") |
| 55 |
' |
| 56 |
|
| 57 |
# Deterministically exercise Server.start_receive_wal()'s dispatch (systemd unit |
| 58 |
# vs forked fallback) without a running systemd, covering the non-systemd path |
| 59 |
# and the no-polkit / unsafe-name fallbacks. |
| 60 |
echo "Testing Server.start_receive_wal dispatch..." |
| 61 |
|
| 62 |
python3 -c ' |
| 63 |
import types |
| 64 |
from unittest import mock |
| 65 |
from barman.server import Server |
| 66 |
|
| 67 |
|
| 68 |
def run(systemd_running, name, systemctl_ok): |
| 69 |
srv = Server.__new__(Server) |
| 70 |
srv.config = types.SimpleNamespace(name=name) |
| 71 |
calls = [] |
| 72 |
srv.systemd_receive_wal = lambda: (calls.append("unit"), systemctl_ok)[1] |
| 73 |
srv.background_receive_wal = lambda keep_descriptors: calls.append("fork") |
| 74 |
with mock.patch("barman.server.systemd_is_running", return_value=systemd_running): |
| 75 |
srv.start_receive_wal(keep_descriptors=False) |
| 76 |
return calls |
| 77 |
|
| 78 |
|
| 79 |
# No systemd: always fork (covers the non-systemd path). |
| 80 |
assert run(False, "main", True) == ["fork"], "non-systemd" |
| 81 |
# Systemd and the unit can be managed: supervise via the unit, no fork. |
| 82 |
assert run(True, "main", True) == ["unit"], "systemd unit" |
| 83 |
# Systemd but the unit cannot be managed (e.g. no polkit): fall back to fork. |
| 84 |
assert run(True, "main", False) == ["unit", "fork"], "no-polkit fallback" |
| 85 |
# Systemd but the server name is not unit-safe: fork without trying the unit. |
| 86 |
assert run(True, "pg/prod", True) == ["fork"], "unsafe name" |
| 87 |
|
| 88 |
print("start_receive_wal dispatch tests passed.") |
| 89 |
' |