blob: 563947c1324a49796e9c9f6fb4a0aef9520cc587 [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.
5
6"""Unit tests for test_env.py functionality.
7
8Each unit test is launches python process that uses test_env.py
9to launch another python process. Then signal handling and
10propagation is tested. This similates how Swarming uses test_env.py.
11"""
12
13import os
14import signal
15import subprocess
16import sys
17import time
18import unittest
19
Chris McDonaldc3e0f26b2020-06-04 02:15:1420HERE = os.path.dirname(os.path.abspath(__file__))
21TEST_SCRIPT = os.path.join(HERE, 'test_env_user_script.py')
Caleb Rouleau6844df152019-09-11 01:11:5922
23
24def launch_process_windows(args):
Chris McDonaldc3e0f26b2020-06-04 02:15:1425 # The `universal_newlines` option is equivalent to `text` in Python 3.
Caleb Rouleau6844df152019-09-11 01:11:5926 return subprocess.Popen(
Chris McDonaldc3e0f26b2020-06-04 02:15:1427 [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 Rouleau6844df152019-09-11 01:11:5934
35def launch_process_nonwindows(args):
Chris McDonaldc3e0f26b2020-06-04 02:15:1436 # The `universal_newlines` option is equivalent to `text` in Python 3.
Caleb Rouleau6844df152019-09-11 01:11:5937 return subprocess.Popen(
Chris McDonaldc3e0f26b2020-06-04 02:15:1438 [sys.executable, TEST_SCRIPT] + args,
39 stdout=subprocess.PIPE,
40 stderr=subprocess.STDOUT,
41 env=os.environ.copy(),
42 universal_newlines=True)
Caleb Rouleau6844df152019-09-11 01:11:5943
44
Joshua Hood3fade1f2022-05-04 16:00:4245# pylint: disable=inconsistent-return-statements
Caleb Rouleau6844df152019-09-11 01:11:5946def read_subprocess_message(proc, starts_with):
47 """Finds the value after first line prefix condition."""
48 for line in proc.stdout:
49 if line.startswith(starts_with):
50 return line.rstrip().replace(starts_with, '')
Joshua Hood3fade1f2022-05-04 16:00:4251# pylint: enable=inconsistent-return-statements
Caleb Rouleau6844df152019-09-11 01:11:5952
53
Bruce Dawson5b5681a2022-08-04 17:17:1854def send_and_wait(proc, sig, sleep_time=0.6):
Caleb Rouleau6844df152019-09-11 01:11:5955 """Sends a signal to subprocess."""
56 time.sleep(sleep_time) # gives process time to launch.
57 os.kill(proc.pid, sig)
58 proc.wait()
59
60
61class SignalingWindowsTest(unittest.TestCase):
62
63 def setUp(self):
Bruce Dawson5b5681a2022-08-04 17:17:1864 super().setUp()
Caleb Rouleau6844df152019-09-11 01:11:5965 if sys.platform != 'win32':
66 self.skipTest('test only runs on Windows')
67
68 def test_send_ctrl_break_event(self):
69 proc = launch_process_windows([])
Joshua Hood3fade1f2022-05-04 16:00:4270 send_and_wait(proc, signal.CTRL_BREAK_EVENT) # pylint: disable=no-member
Caleb Rouleau6844df152019-09-11 01:11:5971 sig = read_subprocess_message(proc, 'Signal :')
Bruce Dawson5b5681a2022-08-04 17:17:1872 # This test is flaky because it relies on the child process starting quickly
73 # "enough", which it fails to do sometimes. This is tracked by
74 # https://crbug.com/1335123 and it is hoped that increasing the timeout will
75 # reduce the flakiness.
Joshua Hood3fade1f2022-05-04 16:00:4276 self.assertEqual(sig, str(int(signal.SIGBREAK))) # pylint: disable=no-member
Caleb Rouleau6844df152019-09-11 01:11:5977
78
79class SignalingNonWindowsTest(unittest.TestCase):
80
81 def setUp(self):
Bruce Dawson5b5681a2022-08-04 17:17:1882 super().setUp()
Caleb Rouleau6844df152019-09-11 01:11:5983 if sys.platform == 'win32':
84 self.skipTest('test does not run on Windows')
85
86 def test_send_sigterm(self):
87 proc = launch_process_nonwindows([])
88 send_and_wait(proc, signal.SIGTERM)
89 sig = read_subprocess_message(proc, 'Signal :')
Chris McDonaldc3e0f26b2020-06-04 02:15:1490 self.assertEqual(sig, str(int(signal.SIGTERM)))
Caleb Rouleau6844df152019-09-11 01:11:5991
92 def test_send_sigint(self):
93 proc = launch_process_nonwindows([])
94 send_and_wait(proc, signal.SIGINT)
95 sig = read_subprocess_message(proc, 'Signal :')
Chris McDonaldc3e0f26b2020-06-04 02:15:1496 self.assertEqual(sig, str(int(signal.SIGINT)))
Caleb Rouleau6844df152019-09-11 01:11:5997
98
99if __name__ == '__main__':
100 unittest.main()