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