1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
#!/usr/bin/env python
import os
import signal
import sys
import time
import unittest
import mozunit
import proctest
from mozprocess import processhandler
here = os.path.dirname(os.path.abspath(__file__))
class ProcTestKill(proctest.ProcTest):
"""Class to test various process tree killing scenatios"""
def test_kill_before_run(self):
"""Process is not started, and kill() is called"""
p = processhandler.ProcessHandler([self.python, "-V"])
self.assertRaises(RuntimeError, p.kill)
def test_process_kill(self):
"""Process is started, we kill it"""
p = processhandler.ProcessHandler(
[self.python, self.proclaunch, "process_normal_finish.ini"], cwd=here
)
p.run()
p.kill()
self.determine_status(p, expectedfail=("returncode",))
def test_process_kill_deep(self):
"""Process is started, we kill it, we use a deep process tree"""
p = processhandler.ProcessHandler(
[self.python, self.proclaunch, "process_normal_deep.ini"], cwd=here
)
p.run()
p.kill()
self.determine_status(p, expectedfail=("returncode",))
def test_process_kill_deep_wait(self):
"""Process is started, we use a deep process tree, we let it spawn
for a bit, we kill it"""
myenv = None
# On macosx1014, subprocess fails to find `six` when run with python3.
# This ensures that subprocess first looks to sys.path to find `six`.
# See https://bugzilla.mozilla.org/show_bug.cgi?id=1562083
if sys.platform == "darwin" and sys.version_info[0] > 2:
myenv = os.environ.copy()
myenv["PYTHONPATH"] = ":".join(sys.path)
p = processhandler.ProcessHandler(
[self.python, self.proclaunch, "process_normal_deep.ini"],
cwd=here,
env=myenv,
)
p.run()
# Let the tree spawn a bit, before attempting to kill
time.sleep(3)
p.kill()
self.determine_status(p, expectedfail=("returncode",))
def test_process_kill_broad(self):
"""Process is started, we kill it, we use a broad process tree"""
p = processhandler.ProcessHandler(
[self.python, self.proclaunch, "process_normal_broad.ini"], cwd=here
)
p.run()
p.kill()
self.determine_status(p, expectedfail=("returncode",))
def test_process_kill_broad_delayed(self):
"""Process is started, we use a broad process tree, we let it spawn
for a bit, we kill it"""
myenv = None
# On macosx1014, subprocess fails to find `six` when run with python3.
# This ensures that subprocess first looks to sys.path to find `six`.
# See https://bugzilla.mozilla.org/show_bug.cgi?id=1562083
if sys.platform == "darwin" and sys.version_info[0] > 2:
myenv = os.environ.copy()
myenv["PYTHONPATH"] = ":".join(sys.path)
p = processhandler.ProcessHandler(
[self.python, self.proclaunch, "process_normal_broad.ini"],
cwd=here,
env=myenv,
)
p.run()
# Let the tree spawn a bit, before attempting to kill
time.sleep(3)
p.kill()
self.determine_status(p, expectedfail=("returncode",))
@unittest.skipUnless(processhandler.isPosix, "posix only")
def test_process_kill_with_sigterm(self):
script = os.path.join(here, "scripts", "infinite_loop.py")
p = processhandler.ProcessHandler([self.python, script])
p.run()
p.kill()
self.assertEqual(p.proc.returncode, -signal.SIGTERM)
@unittest.skipUnless(processhandler.isPosix, "posix only")
def test_process_kill_with_sigint_if_needed(self):
script = os.path.join(here, "scripts", "infinite_loop.py")
p = processhandler.ProcessHandler([self.python, script, "deadlock"])
p.run()
time.sleep(1)
p.kill()
self.assertEqual(p.proc.returncode, -signal.SIGKILL)
@unittest.skipUnless(processhandler.isPosix, "posix only")
def test_process_kill_with_timeout(self):
script = os.path.join(here, "scripts", "ignore_sigterm.py")
p = processhandler.ProcessHandler([self.python, script])
p.run()
time.sleep(1)
t0 = time.time()
p.kill(sig=signal.SIGTERM, timeout=2)
self.assertEqual(p.proc.returncode, None)
self.assertGreaterEqual(time.time(), t0 + 2)
p.kill(sig=signal.SIGKILL)
self.assertEqual(p.proc.returncode, -signal.SIGKILL)
if __name__ == "__main__":
mozunit.main()
|