test: fix: .

This commit is contained in:
2026-05-01 22:19:43 +00:00
parent 0a54a41670
commit 35e35980b4
4 changed files with 29 additions and 27 deletions

View File

@@ -42,6 +42,8 @@ PEERS_TOKEN=${PEERS_TOKEN:-}
TG_TOKEN=${TG_TOKEN:-} TG_TOKEN=${TG_TOKEN:-}
TG_CHAT_ID=${TG_CHAT_ID:-} TG_CHAT_ID=${TG_CHAT_ID:-}
SPAM=${SPAM:-0} SPAM=${SPAM:-0}
DIG=${DIG:-dig}
GETENT=${GETENT:-getent}
STATE_DIR=${STATE_DIR:-/var/lib/sentinella} STATE_DIR=${STATE_DIR:-/var/lib/sentinella}
mkdir -p "$STATE_DIR" 2>/dev/null || { mkdir -p "$STATE_DIR" 2>/dev/null || {
@@ -81,7 +83,7 @@ is_local_ip() {
# Emits "host port ip" per non-local peer, one per line. # Emits "host port ip" per non-local peer, one per line.
resolve_peers() { resolve_peers() {
# dig +short SRV output: "<prio> <weight> <port> <target>." # dig +short SRV output: "<prio> <weight> <port> <target>."
_srv_out=$(dig +short +time=3 +tries=2 SRV "$PEERS_SRV" 2>&1) || { _srv_out=$($DIG +short +time=3 +tries=2 SRV "$PEERS_SRV" 2>&1) || {
log warn "dig SRV ${WHITE}${PEERS_SRV}${NC} failed: ${_srv_out}" log warn "dig SRV ${WHITE}${PEERS_SRV}${NC} failed: ${_srv_out}"
return 0 return 0
} }
@@ -94,7 +96,7 @@ resolve_peers() {
printf '%s\n' "$_parsed" \ printf '%s\n' "$_parsed" \
| while IFS=' ' read -r port target; do | while IFS=' ' read -r port target; do
[ -n "$target" ] || continue [ -n "$target" ] || continue
ip=$(getent hosts "$target" | awk '{print $1; exit}') ip=$($GETENT hosts "$target" | awk '{print $1; exit}')
[ -n "$ip" ] || { log warn "could not resolve ${WHITE}${target}${NC}"; continue; } [ -n "$ip" ] || { log warn "could not resolve ${WHITE}${target}${NC}"; continue; }
is_local_ip "$ip" && continue is_local_ip "$ip" && continue
printf '%s %s %s\n' "$target" "$port" "$ip" printf '%s %s %s\n' "$target" "$port" "$ip"

View File

@@ -5,5 +5,5 @@ CREATE TABLE comments (
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
content TEXT NOT NULL, content TEXT NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW() created_at TIMESTAMPTZ DEFAULT NOW()
); ) INHERITS ("hectic"."created_at");

View File

@@ -1,4 +1,4 @@
CREATE TABLE items ( CREATE TABLE items (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
name TEXT NOT NULL name TEXT NOT NULL
); ) INHERITS ("hectic"."created_at");

View File

@@ -3,9 +3,9 @@
# #
# Setup: # Setup:
# - Start a probe on 127.0.0.1:15990 # - Start a probe on 127.0.0.1:15990
# - Stub getent to resolve peers.test -> 127.0.0.1 (the probe) and 10.0.0.1 (fake peer) # - Stub dig to return SRV record for _sentinella._tcp.peers.test
# - Stub hostname to return 10.0.0.1 as the local IP so 10.0.0.1 is excluded # - Stub getent to resolve node-a.peers.test -> 127.0.0.1 (the probe)
# and 127.0.0.1 (the real probe) is kept as a peer # - Set SELF=10.0.0.1 so the local IP is excluded and 127.0.0.1 is kept as peer
# - Assert a state file appears in STATE_DIR within 15s # - Assert a state file appears in STATE_DIR within 15s
log notice "test case: ${WHITE}watcher writes state file after first successful poll" log notice "test case: ${WHITE}watcher writes state file after first successful poll"
@@ -22,46 +22,46 @@ sleep 2
# Create stubs directory # Create stubs directory
stub_dir=$(mktemp -d) stub_dir=$(mktemp -d)
# Stub getent: returns two IPs for peers.test # Stub dig: returns SRV record
# The watcher calls: $DIG +short +time=3 +tries=2 SRV "$PEERS_SRV"
# SRV format: "priority weight port target."
cat >"${stub_dir}/dig" <<'EOF'
#!/bin/sh
printf '0 10 15990 node-a.peers.test.\n'
EOF
chmod +x "${stub_dir}/dig"
# Stub getent: resolves the SRV target to the probe IP
# The watcher calls: $GETENT hosts "$target"
cat >"${stub_dir}/getent" <<'EOF' cat >"${stub_dir}/getent" <<'EOF'
#!/bin/sh #!/bin/sh
if [ "$1" = "hosts" ] && [ "$2" = "peers.test" ]; then if [ "$1" = "hosts" ] && [ "$2" = "node-a.peers.test" ]; then
printf '127.0.0.1 peers.test\n' printf '127.0.0.1 node-a.peers.test\n'
printf '10.0.0.1 peers.test\n'
else else
/usr/bin/getent "$@" /usr/bin/getent "$@"
fi fi
EOF EOF
chmod +x "${stub_dir}/getent" chmod +x "${stub_dir}/getent"
# Stub hostname: -I returns 10.0.0.1 so watcher excludes it and keeps 127.0.0.1
cat >"${stub_dir}/hostname" <<'EOF'
#!/bin/sh
case "$1" in
-I) printf '10.0.0.1\n' ;;
*) /bin/hostname "$@" ;;
esac
EOF
chmod +x "${stub_dir}/hostname"
state_dir=$(mktemp -d) state_dir=$(mktemp -d)
export PEERS_DNS="peers.test" export PEERS_SRV="_sentinella._tcp.peers.test"
export PEERS_PORT="$PORT"
export PEERS_SCHEME="http" export PEERS_SCHEME="http"
export TG_TOKEN="test-token" export TG_TOKEN="test-token"
export TG_CHAT_ID="test-chat" export TG_CHAT_ID="test-chat"
export STATE_DIR="$state_dir" export STATE_DIR="$state_dir"
export POLLING_INTERVAL_SEC="1" export POLLING_INTERVAL_SEC="1"
export SPAM="0" export SPAM="0"
unset SELF # ensure auto-detection is used export SELF="10.0.0.1" # exclude this IP, keep 127.0.0.1 as peer
export DIG="${stub_dir}/dig"
export GETENT="${stub_dir}/getent"
PATH="${stub_dir}:${PATH}" watcher & watcher &
watcher_pid=$! watcher_pid=$!
log info "waiting for state file in $state_dir ..." log info "waiting for state file in $state_dir ..."
peer_url="http://127.0.0.1:${PORT}" peer_host="node-a.peers.test"
state_file="${state_dir}/$(printf '%s' "$peer_url" | cksum | awk '{print $1}').state" state_file="${state_dir}/$(printf '%s' "$peer_host" | cksum | awk '{print $1}').state"
wait_for_file "$state_file" 15 wait_for_file "$state_file" 15
state=$(cat "$state_file") state=$(cat "$state_file")