mirror of
https://github.com/hasso5703/alzeo.git
synced 2026-12-05 12:24:28 +01:00
tests: the suite runs on a machine without ss
Two parts of the offline suite needed iproute2's ss from the machine
that runs it. tests/suite_reaper.py named a leftover's listeners with
`ss -Htlnp`; without ss it died there on a traceback, before stop(),
so what it was there to stop kept running, and each test of it that
leaves one waited its 60 s on a pipe the leftover held (3 errors, 181
s). It now reads the same from /proc when ss is missing: the
processes' socket descriptors, then /proc/net/tcp and tcp6, spelled as
ss spells them. With ss present nothing changes. And the units-only
run of install.sh used the host's ss, which the preflight requires
("ss not found") although a lane's run asks it nothing; it gets a stub
as the other box commands have.
Found in a cloud review of v1.36.1 (pull request #52), in a container
without iproute2. The reference box and the GitHub runner have ss, so
nothing changes there.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0a279c580a
commit
2e7cefee3c
3 files changed
+88
-4
No files matched your search
+48
-1
@@ -24,6 +24,8 @@ import ctypes
|
||||
import os
|
||||
import re
|
||||
import signal
|
||||
import socket
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
@@ -72,7 +74,13 @@ def descendants(root):
|
||||
|
||||
def listening(pids):
|
||||
"""{pid: [local address, ...]} for the TCP listeners these processes hold."""
|
||||
out = subprocess.run(["ss", "-Htlnp"], capture_output=True, text=True).stdout
|
||||
try:
|
||||
out = subprocess.run(["ss", "-Htlnp"], capture_output=True, text=True).stdout
|
||||
except OSError:
|
||||
# no ss on this machine (a container without iproute2): the reaper died here on a
|
||||
# traceback and left running what it was there to stop, so a test that waited on
|
||||
# its output waited for its timeout (found in a cloud review, 2026-10-18)
|
||||
return listening_in_proc(pids)
|
||||
held = {}
|
||||
for line in out.splitlines():
|
||||
cols = line.split()
|
||||
@@ -83,6 +91,45 @@ def listening(pids):
|
||||
return held
|
||||
|
||||
|
||||
def proc_address(field, family):
|
||||
"""A /proc/net/tcp or tcp6 local address as ss spells it: the kernel prints each 32-bit
|
||||
word of the address in this machine's byte order, then the port, in hex."""
|
||||
addr, port = field.split(":")
|
||||
words = [int(addr[i:i + 8], 16) for i in range(0, len(addr), 8)]
|
||||
host = socket.inet_ntop(family, struct.pack(f"={len(words)}I", *words))
|
||||
return f"{host}:{int(port, 16)}" if family == socket.AF_INET else f"[{host}]:{int(port, 16)}"
|
||||
|
||||
|
||||
def listening_in_proc(pids):
|
||||
"""listening(), read from /proc: the processes' socket descriptors, then the listeners."""
|
||||
owner = {}
|
||||
for pid in pids:
|
||||
try:
|
||||
fds = os.listdir(f"/proc/{pid}/fd")
|
||||
except OSError:
|
||||
continue # gone, or not ours to read
|
||||
for fd in fds:
|
||||
try:
|
||||
link = os.readlink(f"/proc/{pid}/fd/{fd}")
|
||||
except OSError:
|
||||
continue # closed between the listing and the read
|
||||
if link.startswith("socket:["):
|
||||
owner[link[len("socket:["):-1]] = pid
|
||||
held = {}
|
||||
for table, family in (("/proc/net/tcp", socket.AF_INET), ("/proc/net/tcp6", socket.AF_INET6)):
|
||||
try:
|
||||
with open(table) as f:
|
||||
rows = f.read().splitlines()[1:]
|
||||
except OSError:
|
||||
continue # not on this machine (tcp6 without IPv6)
|
||||
for row in rows:
|
||||
cols = row.split()
|
||||
# cols[3] is the state, 0A for LISTEN; cols[9] the socket's inode
|
||||
if len(cols) > 9 and cols[3] == "0A" and cols[9] in owner:
|
||||
held.setdefault(owner[cols[9]], []).append(proc_address(cols[1], family))
|
||||
return held
|
||||
|
||||
|
||||
def command_line(pid):
|
||||
try:
|
||||
with open(f"/proc/{pid}/cmdline", "rb") as f:
|
||||
|
||||
@@ -47,7 +47,11 @@ def run_units_only(lane):
|
||||
("docker", f'case "$1 $2" in "info "*) echo "{docker_root}" ;; "image inspect") exit 1 ;; esac\nexit 0\n'),
|
||||
("sudo", 'while [ -n "${1:-}" ] && [ "${1#-}" != "$1" ]; do shift; done\n'
|
||||
'[ "$#" -eq 0 ] || [ "$1" = true ] && exit 0\n"$@"\n'),
|
||||
("systemctl", 'case "$1" in is-enabled|is-active) exit 1 ;; esac\nexit 0\n')):
|
||||
("systemctl", 'case "$1" in is-enabled|is-active) exit 1 ;; esac\nexit 0\n'),
|
||||
# the preflight has to find it, and a lane's run then asks it nothing (the parent
|
||||
# checked the ports): the host's was used, and a host without iproute2 died there,
|
||||
# "ss not found" (a cloud container, 2026-10-18)
|
||||
("ss", 'exit 0\n')):
|
||||
(stubs / name).write_text(f'#!/bin/bash\necho "{name} $*" >> "{calls}"\n' + body)
|
||||
(stubs / name).chmod(0o755)
|
||||
env, fenced = wall.fenced_env(home=home, first=str(stubs), QWEN38_SECONDARY=lane, QWEN38_UNITS_ONLY="1",
|
||||
|
||||
@@ -9,13 +9,18 @@ environment variable or a session, which a test's subprocess is free to drop.
|
||||
"""
|
||||
import os
|
||||
import pathlib
|
||||
import shutil
|
||||
import socket
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
import unittest
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
|
||||
import suite_reaper # noqa: E402
|
||||
|
||||
REPO = pathlib.Path(__file__).resolve().parents[1]
|
||||
REAPER = str(REPO / "tests" / "suite_reaper.py")
|
||||
|
||||
@@ -24,9 +29,12 @@ LISTENER = ("import socket, sys, time; s = socket.socket(); s.bind(('127.0.0.1',
|
||||
"open(sys.argv[1], 'w').write(str(s.getsockname()[1])); time.sleep(60)")
|
||||
|
||||
|
||||
def reaper(command, grace="1"):
|
||||
def reaper(command, grace="1", path=None):
|
||||
env = dict(os.environ, SUITE_REAPER_GRACE_S=grace)
|
||||
if path:
|
||||
env["PATH"] = path
|
||||
return subprocess.run([sys.executable, REAPER, "bash", "-c", command], capture_output=True,
|
||||
text=True, timeout=60, env=dict(os.environ, SUITE_REAPER_GRACE_S=grace))
|
||||
text=True, timeout=60, env=env)
|
||||
|
||||
|
||||
def refused(port):
|
||||
@@ -85,6 +93,31 @@ class TheCommandsOwnLeftovers(unittest.TestCase):
|
||||
self.assertNotIn("listening socket", r.stdout)
|
||||
self.assertTrue(gone(pid))
|
||||
|
||||
def test_without_ss_the_listener_is_still_named_then_stopped(self):
|
||||
# a machine without iproute2 (a cloud container, 2026-10-18): the reaper died on a
|
||||
# traceback looking for ss and left the listener running, so each test above that
|
||||
# leaves one waited its 60 s on a pipe the listener held. ss is kept off the PATH
|
||||
# here, wherever it is installed.
|
||||
tools = os.path.join(self.dir.name, "bin")
|
||||
os.mkdir(tools)
|
||||
for tool in ("bash", "sleep"):
|
||||
os.symlink(shutil.which(tool), os.path.join(tools, tool))
|
||||
r = reaper(f'{sys.executable} -c "{LISTENER}" "{self.portfile}" & {self.wait_for_port()}',
|
||||
path=tools)
|
||||
port = int(pathlib.Path(self.portfile).read_text())
|
||||
self.assertEqual(r.returncode, 3, r.stdout + r.stderr)
|
||||
self.assertIn(f"127.0.0.1:{port} held by pid", r.stdout)
|
||||
self.assertTrue(refused(port), "the listener must be stopped once reported")
|
||||
|
||||
def test_an_address_read_in_proc_is_spelled_as_ss_spells_it(self):
|
||||
# the kernel's spelling of each, built with the address's words in this machine's
|
||||
# order, as the kernel prints them; the IPv6 one is checked even where IPv6 is off
|
||||
for family, host, spelled in ((socket.AF_INET, "127.0.0.1", "127.0.0.1:30001"),
|
||||
(socket.AF_INET6, "::1", "[::1]:30001")):
|
||||
raw = socket.inet_pton(family, host)
|
||||
field = "".join(f"{w:08X}" for w in struct.unpack(f"={len(raw) // 4}I", raw)) + ":7531"
|
||||
self.assertEqual(suite_reaper.proc_address(field, family), spelled)
|
||||
|
||||
def test_a_process_on_its_way_out_is_given_the_grace(self):
|
||||
r = reaper("(sleep 0.3) & exit 0", grace="3")
|
||||
self.assertEqual((r.returncode, r.stdout), (0, ""))
|
||||
|
||||
Reference in new issue
Block a user