test_process.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for implementations of L{IReactorProcess}.
  5. @var properEnv: A copy of L{os.environ} which has L{bytes} keys/values on POSIX
  6. platforms and native L{str} keys/values on Windows.
  7. """
  8. from __future__ import division, absolute_import, print_function
  9. import io
  10. import os
  11. import signal
  12. import sys
  13. import threading
  14. import twisted
  15. import subprocess
  16. from twisted.trial.unittest import TestCase
  17. from twisted.internet.test.reactormixins import ReactorBuilder
  18. from twisted.python.log import msg, err
  19. from twisted.python.runtime import platform
  20. from twisted.python.filepath import FilePath, _asFilesystemBytes
  21. from twisted.python.compat import (networkString, range, items,
  22. bytesEnviron, unicode)
  23. from twisted.internet import utils
  24. from twisted.internet.interfaces import IReactorProcess, IProcessTransport
  25. from twisted.internet.defer import Deferred, succeed
  26. from twisted.internet.protocol import ProcessProtocol
  27. from twisted.internet.error import ProcessDone, ProcessTerminated
  28. # Get the current Python executable as a bytestring.
  29. pyExe = FilePath(sys.executable)._asBytesPath()
  30. twistedRoot = FilePath(twisted.__file__).parent().parent()
  31. _uidgidSkip = None
  32. if platform.isWindows():
  33. resource = None
  34. process = None
  35. _uidgidSkip = "Cannot change UID/GID on Windows"
  36. properEnv = dict(os.environ)
  37. properEnv["PYTHONPATH"] = os.pathsep.join(sys.path)
  38. else:
  39. import resource
  40. from twisted.internet import process
  41. if os.getuid() != 0:
  42. _uidgidSkip = "Cannot change UID/GID except as root"
  43. properEnv = bytesEnviron()
  44. properEnv[b"PYTHONPATH"] = os.pathsep.join(sys.path).encode(
  45. sys.getfilesystemencoding())
  46. def onlyOnPOSIX(testMethod):
  47. """
  48. Only run this test on POSIX platforms.
  49. @param testMethod: A test function, being decorated.
  50. @return: the C{testMethod} argument.
  51. """
  52. if resource is None:
  53. testMethod.skip = "Test only applies to POSIX platforms."
  54. return testMethod
  55. class _ShutdownCallbackProcessProtocol(ProcessProtocol):
  56. """
  57. An L{IProcessProtocol} which fires a Deferred when the process it is
  58. associated with ends.
  59. @ivar received: A C{dict} mapping file descriptors to lists of bytes
  60. received from the child process on those file descriptors.
  61. """
  62. def __init__(self, whenFinished):
  63. self.whenFinished = whenFinished
  64. self.received = {}
  65. def childDataReceived(self, fd, bytes):
  66. self.received.setdefault(fd, []).append(bytes)
  67. def processEnded(self, reason):
  68. self.whenFinished.callback(None)
  69. class ProcessTestsBuilderBase(ReactorBuilder):
  70. """
  71. Base class for L{IReactorProcess} tests which defines some tests which
  72. can be applied to PTY or non-PTY uses of C{spawnProcess}.
  73. Subclasses are expected to set the C{usePTY} attribute to C{True} or
  74. C{False}.
  75. """
  76. requiredInterfaces = [IReactorProcess]
  77. def test_processTransportInterface(self):
  78. """
  79. L{IReactorProcess.spawnProcess} connects the protocol passed to it
  80. to a transport which provides L{IProcessTransport}.
  81. """
  82. ended = Deferred()
  83. protocol = _ShutdownCallbackProcessProtocol(ended)
  84. reactor = self.buildReactor()
  85. transport = reactor.spawnProcess(
  86. protocol, pyExe, [pyExe, b"-c", b""],
  87. usePTY=self.usePTY)
  88. # The transport is available synchronously, so we can check it right
  89. # away (unlike many transport-based tests). This is convenient even
  90. # though it's probably not how the spawnProcess interface should really
  91. # work.
  92. # We're not using verifyObject here because part of
  93. # IProcessTransport is a lie - there are no getHost or getPeer
  94. # methods. See #1124.
  95. self.assertTrue(IProcessTransport.providedBy(transport))
  96. # Let the process run and exit so we don't leave a zombie around.
  97. ended.addCallback(lambda ignored: reactor.stop())
  98. self.runReactor(reactor)
  99. def _writeTest(self, write):
  100. """
  101. Helper for testing L{IProcessTransport} write functionality. This
  102. method spawns a child process and gives C{write} a chance to write some
  103. bytes to it. It then verifies that the bytes were actually written to
  104. it (by relying on the child process to echo them back).
  105. @param write: A two-argument callable. This is invoked with a process
  106. transport and some bytes to write to it.
  107. """
  108. reactor = self.buildReactor()
  109. ended = Deferred()
  110. protocol = _ShutdownCallbackProcessProtocol(ended)
  111. bytesToSend = b"hello, world" + networkString(os.linesep)
  112. program = (
  113. b"import sys\n"
  114. b"sys.stdout.write(sys.stdin.readline())\n"
  115. )
  116. def startup():
  117. transport = reactor.spawnProcess(
  118. protocol, pyExe, [pyExe, b"-c", program])
  119. try:
  120. write(transport, bytesToSend)
  121. except:
  122. err(None, "Unhandled exception while writing")
  123. transport.signalProcess('KILL')
  124. reactor.callWhenRunning(startup)
  125. ended.addCallback(lambda ignored: reactor.stop())
  126. self.runReactor(reactor)
  127. self.assertEqual(bytesToSend, b"".join(protocol.received[1]))
  128. def test_write(self):
  129. """
  130. L{IProcessTransport.write} writes the specified C{bytes} to the standard
  131. input of the child process.
  132. """
  133. def write(transport, bytesToSend):
  134. transport.write(bytesToSend)
  135. self._writeTest(write)
  136. def test_writeSequence(self):
  137. """
  138. L{IProcessTransport.writeSequence} writes the specified C{list} of
  139. C{bytes} to the standard input of the child process.
  140. """
  141. def write(transport, bytesToSend):
  142. transport.writeSequence([bytesToSend])
  143. self._writeTest(write)
  144. def test_writeToChild(self):
  145. """
  146. L{IProcessTransport.writeToChild} writes the specified C{bytes} to the
  147. specified file descriptor of the child process.
  148. """
  149. def write(transport, bytesToSend):
  150. transport.writeToChild(0, bytesToSend)
  151. self._writeTest(write)
  152. def test_writeToChildBadFileDescriptor(self):
  153. """
  154. L{IProcessTransport.writeToChild} raises L{KeyError} if passed a file
  155. descriptor which is was not set up by L{IReactorProcess.spawnProcess}.
  156. """
  157. def write(transport, bytesToSend):
  158. try:
  159. self.assertRaises(KeyError, transport.writeToChild, 13, bytesToSend)
  160. finally:
  161. # Just get the process to exit so the test can complete
  162. transport.write(bytesToSend)
  163. self._writeTest(write)
  164. def test_spawnProcessEarlyIsReaped(self):
  165. """
  166. If, before the reactor is started with L{IReactorCore.run}, a
  167. process is started with L{IReactorProcess.spawnProcess} and
  168. terminates, the process is reaped once the reactor is started.
  169. """
  170. reactor = self.buildReactor()
  171. # Create the process with no shared file descriptors, so that there
  172. # are no other events for the reactor to notice and "cheat" with.
  173. # We want to be sure it's really dealing with the process exiting,
  174. # not some associated event.
  175. if self.usePTY:
  176. childFDs = None
  177. else:
  178. childFDs = {}
  179. # Arrange to notice the SIGCHLD.
  180. signaled = threading.Event()
  181. def handler(*args):
  182. signaled.set()
  183. signal.signal(signal.SIGCHLD, handler)
  184. # Start a process - before starting the reactor!
  185. ended = Deferred()
  186. reactor.spawnProcess(
  187. _ShutdownCallbackProcessProtocol(ended), pyExe,
  188. [pyExe, b"-c", b""], usePTY=self.usePTY, childFDs=childFDs)
  189. # Wait for the SIGCHLD (which might have been delivered before we got
  190. # here, but that's okay because the signal handler was installed above,
  191. # before we could have gotten it).
  192. signaled.wait(120)
  193. if not signaled.isSet():
  194. self.fail("Timed out waiting for child process to exit.")
  195. # Capture the processEnded callback.
  196. result = []
  197. ended.addCallback(result.append)
  198. if result:
  199. # The synchronous path through spawnProcess / Process.__init__ /
  200. # registerReapProcessHandler was encountered. There's no reason to
  201. # start the reactor, because everything is done already.
  202. return
  203. # Otherwise, though, start the reactor so it can tell us the process
  204. # exited.
  205. ended.addCallback(lambda ignored: reactor.stop())
  206. self.runReactor(reactor)
  207. # Make sure the reactor stopped because the Deferred fired.
  208. self.assertTrue(result)
  209. if getattr(signal, 'SIGCHLD', None) is None:
  210. test_spawnProcessEarlyIsReaped.skip = (
  211. "Platform lacks SIGCHLD, early-spawnProcess test can't work.")
  212. def test_processExitedWithSignal(self):
  213. """
  214. The C{reason} argument passed to L{IProcessProtocol.processExited} is a
  215. L{ProcessTerminated} instance if the child process exits with a signal.
  216. """
  217. sigName = 'TERM'
  218. sigNum = getattr(signal, 'SIG' + sigName)
  219. exited = Deferred()
  220. source = (
  221. b"import sys\n"
  222. # Talk so the parent process knows the process is running. This is
  223. # necessary because ProcessProtocol.makeConnection may be called
  224. # before this process is exec'd. It would be unfortunate if we
  225. # SIGTERM'd the Twisted process while it was on its way to doing
  226. # the exec.
  227. b"sys.stdout.write('x')\n"
  228. b"sys.stdout.flush()\n"
  229. b"sys.stdin.read()\n")
  230. class Exiter(ProcessProtocol):
  231. def childDataReceived(self, fd, data):
  232. msg('childDataReceived(%d, %r)' % (fd, data))
  233. self.transport.signalProcess(sigName)
  234. def childConnectionLost(self, fd):
  235. msg('childConnectionLost(%d)' % (fd,))
  236. def processExited(self, reason):
  237. msg('processExited(%r)' % (reason,))
  238. # Protect the Deferred from the failure so that it follows
  239. # the callback chain. This doesn't use the errback chain
  240. # because it wants to make sure reason is a Failure. An
  241. # Exception would also make an errback-based test pass, and
  242. # that would be wrong.
  243. exited.callback([reason])
  244. def processEnded(self, reason):
  245. msg('processEnded(%r)' % (reason,))
  246. reactor = self.buildReactor()
  247. reactor.callWhenRunning(
  248. reactor.spawnProcess, Exiter(), pyExe,
  249. [pyExe, b"-c", source], usePTY=self.usePTY)
  250. def cbExited(args):
  251. failure, = args
  252. # Trapping implicitly verifies that it's a Failure (rather than
  253. # an exception) and explicitly makes sure it's the right type.
  254. failure.trap(ProcessTerminated)
  255. err = failure.value
  256. if platform.isWindows():
  257. # Windows can't really /have/ signals, so it certainly can't
  258. # report them as the reason for termination. Maybe there's
  259. # something better we could be doing here, anyway? Hard to
  260. # say. Anyway, this inconsistency between different platforms
  261. # is extremely unfortunate and I would remove it if I
  262. # could. -exarkun
  263. self.assertIsNone(err.signal)
  264. self.assertEqual(err.exitCode, 1)
  265. else:
  266. self.assertEqual(err.signal, sigNum)
  267. self.assertIsNone(err.exitCode)
  268. exited.addCallback(cbExited)
  269. exited.addErrback(err)
  270. exited.addCallback(lambda ign: reactor.stop())
  271. self.runReactor(reactor)
  272. def test_systemCallUninterruptedByChildExit(self):
  273. """
  274. If a child process exits while a system call is in progress, the system
  275. call should not be interfered with. In particular, it should not fail
  276. with EINTR.
  277. Older versions of Twisted installed a SIGCHLD handler on POSIX without
  278. using the feature exposed by the SA_RESTART flag to sigaction(2). The
  279. most noticeable problem this caused was for blocking reads and writes to
  280. sometimes fail with EINTR.
  281. """
  282. reactor = self.buildReactor()
  283. result = []
  284. def f():
  285. try:
  286. exe = pyExe.decode(sys.getfilesystemencoding())
  287. subprocess.Popen([exe, "-c", "import time; time.sleep(0.1)"])
  288. f2 = subprocess.Popen([exe, "-c",
  289. ("import time; time.sleep(0.5);"
  290. "print(\'Foo\')")],
  291. stdout=subprocess.PIPE)
  292. # The read call below will blow up with an EINTR from the
  293. # SIGCHLD from the first process exiting if we install a
  294. # SIGCHLD handler without SA_RESTART. (which we used to do)
  295. with f2.stdout:
  296. result.append(f2.stdout.read())
  297. finally:
  298. reactor.stop()
  299. reactor.callWhenRunning(f)
  300. self.runReactor(reactor)
  301. self.assertEqual(result, [b"Foo" + os.linesep.encode('ascii')])
  302. @onlyOnPOSIX
  303. def test_openFileDescriptors(self):
  304. """
  305. Processes spawned with spawnProcess() close all extraneous file
  306. descriptors in the parent. They do have a stdin, stdout, and stderr
  307. open.
  308. """
  309. # To test this, we are going to open a file descriptor in the parent
  310. # that is unlikely to be opened in the child, then verify that it's not
  311. # open in the child.
  312. source = networkString("""
  313. import sys
  314. sys.path.insert(0, '{0}')
  315. from twisted.internet import process
  316. sys.stdout.write(repr(process._listOpenFDs()))
  317. sys.stdout.flush()""".format(twistedRoot.path))
  318. r, w = os.pipe()
  319. self.addCleanup(os.close, r)
  320. self.addCleanup(os.close, w)
  321. # The call to "os.listdir()" (in _listOpenFDs's implementation) opens a
  322. # file descriptor (with "opendir"), which shows up in _listOpenFDs's
  323. # result. And speaking of "random" file descriptors, the code required
  324. # for _listOpenFDs itself imports logger, which imports random, which
  325. # (depending on your Python version) might leave /dev/urandom open.
  326. # More generally though, even if we were to use an extremely minimal C
  327. # program, the operating system would be within its rights to open file
  328. # descriptors we might not know about in the C library's
  329. # initialization; things like debuggers, profilers, or nsswitch plugins
  330. # might open some and this test should pass in those environments.
  331. # Although some of these file descriptors aren't predictable, we should
  332. # at least be able to select a very large file descriptor which is very
  333. # unlikely to be opened automatically in the subprocess. (Apply a
  334. # fudge factor to avoid hard-coding something too near a limit
  335. # condition like the maximum possible file descriptor, which a library
  336. # might at least hypothetically select.)
  337. fudgeFactor = 17
  338. unlikelyFD = (resource.getrlimit(resource.RLIMIT_NOFILE)[0]
  339. - fudgeFactor)
  340. os.dup2(w, unlikelyFD)
  341. self.addCleanup(os.close, unlikelyFD)
  342. output = io.BytesIO()
  343. class GatheringProtocol(ProcessProtocol):
  344. outReceived = output.write
  345. def processEnded(self, reason):
  346. reactor.stop()
  347. reactor = self.buildReactor()
  348. reactor.callWhenRunning(
  349. reactor.spawnProcess, GatheringProtocol(), pyExe,
  350. [pyExe, b"-Wignore", b"-c", source], usePTY=self.usePTY)
  351. self.runReactor(reactor)
  352. reportedChildFDs = set(eval(output.getvalue()))
  353. stdFDs = [0, 1, 2]
  354. # Unfortunately this assertion is still not *entirely* deterministic,
  355. # since hypothetically, any library could open any file descriptor at
  356. # any time. See comment above.
  357. self.assertEqual(
  358. reportedChildFDs.intersection(set(stdFDs + [unlikelyFD])),
  359. set(stdFDs)
  360. )
  361. @onlyOnPOSIX
  362. def test_errorDuringExec(self):
  363. """
  364. When L{os.execvpe} raises an exception, it will format that exception
  365. on stderr as UTF-8, regardless of system encoding information.
  366. """
  367. def execvpe(*args, **kw):
  368. # Ensure that real traceback formatting has some non-ASCII in it,
  369. # by forcing the filename of the last frame to contain non-ASCII.
  370. filename = u"<\N{SNOWMAN}>"
  371. if not isinstance(filename, str):
  372. filename = filename.encode("utf-8")
  373. codeobj = compile("1/0", filename, "single")
  374. eval(codeobj)
  375. self.patch(os, "execvpe", execvpe)
  376. self.patch(sys, "getfilesystemencoding", lambda: "ascii")
  377. reactor = self.buildReactor()
  378. output = io.BytesIO()
  379. @reactor.callWhenRunning
  380. def whenRunning():
  381. class TracebackCatcher(ProcessProtocol, object):
  382. errReceived = output.write
  383. def processEnded(self, reason):
  384. reactor.stop()
  385. reactor.spawnProcess(TracebackCatcher(), pyExe,
  386. [pyExe, b"-c", b""])
  387. self.runReactor(reactor, timeout=30)
  388. self.assertIn(u"\N{SNOWMAN}".encode("utf-8"), output.getvalue())
  389. def test_timelyProcessExited(self):
  390. """
  391. If a spawned process exits, C{processExited} will be called in a
  392. timely manner.
  393. """
  394. reactor = self.buildReactor()
  395. class ExitingProtocol(ProcessProtocol):
  396. exited = False
  397. def processExited(protoSelf, reason):
  398. protoSelf.exited = True
  399. reactor.stop()
  400. self.assertEqual(reason.value.exitCode, 0)
  401. protocol = ExitingProtocol()
  402. reactor.callWhenRunning(
  403. reactor.spawnProcess, protocol, pyExe,
  404. [pyExe, b"-c", b"raise SystemExit(0)"],
  405. usePTY=self.usePTY)
  406. # This will timeout if processExited isn't called:
  407. self.runReactor(reactor, timeout=30)
  408. self.assertTrue(protocol.exited)
  409. def _changeIDTest(self, which):
  410. """
  411. Launch a child process, using either the C{uid} or C{gid} argument to
  412. L{IReactorProcess.spawnProcess} to change either its UID or GID to a
  413. different value. If the child process reports this hasn't happened,
  414. raise an exception to fail the test.
  415. @param which: Either C{b"uid"} or C{b"gid"}.
  416. """
  417. program = [
  418. "import os",
  419. "raise SystemExit(os.get%s() != 1)" % (which,)]
  420. container = []
  421. class CaptureExitStatus(ProcessProtocol):
  422. def processEnded(self, reason):
  423. container.append(reason)
  424. reactor.stop()
  425. reactor = self.buildReactor()
  426. protocol = CaptureExitStatus()
  427. reactor.callWhenRunning(
  428. reactor.spawnProcess, protocol, pyExe,
  429. [pyExe, "-c", "\n".join(program)],
  430. **{which: 1})
  431. self.runReactor(reactor)
  432. self.assertEqual(0, container[0].value.exitCode)
  433. def test_changeUID(self):
  434. """
  435. If a value is passed for L{IReactorProcess.spawnProcess}'s C{uid}, the
  436. child process is run with that UID.
  437. """
  438. self._changeIDTest("uid")
  439. if _uidgidSkip is not None:
  440. test_changeUID.skip = _uidgidSkip
  441. def test_changeGID(self):
  442. """
  443. If a value is passed for L{IReactorProcess.spawnProcess}'s C{gid}, the
  444. child process is run with that GID.
  445. """
  446. self._changeIDTest("gid")
  447. if _uidgidSkip is not None:
  448. test_changeGID.skip = _uidgidSkip
  449. def test_processExitedRaises(self):
  450. """
  451. If L{IProcessProtocol.processExited} raises an exception, it is logged.
  452. """
  453. # Ideally we wouldn't need to poke the process module; see
  454. # https://twistedmatrix.com/trac/ticket/6889
  455. reactor = self.buildReactor()
  456. class TestException(Exception):
  457. pass
  458. class Protocol(ProcessProtocol):
  459. def processExited(self, reason):
  460. reactor.stop()
  461. raise TestException("processedExited raised")
  462. protocol = Protocol()
  463. transport = reactor.spawnProcess(
  464. protocol, pyExe, [pyExe, b"-c", b""],
  465. usePTY=self.usePTY)
  466. self.runReactor(reactor)
  467. # Manually clean-up broken process handler.
  468. # Only required if the test fails on systems that support
  469. # the process module.
  470. if process is not None:
  471. for pid, handler in items(process.reapProcessHandlers):
  472. if handler is not transport:
  473. continue
  474. process.unregisterReapProcessHandler(pid, handler)
  475. self.fail("After processExited raised, transport was left in"
  476. " reapProcessHandlers")
  477. self.assertEqual(1, len(self.flushLoggedErrors(TestException)))
  478. class ProcessTestsBuilder(ProcessTestsBuilderBase):
  479. """
  480. Builder defining tests relating to L{IReactorProcess} for child processes
  481. which do not have a PTY.
  482. """
  483. usePTY = False
  484. keepStdioOpenProgram = b'twisted.internet.test.process_helper'
  485. if platform.isWindows():
  486. keepStdioOpenArg = b"windows"
  487. else:
  488. # Just a value that doesn't equal "windows"
  489. keepStdioOpenArg = b""
  490. # Define this test here because PTY-using processes only have stdin and
  491. # stdout and the test would need to be different for that to work.
  492. def test_childConnectionLost(self):
  493. """
  494. L{IProcessProtocol.childConnectionLost} is called each time a file
  495. descriptor associated with a child process is closed.
  496. """
  497. connected = Deferred()
  498. lost = {0: Deferred(), 1: Deferred(), 2: Deferred()}
  499. class Closer(ProcessProtocol):
  500. def makeConnection(self, transport):
  501. connected.callback(transport)
  502. def childConnectionLost(self, childFD):
  503. lost[childFD].callback(None)
  504. target = b"twisted.internet.test.process_loseconnection"
  505. reactor = self.buildReactor()
  506. reactor.callWhenRunning(
  507. reactor.spawnProcess, Closer(), pyExe,
  508. [pyExe, b"-m", target], env=properEnv, usePTY=self.usePTY)
  509. def cbConnected(transport):
  510. transport.write(b'2\n')
  511. return lost[2].addCallback(lambda ign: transport)
  512. connected.addCallback(cbConnected)
  513. def lostSecond(transport):
  514. transport.write(b'1\n')
  515. return lost[1].addCallback(lambda ign: transport)
  516. connected.addCallback(lostSecond)
  517. def lostFirst(transport):
  518. transport.write(b'\n')
  519. connected.addCallback(lostFirst)
  520. connected.addErrback(err)
  521. def cbEnded(ignored):
  522. reactor.stop()
  523. connected.addCallback(cbEnded)
  524. self.runReactor(reactor)
  525. # This test is here because PTYProcess never delivers childConnectionLost.
  526. def test_processEnded(self):
  527. """
  528. L{IProcessProtocol.processEnded} is called after the child process
  529. exits and L{IProcessProtocol.childConnectionLost} is called for each of
  530. its file descriptors.
  531. """
  532. ended = Deferred()
  533. lost = []
  534. class Ender(ProcessProtocol):
  535. def childDataReceived(self, fd, data):
  536. msg('childDataReceived(%d, %r)' % (fd, data))
  537. self.transport.loseConnection()
  538. def childConnectionLost(self, childFD):
  539. msg('childConnectionLost(%d)' % (childFD,))
  540. lost.append(childFD)
  541. def processExited(self, reason):
  542. msg('processExited(%r)' % (reason,))
  543. def processEnded(self, reason):
  544. msg('processEnded(%r)' % (reason,))
  545. ended.callback([reason])
  546. reactor = self.buildReactor()
  547. reactor.callWhenRunning(
  548. reactor.spawnProcess, Ender(), pyExe,
  549. [pyExe, b"-m", self.keepStdioOpenProgram, b"child",
  550. self.keepStdioOpenArg],
  551. env=properEnv, usePTY=self.usePTY)
  552. def cbEnded(args):
  553. failure, = args
  554. failure.trap(ProcessDone)
  555. self.assertEqual(set(lost), set([0, 1, 2]))
  556. ended.addCallback(cbEnded)
  557. ended.addErrback(err)
  558. ended.addCallback(lambda ign: reactor.stop())
  559. self.runReactor(reactor)
  560. # This test is here because PTYProcess.loseConnection does not actually
  561. # close the file descriptors to the child process. This test needs to be
  562. # written fairly differently for PTYProcess.
  563. def test_processExited(self):
  564. """
  565. L{IProcessProtocol.processExited} is called when the child process
  566. exits, even if file descriptors associated with the child are still
  567. open.
  568. """
  569. exited = Deferred()
  570. allLost = Deferred()
  571. lost = []
  572. class Waiter(ProcessProtocol):
  573. def childDataReceived(self, fd, data):
  574. msg('childDataReceived(%d, %r)' % (fd, data))
  575. def childConnectionLost(self, childFD):
  576. msg('childConnectionLost(%d)' % (childFD,))
  577. lost.append(childFD)
  578. if len(lost) == 3:
  579. allLost.callback(None)
  580. def processExited(self, reason):
  581. msg('processExited(%r)' % (reason,))
  582. # See test_processExitedWithSignal
  583. exited.callback([reason])
  584. self.transport.loseConnection()
  585. reactor = self.buildReactor()
  586. reactor.callWhenRunning(
  587. reactor.spawnProcess, Waiter(), pyExe,
  588. [pyExe, b"-u", b"-m", self.keepStdioOpenProgram, b"child",
  589. self.keepStdioOpenArg],
  590. env=properEnv, usePTY=self.usePTY)
  591. def cbExited(args):
  592. failure, = args
  593. failure.trap(ProcessDone)
  594. msg('cbExited; lost = %s' % (lost,))
  595. self.assertEqual(lost, [])
  596. return allLost
  597. exited.addCallback(cbExited)
  598. def cbAllLost(ignored):
  599. self.assertEqual(set(lost), set([0, 1, 2]))
  600. exited.addCallback(cbAllLost)
  601. exited.addErrback(err)
  602. exited.addCallback(lambda ign: reactor.stop())
  603. self.runReactor(reactor)
  604. def makeSourceFile(self, sourceLines):
  605. """
  606. Write the given list of lines to a text file and return the absolute
  607. path to it.
  608. """
  609. script = _asFilesystemBytes(self.mktemp())
  610. with open(script, 'wt') as scriptFile:
  611. scriptFile.write(os.linesep.join(sourceLines) + os.linesep)
  612. return os.path.abspath(script)
  613. def test_shebang(self):
  614. """
  615. Spawning a process with an executable which is a script starting
  616. with an interpreter definition line (#!) uses that interpreter to
  617. evaluate the script.
  618. """
  619. shebangOutput = b'this is the shebang output'
  620. scriptFile = self.makeSourceFile([
  621. "#!%s" % (pyExe.decode('ascii'),),
  622. "import sys",
  623. "sys.stdout.write('%s')" % (shebangOutput.decode('ascii'),),
  624. "sys.stdout.flush()"])
  625. os.chmod(scriptFile, 0o700)
  626. reactor = self.buildReactor()
  627. def cbProcessExited(args):
  628. out, err, code = args
  629. msg("cbProcessExited((%r, %r, %d))" % (out, err, code))
  630. self.assertEqual(out, shebangOutput)
  631. self.assertEqual(err, b"")
  632. self.assertEqual(code, 0)
  633. def shutdown(passthrough):
  634. reactor.stop()
  635. return passthrough
  636. def start():
  637. d = utils.getProcessOutputAndValue(scriptFile, reactor=reactor)
  638. d.addBoth(shutdown)
  639. d.addCallback(cbProcessExited)
  640. d.addErrback(err)
  641. reactor.callWhenRunning(start)
  642. self.runReactor(reactor)
  643. def test_processCommandLineArguments(self):
  644. """
  645. Arguments given to spawnProcess are passed to the child process as
  646. originally intended.
  647. """
  648. us = b"twisted.internet.test.process_cli"
  649. args = [b'hello', b'"', b' \t|<>^&', br'"\\"hello\\"', br'"foo\ bar baz\""']
  650. # Ensure that all non-NUL characters can be passed too.
  651. allChars = "".join(map(chr, range(1, 255)))
  652. if isinstance(allChars, unicode):
  653. allChars.encode("utf-8")
  654. reactor = self.buildReactor()
  655. def processFinished(finishedArgs):
  656. output, err, code = finishedArgs
  657. output = output.split(b'\0')
  658. # Drop the trailing \0.
  659. output.pop()
  660. self.assertEqual(args, output)
  661. def shutdown(result):
  662. reactor.stop()
  663. return result
  664. def spawnChild():
  665. d = succeed(None)
  666. d.addCallback(lambda dummy: utils.getProcessOutputAndValue(
  667. pyExe, [b"-m", us] + args, env=properEnv,
  668. reactor=reactor))
  669. d.addCallback(processFinished)
  670. d.addBoth(shutdown)
  671. reactor.callWhenRunning(spawnChild)
  672. self.runReactor(reactor)
  673. globals().update(ProcessTestsBuilder.makeTestCaseClasses())
  674. class PTYProcessTestsBuilder(ProcessTestsBuilderBase):
  675. """
  676. Builder defining tests relating to L{IReactorProcess} for child processes
  677. which have a PTY.
  678. """
  679. usePTY = True
  680. if platform.isWindows():
  681. skip = "PTYs are not supported on Windows."
  682. elif platform.isMacOSX():
  683. skip = "PTYs are flaky from a Darwin bug. See #8840."
  684. skippedReactors = {
  685. "twisted.internet.pollreactor.PollReactor":
  686. "OS X's poll() does not support PTYs"}
  687. globals().update(PTYProcessTestsBuilder.makeTestCaseClasses())
  688. class PotentialZombieWarningTests(TestCase):
  689. """
  690. Tests for L{twisted.internet.error.PotentialZombieWarning}.
  691. """
  692. def test_deprecated(self):
  693. """
  694. Accessing L{PotentialZombieWarning} via the
  695. I{PotentialZombieWarning} attribute of L{twisted.internet.error}
  696. results in a deprecation warning being emitted.
  697. """
  698. from twisted.internet import error
  699. error.PotentialZombieWarning
  700. warnings = self.flushWarnings([self.test_deprecated])
  701. self.assertEqual(warnings[0]['category'], DeprecationWarning)
  702. self.assertEqual(
  703. warnings[0]['message'],
  704. "twisted.internet.error.PotentialZombieWarning was deprecated in "
  705. "Twisted 10.0.0: There is no longer any potential for zombie "
  706. "process.")
  707. self.assertEqual(len(warnings), 1)
  708. class ProcessIsUnimportableOnUnsupportedPlatormsTests(TestCase):
  709. """
  710. Tests to ensure that L{twisted.internet.process} is unimportable on
  711. platforms where it does not work (namely Windows).
  712. """
  713. def test_unimportableOnWindows(self):
  714. """
  715. L{twisted.internet.process} is unimportable on Windows.
  716. """
  717. with self.assertRaises(ImportError):
  718. import twisted.internet.process
  719. twisted.internet.process # shh pyflakes
  720. if not platform.isWindows():
  721. test_unimportableOnWindows.skip = "Only relevant on Windows."