Skip to content

Commit c508bfb

Browse files
committed
tests: Check stdio is blocking in sudo contexts
refs #712
1 parent 76f6eb7 commit c508bfb

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ In progress (unreleased)
2525
* :gh:issue:`1306` CI: Move sudo test users defaults into ``/etc/sudoers.d``
2626
* :gh:issue:`1306` preamble_size: Fix variability of measured command size
2727
* :gh:issue:`1306` tests: Count bytes written in ``stdio_test.StdIOTest``
28+
* :gh:issue:`1306` tests: Check stdio is blocking in sudo contexts
2829

2930

3031
v0.3.27 (2025-08-20)

tests/stdio_test.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,46 @@
1+
import unittest
2+
13
import testlib
24

35
import stdio_checks
46

57

6-
class StdIOTest(testlib.RouterMixin, testlib.TestCase):
8+
class StdIOMixin(testlib.RouterMixin):
79
"""
810
Test that stdin, stdout, and stderr conform to common expectations,
911
such as blocking IO.
1012
"""
11-
def test_can_write_stdout_1_mib(self):
13+
def check_can_write_stdout_1_mib(self, context):
1214
"""
1315
Writing to stdout should not raise EAGAIN. Regression test for
1416
https://github.com/mitogen-hq/mitogen/issues/712.
1517
"""
1618
size = 1 * 2**20
17-
context = self.router.local()
1819
nwritten = context.call(stdio_checks.shout_stdout, size)
1920
self.assertEqual(nwritten, size)
2021

21-
def test_stdio_is_blocking(self):
22-
context = self.router.local()
22+
def check_stdio_is_blocking(self, context):
2323
stdin_blocking, stdout_blocking, stderr_blocking = context.call(
2424
stdio_checks.stdio_is_blocking,
2525
)
2626
self.assertTrue(stdin_blocking)
2727
self.assertTrue(stdout_blocking)
2828
self.assertTrue(stderr_blocking)
29+
30+
31+
class LocalTest(StdIOMixin, testlib.TestCase):
32+
def test_can_write_stdout_1_mib(self):
33+
self.check_can_write_stdout_1_mib(self.router.local())
34+
35+
def test_stdio_is_blocking(self):
36+
self.check_stdio_is_blocking(self.router.local())
37+
38+
39+
class SudoTest(StdIOMixin, testlib.TestCase):
40+
@unittest.skipIf(not testlib.have_sudo_nopassword(), 'Needs passwordless sudo')
41+
def test_can_write_stdout_1_mib(self):
42+
self.check_can_write_stdout_1_mib(self.router.sudo())
43+
44+
@unittest.skipIf(not testlib.have_sudo_nopassword(), 'Needs passwordless sudo')
45+
def test_stdio_is_blocking(self):
46+
self.check_stdio_is_blocking(self.router.sudo())

tests/testlib.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,15 @@ def have_python3():
179179
return _have_cmd(['python3'])
180180

181181

182+
def have_sudo_nopassword():
183+
"""
184+
Return True if we can run `sudo` with no password, otherwise False.
185+
186+
Any cached credentials are ignored.
187+
"""
188+
return _have_cmd(['sudo', '-kn', 'true'])
189+
190+
182191
def retry(fn, on, max_attempts, delay):
183192
for i in range(max_attempts):
184193
try:

0 commit comments

Comments
 (0)