You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/engine/tests/test_fabric_proc_sysctl.py

215 lines
7.9 KiB
Python

"""Exercise the production proc/sys reader with real POSIX shells."""
from __future__ import annotations
import base64
import os
import re
import shlex
import subprocess
from pathlib import Path
import pytest
from virsh_panel.fabric import (
_BOOT_HELPER,
_FABRIC_SYSCTL_VALIDATION_SH,
_apply_script,
_observe_script,
parse_fabric_plan,
)
SHELLS = tuple(path for path in ("/bin/sh", "/bin/dash", "/bin/bash")
if os.access(path, os.X_OK))
@pytest.fixture(params=SHELLS)
def shell(request):
return request.param
def run_reader(shell, path, prefix="", reader="vc_read_proc_sysctl",
source=_FABRIC_SYSCTL_VALIDATION_SH):
return subprocess.run(
[shell, "-c", prefix + "\n" + source
+ f'\n{reader} "$1"\n', "sysctl-reader", str(path)],
text=True, capture_output=True, timeout=5, check=False,
)
@pytest.mark.parametrize("value", ("0", "1", "2"))
def test_buffered_reader_accepts_one_kernel_integer_line(tmp_path, shell, value):
path = tmp_path / "sysctl"
path.write_text(value + "\n", encoding="ascii")
result = run_reader(shell, path)
assert result.returncode == 0, result.stderr
assert result.stdout == value
@pytest.mark.parametrize("payload", (
"", "1", "1\n\n", "1\n2\n", "1\ninvalid\n", " 1\n", "1 \n",
"1\r\n", "3\n", "-1\n", "01\n", "1.0\n",
))
def test_buffered_reader_rejects_empty_multiline_or_malformed_values(
tmp_path, shell, payload):
path = tmp_path / "sysctl"
path.write_text(payload, encoding="ascii")
result = run_reader(shell, path)
assert result.returncode != 0
assert result.stdout == ""
@pytest.mark.parametrize("target", ("missing", "directory", "partial_io"))
def test_buffered_reader_rejects_io_failure_even_after_a_valid_digit(
tmp_path, shell, target):
path = tmp_path / "sysctl"
prefix = ""
if target == "directory":
path.mkdir()
elif target == "partial_io":
path.write_text("1\n", encoding="ascii")
# A partial read must not become valid merely because stdout already
# contained a valid-looking value before cat reported an I/O error.
prefix = 'cat() { command cat "$@"; return 42; }'
result = run_reader(shell, path, prefix)
assert result.returncode != 0
assert result.stdout == ""
def test_buffered_reader_survives_proc_single_read_eof(tmp_path, shell):
path = tmp_path / "sysctl"
path.write_text("1\n", encoding="ascii")
# Model the observable result of a one-byte shell read from an integer
# proc sysctl: it assigns the digit, then sees EOF before the newline.
# The buffered production reader must not depend on that builtin result.
prefix = '''read() { proc_digit=1; return 1; }
legacy_reader() {
proc_digit=
IFS= read -r proc_digit < "$1" || return 1
printf '%s' "$proc_digit"
}
'''
legacy = run_reader(shell, path, prefix, "legacy_reader")
assert legacy.returncode != 0
buffered = run_reader(shell, path, prefix)
assert buffered.returncode == 0, buffered.stderr
assert buffered.stdout == "1"
@pytest.fixture(scope="module")
def production_scripts():
plan = parse_fabric_plan({
"fabric_id": "proc-test", "region": "hk", "revision": 1,
"overlay_cidr": "100.64.1.0/24", "listen_port": 51820,
"nodes": [
{"host_id": "a", "overlay_ip": "100.64.1.1",
"endpoint": "203.0.113.1:51820"},
{"host_id": "b", "overlay_ip": "100.64.1.2",
"endpoint": "203.0.113.2:51820"},
],
})
keys = {node.host_id: base64.b64encode(bytes([index + 1]) * 32).decode()
for index, node in enumerate(plan.nodes)}
return {
"apply": _apply_script(plan, plan.nodes[0], keys),
"boot": _BOOT_HELPER,
"observe": _observe_script(
("100.64.1.2",), "100.64.1.1",
expected_digest=plan.digest,
),
}
@pytest.mark.parametrize("script_name", ("apply", "boot", "observe"))
def test_each_production_sysctl_reader_uses_buffered_io(
tmp_path, shell, production_scripts, script_name):
script = production_scripts[script_name]
reader = "read_observed_sysctl" if script_name == "observe" else "read_sysctl"
function = re.search(
rf"^{reader}\(\) \{{\n.*?^\}}", script, re.MULTILINE | re.DOTALL)
assert function is not None
shared_reader = re.search(
r"^vc_read_proc_sysctl\(\) \{\n.*?^\}",
script, re.MULTILINE | re.DOTALL)
assert shared_reader is not None
path = tmp_path / "sysctl"
path.write_text("1\n", encoding="ascii")
result = run_reader(
shell, path, function.group(0), reader, shared_reader.group(0))
assert result.returncode == 0, result.stderr
assert result.stdout == "1"
assert 'IFS= read -r sysctl_value' not in script
assert 'IFS= read -r observed_sysctl_value' not in script
assert 'IFS= read -r vc_rpf_value' not in script
@pytest.mark.parametrize("script_name", ("apply", "boot"))
@pytest.mark.parametrize("value,expected_rc", (("0", 71), ("1", 0), ("2", 71)))
def test_preflight_and_boot_still_require_exact_ip_forward_one(
tmp_path, shell, production_scripts, script_name, value, expected_rc):
script = production_scripts[script_name]
reader = re.search(
r'^read_sysctl\(\) \{\n vc_read_proc_sysctl "\$1"\n\}',
script, re.MULTILINE)
check = re.search(
r'^if ! sysctl_forward=\$\(read_sysctl /proc/sys/net/ipv4/ip_forward\); '
r'then exit 71; fi\n\[ "\$sysctl_forward" = 1 \] \|\| exit 71',
script, re.MULTILINE)
assert reader is not None and check is not None
path = tmp_path / "sysctl"
path.write_text(value + "\n", encoding="ascii")
# Phase logging may occur between the reader and the check; extract only
# these exact fragments, never intervening rendered helper/transaction code.
check_script = reader.group(0) + "\n" + check.group(0).replace(
"/proc/sys/net/ipv4/ip_forward", '"$1"')
result = subprocess.run(
[shell, "-c", _FABRIC_SYSCTL_VALIDATION_SH + "\n" + check_script,
"sysctl-preflight", str(path)],
text=True, capture_output=True, timeout=5, check=False,
)
assert result.returncode == expected_rc, result.stderr
@pytest.mark.parametrize("io_fails", (False, True))
def test_platform_rp_filter_write_requires_buffered_exact_two_readback(
tmp_path, shell, io_fails):
owner = tmp_path / "owner"
owner.write_text("virt-cloud-wg-fabric-v1\n", encoding="ascii")
path = tmp_path / "rp_filter"
path.write_text("1\n", encoding="ascii")
setter = re.search(
r"^vc_fabric_rp_filter_set\(\) \{\n.*?^\}",
_FABRIC_SYSCTL_VALIDATION_SH, re.MULTILINE | re.DOTALL)
assert setter is not None
source = _FABRIC_SYSCTL_VALIDATION_SH + "\n" + setter.group(0).replace(
'vc_rpf_path="/proc/sys/net/ipv4/conf/$IFACE/rp_filter"',
'vc_rpf_path="$RPF_FILE"',
)
prefix = (f"OWNER={shlex.quote(str(owner))}\n"
f"RPF_FILE={shlex.quote(str(path))}\nWG=:; IFACE=wg-fabric0\n")
if io_fails:
prefix += '''cat() {
command cat "$@" || return 1
[ "$1" != "$RPF_FILE" ] || return 42
}
'''
result = run_reader(
shell, path, prefix, "vc_fabric_rp_filter_set", source)
assert path.read_text(encoding="ascii") == "2\n"
assert result.returncode == (1 if io_fails else 0), result.stderr
@pytest.mark.parametrize("path", (
"/proc/sys/net/ipv4/ip_forward", "/proc/sys/net/ipv4/conf/all/rp_filter",
"/proc/sys/net/ipv4/conf/default/rp_filter",
))
def test_buffered_reader_reads_real_linux_proc_sysctl(shell, path):
source = Path(path)
if not source.is_file():
pytest.skip("real Linux proc sysctls are unavailable on this test host")
expected = source.read_text(encoding="ascii").strip()
assert expected in ("0", "1", "2")
result = run_reader(shell, source)
assert result.returncode == 0, result.stderr
assert result.stdout == expected