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