Ilia Samsonov | a0083530 | 2019-04-19 17:37: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 xvfb.py functionality. |
| 7 | |
| 8 | Each unit test is launching xvfb_test_script.py |
| 9 | through xvfb.py as a subprocess, then tests its expected output. |
| 10 | """ |
| 11 | |
| 12 | import os |
| 13 | import signal |
| 14 | import subprocess |
| 15 | import sys |
| 16 | import time |
| 17 | import unittest |
| 18 | |
| 19 | |
| 20 | TEST_FILE = __file__.replace('.pyc', '.py') |
| 21 | XVFB = TEST_FILE.replace('_unittest', '') |
| 22 | XVFB_TEST_SCRIPT = TEST_FILE.replace('_unittest', '_test_script') |
| 23 | |
| 24 | |
| 25 | def launch_process(args): |
| 26 | """Launches a sub process to run through xvfb.py.""" |
Ilia Samsonov | a0083530 | 2019-04-19 17:37:59 | [diff] [blame] | 27 | return subprocess.Popen( |
| 28 | [XVFB, XVFB_TEST_SCRIPT] + args, stdout=subprocess.PIPE, |
| 29 | stderr=subprocess.STDOUT, env=os.environ.copy()) |
| 30 | |
| 31 | |
| 32 | def read_subprocess_message(proc, starts_with): |
| 33 | """Finds the value after first line prefix condition.""" |
| 34 | for line in proc.stdout: |
| 35 | if line.startswith(starts_with): |
| 36 | return line.rstrip().replace(starts_with, '') |
| 37 | |
| 38 | |
| 39 | def send_signal(proc, sig, sleep_time=0.3): |
| 40 | """Sends a signal to subprocess.""" |
| 41 | time.sleep(sleep_time) # gives process time to launch. |
| 42 | os.kill(proc.pid, sig) |
| 43 | proc.wait() |
| 44 | |
| 45 | |
| 46 | class XvfbLinuxTest(unittest.TestCase): |
| 47 | |
| 48 | def setUp(self): |
| 49 | super(XvfbLinuxTest, self).setUp() |
| 50 | if sys.platform != 'linux2': |
| 51 | self.skipTest('linux only test') |
| 52 | |
| 53 | def test_no_xvfb_display(self): |
| 54 | proc = launch_process(['--no-xvfb']) |
| 55 | proc.wait() |
| 56 | display = read_subprocess_message(proc, 'Display :') |
| 57 | self.assertEqual(display, os.environ.get('DISPLAY', 'None')) |
| 58 | |
| 59 | def test_xvfb_display(self): |
| 60 | proc = launch_process([]) |
| 61 | proc.wait() |
| 62 | display = read_subprocess_message(proc, 'Display :') |
| 63 | self.assertIsNotNone(display) |
| 64 | self.assertNotEqual(display, os.environ.get('DISPLAY', 'None')) |
| 65 | |
| 66 | def test_no_xvfb_flag(self): |
| 67 | proc = launch_process(['--no-xvfb']) |
| 68 | proc.wait() |
| 69 | environ_flag = read_subprocess_message(proc, 'Inside_xvfb :') |
| 70 | self.assertEqual(environ_flag, 'None') |
| 71 | |
| 72 | def test_xvfb_flag(self): |
| 73 | proc = launch_process([]) |
| 74 | proc.wait() |
| 75 | environ_flag = read_subprocess_message(proc, 'Inside_xvfb :') |
| 76 | self.assertEqual(environ_flag, '1') |
| 77 | |
| 78 | def test_xvfb_race_condition(self): |
| 79 | proc_list = [launch_process([]) for _ in range(15)] |
| 80 | for proc in proc_list: |
| 81 | proc.wait() |
| 82 | display_list = [read_subprocess_message(p, 'Display :') for p in proc_list] |
| 83 | for display in display_list: |
| 84 | self.assertIsNotNone(display) |
| 85 | self.assertNotEqual(display, os.environ.get('DISPLAY', 'None')) |
| 86 | |
| 87 | |
| 88 | class XvfbTest(unittest.TestCase): |
| 89 | |
| 90 | def setUp(self): |
| 91 | super(XvfbTest, self).setUp() |
| 92 | if sys.platform == 'win32': |
| 93 | self.skipTest('non-win32 test') |
| 94 | |
| 95 | def test_send_sigint(self): |
| 96 | proc = launch_process(['--sleep']) |
Gary Tong | 87fbe5a | 2020-08-10 22:58:40 | [diff] [blame] | 97 | send_signal(proc, signal.SIGINT, 1) |
Ilia Samsonov | a0083530 | 2019-04-19 17:37:59 | [diff] [blame] | 98 | sig = read_subprocess_message(proc, 'Signal :') |
| 99 | self.assertEqual(sig, str(signal.SIGINT)) |
| 100 | |
| 101 | def test_send_sigterm(self): |
| 102 | proc = launch_process(['--sleep']) |
Gary Tong | 87fbe5a | 2020-08-10 22:58:40 | [diff] [blame] | 103 | send_signal(proc, signal.SIGTERM, 1) |
Ilia Samsonov | a0083530 | 2019-04-19 17:37:59 | [diff] [blame] | 104 | sig = read_subprocess_message(proc, 'Signal :') |
| 105 | self.assertEqual(sig, str(signal.SIGTERM)) |
| 106 | |
| 107 | if __name__ == '__main__': |
| 108 | unittest.main() |