blob: 82cb75bc6a2ef1cf42ba36f6389f54d89ffdbaf6 [file] [log] [blame]
Oleh Prypin69c02222018-05-23 15:18:12 +02001#!/usr/bin/env python
2
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():
Mirko Bonadei8cc66952020-10-30 10:13:45 +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):
Mirko Bonadei8cc66952020-10-30 10:13:45 +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
Mirko Bonadei8cc66952020-10-30 10:13:45 +010034 def testGetTwiceWorkers(self):
35 expected = 2 * multiprocessing.cpu_count()
36 # pylint: disable=protected-access
37 self.assertEqual(gtest_parallel_wrapper._ParseWorkersOption('2x'),
38 expected)
Yves Gereyea766fa2018-09-25 15:34:27 +020039
Mirko Bonadei8cc66952020-10-30 10:13:45 +010040 def testGetHalfWorkers(self):
41 expected = max(multiprocessing.cpu_count() // 2, 1)
42 # pylint: disable=protected-access
43 self.assertEqual(gtest_parallel_wrapper._ParseWorkersOption('0.5x'),
44 expected)
Yves Gereyea766fa2018-09-25 15:34:27 +020045
46
Oleh Prypin69c02222018-05-23 15:18:12 +020047class GtestParallelWrapperTest(unittest.TestCase):
Mirko Bonadei8cc66952020-10-30 10:13:45 +010048 @classmethod
49 def _Expected(cls, gtest_parallel_args):
50 return ['--shard_index=0', '--shard_count=1'] + gtest_parallel_args
Yves Gereyea766fa2018-09-25 15:34:27 +020051
Mirko Bonadei8cc66952020-10-30 10:13:45 +010052 def testOverwrite(self):
53 result = gtest_parallel_wrapper.ParseArgs(
54 ['--timeout=123', 'exec', '--timeout', '124'])
55 expected = self._Expected(['--timeout=124', 'exec'])
56 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +020057
Mirko Bonadei8cc66952020-10-30 10:13:45 +010058 def testMixing(self):
59 result = gtest_parallel_wrapper.ParseArgs([
60 '--timeout=123', '--param1', 'exec', '--param2', '--timeout', '124'
61 ])
62 expected = self._Expected(
63 ['--timeout=124', 'exec', '--', '--param1', '--param2'])
64 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +020065
Mirko Bonadei8cc66952020-10-30 10:13:45 +010066 def testMixingPositional(self):
67 result = gtest_parallel_wrapper.ParseArgs([
68 '--timeout=123', 'exec', '--foo1', 'bar1', '--timeout', '124',
69 '--foo2', 'bar2'
70 ])
71 expected = self._Expected([
72 '--timeout=124', 'exec', '--', '--foo1', 'bar1', '--foo2', 'bar2'
73 ])
74 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +020075
Mirko Bonadei8cc66952020-10-30 10:13:45 +010076 def testDoubleDash1(self):
77 result = gtest_parallel_wrapper.ParseArgs(
78 ['--timeout', '123', 'exec', '--', '--timeout', '124'])
79 expected = self._Expected(
80 ['--timeout=123', 'exec', '--', '--timeout', '124'])
81 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +020082
Mirko Bonadei8cc66952020-10-30 10:13:45 +010083 def testDoubleDash2(self):
84 result = gtest_parallel_wrapper.ParseArgs(
85 ['--timeout=123', '--', 'exec', '--timeout=124'])
86 expected = self._Expected(
87 ['--timeout=123', 'exec', '--', '--timeout=124'])
88 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +020089
Mirko Bonadei8cc66952020-10-30 10:13:45 +010090 def testArtifacts(self):
91 with TemporaryDirectory() as tmp_dir:
92 output_dir = os.path.join(tmp_dir, 'foo')
93 result = gtest_parallel_wrapper.ParseArgs(
94 ['exec', '--store-test-artifacts', '--output_dir', output_dir])
95 exp_artifacts_dir = os.path.join(output_dir, 'test_artifacts')
96 exp = self._Expected([
97 '--output_dir=' + output_dir, 'exec', '--',
98 '--test_artifacts_dir=' + exp_artifacts_dir
99 ])
100 self.assertEqual(result.gtest_parallel_args, exp)
101 self.assertEqual(result.output_dir, output_dir)
102 self.assertEqual(result.test_artifacts_dir, exp_artifacts_dir)
Oleh Prypin69c02222018-05-23 15:18:12 +0200103
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100104 def testNoDirsSpecified(self):
105 result = gtest_parallel_wrapper.ParseArgs(['exec'])
106 self.assertEqual(result.output_dir, None)
107 self.assertEqual(result.test_artifacts_dir, None)
Oleh Prypin69c02222018-05-23 15:18:12 +0200108
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100109 def testOutputDirSpecified(self):
110 result = gtest_parallel_wrapper.ParseArgs(
111 ['exec', '--output_dir', '/tmp/foo'])
112 self.assertEqual(result.output_dir, '/tmp/foo')
113 self.assertEqual(result.test_artifacts_dir, None)
Oleh Prypin69c02222018-05-23 15:18:12 +0200114
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100115 def testShortArg(self):
116 result = gtest_parallel_wrapper.ParseArgs(['-d', '/tmp/foo', 'exec'])
117 expected = self._Expected(['--output_dir=/tmp/foo', 'exec'])
118 self.assertEqual(result.gtest_parallel_args, expected)
119 self.assertEqual(result.output_dir, '/tmp/foo')
Oleh Prypin69c02222018-05-23 15:18:12 +0200120
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100121 def testBoolArg(self):
122 result = gtest_parallel_wrapper.ParseArgs(
123 ['--gtest_also_run_disabled_tests', 'exec'])
124 expected = self._Expected(['--gtest_also_run_disabled_tests', 'exec'])
125 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +0200126
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100127 def testNoArgs(self):
128 result = gtest_parallel_wrapper.ParseArgs(['exec'])
129 expected = self._Expected(['exec'])
130 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +0200131
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100132 def testDocExample(self):
133 with TemporaryDirectory() as tmp_dir:
134 output_dir = os.path.join(tmp_dir, 'foo')
135 result = gtest_parallel_wrapper.ParseArgs([
136 'some_test', '--some_flag=some_value', '--another_flag',
137 '--output_dir=' + output_dir, '--store-test-artifacts',
138 '--isolated-script-test-perf-output=SOME_OTHER_DIR',
139 '--foo=bar', '--baz'
140 ])
141 expected_artifacts_dir = os.path.join(output_dir, 'test_artifacts')
142 expected = self._Expected([
143 '--output_dir=' + output_dir, 'some_test', '--',
144 '--test_artifacts_dir=' + expected_artifacts_dir,
145 '--some_flag=some_value', '--another_flag',
146 '--isolated_script_test_perf_output=SOME_OTHER_DIR',
147 '--foo=bar', '--baz'
148 ])
149 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +0200150
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100151 def testStandardWorkers(self):
152 """Check integer value is passed as-is."""
153 result = gtest_parallel_wrapper.ParseArgs(['--workers', '17', 'exec'])
154 expected = self._Expected(['--workers=17', 'exec'])
155 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +0200156
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100157 def testTwoWorkersPerCpuCore(self):
158 result = gtest_parallel_wrapper.ParseArgs(['--workers', '2x', 'exec'])
159 workers = 2 * multiprocessing.cpu_count()
160 expected = self._Expected(['--workers=%s' % workers, 'exec'])
161 self.assertEqual(result.gtest_parallel_args, expected)
Yves Gereyea766fa2018-09-25 15:34:27 +0200162
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100163 def testUseHalfTheCpuCores(self):
164 result = gtest_parallel_wrapper.ParseArgs(
165 ['--workers', '0.5x', 'exec'])
166 workers = max(multiprocessing.cpu_count() // 2, 1)
167 expected = self._Expected(['--workers=%s' % workers, 'exec'])
168 self.assertEqual(result.gtest_parallel_args, expected)
Yves Gereyea766fa2018-09-25 15:34:27 +0200169
Oleh Prypin69c02222018-05-23 15:18:12 +0200170
171if __name__ == '__main__':
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100172 unittest.main()