blob: bc24aad39bdd917326ff4f2f628b9ec1a78fc8a0 [file] [log] [blame]
Bruce Dawson5b5681a2022-08-04 17:17:181#!/usr/bin/env python3
Avi Drissmandfd880852022-09-15 20:11:092# Copyright 2019 The Chromium Authors
Caleb Rouleau6844df152019-09-11 01:11:593# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
Caleb Rouleau6844df152019-09-11 01:11:595"""Unit tests for test_env.py functionality.
6
7Each unit test is launches python process that uses test_env.py
8to launch another python process. Then signal handling and
9propagation is tested. This similates how Swarming uses test_env.py.
10"""
11
12import os
13import signal
14import subprocess
15import sys
16import time
17import unittest
18
Chris McDonaldc3e0f26b2020-06-04 02:15:1419HERE = os.path.dirname(os.path.abspath(__file__))
20TEST_SCRIPT = os.path.join(HERE, 'test_env_user_script.py')
Caleb Rouleau6844df152019-09-11 01:11:5921
22
23def launch_process_windows(args):
Chris McDonaldc3e0f26b2020-06-04 02:15:1424 # The `universal_newlines` option is equivalent to `text` in Python 3.
Ben Pasteneb5c67262024-05-15 21:24:0125 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 McDonaldc3e0f26b2020-06-04 02:15:1431
Caleb Rouleau6844df152019-09-11 01:11:5932
33def launch_process_nonwindows(args):
Chris McDonaldc3e0f26b2020-06-04 02:15:1434 # The `universal_newlines` option is equivalent to `text` in Python 3.
Ben Pasteneb5c67262024-05-15 21:24:0135 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 Rouleau6844df152019-09-11 01:11:5940
41
Joshua Hood3fade1f2022-05-04 16:00:4242# pylint: disable=inconsistent-return-statements
Caleb Rouleau6844df152019-09-11 01:11:5943def 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 Pasteneb5c67262024-05-15 21:24:0148
49
Joshua Hood3fade1f2022-05-04 16:00:4250# pylint: enable=inconsistent-return-statements
Caleb Rouleau6844df152019-09-11 01:11:5951
52
Bruce Dawson5b5681a2022-08-04 17:17:1853def send_and_wait(proc, sig, sleep_time=0.6):
Caleb Rouleau6844df152019-09-11 01:11:5954 """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
60class SignalingWindowsTest(unittest.TestCase):
61
62 def setUp(self):
Bruce Dawson5b5681a2022-08-04 17:17:1863 super().setUp()
Caleb Rouleau6844df152019-09-11 01:11:5964 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 Pasteneb5c67262024-05-15 21:24:0169 send_and_wait(proc, signal.CTRL_BREAK_EVENT) # pylint: disable=no-member
Caleb Rouleau6844df152019-09-11 01:11:5970 sig = read_subprocess_message(proc, 'Signal :')
Bruce Dawson5b5681a2022-08-04 17:17:1871 # 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 Pasteneb5c67262024-05-15 21:24:0175 self.assertEqual(sig, str(int(signal.SIGBREAK))) # pylint: disable=no-member
Caleb Rouleau6844df152019-09-11 01:11:5976
77
78class SignalingNonWindowsTest(unittest.TestCase):
79
80 def setUp(self):
Bruce Dawson5b5681a2022-08-04 17:17:1881 super().setUp()
Caleb Rouleau6844df152019-09-11 01:11:5982 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 McDonaldc3e0f26b2020-06-04 02:15:1489 self.assertEqual(sig, str(int(signal.SIGTERM)))
Caleb Rouleau6844df152019-09-11 01:11:5990
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 McDonaldc3e0f26b2020-06-04 02:15:1495 self.assertEqual(sig, str(int(signal.SIGINT)))
Caleb Rouleau6844df152019-09-11 01:11:5996
97
98if __name__ == '__main__':
99 unittest.main()