smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # Copyright 2016 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Unittests for test_runner.py.""" |
| 7 | |
| 8 | import collections |
Eric Aleshire | 97b368c9 | 2018-10-12 18:09:09 | [diff] [blame] | 9 | import glob |
Eric Aleshire | ce66b9f | 2019-02-08 08:00:31 | [diff] [blame] | 10 | import logging |
Maksym Onufriienko | be3cccb | 2019-07-09 23:07:30 | [diff] [blame] | 11 | import mock |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 12 | import os |
Eric Aleshire | 97b368c9 | 2018-10-12 18:09:09 | [diff] [blame] | 13 | import subprocess |
Maksym Onufriienko | 148bbd95 | 2019-09-23 22:20:08 | [diff] [blame^] | 14 | import tempfile |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 15 | import unittest |
| 16 | |
| 17 | import test_runner |
| 18 | |
| 19 | |
| 20 | class TestCase(unittest.TestCase): |
| 21 | """Test case which supports installing mocks. Uninstalls on tear down.""" |
| 22 | |
| 23 | def __init__(self, *args, **kwargs): |
| 24 | """Initializes a new instance of this class.""" |
| 25 | super(TestCase, self).__init__(*args, **kwargs) |
| 26 | |
| 27 | # Maps object to a dict which maps names of mocked members to their |
| 28 | # original values. |
| 29 | self._mocks = collections.OrderedDict() |
| 30 | |
| 31 | def mock(self, obj, member, mock): |
| 32 | """Installs mock in place of the named member of the given obj. |
| 33 | |
| 34 | Args: |
| 35 | obj: Any object. |
| 36 | member: String naming the attribute of the object to mock. |
| 37 | mock: The mock to install. |
| 38 | """ |
| 39 | self._mocks.setdefault(obj, collections.OrderedDict()).setdefault( |
| 40 | member, getattr(obj, member)) |
| 41 | setattr(obj, member, mock) |
| 42 | |
| 43 | def tearDown(self, *args, **kwargs): |
| 44 | """Uninstalls mocks.""" |
| 45 | super(TestCase, self).tearDown(*args, **kwargs) |
| 46 | |
| 47 | for obj in self._mocks: |
| 48 | for member, original_value in self._mocks[obj].iteritems(): |
| 49 | setattr(obj, member, original_value) |
| 50 | |
| 51 | |
| 52 | class GetKIFTestFilterTest(TestCase): |
| 53 | """Tests for test_runner.get_kif_test_filter.""" |
| 54 | |
| 55 | def test_correct(self): |
| 56 | """Ensures correctness of filter.""" |
| 57 | tests = [ |
| 58 | 'KIF.test1', |
| 59 | 'KIF.test2', |
| 60 | ] |
| 61 | expected = 'NAME:test1|test2' |
| 62 | |
| 63 | self.assertEqual(test_runner.get_kif_test_filter(tests), expected) |
| 64 | |
| 65 | def test_correct_inverted(self): |
| 66 | """Ensures correctness of inverted filter.""" |
| 67 | tests = [ |
| 68 | 'KIF.test1', |
| 69 | 'KIF.test2', |
| 70 | ] |
| 71 | expected = '-NAME:test1|test2' |
| 72 | |
| 73 | self.assertEqual( |
| 74 | test_runner.get_kif_test_filter(tests, invert=True), expected) |
| 75 | |
| 76 | |
| 77 | class GetGTestFilterTest(TestCase): |
| 78 | """Tests for test_runner.get_gtest_filter.""" |
| 79 | |
| 80 | def test_correct(self): |
| 81 | """Ensures correctness of filter.""" |
| 82 | tests = [ |
| 83 | 'test.1', |
| 84 | 'test.2', |
| 85 | ] |
| 86 | expected = 'test.1:test.2' |
| 87 | |
| 88 | self.assertEqual(test_runner.get_gtest_filter(tests), expected) |
| 89 | |
| 90 | def test_correct_inverted(self): |
| 91 | """Ensures correctness of inverted filter.""" |
| 92 | tests = [ |
| 93 | 'test.1', |
| 94 | 'test.2', |
| 95 | ] |
| 96 | expected = '-test.1:test.2' |
| 97 | |
| 98 | self.assertEqual( |
| 99 | test_runner.get_gtest_filter(tests, invert=True), expected) |
| 100 | |
| 101 | |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 102 | class InstallXcodeTest(TestCase): |
| 103 | """Tests install_xcode.""" |
| 104 | |
| 105 | def setUp(self): |
| 106 | super(InstallXcodeTest, self).setUp() |
| 107 | self.mock(test_runner, 'xcode_select', lambda _: None) |
| 108 | self.mock(os.path, 'exists', lambda _: True) |
| 109 | |
| 110 | def test_success(self): |
| 111 | self.assertTrue(test_runner.install_xcode('test_build', 'true', 'path')) |
| 112 | |
| 113 | def test_failure(self): |
| 114 | self.assertFalse(test_runner.install_xcode('test_build', 'false', 'path')) |
| 115 | |
| 116 | |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 117 | class SimulatorTestRunnerTest(TestCase): |
| 118 | """Tests for test_runner.SimulatorTestRunner.""" |
| 119 | |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 120 | def setUp(self): |
| 121 | super(SimulatorTestRunnerTest, self).setUp() |
| 122 | |
| 123 | def install_xcode(build, mac_toolchain_cmd, xcode_app_path): |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 124 | return True |
| 125 | |
Sergey Berezin | 7f9b4bf | 2018-05-21 19:49:07 | [diff] [blame] | 126 | self.mock(test_runner, 'get_current_xcode_info', lambda: { |
Sergey Berezin | 8263e5f | 2017-11-29 22:51:36 | [diff] [blame] | 127 | 'version': 'test version', 'build': 'test build', 'path': 'test/path'}) |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 128 | self.mock(test_runner, 'install_xcode', install_xcode) |
| 129 | self.mock(test_runner.subprocess, 'check_output', |
| 130 | lambda _: 'fake-bundle-id') |
| 131 | self.mock(os.path, 'abspath', lambda path: '/abs/path/to/%s' % path) |
| 132 | self.mock(os.path, 'exists', lambda _: True) |
Eric Aleshire | f22c7efb | 2019-02-04 23:41:26 | [diff] [blame] | 133 | self.mock(test_runner.TestRunner, 'set_sigterm_handler', |
| 134 | lambda self, handler: 0) |
Maksym Onufriienko | 148bbd95 | 2019-09-23 22:20:08 | [diff] [blame^] | 135 | self.mock(os, 'listdir', lambda _: []) |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 136 | |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 137 | def test_app_not_found(self): |
| 138 | """Ensures AppNotFoundError is raised.""" |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 139 | |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 140 | self.mock(os.path, 'exists', lambda p: not p.endswith('fake-app')) |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 141 | |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 142 | with self.assertRaises(test_runner.AppNotFoundError): |
| 143 | test_runner.SimulatorTestRunner( |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 144 | 'fake-app', |
| 145 | 'fake-iossim', |
| 146 | 'platform', |
| 147 | 'os', |
| 148 | 'xcode-version', |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 149 | '', # Empty xcode-build |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 150 | 'out-dir', |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 151 | ) |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 152 | |
| 153 | def test_iossim_not_found(self): |
| 154 | """Ensures SimulatorNotFoundError is raised.""" |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 155 | self.mock(os.path, 'exists', lambda p: not p.endswith('fake-iossim')) |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 156 | |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 157 | with self.assertRaises(test_runner.SimulatorNotFoundError): |
| 158 | test_runner.SimulatorTestRunner( |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 159 | 'fake-app', |
| 160 | 'fake-iossim', |
| 161 | 'platform', |
| 162 | 'os', |
| 163 | 'xcode-version', |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 164 | 'xcode-build', |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 165 | 'out-dir', |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 166 | ) |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 167 | |
| 168 | def test_init(self): |
| 169 | """Ensures instance is created.""" |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 170 | tr = test_runner.SimulatorTestRunner( |
| 171 | 'fake-app', |
| 172 | 'fake-iossim', |
| 173 | 'platform', |
| 174 | 'os', |
| 175 | 'xcode-version', |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 176 | 'xcode-build', |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 177 | 'out-dir', |
| 178 | ) |
| 179 | |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 180 | self.assertTrue(tr) |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 181 | |
| 182 | def test_startup_crash(self): |
| 183 | """Ensures test is relaunched once on startup crash.""" |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 184 | def set_up(self): |
| 185 | return |
| 186 | |
| 187 | @staticmethod |
Menglu Huang | 89909a8e | 2018-02-21 00:06:42 | [diff] [blame] | 188 | def _run(cmd, shards=None): |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 189 | return collections.namedtuple('result', ['crashed', 'crashed_test'])( |
| 190 | crashed=True, crashed_test=None) |
| 191 | |
| 192 | def tear_down(self): |
| 193 | return |
| 194 | |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 195 | self.mock(test_runner.SimulatorTestRunner, 'set_up', set_up) |
| 196 | self.mock(test_runner.TestRunner, '_run', _run) |
| 197 | self.mock(test_runner.SimulatorTestRunner, 'tear_down', tear_down) |
| 198 | |
| 199 | tr = test_runner.SimulatorTestRunner( |
| 200 | 'fake-app', |
| 201 | 'fake-iossim', |
| 202 | 'platform', |
| 203 | 'os', |
| 204 | 'xcode-version', |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 205 | 'xcode-build', |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 206 | 'out-dir', |
Maksym Onufriienko | 148bbd95 | 2019-09-23 22:20:08 | [diff] [blame^] | 207 | xctest=True, |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 208 | ) |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 209 | with self.assertRaises(test_runner.AppLaunchError): |
| 210 | tr.launch() |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 211 | |
Menglu Huang | 89909a8e | 2018-02-21 00:06:42 | [diff] [blame] | 212 | def test_run(self): |
| 213 | """Ensures the _run method is correct with test sharding.""" |
| 214 | def shard_xctest(object_path, shards, test_cases=None): |
| 215 | return [['a/1', 'b/2'], ['c/3', 'd/4'], ['e/5']] |
| 216 | |
| 217 | def run_tests(self, test_shard=None): |
| 218 | out = [] |
| 219 | for test in test_shard: |
| 220 | testname = test.split('/') |
| 221 | out.append('Test Case \'-[%s %s]\' started.' % |
| 222 | (testname[0], testname[1])) |
| 223 | out.append('Test Case \'-[%s %s]\' passed (0.1 seconds)' % |
| 224 | (testname[0], testname[1])) |
| 225 | return (out, 0, 0) |
| 226 | |
| 227 | tr = test_runner.SimulatorTestRunner( |
| 228 | 'fake-app', |
| 229 | 'fake-iossim', |
| 230 | 'platform', |
| 231 | 'os', |
| 232 | 'xcode-version', |
| 233 | 'xcode-build', |
| 234 | 'out-dir', |
Maksym Onufriienko | 148bbd95 | 2019-09-23 22:20:08 | [diff] [blame^] | 235 | xctest=True, |
Menglu Huang | 89909a8e | 2018-02-21 00:06:42 | [diff] [blame] | 236 | ) |
| 237 | self.mock(test_runner, 'shard_xctest', shard_xctest) |
| 238 | self.mock(test_runner.SimulatorTestRunner, 'run_tests', run_tests) |
| 239 | |
| 240 | tr.xctest_path = 'fake.xctest' |
| 241 | cmd = tr.get_launch_command() |
| 242 | result = tr._run(cmd=cmd, shards=3) |
| 243 | self.assertIn('a/1', result.passed_tests) |
| 244 | self.assertIn('b/2', result.passed_tests) |
| 245 | self.assertIn('c/3', result.passed_tests) |
| 246 | self.assertIn('d/4', result.passed_tests) |
| 247 | self.assertIn('e/5', result.passed_tests) |
| 248 | |
Menglu Huang | eb4d757 | 2018-05-21 18:37:58 | [diff] [blame] | 249 | def test_run_with_system_alert(self): |
| 250 | """Ensures SystemAlertPresentError is raised when warning 'System alert |
| 251 | view is present, so skipping all tests' is in the output.""" |
| 252 | with self.assertRaises(test_runner.SystemAlertPresentError): |
| 253 | tr = test_runner.SimulatorTestRunner( |
| 254 | 'fake-app', |
| 255 | 'fake-iossim', |
| 256 | 'platform', |
| 257 | 'os', |
| 258 | 'xcode-version', |
| 259 | 'xcode-build', |
| 260 | 'out-dir', |
Maksym Onufriienko | 148bbd95 | 2019-09-23 22:20:08 | [diff] [blame^] | 261 | xctest=True, |
Menglu Huang | eb4d757 | 2018-05-21 18:37:58 | [diff] [blame] | 262 | ) |
| 263 | tr.xctest_path = 'fake.xctest' |
| 264 | cmd = ['echo', 'System alert view is present, so skipping all tests!'] |
| 265 | result = tr._run(cmd=cmd) |
| 266 | |
Menglu Huang | 0f56961 | 2017-12-08 06:35:07 | [diff] [blame] | 267 | def test_get_launch_command(self): |
Menglu Huang | 89909a8e | 2018-02-21 00:06:42 | [diff] [blame] | 268 | """Ensures launch command is correct with test_filters, test sharding and |
| 269 | test_cases.""" |
Menglu Huang | 0f56961 | 2017-12-08 06:35:07 | [diff] [blame] | 270 | tr = test_runner.SimulatorTestRunner( |
| 271 | 'fake-app', |
| 272 | 'fake-iossim', |
| 273 | 'platform', |
| 274 | 'os', |
| 275 | 'xcode-version', |
| 276 | 'xcode-build', |
| 277 | 'out-dir', |
| 278 | ) |
| 279 | tr.xctest_path = 'fake.xctest' |
| 280 | # Cases test_filter is not empty, with empty/non-empty self.test_cases. |
| 281 | tr.test_cases = [] |
| 282 | cmd = tr.get_launch_command(['a'], invert=False) |
| 283 | self.assertIn('-t', cmd) |
| 284 | self.assertIn('a', cmd) |
| 285 | |
| 286 | tr.test_cases = ['a', 'b'] |
| 287 | cmd = tr.get_launch_command(['a'], invert=False) |
| 288 | self.assertIn('-t', cmd) |
| 289 | self.assertIn('a', cmd) |
| 290 | self.assertNotIn('b', cmd) |
| 291 | |
| 292 | # Cases test_filter is empty, with empty/non-empty self.test_cases. |
| 293 | tr.test_cases = [] |
| 294 | cmd = tr.get_launch_command(test_filter=None, invert=False) |
| 295 | self.assertNotIn('-t', cmd) |
| 296 | |
| 297 | tr.test_cases = ['a', 'b'] |
| 298 | cmd = tr.get_launch_command(test_filter=None, invert=False) |
| 299 | self.assertIn('-t', cmd) |
| 300 | self.assertIn('a', cmd) |
| 301 | self.assertIn('b', cmd) |
| 302 | |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 303 | def test_relaunch(self): |
| 304 | """Ensures test is relaunched on test crash until tests complete.""" |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 305 | def set_up(self): |
| 306 | return |
| 307 | |
| 308 | @staticmethod |
Menglu Huang | 89909a8e | 2018-02-21 00:06:42 | [diff] [blame] | 309 | def _run(cmd, shards=None): |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 310 | result = collections.namedtuple( |
| 311 | 'result', [ |
| 312 | 'crashed', |
| 313 | 'crashed_test', |
| 314 | 'failed_tests', |
| 315 | 'flaked_tests', |
| 316 | 'passed_tests', |
| 317 | ], |
| 318 | ) |
Menglu Huang | 89909a8e | 2018-02-21 00:06:42 | [diff] [blame] | 319 | if '-e' not in cmd: |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 320 | # First run, has no test filter supplied. Mock a crash. |
| 321 | return result( |
| 322 | crashed=True, |
| 323 | crashed_test='c', |
| 324 | failed_tests={'b': ['b-out'], 'c': ['Did not complete.']}, |
| 325 | flaked_tests={'d': ['d-out']}, |
| 326 | passed_tests=['a'], |
| 327 | ) |
| 328 | else: |
| 329 | return result( |
| 330 | crashed=False, |
| 331 | crashed_test=None, |
| 332 | failed_tests={}, |
| 333 | flaked_tests={}, |
| 334 | passed_tests=[], |
| 335 | ) |
| 336 | |
| 337 | def tear_down(self): |
| 338 | return |
| 339 | |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 340 | self.mock(test_runner.SimulatorTestRunner, 'set_up', set_up) |
| 341 | self.mock(test_runner.TestRunner, '_run', _run) |
| 342 | self.mock(test_runner.SimulatorTestRunner, 'tear_down', tear_down) |
| 343 | |
| 344 | tr = test_runner.SimulatorTestRunner( |
| 345 | 'fake-app', |
| 346 | 'fake-iossim', |
| 347 | 'platform', |
| 348 | 'os', |
| 349 | 'xcode-version', |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 350 | 'xcode-build', |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 351 | 'out-dir', |
| 352 | ) |
| 353 | tr.launch() |
Sergey Berezin | 1f45788 | 2017-11-20 21:05:39 | [diff] [blame] | 354 | self.assertTrue(tr.logs) |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 355 | |
| 356 | |
Eric Aleshire | 97b368c9 | 2018-10-12 18:09:09 | [diff] [blame] | 357 | class WprProxySimulatorTestRunnerTest(TestCase): |
| 358 | """Tests for test_runner.WprProxySimulatorTestRunner.""" |
| 359 | |
| 360 | def setUp(self): |
| 361 | super(WprProxySimulatorTestRunnerTest, self).setUp() |
| 362 | |
| 363 | def install_xcode(build, mac_toolchain_cmd, xcode_app_path): |
| 364 | return True |
| 365 | |
| 366 | self.mock(test_runner, 'get_current_xcode_info', lambda: { |
| 367 | 'version': 'test version', 'build': 'test build', 'path': 'test/path'}) |
| 368 | self.mock(test_runner, 'install_xcode', install_xcode) |
| 369 | self.mock(test_runner.subprocess, 'check_output', |
| 370 | lambda _: 'fake-bundle-id') |
| 371 | self.mock(os.path, 'abspath', lambda path: '/abs/path/to/%s' % path) |
| 372 | self.mock(os.path, 'exists', lambda _: True) |
Eric Aleshire | f22c7efb | 2019-02-04 23:41:26 | [diff] [blame] | 373 | self.mock(test_runner.TestRunner, 'set_sigterm_handler', |
| 374 | lambda self, handler: 0) |
| 375 | self.mock(test_runner.SimulatorTestRunner, 'getSimulator', |
| 376 | lambda _: 'fake-id') |
| 377 | self.mock(test_runner.SimulatorTestRunner, 'deleteSimulator', |
| 378 | lambda a, b: True) |
| 379 | self.mock(test_runner.WprProxySimulatorTestRunner, |
| 380 | 'copy_trusted_certificate', lambda a, b: True) |
Eric Aleshire | 97b368c9 | 2018-10-12 18:09:09 | [diff] [blame] | 381 | |
| 382 | def test_replay_path_not_found(self): |
| 383 | """Ensures ReplayPathNotFoundError is raised.""" |
| 384 | |
| 385 | self.mock(os.path, 'exists', lambda p: not p.endswith('bad-replay-path')) |
| 386 | |
| 387 | with self.assertRaises(test_runner.ReplayPathNotFoundError): |
| 388 | test_runner.WprProxySimulatorTestRunner( |
| 389 | 'fake-app', |
| 390 | 'fake-iossim', |
| 391 | 'bad-replay-path', |
| 392 | 'platform', |
| 393 | 'os', |
| 394 | 'wpr-tools-path', |
| 395 | 'xcode-version', |
| 396 | 'xcode-build', |
| 397 | 'out-dir', |
| 398 | ) |
| 399 | |
| 400 | def test_wpr_tools_not_found(self): |
| 401 | """Ensures WprToolsNotFoundError is raised.""" |
| 402 | |
| 403 | self.mock(os.path, 'exists', lambda p: not p.endswith('bad-tools-path')) |
| 404 | |
| 405 | with self.assertRaises(test_runner.WprToolsNotFoundError): |
| 406 | test_runner.WprProxySimulatorTestRunner( |
| 407 | 'fake-app', |
| 408 | 'fake-iossim', |
| 409 | 'replay-path', |
| 410 | 'platform', |
| 411 | 'os', |
| 412 | 'bad-tools-path', |
| 413 | 'xcode-version', |
| 414 | 'xcode-build', |
| 415 | 'out-dir', |
| 416 | ) |
| 417 | |
| 418 | def test_init(self): |
| 419 | """Ensures instance is created.""" |
| 420 | tr = test_runner.WprProxySimulatorTestRunner( |
| 421 | 'fake-app', |
| 422 | 'fake-iossim', |
| 423 | 'replay-path', |
| 424 | 'platform', |
| 425 | 'os', |
| 426 | 'wpr-tools-path', |
| 427 | 'xcode-version', |
| 428 | 'xcode-build', |
| 429 | 'out-dir', |
| 430 | ) |
| 431 | |
| 432 | self.assertTrue(tr) |
| 433 | |
Eric Aleshire | 3f8c995 | 2018-12-12 21:57:43 | [diff] [blame] | 434 | def run_wpr_test(self, test_filter=[], invert=False): |
| 435 | """Wrapper that mocks the _run method and returns its result.""" |
Eric Aleshire | 97b368c9 | 2018-10-12 18:09:09 | [diff] [blame] | 436 | class FakeStdout: |
| 437 | def __init__(self): |
| 438 | self.line_index = 0 |
| 439 | self.lines = [ |
| 440 | 'Test Case \'-[a 1]\' started.', |
| 441 | 'Test Case \'-[a 1]\' has uninteresting logs.', |
| 442 | 'Test Case \'-[a 1]\' passed (0.1 seconds)', |
| 443 | 'Test Case \'-[b 2]\' started.', |
| 444 | 'Test Case \'-[b 2]\' passed (0.1 seconds)', |
| 445 | 'Test Case \'-[c 3]\' started.', |
| 446 | 'Test Case \'-[c 3]\' has interesting failure info.', |
| 447 | 'Test Case \'-[c 3]\' failed (0.1 seconds)', |
| 448 | ] |
| 449 | |
| 450 | def readline(self): |
| 451 | if self.line_index < len(self.lines): |
| 452 | return_line = self.lines[self.line_index] |
| 453 | self.line_index += 1 |
| 454 | return return_line |
| 455 | else: |
| 456 | return None |
| 457 | |
| 458 | class FakeProcess: |
| 459 | def __init__(self): |
| 460 | self.stdout = FakeStdout() |
| 461 | self.returncode = 0 |
| 462 | |
| 463 | def stdout(self): |
| 464 | return self.stdout |
| 465 | |
| 466 | def wait(self): |
| 467 | return |
| 468 | |
| 469 | def popen(recipe_cmd, env, stdout, stderr): |
| 470 | return FakeProcess() |
| 471 | |
| 472 | tr = test_runner.WprProxySimulatorTestRunner( |
| 473 | 'fake-app', |
| 474 | 'fake-iossim', |
| 475 | 'replay-path', |
| 476 | 'platform', |
| 477 | 'os', |
| 478 | 'wpr-tools-path', |
| 479 | 'xcode-version', |
| 480 | 'xcode-build', |
| 481 | 'out-dir', |
| 482 | ) |
Eric Aleshire | f22c7efb | 2019-02-04 23:41:26 | [diff] [blame] | 483 | self.mock(test_runner.WprProxySimulatorTestRunner, 'wprgo_start', |
| 484 | lambda a,b: None) |
| 485 | self.mock(test_runner.WprProxySimulatorTestRunner, 'wprgo_stop', |
| 486 | lambda _: None) |
Eric Aleshire | 97b368c9 | 2018-10-12 18:09:09 | [diff] [blame] | 487 | |
| 488 | self.mock(os.path, 'isfile', lambda _: True) |
| 489 | self.mock(glob, 'glob', lambda _: ["file1", "file2"]) |
| 490 | self.mock(subprocess, 'Popen', popen) |
| 491 | |
| 492 | tr.xctest_path = 'fake.xctest' |
Eric Aleshire | 3f8c995 | 2018-12-12 21:57:43 | [diff] [blame] | 493 | cmd = tr.get_launch_command(test_filter=test_filter, invert=invert) |
| 494 | return tr._run(cmd=cmd, shards=1) |
| 495 | |
| 496 | def test_run_no_filter(self): |
| 497 | """Ensures the _run method can handle passed and failed tests.""" |
| 498 | result = self.run_wpr_test() |
Eric Aleshire | 97b368c9 | 2018-10-12 18:09:09 | [diff] [blame] | 499 | self.assertIn('file1.a/1', result.passed_tests) |
| 500 | self.assertIn('file1.b/2', result.passed_tests) |
| 501 | self.assertIn('file1.c/3', result.failed_tests) |
| 502 | self.assertIn('file2.a/1', result.passed_tests) |
| 503 | self.assertIn('file2.b/2', result.passed_tests) |
| 504 | self.assertIn('file2.c/3', result.failed_tests) |
| 505 | |
Eric Aleshire | 3f8c995 | 2018-12-12 21:57:43 | [diff] [blame] | 506 | def test_run_with_filter(self): |
| 507 | """Ensures the _run method works with a filter.""" |
| 508 | result = self.run_wpr_test(test_filter=["file1"], invert=False) |
| 509 | self.assertIn('file1.a/1', result.passed_tests) |
| 510 | self.assertIn('file1.b/2', result.passed_tests) |
| 511 | self.assertIn('file1.c/3', result.failed_tests) |
| 512 | self.assertNotIn('file2.a/1', result.passed_tests) |
| 513 | self.assertNotIn('file2.b/2', result.passed_tests) |
| 514 | self.assertNotIn('file2.c/3', result.failed_tests) |
| 515 | |
| 516 | def test_run_with_inverted_filter(self): |
| 517 | """Ensures the _run method works with an inverted filter.""" |
| 518 | result = self.run_wpr_test(test_filter=["file1"], invert=True) |
| 519 | self.assertNotIn('file1.a/1', result.passed_tests) |
| 520 | self.assertNotIn('file1.b/2', result.passed_tests) |
| 521 | self.assertNotIn('file1.c/3', result.failed_tests) |
| 522 | self.assertIn('file2.a/1', result.passed_tests) |
| 523 | self.assertIn('file2.b/2', result.passed_tests) |
| 524 | self.assertIn('file2.c/3', result.failed_tests) |
Eric Aleshire | 97b368c9 | 2018-10-12 18:09:09 | [diff] [blame] | 525 | |
Menglu Huang | 0f56961 | 2017-12-08 06:35:07 | [diff] [blame] | 526 | class DeviceTestRunnerTest(TestCase): |
| 527 | def setUp(self): |
| 528 | super(DeviceTestRunnerTest, self).setUp() |
| 529 | |
| 530 | def install_xcode(build, mac_toolchain_cmd, xcode_app_path): |
| 531 | return True |
| 532 | |
Sergey Berezin | 7f9b4bf | 2018-05-21 19:49:07 | [diff] [blame] | 533 | self.mock(test_runner, 'get_current_xcode_info', lambda: { |
Menglu Huang | 0f56961 | 2017-12-08 06:35:07 | [diff] [blame] | 534 | 'version': 'test version', 'build': 'test build', 'path': 'test/path'}) |
| 535 | self.mock(test_runner, 'install_xcode', install_xcode) |
| 536 | self.mock(test_runner.subprocess, 'check_output', |
| 537 | lambda _: 'fake-bundle-id') |
| 538 | self.mock(os.path, 'abspath', lambda path: '/abs/path/to/%s' % path) |
| 539 | self.mock(os.path, 'exists', lambda _: True) |
Maksym Onufriienko | 148bbd95 | 2019-09-23 22:20:08 | [diff] [blame^] | 540 | self.mock(os, 'listdir', lambda _: []) |
| 541 | self.mock(tempfile, 'mkstemp', lambda: '/tmp/tmp_file') |
Menglu Huang | 0f56961 | 2017-12-08 06:35:07 | [diff] [blame] | 542 | |
| 543 | self.tr = test_runner.DeviceTestRunner( |
| 544 | 'fake-app', |
| 545 | 'xcode-version', |
| 546 | 'xcode-build', |
| 547 | 'out-dir', |
| 548 | ) |
| 549 | self.tr.xctestrun_data = {'TestTargetName':{}} |
| 550 | |
| 551 | def test_with_test_filter_without_test_cases(self): |
| 552 | """Ensures tests in the run with test_filter and no test_cases.""" |
| 553 | self.tr.set_xctest_filters(['a', 'b'], invert=False) |
| 554 | self.assertEqual( |
| 555 | self.tr.xctestrun_data['TestTargetName']['OnlyTestIdentifiers'], |
| 556 | ['a', 'b'] |
| 557 | ) |
| 558 | |
| 559 | def test_invert_with_test_filter_without_test_cases(self): |
| 560 | """Ensures tests in the run invert with test_filter and no test_cases.""" |
| 561 | self.tr.set_xctest_filters(['a', 'b'], invert=True) |
| 562 | self.assertEqual( |
| 563 | self.tr.xctestrun_data['TestTargetName']['SkipTestIdentifiers'], |
| 564 | ['a', 'b'] |
| 565 | ) |
| 566 | |
| 567 | def test_with_test_filter_with_test_cases(self): |
| 568 | """Ensures tests in the run with test_filter and test_cases.""" |
| 569 | self.tr.test_cases = ['a', 'b', 'c', 'd'] |
| 570 | self.tr.set_xctest_filters(['a', 'b', 'irrelevant test'], invert=False) |
| 571 | self.assertEqual( |
| 572 | self.tr.xctestrun_data['TestTargetName']['OnlyTestIdentifiers'], |
| 573 | ['a', 'b'] |
| 574 | ) |
| 575 | |
| 576 | def test_invert_with_test_filter_with_test_cases(self): |
| 577 | """Ensures tests in the run invert with test_filter and test_cases.""" |
| 578 | self.tr.test_cases = ['a', 'b', 'c', 'd'] |
| 579 | self.tr.set_xctest_filters(['a', 'b', 'irrelevant test'], invert=True) |
| 580 | self.assertEqual( |
| 581 | self.tr.xctestrun_data['TestTargetName']['OnlyTestIdentifiers'], |
| 582 | ['c', 'd'] |
| 583 | ) |
| 584 | |
| 585 | def test_without_test_filter_without_test_cases(self): |
| 586 | """Ensures tests in the run with no test_filter and no test_cases.""" |
| 587 | self.tr.set_xctest_filters(test_filter=None, invert=False) |
| 588 | self.assertIsNone( |
| 589 | self.tr.xctestrun_data['TestTargetName'].get('OnlyTestIdentifiers')) |
| 590 | |
| 591 | def test_invert_without_test_filter_without_test_cases(self): |
| 592 | """Ensures tests in the run invert with no test_filter and no test_cases.""" |
| 593 | self.tr.set_xctest_filters(test_filter=None, invert=True) |
| 594 | self.assertIsNone( |
| 595 | self.tr.xctestrun_data['TestTargetName'].get('OnlyTestIdentifiers')) |
| 596 | |
| 597 | def test_without_test_filter_with_test_cases(self): |
| 598 | """Ensures tests in the run with no test_filter but test_cases.""" |
| 599 | self.tr.test_cases = ['a', 'b', 'c', 'd'] |
| 600 | self.tr.set_xctest_filters(test_filter=None, invert=False) |
| 601 | self.assertEqual( |
| 602 | self.tr.xctestrun_data['TestTargetName']['OnlyTestIdentifiers'], |
| 603 | ['a', 'b', 'c', 'd'] |
| 604 | ) |
| 605 | |
| 606 | def test_invert_without_test_filter_with_test_cases(self): |
| 607 | """Ensures tests in the run invert with no test_filter but test_cases.""" |
| 608 | self.tr.test_cases = ['a', 'b', 'c', 'd'] |
| 609 | self.tr.set_xctest_filters(test_filter=None, invert=True) |
| 610 | self.assertEqual( |
| 611 | self.tr.xctestrun_data['TestTargetName']['OnlyTestIdentifiers'], |
| 612 | ['a', 'b', 'c', 'd'] |
| 613 | ) |
| 614 | |
Maksym Onufriienko | be3cccb | 2019-07-09 23:07:30 | [diff] [blame] | 615 | @mock.patch('subprocess.check_output', autospec=True) |
| 616 | def test_get_test_names(self, mock_subprocess): |
| 617 | otool_output = ( |
| 618 | 'imp 0x102492020 -[BrowserViewControllerTestCase testJavaScript]' |
| 619 | 'name 0x105ee8b84 testFixForCrbug801165' |
| 620 | 'types 0x105f0c842 v16 @ 0:8' |
| 621 | 'name 0x105ee8b9a testOpenURLFromNTP' |
| 622 | 'types 0x105f0c842 v16 @ 0:8' |
| 623 | 'imp 0x102493b30 -[BrowserViewControllerTestCase testOpenURLFromNTP]' |
| 624 | 'name 0x105ee8bad testOpenURLFromTab' |
| 625 | 'types 0x105f0c842 v16 @ 0:8' |
| 626 | 'imp 0x102494180 -[BrowserViewControllerTestCase testOpenURLFromTab]' |
| 627 | 'name 0x105ee8bc0 testOpenURLFromTabSwitcher' |
| 628 | 'types 0x105f0c842 v16 @ 0:8' |
| 629 | 'imp 0x102494f70 -[BrowserViewControllerTestCase testTabSwitch]' |
| 630 | 'types 0x105f0c842 v16 @ 0:8' |
| 631 | 'imp 0x102494f70 -[BrowserViewControllerTestCase helper]' |
| 632 | 'imp 0x102494f70 -[BrowserViewControllerTestCCCCCCCCC testMethod]' |
| 633 | ) |
| 634 | mock_subprocess.return_value = otool_output |
| 635 | tests = test_runner.get_test_names('') |
| 636 | self.assertEqual( |
| 637 | [ |
| 638 | ('BrowserViewControllerTestCase', 'testJavaScript'), |
| 639 | ('BrowserViewControllerTestCase', 'testOpenURLFromNTP'), |
| 640 | ('BrowserViewControllerTestCase', 'testOpenURLFromTab'), |
| 641 | ('BrowserViewControllerTestCase', 'testTabSwitch') |
| 642 | ], |
| 643 | tests |
| 644 | ) |
| 645 | |
Menglu Huang | 0f56961 | 2017-12-08 06:35:07 | [diff] [blame] | 646 | |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 647 | if __name__ == '__main__': |
Eric Aleshire | ce66b9f | 2019-02-08 08:00:31 | [diff] [blame] | 648 | logging.basicConfig(format='[%(asctime)s:%(levelname)s] %(message)s', |
| 649 | level=logging.DEBUG, datefmt='%I:%M:%S') |
smut | 64cb942 | 2016-05-07 00:24:20 | [diff] [blame] | 650 | unittest.main() |