| 1 | import re |
| 2 | import resource |
| 3 | import subprocess |
| 4 | from pathlib import Path |
| 5 | |
| 6 | import pytest |
| 7 | |
| 8 | |
| 9 | REPO_ROOT = Path(__file__).resolve().parents[1] |
| 10 | INITIALIZE_SCRIPT = REPO_ROOT / "docker" / "run" / "fs" / "exe" / "initialize.sh" |
| 11 | |
| 12 | |
| 13 | def _raise_limit_function() -> str: |
| 14 | text = INITIALIZE_SCRIPT.read_text(encoding="utf-8") |
| 15 | match = re.search(r"^raise_open_file_limit\(\) \{\n.*?^\}\n", text, re.M | re.S) |
| 16 | assert match, "initialize.sh must define raise_open_file_limit" |
| 17 | return match.group(0) |
| 18 | |
| 19 | |
| 20 | def _run_bash(script: str) -> subprocess.CompletedProcess[str]: |
| 21 | return subprocess.run( |
| 22 | ["bash", "-c", script], |
| 23 | check=True, |
| 24 | text=True, |
| 25 | capture_output=True, |
| 26 | ) |
| 27 | |
| 28 | |
| 29 | def test_initialize_raises_soft_open_file_limit_to_requested_target(): |
| 30 | function = _raise_limit_function() |
| 31 | |
| 32 | result = _run_bash( |
| 33 | f""" |
| 34 | set -euo pipefail |
| 35 | {function} |
| 36 | ulimit -S -n 1024 |
| 37 | A0_NOFILE_LIMIT=4096 |
| 38 | raise_open_file_limit |
| 39 | test "$(ulimit -S -n)" = "4096" |
| 40 | """ |
| 41 | ) |
| 42 | |
| 43 | assert "Raised open file soft limit from 1024 to 4096" in result.stdout |
| 44 | |
| 45 | |
| 46 | def test_initialize_caps_open_file_limit_at_hard_limit(): |
| 47 | _soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) |
| 48 | if hard != resource.RLIM_INFINITY and hard < 2048: |
| 49 | pytest.skip("host hard open-file limit is too low for this regression test") |
| 50 | |
| 51 | function = _raise_limit_function() |
| 52 | |
| 53 | result = _run_bash( |
| 54 | f""" |
| 55 | set -euo pipefail |
| 56 | {function} |
| 57 | ulimit -S -n 1024 |
| 58 | ulimit -H -n 2048 |
| 59 | A0_NOFILE_LIMIT=65535 |
| 60 | raise_open_file_limit |
| 61 | test "$(ulimit -S -n)" = "2048" |
| 62 | """ |
| 63 | ) |
| 64 | |
| 65 | assert "Raised open file soft limit from 1024 to 2048" in result.stdout |