2015-09-12 01:57:22 +03:00
|
|
|
import os
|
|
|
|
|
|
|
|
|
2015-09-10 01:05:34 +03:00
|
|
|
EOF = b'\x04'
|
|
|
|
|
|
|
|
|
|
|
|
def ttyflags(fd):
|
2015-09-11 00:05:05 +03:00
|
|
|
"""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)
|
|
|
|
|
|
|
|
|
2015-09-12 01:57:22 +03:00
|
|
|
def tac():
|
2015-09-11 00:05:05 +03:00
|
|
|
"""
|
|
|
|
run tac. if it fails to complete in 1 second send SIGKILL and exit with an
|
|
|
|
error.
|
|
|
|
"""
|
2015-09-10 01:05:34 +03:00
|
|
|
from os import execvp
|
2015-09-12 01:57:22 +03:00
|
|
|
execvp('timeout', ('timeout', '1', 'dumb-init', 'tac'))
|
2015-09-10 01:05:34 +03:00
|
|
|
|
|
|
|
|
2015-09-11 00:05:05 +03:00
|
|
|
def readall(fd):
|
|
|
|
"""read until EOF"""
|
|
|
|
from os import read
|
2015-09-11 04:04:33 +03:00
|
|
|
result = b''
|
2015-09-11 00:05:05 +03:00
|
|
|
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):
|
2015-09-11 00:05:05 +03:00
|
|
|
"""write to tac via the pty and verify its output"""
|
2015-09-10 01:05:34 +03:00
|
|
|
ttyflags(fd)
|
2015-09-11 00:05:05 +03:00
|
|
|
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
|
2015-09-11 00:05:05 +03:00
|
|
|
output = readall(fd)
|
2015-09-10 01:05:34 +03:00
|
|
|
assert output == b'3\n2\n1\n', repr(output)
|
|
|
|
print('PASS')
|
|
|
|
|
|
|
|
|
2015-09-12 01:57:22 +03:00
|
|
|
def test_tty():
|
2015-09-11 00:05:05 +03:00
|
|
|
"""
|
|
|
|
Ensure processes wrapped by dumb-init can write successfully, given a tty
|
|
|
|
"""
|
2015-09-12 01:57:22 +03:00
|
|
|
# disable debug output so it doesn't break our assertion
|
|
|
|
os.environ['DUMB_INIT_DEBUG'] = '0'
|
2015-09-10 01:05:34 +03:00
|
|
|
import pty
|
|
|
|
pid, fd = pty.fork()
|
|
|
|
if pid == 0:
|
2015-09-12 01:57:22 +03:00
|
|
|
tac()
|
2015-09-10 01:05:34 +03:00
|
|
|
else:
|
2015-09-12 01:57:22 +03:00
|
|
|
_test(fd)
|