Caleb Rouleau | 6844df15 | 2019-09-11 01:11:59 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) 2019 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Unit tests for test_env.py functionality. |
| 7 | |
| 8 | Each unit test is launches python process that uses test_env.py |
| 9 | to launch another python process. Then signal handling and |
| 10 | propagation is tested. This similates how Swarming uses test_env.py. |
| 11 | """ |
| 12 | |
| 13 | import os |
| 14 | import signal |
| 15 | import subprocess |
| 16 | import sys |
| 17 | import time |
| 18 | import unittest |
| 19 | |
Chris McDonald | c3e0f26b | 2020-06-04 02:15:14 | [diff] [blame] | 20 | HERE = os.path.dirname(os.path.abspath(__file__)) |
| 21 | TEST_SCRIPT = os.path.join(HERE, 'test_env_user_script.py') |
Caleb Rouleau | 6844df15 | 2019-09-11 01:11:59 | [diff] [blame] | 22 | |
| 23 | |
| 24 | def launch_process_windows(args): |
Chris McDonald | c3e0f26b | 2020-06-04 02:15:14 | [diff] [blame] | 25 | # The `universal_newlines` option is equivalent to `text` in Python 3. |
Caleb Rouleau | 6844df15 | 2019-09-11 01:11:59 | [diff] [blame] | 26 | return subprocess.Popen( |
Chris McDonald | c3e0f26b | 2020-06-04 02:15:14 | [diff] [blame] | 27 | [sys.executable, TEST_SCRIPT] + args, |
| 28 | stdout=subprocess.PIPE, |
| 29 | stderr=subprocess.STDOUT, |
| 30 | env=os.environ.copy(), |
| 31 | creationflags=subprocess.CREATE_NEW_PROCESS_GROUP, |
| 32 | universal_newlines=True) |
| 33 | |
Caleb Rouleau | 6844df15 | 2019-09-11 01:11:59 | [diff] [blame] | 34 | |
| 35 | def launch_process_nonwindows(args): |
Chris McDonald | c3e0f26b | 2020-06-04 02:15:14 | [diff] [blame] | 36 | # The `universal_newlines` option is equivalent to `text` in Python 3. |
Caleb Rouleau | 6844df15 | 2019-09-11 01:11:59 | [diff] [blame] | 37 | return subprocess.Popen( |
Chris McDonald | c3e0f26b | 2020-06-04 02:15:14 | [diff] [blame] | 38 | [sys.executable, TEST_SCRIPT] + args, |
| 39 | stdout=subprocess.PIPE, |
| 40 | stderr=subprocess.STDOUT, |
| 41 | env=os.environ.copy(), |
| 42 | universal_newlines=True) |
Caleb Rouleau | 6844df15 | 2019-09-11 01:11:59 | [diff] [blame] | 43 | |
| 44 | |
| 45 | def read_subprocess_message(proc, starts_with): |
| 46 | """Finds the value after first line prefix condition.""" |
| 47 | for line in proc.stdout: |
| 48 | if line.startswith(starts_with): |
| 49 | return line.rstrip().replace(starts_with, '') |
| 50 | |
| 51 | |
| 52 | def send_and_wait(proc, sig, sleep_time=0.3): |
| 53 | """Sends a signal to subprocess.""" |
| 54 | time.sleep(sleep_time) # gives process time to launch. |
| 55 | os.kill(proc.pid, sig) |
| 56 | proc.wait() |
| 57 | |
| 58 | |
| 59 | class SignalingWindowsTest(unittest.TestCase): |
| 60 | |
| 61 | def setUp(self): |
| 62 | super(SignalingWindowsTest, self).setUp() |
| 63 | if sys.platform != 'win32': |
| 64 | self.skipTest('test only runs on Windows') |
| 65 | |
| 66 | def test_send_ctrl_break_event(self): |
| 67 | proc = launch_process_windows([]) |
| 68 | send_and_wait(proc, signal.CTRL_BREAK_EVENT) |
| 69 | sig = read_subprocess_message(proc, 'Signal :') |
Chris McDonald | c3e0f26b | 2020-06-04 02:15:14 | [diff] [blame] | 70 | self.assertEqual(sig, str(int(signal.SIGBREAK))) |
Caleb Rouleau | 6844df15 | 2019-09-11 01:11:59 | [diff] [blame] | 71 | |
| 72 | |
| 73 | class SignalingNonWindowsTest(unittest.TestCase): |
| 74 | |
| 75 | def setUp(self): |
| 76 | super(SignalingNonWindowsTest, self).setUp() |
| 77 | if sys.platform == 'win32': |
| 78 | self.skipTest('test does not run on Windows') |
| 79 | |
| 80 | def test_send_sigterm(self): |
| 81 | proc = launch_process_nonwindows([]) |
| 82 | send_and_wait(proc, signal.SIGTERM) |
| 83 | sig = read_subprocess_message(proc, 'Signal :') |
Chris McDonald | c3e0f26b | 2020-06-04 02:15:14 | [diff] [blame] | 84 | self.assertEqual(sig, str(int(signal.SIGTERM))) |
Caleb Rouleau | 6844df15 | 2019-09-11 01:11:59 | [diff] [blame] | 85 | |
| 86 | def test_send_sigint(self): |
| 87 | proc = launch_process_nonwindows([]) |
| 88 | send_and_wait(proc, signal.SIGINT) |
| 89 | sig = read_subprocess_message(proc, 'Signal :') |
Chris McDonald | c3e0f26b | 2020-06-04 02:15:14 | [diff] [blame] | 90 | self.assertEqual(sig, str(int(signal.SIGINT))) |
Caleb Rouleau | 6844df15 | 2019-09-11 01:11:59 | [diff] [blame] | 91 | |
| 92 | |
| 93 | if __name__ == '__main__': |
| 94 | unittest.main() |