dumb-init/tests/tty_test.py

56 lines
1.3 KiB
Python
Raw Normal View History

2015-09-10 01:05:34 +03:00
EOF = b'\x04'
def ttyflags(fd):
"""normalize tty i/o for testing"""
2015-09-12 01:57:22 +03:00
# see:
# http://www.gnu.org/software/libc/manual/html_mono/libc.html#Output-Modes
2015-09-10 01:05:34 +03:00
import termios as T
attrs = T.tcgetattr(fd)
attrs[1] &= ~T.OPOST # don't munge output
attrs[3] &= ~T.ECHO # don't echo input
T.tcsetattr(fd, T.TCSANOW, attrs)
def readall(fd):
"""read until EOF"""
from os import read
2015-09-11 04:04:33 +03:00
result = b''
while True:
try:
chunk = read(fd, 1 << 10)
except OSError as error:
if error.errno == 5: # linux pty EOF
return result
else:
raise
if chunk == '':
return result
else:
result += chunk
2015-09-12 01:57:22 +03:00
def _test(fd):
"""write to tac via the pty and verify its output"""
2015-09-10 01:05:34 +03:00
ttyflags(fd)
from os import write
2015-09-10 01:05:34 +03:00
assert write(fd, b'1\n2\n3\n') == 6
assert write(fd, EOF * 2) == 2
output = readall(fd)
2015-09-10 01:05:34 +03:00
assert output == b'3\n2\n1\n', repr(output)
print('PASS')
# disable debug output so it doesn't break our assertion
def test_tty(debug_disabled):
"""
Ensure processes wrapped by dumb-init can write successfully, given a tty
"""
2015-09-10 01:05:34 +03:00
import pty
pid, fd = pty.fork()
if pid == 0:
from os import execvp
execvp('dumb-init', ('dumb-init', 'tac'))
2015-09-10 01:05:34 +03:00
else:
2015-09-12 01:57:22 +03:00
_test(fd)