1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
|
#!/usr/bin/env vpython3
# Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
#
# Use of this source code is governed by a BSD-style license
# that can be found in the LICENSE file in the root of the source
# tree. An additional intellectual property rights grant can be found
# in the file PATENTS. All contributing project authors may
# be found in the AUTHORS file in the root of the source tree.
"""Tests for mb.py."""
import ast
import os
import re
import sys
import tempfile
import unittest
_SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
_SRC_DIR = os.path.dirname(os.path.dirname(_SCRIPT_DIR))
sys.path.insert(0, _SRC_DIR)
from tools_webrtc.mb import mb
class FakeMBW(mb.WebRTCMetaBuildWrapper):
def __init__(self, win32=False):
super().__init__()
# Override vars for test portability.
if win32:
self.chromium_src_dir = 'c:\\fake_src'
self.default_config = (
'c:\\fake_src\\tools_webrtc\\mb\\mb_config.pyl')
self.default_isolate_map = ('c:\\fake_src\\testing\\buildbot\\'
'gn_isolate_map.pyl')
self.platform = 'win32'
self.executable = 'c:\\python\\vpython3.exe'
self.sep = '\\'
self.cwd = 'c:\\fake_src\\out\\Default'
else:
self.chromium_src_dir = '/fake_src'
self.default_config = '/fake_src/tools_webrtc/mb/mb_config.pyl'
self.default_isolate_map = (
'/fake_src/testing/buildbot/gn_isolate_map.pyl')
self.executable = '/usr/bin/vpython3'
self.platform = 'linux2'
self.sep = '/'
self.cwd = '/fake_src/out/Default'
self.files = {}
self.dirs = set()
self.calls = []
self.cmds = []
self.cross_compile = None
self.out = ''
self.err = ''
self.rmdirs = []
def ExpandUser(self, path):
# pylint: disable=no-self-use
return '$HOME/%s' % path
def Exists(self, path):
abs_path = self._AbsPath(path)
return self.files.get(abs_path) is not None or abs_path in self.dirs
def ListDir(self, path):
dir_contents = []
for f in list(self.files.keys()) + list(self.dirs):
head, _ = os.path.split(f)
if head == path:
dir_contents.append(f)
return dir_contents
def MaybeMakeDirectory(self, path):
abpath = self._AbsPath(path)
self.dirs.add(abpath)
def PathJoin(self, *comps):
return self.sep.join(comps)
def ReadFile(self, path):
try:
return self.files[self._AbsPath(path)]
except KeyError as e:
raise IOError('%s not found' % path) from e
def WriteFile(self, path, contents, force_verbose=False):
if self.args.dryrun or self.args.verbose or force_verbose:
self.Print('\nWriting """\\\n%s""" to %s.\n' % (contents, path))
abpath = self._AbsPath(path)
self.files[abpath] = contents
def Call(self, cmd, env=None, capture_output=True, input=None):
# pylint: disable=redefined-builtin
del env
del capture_output
del input
self.calls.append(cmd)
if self.cmds:
return self.cmds.pop(0)
return 0, '', ''
def Print(self, *args, **kwargs):
sep = kwargs.get('sep', ' ')
end = kwargs.get('end', '\n')
f = kwargs.get('file', sys.stdout)
if f == sys.stderr:
self.err += sep.join(args) + end
else:
self.out += sep.join(args) + end
def TempDir(self):
tmp_dir = os.path.join(tempfile.gettempdir(), 'mb_test')
self.dirs.add(tmp_dir)
return tmp_dir
def TempFile(self, mode='w'):
del mode
return FakeFile(self.files)
def RemoveFile(self, path):
abpath = self._AbsPath(path)
self.files[abpath] = None
def RemoveDirectory(self, abs_path):
# Normalize the passed-in path to handle different working directories
# used during unit testing.
abs_path = self._AbsPath(abs_path)
self.rmdirs.append(abs_path)
files_to_delete = [f for f in self.files if f.startswith(abs_path)]
for f in files_to_delete:
self.files[f] = None
def _AbsPath(self, path):
if not ((self.platform == 'win32' and path.startswith('c:')) or
(self.platform != 'win32' and path.startswith('/'))):
path = self.PathJoin(self.cwd, path)
if self.sep == '\\':
return re.sub(r'\\+', r'\\', path)
return re.sub('/+', '/', path)
class FakeFile:
# pylint: disable=invalid-name
def __init__(self, files):
self.name = '/tmp/file'
self.buf = ''
self.files = files
def write(self, contents):
self.buf += contents
def close(self):
self.files[self.name] = self.buf
TEST_CONFIG = """\
{
'builder_groups': {
'chromium': {},
'fake_group': {
'fake_builder': 'rel_bot',
'fake_debug_builder': 'debug_goma',
'fake_args_bot': 'fake_args_bot',
'fake_multi_phase': { 'phase_1': 'phase_1', 'phase_2': 'phase_2'},
'fake_android_bot': 'android_bot',
'fake_args_file': 'args_file_goma',
'fake_ios_error': 'ios_error',
},
},
'configs': {
'args_file_goma': ['fake_args_bot', 'goma'],
'fake_args_bot': ['fake_args_bot'],
'rel_bot': ['rel', 'goma', 'fake_feature1'],
'debug_goma': ['debug', 'goma'],
'phase_1': ['rel', 'phase_1'],
'phase_2': ['rel', 'phase_2'],
'android_bot': ['android'],
'ios_error': ['error'],
},
'mixins': {
'error': {
'gn_args': 'error',
},
'fake_args_bot': {
'args_file': '//build/args/bots/fake_group/fake_args_bot.gn',
},
'fake_feature1': {
'gn_args': 'enable_doom_melon=true',
},
'goma': {
'gn_args': 'use_goma=true',
},
'phase_1': {
'gn_args': 'phase=1',
},
'phase_2': {
'gn_args': 'phase=2',
},
'rel': {
'gn_args': 'is_debug=false dcheck_always_on=false',
},
'debug': {
'gn_args': 'is_debug=true',
},
'android': {
'gn_args': 'target_os="android" dcheck_always_on=false',
}
},
}
"""
def CreateFakeMBW(files=None, win32=False):
mbw = FakeMBW(win32=win32)
mbw.files.setdefault(mbw.default_config, TEST_CONFIG)
mbw.files.setdefault(
mbw.ToAbsPath('//testing/buildbot/gn_isolate_map.pyl'), '''{
"foo_unittests": {
"label": "//foo:foo_unittests",
"type": "console_test_launcher",
"args": [],
},
}''')
mbw.files.setdefault(
mbw.ToAbsPath('//build/args/bots/fake_group/fake_args_bot.gn'),
'is_debug = false\ndcheck_always_on=false\n')
mbw.files.setdefault(mbw.ToAbsPath('//tools/mb/rts_banned_suites.json'),
'{}')
if files:
for path, contents in list(files.items()):
mbw.files[path] = contents
if path.endswith('.runtime_deps'):
def FakeCall(cmd, env=None, capture_output=True, stdin=None):
# pylint: disable=cell-var-from-loop
del cmd
del env
del capture_output
del stdin
mbw.files[path] = contents
return 0, '', ''
# pylint: disable=invalid-name
mbw.Call = FakeCall
return mbw
class UnitTest(unittest.TestCase):
# pylint: disable=invalid-name
def check(self,
args,
mbw=None,
files=None,
out=None,
err=None,
ret=None,
env=None):
if not mbw:
mbw = CreateFakeMBW(files)
try:
prev_env = os.environ.copy()
os.environ = env if env else prev_env
actual_ret = mbw.Main(args)
finally:
os.environ = prev_env
self.assertEqual(
actual_ret, ret,
"ret: %s, out: %s, err: %s" % (actual_ret, mbw.out, mbw.err))
if out is not None:
self.assertEqual(mbw.out, out)
if err is not None:
self.assertEqual(mbw.err, err)
return mbw
def test_gen_swarming(self):
files = {
'/tmp/swarming_targets':
'foo_unittests\n',
'/fake_src/testing/buildbot/gn_isolate_map.pyl':
("{'foo_unittests': {"
" 'label': '//foo:foo_unittests',"
" 'type': 'raw',"
" 'args': [],"
"}}\n"),
'/fake_src/out/Default/foo_unittests.runtime_deps':
("foo_unittests\n"),
}
mbw = CreateFakeMBW(files)
self.check([
'gen', '-c', 'debug_goma', '--swarming-targets-file',
'/tmp/swarming_targets', '//out/Default'
],
mbw=mbw,
ret=0)
self.assertIn('/fake_src/out/Default/foo_unittests.isolate', mbw.files)
self.assertIn('/fake_src/out/Default/foo_unittests.isolated.gen.json',
mbw.files)
def test_gen_swarming_android(self):
test_files = {
'/tmp/swarming_targets':
'foo_unittests\n',
'/fake_src/testing/buildbot/gn_isolate_map.pyl':
("{'foo_unittests': {"
" 'label': '//foo:foo_unittests',"
" 'type': 'console_test_launcher',"
"}}\n"),
'/fake_src/out/Default/foo_unittests.runtime_deps':
("foo_unittests\n"),
}
mbw = self.check([
'gen', '-c', 'android_bot', '//out/Default',
'--swarming-targets-file', '/tmp/swarming_targets',
'--isolate-map-file',
'/fake_src/testing/buildbot/gn_isolate_map.pyl'
],
files=test_files,
ret=0)
isolate_file = mbw.files['/fake_src/out/Default/foo_unittests.isolate']
isolate_file_contents = ast.literal_eval(isolate_file)
files = isolate_file_contents['variables']['files']
command = isolate_file_contents['variables']['command']
self.assertEqual(
files,
['../../.vpython3', '../../testing/test_env.py', 'foo_unittests'])
self.assertEqual(command, [
'luci-auth',
'context',
'--',
'vpython3',
'../../build/android/test_wrapper/logdog_wrapper.py',
'--target',
'foo_unittests',
'--logdog-bin-cmd',
'../../.task_template_packages/logdog_butler',
'--logcat-output-file',
'${ISOLATED_OUTDIR}/logcats',
'--store-tombstones',
])
def test_gen_swarming_android_test(self):
test_files = {
'/tmp/swarming_targets':
'foo_unittests\n',
'/fake_src/testing/buildbot/gn_isolate_map.pyl':
("{'foo_unittests': {"
" 'label': '//foo:foo_unittests',"
" 'type': 'console_test_launcher',"
"}}\n"),
'/fake_src/out/Default/foo_unittests.runtime_deps':
("foo_unittests\n"),
}
mbw = self.check([
'gen', '-c', 'android_bot', '//out/Default',
'--swarming-targets-file', '/tmp/swarming_targets',
'--isolate-map-file',
'/fake_src/testing/buildbot/gn_isolate_map.pyl'
],
files=test_files,
ret=0)
isolate_file = mbw.files['/fake_src/out/Default/foo_unittests.isolate']
isolate_file_contents = ast.literal_eval(isolate_file)
files = isolate_file_contents['variables']['files']
command = isolate_file_contents['variables']['command']
self.assertEqual(
files,
['../../.vpython3', '../../testing/test_env.py', 'foo_unittests'])
self.assertEqual(command, [
'luci-auth',
'context',
'--',
'vpython3',
'../../build/android/test_wrapper/logdog_wrapper.py',
'--target',
'foo_unittests',
'--logdog-bin-cmd',
'../../.task_template_packages/logdog_butler',
'--logcat-output-file',
'${ISOLATED_OUTDIR}/logcats',
'--store-tombstones',
])
def test_gen_raw(self):
test_files = {
'/tmp/swarming_targets':
'foo_unittests\n',
'/fake_src/testing/buildbot/gn_isolate_map.pyl':
("{'foo_unittests': {"
" 'label': '//foo:foo_unittests',"
" 'type': 'raw',"
"}}\n"),
'/fake_src/out/Default/foo_unittests.runtime_deps':
("foo_unittests\n"),
}
mbw = self.check([
'gen', '-c', 'debug_goma', '//out/Default',
'--swarming-targets-file', '/tmp/swarming_targets',
'--isolate-map-file',
'/fake_src/testing/buildbot/gn_isolate_map.pyl'
],
files=test_files,
ret=0)
isolate_file = mbw.files['/fake_src/out/Default/foo_unittests.isolate']
isolate_file_contents = ast.literal_eval(isolate_file)
files = isolate_file_contents['variables']['files']
command = isolate_file_contents['variables']['command']
self.assertEqual(files, [
'../../.vpython3',
'../../testing/test_env.py',
'foo_unittests',
])
self.assertEqual(command, ['bin/run_foo_unittests'])
def test_gen_non_parallel_console_test_launcher(self):
test_files = {
'/tmp/swarming_targets':
'foo_unittests\n',
'/fake_src/testing/buildbot/gn_isolate_map.pyl':
("{'foo_unittests': {"
" 'label': '//foo:foo_unittests',"
" 'type': 'non_parallel_console_test_launcher',"
"}}\n"),
'/fake_src/out/Default/foo_unittests.runtime_deps':
("foo_unittests\n"),
}
mbw = self.check([
'gen', '-c', 'debug_goma', '//out/Default',
'--swarming-targets-file', '/tmp/swarming_targets',
'--isolate-map-file',
'/fake_src/testing/buildbot/gn_isolate_map.pyl'
],
files=test_files,
ret=0)
isolate_file = mbw.files['/fake_src/out/Default/foo_unittests.isolate']
isolate_file_contents = ast.literal_eval(isolate_file)
files = isolate_file_contents['variables']['files']
command = isolate_file_contents['variables']['command']
self.assertEqual(files, [
'../../.vpython3',
'../../testing/test_env.py',
'../../third_party/gtest-parallel/gtest-parallel',
'../../third_party/gtest-parallel/gtest_parallel.py',
'../../tools_webrtc/gtest-parallel-wrapper.py',
'foo_unittests',
])
self.assertEqual(command, [
'vpython3',
'../../testing/test_env.py',
'../../tools_webrtc/gtest-parallel-wrapper.py',
'--output_dir=${ISOLATED_OUTDIR}/test_logs',
'--gtest_color=no',
'--workers=1',
'--retry_failed=3',
'./foo_unittests',
'--asan=0',
'--lsan=0',
'--msan=0',
'--tsan=0',
])
def test_isolate_windowed_test_launcher_linux(self):
test_files = {
'/tmp/swarming_targets':
'foo_unittests\n',
'/fake_src/testing/buildbot/gn_isolate_map.pyl':
("{'foo_unittests': {"
" 'label': '//foo:foo_unittests',"
" 'type': 'windowed_test_launcher',"
"}}\n"),
'/fake_src/out/Default/foo_unittests.runtime_deps':
("foo_unittests\n"
"some_resource_file\n"),
}
mbw = self.check([
'gen', '-c', 'debug_goma', '//out/Default',
'--swarming-targets-file', '/tmp/swarming_targets',
'--isolate-map-file',
'/fake_src/testing/buildbot/gn_isolate_map.pyl'
],
files=test_files,
ret=0)
isolate_file = mbw.files['/fake_src/out/Default/foo_unittests.isolate']
isolate_file_contents = ast.literal_eval(isolate_file)
files = isolate_file_contents['variables']['files']
command = isolate_file_contents['variables']['command']
self.assertEqual(files, [
'../../.vpython3',
'../../testing/test_env.py',
'../../testing/xvfb.py',
'../../third_party/gtest-parallel/gtest-parallel',
'../../third_party/gtest-parallel/gtest_parallel.py',
'../../tools_webrtc/gtest-parallel-wrapper.py',
'foo_unittests',
'some_resource_file',
])
self.assertEqual(command, [
'vpython3',
'../../testing/xvfb.py',
'../../tools_webrtc/gtest-parallel-wrapper.py',
'--output_dir=${ISOLATED_OUTDIR}/test_logs',
'--gtest_color=no',
'--retry_failed=3',
'./foo_unittests',
'--asan=0',
'--lsan=0',
'--msan=0',
'--tsan=0',
])
def test_gen_windowed_test_launcher_win(self):
files = {
'c:\\fake_src\\out\\Default\\tmp\\swarming_targets':
'unittests\n',
'c:\\fake_src\\testing\\buildbot\\gn_isolate_map.pyl':
("{'unittests': {"
" 'label': '//somewhere:unittests',"
" 'type': 'windowed_test_launcher',"
"}}\n"),
r'c:\fake_src\out\Default\unittests.exe.runtime_deps':
("unittests.exe\n"
"some_dependency\n"),
}
mbw = CreateFakeMBW(files=files, win32=True)
self.check([
'gen', '-c', 'debug_goma', '--swarming-targets-file',
'c:\\fake_src\\out\\Default\\tmp\\swarming_targets',
'--isolate-map-file',
'c:\\fake_src\\testing\\buildbot\\gn_isolate_map.pyl',
'//out/Default'
],
mbw=mbw,
ret=0)
isolate_file = mbw.files[
'c:\\fake_src\\out\\Default\\unittests.isolate']
isolate_file_contents = ast.literal_eval(isolate_file)
files = isolate_file_contents['variables']['files']
command = isolate_file_contents['variables']['command']
self.assertEqual(files, [
'../../.vpython3',
'../../testing/test_env.py',
'../../third_party/gtest-parallel/gtest-parallel',
'../../third_party/gtest-parallel/gtest_parallel.py',
'../../tools_webrtc/gtest-parallel-wrapper.py',
'some_dependency',
'unittests.exe',
])
self.assertEqual(command, [
'vpython3',
'../../testing/test_env.py',
'../../tools_webrtc/gtest-parallel-wrapper.py',
'--output_dir=${ISOLATED_OUTDIR}/test_logs',
'--gtest_color=no',
'--retry_failed=3',
r'.\unittests.exe',
'--asan=0',
'--lsan=0',
'--msan=0',
'--tsan=0',
])
def test_gen_console_test_launcher(self):
test_files = {
'/tmp/swarming_targets':
'foo_unittests\n',
'/fake_src/testing/buildbot/gn_isolate_map.pyl':
("{'foo_unittests': {"
" 'label': '//foo:foo_unittests',"
" 'type': 'console_test_launcher',"
"}}\n"),
'/fake_src/out/Default/foo_unittests.runtime_deps':
("foo_unittests\n"),
}
mbw = self.check([
'gen', '-c', 'debug_goma', '//out/Default',
'--swarming-targets-file', '/tmp/swarming_targets',
'--isolate-map-file',
'/fake_src/testing/buildbot/gn_isolate_map.pyl'
],
files=test_files,
ret=0)
isolate_file = mbw.files['/fake_src/out/Default/foo_unittests.isolate']
isolate_file_contents = ast.literal_eval(isolate_file)
files = isolate_file_contents['variables']['files']
command = isolate_file_contents['variables']['command']
self.assertEqual(files, [
'../../.vpython3',
'../../testing/test_env.py',
'../../third_party/gtest-parallel/gtest-parallel',
'../../third_party/gtest-parallel/gtest_parallel.py',
'../../tools_webrtc/gtest-parallel-wrapper.py',
'foo_unittests',
])
self.assertEqual(command, [
'vpython3',
'../../testing/test_env.py',
'../../tools_webrtc/gtest-parallel-wrapper.py',
'--output_dir=${ISOLATED_OUTDIR}/test_logs',
'--gtest_color=no',
'--retry_failed=3',
'./foo_unittests',
'--asan=0',
'--lsan=0',
'--msan=0',
'--tsan=0',
])
def test_isolate(self):
files = {
'/fake_src/out/Default/toolchain.ninja':
"",
'/fake_src/testing/buildbot/gn_isolate_map.pyl':
("{'foo_unittests': {"
" 'label': '//foo:foo_unittests',"
" 'type': 'non_parallel_console_test_launcher',"
"}}\n"),
'/fake_src/out/Default/foo_unittests.runtime_deps':
("foo_unittests\n"),
}
self.check(
['isolate', '-c', 'debug_goma', '//out/Default', 'foo_unittests'],
files=files,
ret=0)
# test running isolate on an existing build_dir
files['/fake_src/out/Default/args.gn'] = 'is_debug = true\n'
self.check(['isolate', '//out/Default', 'foo_unittests'],
files=files,
ret=0)
files['/fake_src/out/Default/mb_type'] = 'gn\n'
self.check(['isolate', '//out/Default', 'foo_unittests'],
files=files,
ret=0)
if __name__ == '__main__':
unittest.main()
|