blob: 26135e1abb170e68e2c0d58398966b5426656bf3 [file] [log] [blame]
Oleh Prypin69c02222018-05-23 13:18:121#!/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 07:12:3411from contextlib import contextmanager
12
Yves Gereyea766fa2018-09-25 13:34:2713import multiprocessing
Mirko Bonadeidac422f2018-06-05 07:12:3414import os
15import tempfile
Oleh Prypin69c02222018-05-23 13:18:1216import unittest
17
Yves Gereyea766fa2018-09-25 13:34:2718# pylint: disable=invalid-name
19gtest_parallel_wrapper = __import__('gtest-parallel-wrapper')
Oleh Prypin69c02222018-05-23 13:18:1220
21
Mirko Bonadeidac422f2018-06-05 07:12:3422@contextmanager
23def TemporaryDirectory():
24 tmp_dir = tempfile.mkdtemp()
25 yield tmp_dir
26 os.rmdir(tmp_dir)
27
28
Yves Gereyea766fa2018-09-25 13:34:2729class GtestParallelWrapperHelpersTest(unittest.TestCase):
30
31 def testGetWorkersAsIs(self):
32 # pylint: disable=protected-access
33 self.assertEqual(gtest_parallel_wrapper._ParseWorkersOption('12'), 12)
34
35 def testGetTwiceWorkers(self):
36 expected = 2 * multiprocessing.cpu_count()
37 # pylint: disable=protected-access
38 self.assertEqual(gtest_parallel_wrapper._ParseWorkersOption('2x'), expected)
39
40 def testGetHalfWorkers(self):
41 expected = max(multiprocessing.cpu_count() // 2, 1)
42 # pylint: disable=protected-access
43 self.assertEqual(
44 gtest_parallel_wrapper._ParseWorkersOption('0.5x'), expected)
45
46
Oleh Prypin69c02222018-05-23 13:18:1247class GtestParallelWrapperTest(unittest.TestCase):
Yves Gereyea766fa2018-09-25 13:34:2748
Oleh Prypin69c02222018-05-23 13:18:1249 @classmethod
50 def _Expected(cls, gtest_parallel_args):
51 return ['--shard_index=0', '--shard_count=1'] + gtest_parallel_args
52
53 def testOverwrite(self):
Yves Gereyea766fa2018-09-25 13:34:2754 result = gtest_parallel_wrapper.ParseArgs(
55 ['--timeout=123', 'exec', '--timeout', '124'])
Oleh Prypin69c02222018-05-23 13:18:1256 expected = self._Expected(['--timeout=124', 'exec'])
57 self.assertEqual(result.gtest_parallel_args, expected)
58
59 def testMixing(self):
Yves Gereyea766fa2018-09-25 13:34:2760 result = gtest_parallel_wrapper.ParseArgs(
Oleh Prypin69c02222018-05-23 13:18:1261 ['--timeout=123', '--param1', 'exec', '--param2', '--timeout', '124'])
62 expected = self._Expected(
63 ['--timeout=124', 'exec', '--', '--param1', '--param2'])
64 self.assertEqual(result.gtest_parallel_args, expected)
65
66 def testMixingPositional(self):
Yves Gereyea766fa2018-09-25 13:34:2767 result = gtest_parallel_wrapper.ParseArgs([
68 '--timeout=123', 'exec', '--foo1', 'bar1', '--timeout', '124', '--foo2',
69 'bar2'
70 ])
71 expected = self._Expected(
72 ['--timeout=124', 'exec', '--', '--foo1', 'bar1', '--foo2', 'bar2'])
Oleh Prypin69c02222018-05-23 13:18:1273 self.assertEqual(result.gtest_parallel_args, expected)
74
75 def testDoubleDash1(self):
Yves Gereyea766fa2018-09-25 13:34:2776 result = gtest_parallel_wrapper.ParseArgs(
Oleh Prypin69c02222018-05-23 13:18:1277 ['--timeout', '123', 'exec', '--', '--timeout', '124'])
78 expected = self._Expected(
79 ['--timeout=123', 'exec', '--', '--timeout', '124'])
80 self.assertEqual(result.gtest_parallel_args, expected)
81
82 def testDoubleDash2(self):
Yves Gereyea766fa2018-09-25 13:34:2783 result = gtest_parallel_wrapper.ParseArgs(
84 ['--timeout=123', '--', 'exec', '--timeout=124'])
Oleh Prypin69c02222018-05-23 13:18:1285 expected = self._Expected(['--timeout=123', 'exec', '--', '--timeout=124'])
86 self.assertEqual(result.gtest_parallel_args, expected)
87
88 def testArtifacts(self):
Mirko Bonadeidac422f2018-06-05 07:12:3489 with TemporaryDirectory() as tmp_dir:
90 output_dir = os.path.join(tmp_dir, 'foo')
Yves Gereyea766fa2018-09-25 13:34:2791 result = gtest_parallel_wrapper.ParseArgs(
92 ['exec', '--store-test-artifacts', '--output_dir', output_dir])
Mirko Bonadeidac422f2018-06-05 07:12:3493 exp_artifacts_dir = os.path.join(output_dir, 'test_artifacts')
Yves Gereyea766fa2018-09-25 13:34:2794 exp = self._Expected([
95 '--output_dir=' + output_dir, 'exec', '--',
96 '--test_artifacts_dir=' + exp_artifacts_dir
97 ])
Mirko Bonadeidac422f2018-06-05 07:12:3498 self.assertEqual(result.gtest_parallel_args, exp)
99 self.assertEqual(result.output_dir, output_dir)
Mirko Bonadei40f876d2018-08-27 14:32:50100 self.assertEqual(result.test_artifacts_dir, exp_artifacts_dir)
Oleh Prypin69c02222018-05-23 13:18:12101
102 def testNoDirsSpecified(self):
Yves Gereyea766fa2018-09-25 13:34:27103 result = gtest_parallel_wrapper.ParseArgs(['exec'])
Oleh Prypin69c02222018-05-23 13:18:12104 self.assertEqual(result.output_dir, None)
105 self.assertEqual(result.test_artifacts_dir, None)
106
107 def testOutputDirSpecified(self):
Yves Gereyea766fa2018-09-25 13:34:27108 result = gtest_parallel_wrapper.ParseArgs(
109 ['exec', '--output_dir', '/tmp/foo'])
Oleh Prypin69c02222018-05-23 13:18:12110 self.assertEqual(result.output_dir, '/tmp/foo')
111 self.assertEqual(result.test_artifacts_dir, None)
112
Oleh Prypin69c02222018-05-23 13:18:12113 def testShortArg(self):
Yves Gereyea766fa2018-09-25 13:34:27114 result = gtest_parallel_wrapper.ParseArgs(['-d', '/tmp/foo', 'exec'])
Oleh Prypin69c02222018-05-23 13:18:12115 expected = self._Expected(['--output_dir=/tmp/foo', 'exec'])
116 self.assertEqual(result.gtest_parallel_args, expected)
117 self.assertEqual(result.output_dir, '/tmp/foo')
118
119 def testBoolArg(self):
Yves Gereyea766fa2018-09-25 13:34:27120 result = gtest_parallel_wrapper.ParseArgs(
121 ['--gtest_also_run_disabled_tests', 'exec'])
Oleh Prypin69c02222018-05-23 13:18:12122 expected = self._Expected(['--gtest_also_run_disabled_tests', 'exec'])
123 self.assertEqual(result.gtest_parallel_args, expected)
124
125 def testNoArgs(self):
Yves Gereyea766fa2018-09-25 13:34:27126 result = gtest_parallel_wrapper.ParseArgs(['exec'])
Oleh Prypin69c02222018-05-23 13:18:12127 expected = self._Expected(['exec'])
128 self.assertEqual(result.gtest_parallel_args, expected)
129
130 def testDocExample(self):
Mirko Bonadeidac422f2018-06-05 07:12:34131 with TemporaryDirectory() as tmp_dir:
132 output_dir = os.path.join(tmp_dir, 'foo')
Yves Gereyea766fa2018-09-25 13:34:27133 result = gtest_parallel_wrapper.ParseArgs([
Mirko Bonadeidac422f2018-06-05 07:12:34134 'some_test', '--some_flag=some_value', '--another_flag',
135 '--output_dir=' + output_dir, '--store-test-artifacts',
Patrik Höglund28b8a0b2020-03-26 19:30:50136 '--isolated-script-test-perf-output=SOME_OTHER_DIR', '--foo=bar',
Yves Gereyea766fa2018-09-25 13:34:27137 '--baz'
138 ])
Mirko Bonadeidac422f2018-06-05 07:12:34139 expected_artifacts_dir = os.path.join(output_dir, 'test_artifacts')
140 expected = self._Expected([
Patrik Höglund1b20c412020-03-25 07:58:51141 '--output_dir=' + output_dir,
Yves Gereyea766fa2018-09-25 13:34:27142 'some_test', '--', '--test_artifacts_dir=' + expected_artifacts_dir,
Mirko Bonadeidac422f2018-06-05 07:12:34143 '--some_flag=some_value', '--another_flag',
Mirko Bonadei2ab97f62019-07-18 11:44:12144 '--isolated_script_test_perf_output=SOME_OTHER_DIR', '--foo=bar',
Yves Gereyea766fa2018-09-25 13:34:27145 '--baz'
146 ])
Mirko Bonadeidac422f2018-06-05 07:12:34147 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 13:18:12148
Yves Gereyea766fa2018-09-25 13:34:27149 def testStandardWorkers(self):
150 """Check integer value is passed as-is."""
151 result = gtest_parallel_wrapper.ParseArgs(['--workers', '17', 'exec'])
152 expected = self._Expected(['--workers=17', 'exec'])
153 self.assertEqual(result.gtest_parallel_args, expected)
154
155 def testTwoWorkersPerCpuCore(self):
156 result = gtest_parallel_wrapper.ParseArgs(['--workers', '2x', 'exec'])
157 workers = 2 * multiprocessing.cpu_count()
158 expected = self._Expected(['--workers=%s' % workers, 'exec'])
159 self.assertEqual(result.gtest_parallel_args, expected)
160
161 def testUseHalfTheCpuCores(self):
162 result = gtest_parallel_wrapper.ParseArgs(['--workers', '0.5x', 'exec'])
163 workers = max(multiprocessing.cpu_count() // 2, 1)
164 expected = self._Expected(['--workers=%s' % workers, 'exec'])
165 self.assertEqual(result.gtest_parallel_args, expected)
166
Oleh Prypin69c02222018-05-23 13:18:12167
168if __name__ == '__main__':
169 unittest.main()