blob: 47b78f8705a0dd6143d68907fc7d72cf86959e8d [file] [log] [blame]
Ilia Samsonova00835302019-04-19 17:37:591#!/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
8Each unit test is launching xvfb_test_script.py
9through xvfb.py as a subprocess, then tests its expected output.
10"""
11
12import os
13import signal
14import subprocess
15import sys
16import time
17import unittest
18
Joshua Hood8defc6c2022-04-26 15:35:0619# pylint: disable=super-with-arguments
20
Ilia Samsonova00835302019-04-19 17:37:5921
22TEST_FILE = __file__.replace('.pyc', '.py')
23XVFB = TEST_FILE.replace('_unittest', '')
24XVFB_TEST_SCRIPT = TEST_FILE.replace('_unittest', '_test_script')
25
26
27def launch_process(args):
28 """Launches a sub process to run through xvfb.py."""
Ilia Samsonova00835302019-04-19 17:37:5929 return subprocess.Popen(
30 [XVFB, XVFB_TEST_SCRIPT] + args, stdout=subprocess.PIPE,
31 stderr=subprocess.STDOUT, env=os.environ.copy())
32
33
Joshua Hood8defc6c2022-04-26 15:35:0634# pylint: disable=inconsistent-return-statements
Ilia Samsonova00835302019-04-19 17:37:5935def read_subprocess_message(proc, starts_with):
36 """Finds the value after first line prefix condition."""
Joshua Hood8defc6c2022-04-26 15:35:0637 for line in proc.stdout.read().decode('utf-8').splitlines(True):
Ilia Samsonova00835302019-04-19 17:37:5938 if line.startswith(starts_with):
39 return line.rstrip().replace(starts_with, '')
Joshua Hood8defc6c2022-04-26 15:35:0640# pylint: enable=inconsistent-return-statements
Ilia Samsonova00835302019-04-19 17:37:5941
42
43def send_signal(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
50class XvfbLinuxTest(unittest.TestCase):
51
52 def setUp(self):
53 super(XvfbLinuxTest, self).setUp()
Joshua Hood8defc6c2022-04-26 15:35:0654 if sys.platform != 'linux':
Ilia Samsonova00835302019-04-19 17:37:5955 self.skipTest('linux only test')
56
57 def test_no_xvfb_display(self):
58 proc = launch_process(['--no-xvfb'])
59 proc.wait()
60 display = read_subprocess_message(proc, 'Display :')
61 self.assertEqual(display, os.environ.get('DISPLAY', 'None'))
62
63 def test_xvfb_display(self):
64 proc = launch_process([])
65 proc.wait()
66 display = read_subprocess_message(proc, 'Display :')
67 self.assertIsNotNone(display)
68 self.assertNotEqual(display, os.environ.get('DISPLAY', 'None'))
69
70 def test_no_xvfb_flag(self):
71 proc = launch_process(['--no-xvfb'])
72 proc.wait()
Ilia Samsonova00835302019-04-19 17:37:5973
74 def test_xvfb_flag(self):
75 proc = launch_process([])
76 proc.wait()
Ilia Samsonova00835302019-04-19 17:37:5977
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
88class 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 Tong87fbe5a2020-08-10 22:58:4097 send_signal(proc, signal.SIGINT, 1)
Ilia Samsonova00835302019-04-19 17:37:5998 sig = read_subprocess_message(proc, 'Signal :')
Joshua Hood8defc6c2022-04-26 15:35:0699 self.assertEqual(int(sig), int(signal.SIGINT))
Ilia Samsonova00835302019-04-19 17:37:59100
101 def test_send_sigterm(self):
102 proc = launch_process(['--sleep'])
Gary Tong87fbe5a2020-08-10 22:58:40103 send_signal(proc, signal.SIGTERM, 1)
Ilia Samsonova00835302019-04-19 17:37:59104 sig = read_subprocess_message(proc, 'Signal :')
Joshua Hood8defc6c2022-04-26 15:35:06105 self.assertEqual(int(sig), int(signal.SIGTERM))
Ilia Samsonova00835302019-04-19 17:37:59106
107if __name__ == '__main__':
108 unittest.main()