Movatterモバイル変換


[0]ホーム

URL:


Google Git
Sign in
chromium /chromium /src /refs/heads/main /. /testing /test_env_unittest.py
blob: bc24aad39bdd917326ff4f2f628b9ec1a78fc8a0 [file] [log] [blame]
Bruce Dawson5b5681a2022-08-04 17:17:18[diff] [blame]1#!/usr/bin/env python3
Avi Drissmandfd880852022-09-15 20:11:09[diff] [blame]2# Copyright 2019 The Chromium Authors
Caleb Rouleau6844df152019-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 Rouleau6844df152019-09-11 01:11:59[diff] [blame]5"""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:14[diff] [blame]19HERE= os.path.dirname(os.path.abspath(__file__))
20TEST_SCRIPT= os.path.join(HERE,'test_env_user_script.py')
Caleb Rouleau6844df152019-09-11 01:11:59[diff] [blame]21
22
23def launch_process_windows(args):
Chris McDonaldc3e0f26b2020-06-04 02:15:14[diff] [blame]24# The `universal_newlines` option is equivalent to `text` in Python 3.
Ben Pasteneb5c67262024-05-15 21:24:01[diff] [blame]25return 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:14[diff] [blame]31
Caleb Rouleau6844df152019-09-11 01:11:59[diff] [blame]32
33def launch_process_nonwindows(args):
Chris McDonaldc3e0f26b2020-06-04 02:15:14[diff] [blame]34# The `universal_newlines` option is equivalent to `text` in Python 3.
Ben Pasteneb5c67262024-05-15 21:24:01[diff] [blame]35return 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:59[diff] [blame]40
41
Joshua Hood3fade1f2022-05-04 16:00:42[diff] [blame]42# pylint: disable=inconsistent-return-statements
Caleb Rouleau6844df152019-09-11 01:11:59[diff] [blame]43def read_subprocess_message(proc, starts_with):
44"""Finds the value after first line prefix condition."""
45for linein proc.stdout:
46if line.startswith(starts_with):
47return line.rstrip().replace(starts_with,'')
Ben Pasteneb5c67262024-05-15 21:24:01[diff] [blame]48
49
Joshua Hood3fade1f2022-05-04 16:00:42[diff] [blame]50# pylint: enable=inconsistent-return-statements
Caleb Rouleau6844df152019-09-11 01:11:59[diff] [blame]51
52
Bruce Dawson5b5681a2022-08-04 17:17:18[diff] [blame]53def send_and_wait(proc, sig, sleep_time=0.6):
Caleb Rouleau6844df152019-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
60classSignalingWindowsTest(unittest.TestCase):
61
62def setUp(self):
Bruce Dawson5b5681a2022-08-04 17:17:18[diff] [blame]63 super().setUp()
Caleb Rouleau6844df152019-09-11 01:11:59[diff] [blame]64if sys.platform!='win32':
65 self.skipTest('test only runs on Windows')
66
67def test_send_ctrl_break_event(self):
68 proc= launch_process_windows([])
Ben Pasteneb5c67262024-05-15 21:24:01[diff] [blame]69 send_and_wait(proc, signal.CTRL_BREAK_EVENT)# pylint: disable=no-member
Caleb Rouleau6844df152019-09-11 01:11:59[diff] [blame]70 sig= read_subprocess_message(proc,'Signal :')
Bruce Dawson5b5681a2022-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 Pasteneb5c67262024-05-15 21:24:01[diff] [blame]75 self.assertEqual(sig, str(int(signal.SIGBREAK)))# pylint: disable=no-member
Caleb Rouleau6844df152019-09-11 01:11:59[diff] [blame]76
77
78classSignalingNonWindowsTest(unittest.TestCase):
79
80def setUp(self):
Bruce Dawson5b5681a2022-08-04 17:17:18[diff] [blame]81 super().setUp()
Caleb Rouleau6844df152019-09-11 01:11:59[diff] [blame]82if sys.platform=='win32':
83 self.skipTest('test does not run on Windows')
84
85def 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:14[diff] [blame]89 self.assertEqual(sig, str(int(signal.SIGTERM)))
Caleb Rouleau6844df152019-09-11 01:11:59[diff] [blame]90
91def 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:14[diff] [blame]95 self.assertEqual(sig, str(int(signal.SIGINT)))
Caleb Rouleau6844df152019-09-11 01:11:59[diff] [blame]96
97
98if __name__=='__main__':
99 unittest.main()

[8]ページ先頭

©2009-2025 Movatter.jp