Skip to content

Commit

Permalink
Detect --suppress-sync=true and limit the skips to that
Browse files Browse the repository at this point in the history
Call `os.open("", os.O_RDONLY | os.O_SYNC)` and check the errno to
detect whether `--suppress-sync=true` is actually used, and skip
the tests only in that scenario.
  • Loading branch information
mgorny committed Sep 20, 2024
1 parent c4dbdeb commit e580bb5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
33 changes: 26 additions & 7 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"without_optimizer",
"force_not_colorized",
"BrokenIter",
"in_systemd_nspawn",
"in_systemd_nspawn", "in_systemd_nspawn_sync_suppressed",
]


Expand Down Expand Up @@ -2879,16 +2879,35 @@ def __iter__(self):
def in_systemd_nspawn() -> bool:
"""
Test whether the test suite is running inside of systemd-nspawn container
Return True if the test suite is being run inside a systemd-nspawn
container, False otherwise. This can be used to skip tests that are
known to be reliable inside this kind of virtualization, for example
tests that are relying on fsync() not being stubbed out
(as ``systemd-nspawn --suppress-sync=true` does).
"""

try:
with open("/run/systemd/container", "rb") as fp:
return fp.read().rstrip() == b"systemd-nspawn"
except FileNotFoundError:
return False


def in_systemd_nspawn_sync_suppressed() -> bool:
"""
Test whether the test suite is runing in systemd-nspawn with ``--suppress-sync=true``
Return True if the test suite is being run inside a systemd-nspawn
container and ``--suppress-sync=true`` option is detected to be used,
False otherwise. This can be used to skip tests that rely
on ``fsync()`` calls and similar not being intercepted.
"""

if hasattr(os, "O_SYNC") and in_systemd_nspawn():
import errno

# If systemd-nspawn is used, O_SYNC flag will immediately trigger
# EINVAL. Otherwise, ENOENT will be given instead.
try:
with os.open("", os.O_RDONLY | os.O_SYNC):
pass
except OSError as err:
if err.errno == errno.EINVAL:
return True

return False
4 changes: 2 additions & 2 deletions Lib/test/test_mmap.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from test.support import (
requires, _2G, _4G, gc_collect, cpython_only, is_emscripten, is_apple,
in_systemd_nspawn,
in_systemd_nspawn_sync_suppressed,
)
from test.support.import_helper import import_module
from test.support.os_helper import TESTFN, unlink
Expand Down Expand Up @@ -840,7 +840,7 @@ def test_flush_return_value(self):
mm.write(b'python')
result = mm.flush()
self.assertIsNone(result)
if sys.platform.startswith(('linux', 'android')) and not in_systemd_nspawn():
if sys.platform.startswith(('linux', 'android')) and not in_systemd_nspawn_sync_suppressed():
# 'offset' must be a multiple of mmap.PAGESIZE on Linux.
# See bpo-34754 for details.
self.assertRaises(OSError, mm.flush, 1, len(b'python'))
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -2355,7 +2355,7 @@ class TestInvalidFD(unittest.TestCase):
singles_fildes = {"fchdir"}
# systemd-nspawn --suppress-sync=true does not verify fd passed
# fdatasync() and fsync(), and always returns success
if not support.in_systemd_nspawn():
if not support.in_systemd_nspawn_sync_suppressed():
singles += ["fdatasync", "fsync"]
singles_fildes |= {"fdatasync", "fsync"}
#singles.append("close")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Skip ``test_os`` and ``test_mmap`` tests that are failing
with ``--suppress-sync=true`` when running inside a systemd-nspawn
container.
Detect whether the test suite is running inside a systemd-nspawn container
with ``--suppress-sync=true`` option, and skip the `test_os`` and ``test_mmap``
tests that are failing in this scenario.

0 comments on commit e580bb5

Please sign in to comment.