blob: 26135e1abb170e68e2c0d58398966b5426656bf3 [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():
24 tmp_dir = tempfile.mkdtemp()
25 yield tmp_dir
26 os.rmdir(tmp_dir)
27
28
Yves Gereyea766fa2018-09-25 15:34:27 +020029class 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 15:18:12 +020047class GtestParallelWrapperTest(unittest.TestCase):
Yves Gereyea766fa2018-09-25 15:34:27 +020048
Oleh Prypin69c02222018-05-23 15:18:12 +020049 @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 15:34:27 +020054 result = gtest_parallel_wrapper.ParseArgs(
55 ['--timeout=123', 'exec', '--timeout', '124'])
Oleh Prypin69c02222018-05-23 15:18:12 +020056 expected = self._Expected(['--timeout=124', 'exec'])
57 self.assertEqual(result.gtest_parallel_args, expected)
58
59 def testMixing(self):
Yves Gereyea766fa2018-09-25 15:34:27 +020060 result = gtest_parallel_wrapper.ParseArgs(
Oleh Prypin69c02222018-05-23 15:18:12 +020061 ['--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 15:34:27 +020067 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 15:18:12 +020073 self.assertEqual(result.gtest_parallel_args, expected)
74
75 def testDoubleDash1(self):
Yves Gereyea766fa2018-09-25 15:34:27 +020076 result = gtest_parallel_wrapper.ParseArgs(
Oleh Prypin69c02222018-05-23 15:18:12 +020077 ['--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 15:34:27 +020083 result = gtest_parallel_wrapper.ParseArgs(
84 ['--timeout=123', '--', 'exec', '--timeout=124'])
Oleh Prypin69c02222018-05-23 15:18:12 +020085 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 09:12:34 +020089 with TemporaryDirectory() as tmp_dir:
90 output_dir = os.path.join(tmp_dir, 'foo')
Yves Gereyea766fa2018-09-25 15:34:27 +020091 result = gtest_parallel_wrapper.ParseArgs(
92 ['exec', '--store-test-artifacts', '--output_dir', output_dir])
Mirko Bonadeidac422f2018-06-05 09:12:34 +020093 exp_artifacts_dir = os.path.join(output_dir, 'test_artifacts')
Yves Gereyea766fa2018-09-25 15:34:27 +020094 exp = self._Expected([
95 '--output_dir=' + output_dir, 'exec', '--',
96 '--test_artifacts_dir=' + exp_artifacts_dir
97 ])
Mirko Bonadeidac422f2018-06-05 09:12:34 +020098 self.assertEqual(result.gtest_parallel_args, exp)
99 self.assertEqual(result.output_dir, output_dir)
Mirko Bonadei40f876d2018-08-27 16:32:50 +0200100 self.assertEqual(result.test_artifacts_dir, exp_artifacts_dir)
Oleh Prypin69c02222018-05-23 15:18:12 +0200101
102 def testNoDirsSpecified(self):
Yves Gereyea766fa2018-09-25 15:34:27 +0200103 result = gtest_parallel_wrapper.ParseArgs(['exec'])
Oleh Prypin69c02222018-05-23 15:18:12 +0200104 self.assertEqual(result.output_dir, None)
105 self.assertEqual(result.test_artifacts_dir, None)
106
107 def testOutputDirSpecified(self):
Yves Gereyea766fa2018-09-25 15:34:27 +0200108 result = gtest_parallel_wrapper.ParseArgs(
109 ['exec', '--output_dir', '/tmp/foo'])
Oleh Prypin69c02222018-05-23 15:18:12 +0200110 self.assertEqual(result.output_dir, '/tmp/foo')
111 self.assertEqual(result.test_artifacts_dir, None)
112
Oleh Prypin69c02222018-05-23 15:18:12 +0200113 def testShortArg(self):
Yves Gereyea766fa2018-09-25 15:34:27 +0200114 result = gtest_parallel_wrapper.ParseArgs(['-d', '/tmp/foo', 'exec'])
Oleh Prypin69c02222018-05-23 15:18:12 +0200115 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 15:34:27 +0200120 result = gtest_parallel_wrapper.ParseArgs(
121 ['--gtest_also_run_disabled_tests', 'exec'])
Oleh Prypin69c02222018-05-23 15:18:12 +0200122 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 15:34:27 +0200126 result = gtest_parallel_wrapper.ParseArgs(['exec'])
Oleh Prypin69c02222018-05-23 15:18:12 +0200127 expected = self._Expected(['exec'])
128 self.assertEqual(result.gtest_parallel_args, expected)
129
130 def testDocExample(self):
Mirko Bonadeidac422f2018-06-05 09:12:34 +0200131 with TemporaryDirectory() as tmp_dir:
132 output_dir = os.path.join(tmp_dir, 'foo')
Yves Gereyea766fa2018-09-25 15:34:27 +0200133 result = gtest_parallel_wrapper.ParseArgs([
Mirko Bonadeidac422f2018-06-05 09:12:34 +0200134 'some_test', '--some_flag=some_value', '--another_flag',
135 '--output_dir=' + output_dir, '--store-test-artifacts',
Patrik Höglund28b8a0b2020-03-26 20:30:50 +0100136 '--isolated-script-test-perf-output=SOME_OTHER_DIR', '--foo=bar',
Yves Gereyea766fa2018-09-25 15:34:27 +0200137 '--baz'
138 ])
Mirko Bonadeidac422f2018-06-05 09:12:34 +0200139 expected_artifacts_dir = os.path.join(output_dir, 'test_artifacts')
140 expected = self._Expected([
Patrik Höglund1b20c412020-03-25 08:58:51 +0100141 '--output_dir=' + output_dir,
Yves Gereyea766fa2018-09-25 15:34:27 +0200142 'some_test', '--', '--test_artifacts_dir=' + expected_artifacts_dir,
Mirko Bonadeidac422f2018-06-05 09:12:34 +0200143 '--some_flag=some_value', '--another_flag',
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200144 '--isolated_script_test_perf_output=SOME_OTHER_DIR', '--foo=bar',
Yves Gereyea766fa2018-09-25 15:34:27 +0200145 '--baz'
146 ])
Mirko Bonadeidac422f2018-06-05 09:12:34 +0200147 self.assertEqual(result.gtest_parallel_args, expected)
Oleh Prypin69c02222018-05-23 15:18:12 +0200148
Yves Gereyea766fa2018-09-25 15:34:27 +0200149 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 15:18:12 +0200167
168if __name__ == '__main__':
169 unittest.main()