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 | |
| 20 | |
| 21 | TEST_SCRIPT = 'test_env_user_script.py' |
| 22 | |
| 23 | |
| 24 | def launch_process_windows(args): |
| 25 | return subprocess.Popen( |
| 26 | [sys.executable, TEST_SCRIPT] + args, stdout=subprocess.PIPE, |
| 27 | stderr=subprocess.STDOUT, env=os.environ.copy(), |
| 28 | creationflags=subprocess.CREATE_NEW_PROCESS_GROUP) |
| 29 | |
| 30 | def launch_process_nonwindows(args): |
| 31 | return subprocess.Popen( |
| 32 | [sys.executable, TEST_SCRIPT] + args, stdout=subprocess.PIPE, |
| 33 | stderr=subprocess.STDOUT, env=os.environ.copy()) |
| 34 | |
| 35 | |
| 36 | def read_subprocess_message(proc, starts_with): |
| 37 | """Finds the value after first line prefix condition.""" |
| 38 | for line in proc.stdout: |
| 39 | if line.startswith(starts_with): |
| 40 | return line.rstrip().replace(starts_with, '') |
| 41 | |
| 42 | |
| 43 | def send_and_wait(proc, sig, sleep_time=0.3): |
| 44 | """Sends a signal to subprocess.""" |
| 45 | time.sleep(sleep_time) # gives process time to launch. |
| 46 | os.kill(proc.pid, sig) |
| 47 | proc.wait() |
| 48 | |
| 49 | |
| 50 | class SignalingWindowsTest(unittest.TestCase): |
| 51 | |
| 52 | def setUp(self): |
| 53 | super(SignalingWindowsTest, self).setUp() |
| 54 | if sys.platform != 'win32': |
| 55 | self.skipTest('test only runs on Windows') |
| 56 | |
| 57 | def test_send_ctrl_break_event(self): |
| 58 | proc = launch_process_windows([]) |
| 59 | send_and_wait(proc, signal.CTRL_BREAK_EVENT) |
| 60 | sig = read_subprocess_message(proc, 'Signal :') |
| 61 | self.assertEqual(sig, str(signal.SIGBREAK)) |
| 62 | |
| 63 | |
| 64 | class SignalingNonWindowsTest(unittest.TestCase): |
| 65 | |
| 66 | def setUp(self): |
| 67 | super(SignalingNonWindowsTest, self).setUp() |
| 68 | if sys.platform == 'win32': |
| 69 | self.skipTest('test does not run on Windows') |
| 70 | |
| 71 | def test_send_sigterm(self): |
| 72 | proc = launch_process_nonwindows([]) |
| 73 | send_and_wait(proc, signal.SIGTERM) |
| 74 | sig = read_subprocess_message(proc, 'Signal :') |
| 75 | self.assertEqual(sig, str(signal.SIGTERM)) |
| 76 | |
| 77 | def test_send_sigint(self): |
| 78 | proc = launch_process_nonwindows([]) |
| 79 | send_and_wait(proc, signal.SIGINT) |
| 80 | sig = read_subprocess_message(proc, 'Signal :') |
| 81 | self.assertEqual(sig, str(signal.SIGINT)) |
| 82 | |
| 83 | |
| 84 | if __name__ == '__main__': |
| 85 | unittest.main() |