blob: b84196f4914d0ccd8422e46bc8a5f446be3a40f2 [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
19
20TEST_FILE = __file__.replace('.pyc', '.py')
21XVFB = TEST_FILE.replace('_unittest', '')
22XVFB_TEST_SCRIPT = TEST_FILE.replace('_unittest', '_test_script')
23
24
25def launch_process(args):
26 """Launches a sub process to run through xvfb.py."""
Ilia Samsonova00835302019-04-19 17:37:5927 return subprocess.Popen(
28 [XVFB, XVFB_TEST_SCRIPT] + args, stdout=subprocess.PIPE,
29 stderr=subprocess.STDOUT, env=os.environ.copy())
30
31
32def 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
39def 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
46class 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
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 :')
99 self.assertEqual(sig, str(signal.SIGINT))
100
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 :')
105 self.assertEqual(sig, str(signal.SIGTERM))
106
107if __name__ == '__main__':
108 unittest.main()