1#!/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
11from contextlib import contextmanager
12
13import multiprocessing
14import os
15import tempfile
16import unittest
17
18# pylint: disable=invalid-name
19gtest_parallel_wrapper = __import__('gtest-parallel-wrapper')
20
21
22@contextmanager
23def TemporaryDirectory():
24  tmp_dir = tempfile.mkdtemp()
25  yield tmp_dir
26  os.rmdir(tmp_dir)
27
28
29class 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
47class GtestParallelWrapperTest(unittest.TestCase):
48
49  @classmethod
50  def _Expected(cls, gtest_parallel_args):
51    return ['--shard_index=0', '--shard_count=1'] + gtest_parallel_args
52
53  def testOverwrite(self):
54    result = gtest_parallel_wrapper.ParseArgs(
55        ['--timeout=123', 'exec', '--timeout', '124'])
56    expected = self._Expected(['--timeout=124', 'exec'])
57    self.assertEqual(result.gtest_parallel_args, expected)
58
59  def testMixing(self):
60    result = gtest_parallel_wrapper.ParseArgs(
61        ['--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):
67    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'])
73    self.assertEqual(result.gtest_parallel_args, expected)
74
75  def testDoubleDash1(self):
76    result = gtest_parallel_wrapper.ParseArgs(
77        ['--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):
83    result = gtest_parallel_wrapper.ParseArgs(
84        ['--timeout=123', '--', 'exec', '--timeout=124'])
85    expected = self._Expected(['--timeout=123', 'exec', '--', '--timeout=124'])
86    self.assertEqual(result.gtest_parallel_args, expected)
87
88  def testArtifacts(self):
89    with TemporaryDirectory() as tmp_dir:
90      output_dir = os.path.join(tmp_dir, 'foo')
91      result = gtest_parallel_wrapper.ParseArgs(
92          ['exec', '--store-test-artifacts', '--output_dir', output_dir])
93      exp_artifacts_dir = os.path.join(output_dir, 'test_artifacts')
94      exp = self._Expected([
95          '--output_dir=' + output_dir, 'exec', '--',
96          '--test_artifacts_dir=' + exp_artifacts_dir
97      ])
98      self.assertEqual(result.gtest_parallel_args, exp)
99      self.assertEqual(result.output_dir, output_dir)
100      self.assertEqual(result.test_artifacts_dir, exp_artifacts_dir)
101
102  def testNoDirsSpecified(self):
103    result = gtest_parallel_wrapper.ParseArgs(['exec'])
104    self.assertEqual(result.output_dir, None)
105    self.assertEqual(result.test_artifacts_dir, None)
106
107  def testOutputDirSpecified(self):
108    result = gtest_parallel_wrapper.ParseArgs(
109        ['exec', '--output_dir', '/tmp/foo'])
110    self.assertEqual(result.output_dir, '/tmp/foo')
111    self.assertEqual(result.test_artifacts_dir, None)
112
113  def testShortArg(self):
114    result = gtest_parallel_wrapper.ParseArgs(['-d', '/tmp/foo', 'exec'])
115    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):
120    result = gtest_parallel_wrapper.ParseArgs(
121        ['--gtest_also_run_disabled_tests', 'exec'])
122    expected = self._Expected(['--gtest_also_run_disabled_tests', 'exec'])
123    self.assertEqual(result.gtest_parallel_args, expected)
124
125  def testNoArgs(self):
126    result = gtest_parallel_wrapper.ParseArgs(['exec'])
127    expected = self._Expected(['exec'])
128    self.assertEqual(result.gtest_parallel_args, expected)
129
130  def testDocExample(self):
131    with TemporaryDirectory() as tmp_dir:
132      output_dir = os.path.join(tmp_dir, 'foo')
133      result = gtest_parallel_wrapper.ParseArgs([
134          'some_test', '--some_flag=some_value', '--another_flag',
135          '--output_dir=' + output_dir, '--store-test-artifacts',
136          '--isolated-script-test-perf-output=SOME_OTHER_DIR', '--foo=bar',
137          '--baz'
138      ])
139      expected_artifacts_dir = os.path.join(output_dir, 'test_artifacts')
140      expected = self._Expected([
141          '--output_dir=' + output_dir,
142          'some_test', '--', '--test_artifacts_dir=' + expected_artifacts_dir,
143          '--some_flag=some_value', '--another_flag',
144          '--isolated_script_test_perf_output=SOME_OTHER_DIR', '--foo=bar',
145          '--baz'
146      ])
147      self.assertEqual(result.gtest_parallel_args, expected)
148
149  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
167
168if __name__ == '__main__':
169  unittest.main()
170