blob: 609052d2488ef30b7957b6c0a1aaba80b0f3e408 [file] [log] [blame]
Christoffer Jansson4e8a7732022-02-08 09:01:12 +01001#!/usr/bin/env vpython3
Oleh Prypin69c02222018-05-23 15:18:12 +02002
3# Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
4#
5# Use of this source code is governed by a BSD-style license
6# that can be found in the LICENSE file in the root of the source
7# tree. An additional intellectual property rights grant can be found
8# in the file PATENTS. All contributing project authors may
9# be found in the AUTHORS file in the root of the source tree.
10
Mirko Bonadeidac422f2018-06-05 09:12:34 +020011from contextlib import contextmanager
12
Yves Gereyea766fa2018-09-25 15:34:27 +020013import multiprocessing
Mirko Bonadeidac422f2018-06-05 09:12:34 +020014import os
15import tempfile
Oleh Prypin69c02222018-05-23 15:18:12 +020016import unittest
17
Yves Gereyea766fa2018-09-25 15:34:27 +020018# pylint: disable=invalid-name
19gtest_parallel_wrapper = __import__('gtest-parallel-wrapper')
Oleh Prypin69c02222018-05-23 15:18:12 +020020
21
Mirko Bonadeidac422f2018-06-05 09:12:34 +020022@contextmanager
23def TemporaryDirectory():
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010024 tmp_dir = tempfile.mkdtemp()
25 yield tmp_dir
26 os.rmdir(tmp_dir)
Mirko Bonadeidac422f2018-06-05 09:12:34 +020027
28
Yves Gereyea766fa2018-09-25 15:34:27 +020029class GtestParallelWrapperHelpersTest(unittest.TestCase):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010030 def testGetWorkersAsIs(self):
31 # pylint: disable=protected-access
32 self.assertEqual(gtest_parallel_wrapper._ParseWorkersOption('12'), 12)
Yves Gereyea766fa2018-09-25 15:34:27 +020033
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010034 def testGetTwiceWorkers(self):
35 expected = 2 * multiprocessing.cpu_count()
36 # pylint: disable=protected-access
37 self.assertEqual(gtest_parallel_wrapper._ParseWorkersOption('2x'), expected)
Yves Gereyea766fa2018-09-25 15:34:27 +020038
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010039 def testGetHalfWorkers(self):
40 expected = max(multiprocessing.cpu_count() // 2, 1)
41 # pylint: disable=protected-access
42 self.assertEqual(gtest_parallel_wrapper._ParseWorkersOption('0.5x'),
43 expected)
Yves Gereyea766fa2018-09-25 15:34:27 +020044
45
Oleh Prypin69c02222018-05-23 15:18:12 +020046class GtestParallelWrapperTest(unittest.TestCase):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010047 @classmethod
48 def _Expected(cls, gtest_parallel_args):
49 return ['--shard_index=0', '--shard_count=1'] + gtest_parallel_args
Yves Gereyea766fa2018-09-25 15:34:27 +020050
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010051 def testOverwrite(self):
52 result = gtest_parallel_wrapper.ParseArgs(
53 ['--timeout=123', 'exec', '--timeout', '124'])
54 expected = self._Expected(['--timeout=124', 'exec'])
55 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +020056
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010057 def testMixing(self):
58 result = gtest_parallel_wrapper.ParseArgs(
59 ['--timeout=123', '--param1', 'exec', '--param2', '--timeout', '124'])
60 expected = self._Expected(
61 ['--timeout=124', 'exec', '--', '--param1', '--param2'])
62 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +020063
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010064 def testMixingPositional(self):
65 result = gtest_parallel_wrapper.ParseArgs([
66 '--timeout=123', 'exec', '--foo1', 'bar1', '--timeout', '124', '--foo2',
67 'bar2'
68 ])
69 expected = self._Expected(
70 ['--timeout=124', 'exec', '--', '--foo1', 'bar1', '--foo2', 'bar2'])
71 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +020072
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010073 def testDoubleDash1(self):
74 result = gtest_parallel_wrapper.ParseArgs(
75 ['--timeout', '123', 'exec', '--', '--timeout', '124'])
76 expected = self._Expected(
77 ['--timeout=123', 'exec', '--', '--timeout', '124'])
78 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +020079
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010080 def testDoubleDash2(self):
81 result = gtest_parallel_wrapper.ParseArgs(
82 ['--timeout=123', '--', 'exec', '--timeout=124'])
83 expected = self._Expected(['--timeout=123', 'exec', '--', '--timeout=124'])
84 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +020085
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010086 def testArtifacts(self):
87 with TemporaryDirectory() as tmp_dir:
88 output_dir = os.path.join(tmp_dir, 'foo')
89 result = gtest_parallel_wrapper.ParseArgs(
90 ['exec', '--store-test-artifacts', '--output_dir', output_dir])
91 exp_artifacts_dir = os.path.join(output_dir, 'test_artifacts')
92 exp = self._Expected([
93 '--output_dir=' + output_dir, 'exec', '--',
94 '--test_artifacts_dir=' + exp_artifacts_dir
95 ])
96 self.assertEqual(result.gtest_parallel_args, exp)
97 self.assertEqual(result.output_dir, output_dir)
98 self.assertEqual(result.test_artifacts_dir, exp_artifacts_dir)
Oleh Prypin69c02222018-05-23 15:18:12 +020099
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100100 def testNoDirsSpecified(self):
101 result = gtest_parallel_wrapper.ParseArgs(['exec'])
102 self.assertEqual(result.output_dir, None)
103 self.assertEqual(result.test_artifacts_dir, None)
Oleh Prypin69c02222018-05-23 15:18:12 +0200104
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100105 def testOutputDirSpecified(self):
106 result = gtest_parallel_wrapper.ParseArgs(
107 ['exec', '--output_dir', '/tmp/foo'])
108 self.assertEqual(result.output_dir, '/tmp/foo')
109 self.assertEqual(result.test_artifacts_dir, None)
Oleh Prypin69c02222018-05-23 15:18:12 +0200110
Jeremy Leconte63472e52022-02-11 13:46:02 +0100111 def testJsonTestResults(self):
112 result = gtest_parallel_wrapper.ParseArgs(
113 ['--isolated-script-test-output', '/tmp/foo', 'exec'])
114 expected = self._Expected(['--dump_json_test_results=/tmp/foo', 'exec'])
115 self.assertEqual(result.gtest_parallel_args, expected)
116
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100117 def testShortArg(self):
118 result = gtest_parallel_wrapper.ParseArgs(['-d', '/tmp/foo', 'exec'])
119 expected = self._Expected(['--output_dir=/tmp/foo', 'exec'])
120 self.assertEqual(result.gtest_parallel_args, expected)
121 self.assertEqual(result.output_dir, '/tmp/foo')
Oleh Prypin69c02222018-05-23 15:18:12 +0200122
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100123 def testBoolArg(self):
124 result = gtest_parallel_wrapper.ParseArgs(
125 ['--gtest_also_run_disabled_tests', 'exec'])
126 expected = self._Expected(['--gtest_also_run_disabled_tests', 'exec'])
127 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +0200128
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100129 def testNoArgs(self):
130 result = gtest_parallel_wrapper.ParseArgs(['exec'])
131 expected = self._Expected(['exec'])
132 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +0200133
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100134 def testDocExample(self):
135 with TemporaryDirectory() as tmp_dir:
136 output_dir = os.path.join(tmp_dir, 'foo')
137 result = gtest_parallel_wrapper.ParseArgs([
138 'some_test', '--some_flag=some_value', '--another_flag',
139 '--output_dir=' + output_dir, '--store-test-artifacts',
Jeremy Leconte63472e52022-02-11 13:46:02 +0100140 '--isolated-script-test-output=SOME_DIR',
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100141 '--isolated-script-test-perf-output=SOME_OTHER_DIR', '--foo=bar',
142 '--baz'
143 ])
144 expected_artifacts_dir = os.path.join(output_dir, 'test_artifacts')
145 expected = self._Expected([
Jeremy Leconte63472e52022-02-11 13:46:02 +0100146 '--output_dir=' + output_dir, '--dump_json_test_results=SOME_DIR',
147 'some_test', '--', '--test_artifacts_dir=' + expected_artifacts_dir,
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100148 '--some_flag=some_value', '--another_flag',
149 '--isolated_script_test_perf_output=SOME_OTHER_DIR', '--foo=bar',
150 '--baz'
151 ])
152 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +0200153
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100154 def testStandardWorkers(self):
155 """Check integer value is passed as-is."""
156 result = gtest_parallel_wrapper.ParseArgs(['--workers', '17', 'exec'])
157 expected = self._Expected(['--workers=17', 'exec'])
158 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +0200159
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100160 def testTwoWorkersPerCpuCore(self):
161 result = gtest_parallel_wrapper.ParseArgs(['--workers', '2x', 'exec'])
162 workers = 2 * multiprocessing.cpu_count()
163 expected = self._Expected(['--workers=%s' % workers, 'exec'])
164 self.assertEqual(result.gtest_parallel_args, expected)
Yves Gereyea766fa2018-09-25 15:34:27 +0200165
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100166 def testUseHalfTheCpuCores(self):
167 result = gtest_parallel_wrapper.ParseArgs(['--workers', '0.5x', 'exec'])
168 workers = max(multiprocessing.cpu_count() // 2, 1)
169 expected = self._Expected(['--workers=%s' % workers, 'exec'])
170 self.assertEqual(result.gtest_parallel_args, expected)
Yves Gereyea766fa2018-09-25 15:34:27 +0200171
Oleh Prypin69c02222018-05-23 15:18:12 +0200172
173if __name__ == '__main__':
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100174 unittest.main()