blob: f5c5bc272bf8d2c05926a0ca70999398fc415ce8 [file] [log] [blame]
kjellandera013a022016-11-14 05:54:22 -08001#!/usr/bin/python
2# Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3#
4# Use of this source code is governed by a BSD-style license
5# that can be found in the LICENSE file in the root of the source
6# tree. An additional intellectual property rights grant can be found
7# in the file PATENTS. All contributing project authors may
8# be found in the AUTHORS file in the root of the source tree.
9
10"""Tests for mb.py."""
11
ehmaldonadoed8c8ed2016-11-23 12:58:35 -080012import ast
kjellandera013a022016-11-14 05:54:22 -080013import json
14import StringIO
15import os
16import sys
17import unittest
18
19import mb
20
21
22class FakeMBW(mb.MetaBuildWrapper):
23 def __init__(self, win32=False):
24 super(FakeMBW, self).__init__()
25
26 # Override vars for test portability.
27 if win32:
28 self.chromium_src_dir = 'c:\\fake_src'
Henrik Kjellanderb2d55772016-12-18 22:14:50 +010029 self.default_config = 'c:\\fake_src\\tools-webrtc\\mb\\mb_config.pyl'
kjellandera013a022016-11-14 05:54:22 -080030 self.default_isolate_map = ('c:\\fake_src\\testing\\buildbot\\'
31 'gn_isolate_map.pyl')
32 self.platform = 'win32'
33 self.executable = 'c:\\python\\python.exe'
34 self.sep = '\\'
35 else:
36 self.chromium_src_dir = '/fake_src'
Henrik Kjellanderb2d55772016-12-18 22:14:50 +010037 self.default_config = '/fake_src/tools-webrtc/mb/mb_config.pyl'
kjellandera013a022016-11-14 05:54:22 -080038 self.default_isolate_map = '/fake_src/testing/buildbot/gn_isolate_map.pyl'
39 self.executable = '/usr/bin/python'
40 self.platform = 'linux2'
41 self.sep = '/'
42
43 self.files = {}
44 self.calls = []
45 self.cmds = []
46 self.cross_compile = None
47 self.out = ''
48 self.err = ''
49 self.rmdirs = []
50
51 def ExpandUser(self, path):
52 return '$HOME/%s' % path
53
54 def Exists(self, path):
55 return self.files.get(path) is not None
56
57 def MaybeMakeDirectory(self, path):
58 self.files[path] = True
59
60 def PathJoin(self, *comps):
61 return self.sep.join(comps)
62
63 def ReadFile(self, path):
64 return self.files[path]
65
66 def WriteFile(self, path, contents, force_verbose=False):
67 if self.args.dryrun or self.args.verbose or force_verbose:
68 self.Print('\nWriting """\\\n%s""" to %s.\n' % (contents, path))
69 self.files[path] = contents
70
71 def Call(self, cmd, env=None, buffer_output=True):
72 if env:
73 self.cross_compile = env.get('GYP_CROSSCOMPILE')
74 self.calls.append(cmd)
75 if self.cmds:
76 return self.cmds.pop(0)
77 return 0, '', ''
78
79 def Print(self, *args, **kwargs):
80 sep = kwargs.get('sep', ' ')
81 end = kwargs.get('end', '\n')
82 f = kwargs.get('file', sys.stdout)
83 if f == sys.stderr:
84 self.err += sep.join(args) + end
85 else:
86 self.out += sep.join(args) + end
87
88 def TempFile(self, mode='w'):
89 return FakeFile(self.files)
90
91 def RemoveFile(self, path):
92 del self.files[path]
93
94 def RemoveDirectory(self, path):
95 self.rmdirs.append(path)
96 files_to_delete = [f for f in self.files if f.startswith(path)]
97 for f in files_to_delete:
98 self.files[f] = None
99
100
101class FakeFile(object):
102 def __init__(self, files):
103 self.name = '/tmp/file'
104 self.buf = ''
105 self.files = files
106
107 def write(self, contents):
108 self.buf += contents
109
110 def close(self):
111 self.files[self.name] = self.buf
112
113
114TEST_CONFIG = """\
115{
116 'masters': {
117 'chromium': {},
118 'fake_master': {
119 'fake_builder': 'gyp_rel_bot',
120 'fake_gn_builder': 'gn_rel_bot',
121 'fake_gyp_crosscompile_builder': 'gyp_crosscompile',
122 'fake_gn_debug_builder': 'gn_debug_goma',
123 'fake_gyp_builder': 'gyp_debug',
124 'fake_gn_args_bot': '//build/args/bots/fake_master/fake_gn_args_bot.gn',
ehmaldonadoed8c8ed2016-11-23 12:58:35 -0800125 'fake_memcheck_bot': 'gn_memcheck_bot',
kjellandera013a022016-11-14 05:54:22 -0800126 'fake_multi_phase': { 'phase_1': 'gn_phase_1', 'phase_2': 'gn_phase_2'},
ehmaldonadoed8c8ed2016-11-23 12:58:35 -0800127 'fake_android_bot': 'gn_android_bot',
kjellandera013a022016-11-14 05:54:22 -0800128 },
129 },
130 'configs': {
131 'gyp_rel_bot': ['gyp', 'rel', 'goma'],
132 'gn_debug_goma': ['gn', 'debug', 'goma'],
133 'gyp_debug': ['gyp', 'debug', 'fake_feature1'],
134 'gn_rel_bot': ['gn', 'rel', 'goma'],
135 'gyp_crosscompile': ['gyp', 'crosscompile'],
136 'gn_phase_1': ['gn', 'phase_1'],
137 'gn_phase_2': ['gn', 'phase_2'],
ehmaldonadoed8c8ed2016-11-23 12:58:35 -0800138 'gn_memcheck_bot': ['gn', 'memcheck'],
139 'gn_android_bot': ['gn', 'android'],
kjellandera013a022016-11-14 05:54:22 -0800140 },
141 'mixins': {
142 'crosscompile': {
143 'gyp_crosscompile': True,
144 },
145 'fake_feature1': {
146 'gn_args': 'enable_doom_melon=true',
147 'gyp_defines': 'doom_melon=1',
148 },
149 'gyp': {'type': 'gyp'},
150 'gn': {'type': 'gn'},
151 'goma': {
152 'gn_args': 'use_goma=true',
153 'gyp_defines': 'goma=1',
154 },
155 'phase_1': {
156 'gn_args': 'phase=1',
157 'gyp_args': 'phase=1',
158 },
159 'phase_2': {
160 'gn_args': 'phase=2',
161 'gyp_args': 'phase=2',
162 },
163 'rel': {
164 'gn_args': 'is_debug=false',
165 },
166 'debug': {
167 'gn_args': 'is_debug=true',
168 },
ehmaldonadoed8c8ed2016-11-23 12:58:35 -0800169 'memcheck': {
170 'gn_args': 'rtc_use_memcheck=true',
171 },
172 'android': {
173 'gn_args': 'target_os="android"',
174 }
kjellandera013a022016-11-14 05:54:22 -0800175 },
176}
177"""
178
kjellandera013a022016-11-14 05:54:22 -0800179GYP_HACKS_CONFIG = """\
180{
181 'masters': {
182 'chromium': {},
183 'fake_master': {
184 'fake_builder': 'fake_config',
185 },
186 },
187 'configs': {
188 'fake_config': ['fake_mixin'],
189 },
190 'mixins': {
191 'fake_mixin': {
192 'type': 'gyp',
193 'gn_args': '',
194 'gyp_defines':
195 ('foo=bar llvm_force_head_revision=1 '
196 'gyp_link_concurrency=1 baz=1'),
197 },
198 },
199}
200"""
201
202
203class UnitTest(unittest.TestCase):
204 def fake_mbw(self, files=None, win32=False):
205 mbw = FakeMBW(win32=win32)
206 mbw.files.setdefault(mbw.default_config, TEST_CONFIG)
207 mbw.files.setdefault(
208 mbw.ToAbsPath('//testing/buildbot/gn_isolate_map.pyl'),
209 '''{
210 "foo_unittests": {
211 "label": "//foo:foo_unittests",
212 "type": "console_test_launcher",
213 "args": [],
214 },
215 }''')
216 mbw.files.setdefault(
217 mbw.ToAbsPath('//build/args/bots/fake_master/fake_gn_args_bot.gn'),
218 'is_debug = false\n')
219 if files:
220 for path, contents in files.items():
221 mbw.files[path] = contents
222 return mbw
223
224 def check(self, args, mbw=None, files=None, out=None, err=None, ret=None):
225 if not mbw:
226 mbw = self.fake_mbw(files)
227
228 actual_ret = mbw.Main(args)
229
230 self.assertEqual(actual_ret, ret)
231 if out is not None:
232 self.assertEqual(mbw.out, out)
233 if err is not None:
234 self.assertEqual(mbw.err, err)
235 return mbw
236
237 def test_clobber(self):
238 files = {
239 '/fake_src/out/Debug': None,
240 '/fake_src/out/Debug/mb_type': None,
241 }
242 mbw = self.fake_mbw(files)
243
244 # The first time we run this, the build dir doesn't exist, so no clobber.
245 self.check(['gen', '-c', 'gn_debug_goma', '//out/Debug'], mbw=mbw, ret=0)
246 self.assertEqual(mbw.rmdirs, [])
247 self.assertEqual(mbw.files['/fake_src/out/Debug/mb_type'], 'gn')
248
249 # The second time we run this, the build dir exists and matches, so no
250 # clobber.
251 self.check(['gen', '-c', 'gn_debug_goma', '//out/Debug'], mbw=mbw, ret=0)
252 self.assertEqual(mbw.rmdirs, [])
253 self.assertEqual(mbw.files['/fake_src/out/Debug/mb_type'], 'gn')
254
255 # Now we switch build types; this should result in a clobber.
256 self.check(['gen', '-c', 'gyp_debug', '//out/Debug'], mbw=mbw, ret=0)
257 self.assertEqual(mbw.rmdirs, ['/fake_src/out/Debug'])
258 self.assertEqual(mbw.files['/fake_src/out/Debug/mb_type'], 'gyp')
259
260 # Now we delete mb_type; this checks the case where the build dir
261 # exists but wasn't populated by mb; this should also result in a clobber.
262 del mbw.files['/fake_src/out/Debug/mb_type']
263 self.check(['gen', '-c', 'gyp_debug', '//out/Debug'], mbw=mbw, ret=0)
264 self.assertEqual(mbw.rmdirs,
265 ['/fake_src/out/Debug', '/fake_src/out/Debug'])
266 self.assertEqual(mbw.files['/fake_src/out/Debug/mb_type'], 'gyp')
267
268 def test_gn_analyze(self):
269 files = {'/tmp/in.json': '''{\
270 "files": ["foo/foo_unittest.cc"],
271 "test_targets": ["foo_unittests"],
272 "additional_compile_targets": ["all"]
273 }''',
274 '/tmp/out.json.gn': '''{\
275 "status": "Found dependency",
276 "compile_targets": ["//foo:foo_unittests"],
277 "test_targets": ["//foo:foo_unittests"]
278 }'''}
279
280 mbw = self.fake_mbw(files)
281 mbw.Call = lambda cmd, env=None, buffer_output=True: (0, '', '')
282
283 self.check(['analyze', '-c', 'gn_debug_goma', '//out/Default',
284 '/tmp/in.json', '/tmp/out.json'], mbw=mbw, ret=0)
285 out = json.loads(mbw.files['/tmp/out.json'])
286 self.assertEqual(out, {
287 'status': 'Found dependency',
288 'compile_targets': ['foo:foo_unittests'],
289 'test_targets': ['foo_unittests']
290 })
291
292 def test_gn_gen(self):
293 mbw = self.fake_mbw()
294 self.check(['gen', '-c', 'gn_debug_goma', '//out/Default', '-g', '/goma'],
295 mbw=mbw, ret=0)
296 self.assertMultiLineEqual(mbw.files['/fake_src/out/Default/args.gn'],
297 ('goma_dir = "/goma"\n'
298 'is_debug = true\n'
299 'use_goma = true\n'))
300
301 # Make sure we log both what is written to args.gn and the command line.
302 self.assertIn('Writing """', mbw.out)
303 self.assertIn('/fake_src/buildtools/linux64/gn gen //out/Default --check',
304 mbw.out)
305
306 mbw = self.fake_mbw(win32=True)
307 self.check(['gen', '-c', 'gn_debug_goma', '-g', 'c:\\goma', '//out/Debug'],
308 mbw=mbw, ret=0)
309 self.assertMultiLineEqual(mbw.files['c:\\fake_src\\out\\Debug\\args.gn'],
310 ('goma_dir = "c:\\\\goma"\n'
311 'is_debug = true\n'
312 'use_goma = true\n'))
313 self.assertIn('c:\\fake_src\\buildtools\\win\\gn.exe gen //out/Debug '
314 '--check\n', mbw.out)
315
316 mbw = self.fake_mbw()
317 self.check(['gen', '-m', 'fake_master', '-b', 'fake_gn_args_bot',
318 '//out/Debug'],
319 mbw=mbw, ret=0)
320 self.assertEqual(
321 mbw.files['/fake_src/out/Debug/args.gn'],
322 'import("//build/args/bots/fake_master/fake_gn_args_bot.gn")\n')
323
324
325 def test_gn_gen_fails(self):
326 mbw = self.fake_mbw()
327 mbw.Call = lambda cmd, env=None, buffer_output=True: (1, '', '')
328 self.check(['gen', '-c', 'gn_debug_goma', '//out/Default'], mbw=mbw, ret=1)
329
330 def test_gn_gen_swarming(self):
331 files = {
kjellandera013a022016-11-14 05:54:22 -0800332 '/tmp/swarming_targets': 'cc_perftests\n',
333 '/fake_src/testing/buildbot/gn_isolate_map.pyl': (
334 "{'cc_perftests': {"
335 " 'label': '//cc:cc_perftests',"
ehmaldonadob2fcf6d2016-11-15 12:20:30 -0800336 " 'type': 'console_test_launcher',"
kjellandera013a022016-11-14 05:54:22 -0800337 "}}\n"
338 ),
339 'c:\\fake_src\out\Default\cc_perftests.exe.runtime_deps': (
340 "cc_perftests\n"
341 ),
342 }
343 mbw = self.fake_mbw(files=files, win32=True)
344 self.check(['gen',
345 '-c', 'gn_debug_goma',
346 '--swarming-targets-file', '/tmp/swarming_targets',
347 '--isolate-map-file',
348 '/fake_src/testing/buildbot/gn_isolate_map.pyl',
349 '//out/Default'], mbw=mbw, ret=0)
350 self.assertIn('c:\\fake_src\\out\\Default\\cc_perftests.isolate',
351 mbw.files)
352 self.assertIn('c:\\fake_src\\out\\Default\\cc_perftests.isolated.gen.json',
353 mbw.files)
354
ehmaldonadoed8c8ed2016-11-23 12:58:35 -0800355 def test_gn_gen_swarming_android(self):
356 test_files = {
357 '/tmp/swarming_targets': 'base_unittests\n',
358 '/fake_src/testing/buildbot/gn_isolate_map.pyl': (
359 "{'base_unittests': {"
360 " 'label': '//base:base_unittests',"
361 " 'type': 'additional_compile_target',"
362 "}}\n"
363 ),
364 '/fake_src/out/Default/base_unittests.runtime_deps': (
365 "base_unittests\n"
366 ),
367 }
368 mbw = self.check(['gen', '-c', 'gn_android_bot', '//out/Default',
369 '--swarming-targets-file', '/tmp/swarming_targets',
370 '--isolate-map-file',
371 '/fake_src/testing/buildbot/gn_isolate_map.pyl'],
372 files=test_files, ret=0)
373
374 isolate_file = mbw.files['/fake_src/out/Default/base_unittests.isolate']
375 isolate_file_contents = ast.literal_eval(isolate_file)
376 files = isolate_file_contents['variables']['files']
377 command = isolate_file_contents['variables']['command']
378
379 self.assertEqual(files, ['base_unittests'])
380 self.assertEqual(command, [
381 './../../build/android/test_wrapper/logdog_wrapper.py',
382 '--logdog-bin-cmd', './../../bin/logdog_butler',
383 '--project', 'chromium',
384 '--service-account-json',
385 '/creds/service_accounts/service-account-luci-logdog-publisher.json',
386 '--prefix', 'android/swarming/logcats/${SWARMING_TASK_ID}',
387 '--source', '${ISOLATED_OUTDIR}/logcats',
388 '--name', 'unified_logcats',
389 'bin/run_base_unittests',
390 '--logcat-output-file', '${ISOLATED_OUTDIR}/logcats',
391 '--target-devices-file', '${SWARMING_BOT_FILE}',
392 '-v',
393 ])
394
395 def test_gn_gen_swarming_android_junit_test(self):
396 test_files = {
397 '/tmp/swarming_targets': 'base_unittests\n',
398 '/fake_src/testing/buildbot/gn_isolate_map.pyl': (
399 "{'base_unittests': {"
400 " 'label': '//base:base_unittests',"
401 " 'type': 'junit_test',"
402 "}}\n"
403 ),
404 '/fake_src/out/Default/base_unittests.runtime_deps': (
405 "base_unittests\n"
406 ),
407 }
408 mbw = self.check(['gen', '-c', 'gn_android_bot', '//out/Default',
409 '--swarming-targets-file', '/tmp/swarming_targets',
410 '--isolate-map-file',
411 '/fake_src/testing/buildbot/gn_isolate_map.pyl'],
412 files=test_files, ret=0)
413
414 isolate_file = mbw.files['/fake_src/out/Default/base_unittests.isolate']
415 isolate_file_contents = ast.literal_eval(isolate_file)
416 files = isolate_file_contents['variables']['files']
417 command = isolate_file_contents['variables']['command']
418
419 self.assertEqual(files, ['base_unittests'])
420 self.assertEqual(command, [
421 './../../build/android/test_wrapper/logdog_wrapper.py',
422 '--logdog-bin-cmd', './../../bin/logdog_butler',
423 '--project', 'chromium',
424 '--service-account-json',
425 '/creds/service_accounts/service-account-luci-logdog-publisher.json',
426 '--prefix', 'android/swarming/logcats/${SWARMING_TASK_ID}',
427 '--source', '${ISOLATED_OUTDIR}/logcats',
428 '--name', 'unified_logcats',
429 'bin/run_base_unittests',
430 '--logcat-output-file', '${ISOLATED_OUTDIR}/logcats',
ehmaldonadoed8c8ed2016-11-23 12:58:35 -0800431 '-v',
432 ])
433
434 def test_gn_gen_non_parallel_console_test_launcher(self):
435 test_files = {
436 '/tmp/swarming_targets': 'base_unittests\n',
437 '/fake_src/testing/buildbot/gn_isolate_map.pyl': (
438 "{'base_unittests': {"
439 " 'label': '//base:base_unittests',"
440 " 'type': 'non_parallel_console_test_launcher',"
441 "}}\n"
442 ),
443 '/fake_src/out/Default/base_unittests.runtime_deps': (
444 "base_unittests\n"
445 ),
446 }
447 mbw = self.check(['gen', '-c', 'gn_debug_goma', '//out/Default',
448 '--swarming-targets-file', '/tmp/swarming_targets',
449 '--isolate-map-file',
450 '/fake_src/testing/buildbot/gn_isolate_map.pyl'],
451 files=test_files, ret=0)
452
453 isolate_file = mbw.files['/fake_src/out/Default/base_unittests.isolate']
454 isolate_file_contents = ast.literal_eval(isolate_file)
455 files = isolate_file_contents['variables']['files']
456 command = isolate_file_contents['variables']['command']
457
458 self.assertEqual(files, [
459 '../../testing/test_env.py',
460 'base_unittests',
461 ])
462 self.assertEqual(command, [
463 '../../testing/test_env.py',
464 './base_unittests',
465 '--',
466 '--asan=0',
467 '--msan=0',
468 '--tsan=0',
469 ])
470
471 def test_gn_isolate_windowed_test_launcher_linux(self):
472 test_files = {
473 '/tmp/swarming_targets': 'base_unittests\n',
474 '/fake_src/testing/buildbot/gn_isolate_map.pyl': (
475 "{'base_unittests': {"
476 " 'label': '//base:base_unittests',"
477 " 'type': 'windowed_test_launcher',"
478 "}}\n"
479 ),
480 '/fake_src/out/Default/base_unittests.runtime_deps': (
481 "base_unittests\n"
482 "some_resource_file\n"
483 ),
484 }
485 mbw = self.check(['gen', '-c', 'gn_debug_goma', '//out/Default',
486 '--swarming-targets-file', '/tmp/swarming_targets',
487 '--isolate-map-file',
488 '/fake_src/testing/buildbot/gn_isolate_map.pyl'],
489 files=test_files, ret=0)
490
491 isolate_file = mbw.files['/fake_src/out/Default/base_unittests.isolate']
492 isolate_file_contents = ast.literal_eval(isolate_file)
493 files = isolate_file_contents['variables']['files']
494 command = isolate_file_contents['variables']['command']
495
496 self.assertEqual(files, [
497 '../../testing/test_env.py',
498 '../../testing/xvfb.py',
499 '../../third_party/gtest-parallel/gtest-parallel',
500 '../../third_party/gtest-parallel/gtest-parallel-wrapper.py',
501 'base_unittests',
502 'some_resource_file',
ehmaldonadoed8c8ed2016-11-23 12:58:35 -0800503 ])
504 self.assertEqual(command, [
505 '../../testing/xvfb.py',
ehmaldonadoed8c8ed2016-11-23 12:58:35 -0800506 '../../third_party/gtest-parallel/gtest-parallel-wrapper.py',
ehmaldonado55833842017-02-13 03:58:13 -0800507 '--output_dir=${ISOLATED_OUTDIR}/test_logs',
ehmaldonadoed8c8ed2016-11-23 12:58:35 -0800508 './base_unittests',
509 '--',
510 '--asan=0',
511 '--msan=0',
512 '--tsan=0',
513 ])
514
515 def test_gn_gen_windowed_test_launcher_win(self):
516 files = {
517 '/tmp/swarming_targets': 'unittests\n',
518 '/fake_src/testing/buildbot/gn_isolate_map.pyl': (
519 "{'unittests': {"
520 " 'label': '//somewhere:unittests',"
521 " 'type': 'windowed_test_launcher',"
522 "}}\n"
523 ),
524 r'c:\fake_src\out\Default\unittests.exe.runtime_deps': (
525 "unittests.exe\n"
526 "some_dependency\n"
527 ),
528 }
529 mbw = self.fake_mbw(files=files, win32=True)
530 self.check(['gen',
531 '-c', 'gn_debug_goma',
532 '--swarming-targets-file', '/tmp/swarming_targets',
533 '--isolate-map-file',
534 '/fake_src/testing/buildbot/gn_isolate_map.pyl',
535 '//out/Default'], mbw=mbw, ret=0)
536
537 isolate_file = mbw.files['c:\\fake_src\\out\\Default\\unittests.isolate']
538 isolate_file_contents = ast.literal_eval(isolate_file)
539 files = isolate_file_contents['variables']['files']
540 command = isolate_file_contents['variables']['command']
541
542 self.assertEqual(files, [
543 '../../testing/test_env.py',
544 '../../third_party/gtest-parallel/gtest-parallel',
545 '../../third_party/gtest-parallel/gtest-parallel-wrapper.py',
546 'some_dependency',
547 'unittests.exe',
548 ])
549 self.assertEqual(command, [
550 '../../testing/test_env.py',
551 '../../third_party/gtest-parallel/gtest-parallel-wrapper.py',
ehmaldonado55833842017-02-13 03:58:13 -0800552 '--output_dir=${ISOLATED_OUTDIR}\\test_logs',
ehmaldonadoed8c8ed2016-11-23 12:58:35 -0800553 r'.\unittests.exe',
554 '--',
555 '--asan=0',
556 '--msan=0',
557 '--tsan=0',
558 ])
559
560 def test_gn_gen_console_test_launcher(self):
561 test_files = {
562 '/tmp/swarming_targets': 'base_unittests\n',
563 '/fake_src/testing/buildbot/gn_isolate_map.pyl': (
564 "{'base_unittests': {"
565 " 'label': '//base:base_unittests',"
566 " 'type': 'console_test_launcher',"
567 "}}\n"
568 ),
569 '/fake_src/out/Default/base_unittests.runtime_deps': (
570 "base_unittests\n"
571 ),
572 }
573 mbw = self.check(['gen', '-c', 'gn_debug_goma', '//out/Default',
574 '--swarming-targets-file', '/tmp/swarming_targets',
575 '--isolate-map-file',
576 '/fake_src/testing/buildbot/gn_isolate_map.pyl'],
577 files=test_files, ret=0)
578
579 isolate_file = mbw.files['/fake_src/out/Default/base_unittests.isolate']
580 isolate_file_contents = ast.literal_eval(isolate_file)
581 files = isolate_file_contents['variables']['files']
582 command = isolate_file_contents['variables']['command']
583
584 self.assertEqual(files, [
585 '../../testing/test_env.py',
586 '../../third_party/gtest-parallel/gtest-parallel',
587 '../../third_party/gtest-parallel/gtest-parallel-wrapper.py',
588 'base_unittests',
589 ])
590 self.assertEqual(command, [
591 '../../testing/test_env.py',
592 '../../third_party/gtest-parallel/gtest-parallel-wrapper.py',
ehmaldonado55833842017-02-13 03:58:13 -0800593 '--output_dir=${ISOLATED_OUTDIR}/test_logs',
ehmaldonadoed8c8ed2016-11-23 12:58:35 -0800594 './base_unittests',
595 '--',
596 '--asan=0',
597 '--msan=0',
598 '--tsan=0',
599 ])
600
601 def test_gn_isolate_console_test_launcher_memcheck(self):
602 test_files = {
603 '/tmp/swarming_targets': 'base_unittests\n',
604 '/fake_src/testing/buildbot/gn_isolate_map.pyl': (
605 "{'base_unittests': {"
606 " 'label': '//base:base_unittests',"
607 " 'type': 'console_test_launcher',"
608 "}}\n"
609 ),
610 '/fake_src/out/Release/base_unittests.runtime_deps': (
611 "base_unittests\n"
612 "lots_of_memcheck_dependencies\n"
kjellanderafd54942016-12-17 12:21:39 -0800613 "../../tools-webrtc/valgrind/webrtc_tests.sh\n"
ehmaldonadoed8c8ed2016-11-23 12:58:35 -0800614 ),
615 }
616 mbw = self.check(['gen', '-c', 'gn_memcheck_bot', '//out/Release',
617 '--swarming-targets-file', '/tmp/swarming_targets',
618 '--isolate-map-file',
619 '/fake_src/testing/buildbot/gn_isolate_map.pyl'],
620 files=test_files, ret=0)
621
622 isolate_file = mbw.files['/fake_src/out/Release/base_unittests.isolate']
623 isolate_file_contents = ast.literal_eval(isolate_file)
624 files = isolate_file_contents['variables']['files']
625 command = isolate_file_contents['variables']['command']
626
627 self.assertEqual(files, [
628 '../../testing/test_env.py',
kjellanderafd54942016-12-17 12:21:39 -0800629 '../../tools-webrtc/valgrind/webrtc_tests.sh',
ehmaldonadoed8c8ed2016-11-23 12:58:35 -0800630 'base_unittests',
631 'lots_of_memcheck_dependencies',
632 ])
633 self.assertEqual(command, [
634 '../../testing/test_env.py',
635 'bash',
kjellanderafd54942016-12-17 12:21:39 -0800636 '../../tools-webrtc/valgrind/webrtc_tests.sh',
ehmaldonadoed8c8ed2016-11-23 12:58:35 -0800637 '--tool',
638 'memcheck',
639 '--target',
640 'Release',
641 '--build-dir',
642 '..',
643 '--test',
644 './base_unittests',
645 '--',
646 '--asan=0',
647 '--msan=0',
648 '--tsan=0',
649 ])
kjellandera013a022016-11-14 05:54:22 -0800650
651 def test_gn_isolate(self):
652 files = {
653 '/fake_src/out/Default/toolchain.ninja': "",
654 '/fake_src/testing/buildbot/gn_isolate_map.pyl': (
655 "{'base_unittests': {"
656 " 'label': '//base:base_unittests',"
ehmaldonadob2fcf6d2016-11-15 12:20:30 -0800657 " 'type': 'non_parallel_console_test_launcher',"
kjellandera013a022016-11-14 05:54:22 -0800658 "}}\n"
659 ),
660 '/fake_src/out/Default/base_unittests.runtime_deps': (
661 "base_unittests\n"
662 ),
663 }
664 self.check(['isolate', '-c', 'gn_debug_goma', '//out/Default',
665 'base_unittests'], files=files, ret=0)
666
667 # test running isolate on an existing build_dir
668 files['/fake_src/out/Default/args.gn'] = 'is_debug = True\n'
669 self.check(['isolate', '//out/Default', 'base_unittests'],
670 files=files, ret=0)
kjellandera013a022016-11-14 05:54:22 -0800671 files['/fake_src/out/Default/mb_type'] = 'gn\n'
672 self.check(['isolate', '//out/Default', 'base_unittests'],
673 files=files, ret=0)
674
675 def test_gn_run(self):
676 files = {
677 '/fake_src/testing/buildbot/gn_isolate_map.pyl': (
678 "{'base_unittests': {"
679 " 'label': '//base:base_unittests',"
ehmaldonadob2fcf6d2016-11-15 12:20:30 -0800680 " 'type': 'windowed_test_launcher',"
kjellandera013a022016-11-14 05:54:22 -0800681 "}}\n"
682 ),
683 '/fake_src/out/Default/base_unittests.runtime_deps': (
684 "base_unittests\n"
685 ),
686 }
687 self.check(['run', '-c', 'gn_debug_goma', '//out/Default',
688 'base_unittests'], files=files, ret=0)
689
690 def test_gn_lookup(self):
691 self.check(['lookup', '-c', 'gn_debug_goma'], ret=0)
692
693 def test_gn_lookup_goma_dir_expansion(self):
694 self.check(['lookup', '-c', 'gn_rel_bot', '-g', '/foo'], ret=0,
695 out=('\n'
696 'Writing """\\\n'
697 'goma_dir = "/foo"\n'
698 'is_debug = false\n'
699 'use_goma = true\n'
700 '""" to _path_/args.gn.\n\n'
701 '/fake_src/buildtools/linux64/gn gen _path_\n'))
702
703 def test_gyp_analyze(self):
704 mbw = self.check(['analyze', '-c', 'gyp_rel_bot', '//out/Release',
705 '/tmp/in.json', '/tmp/out.json'], ret=0)
706 self.assertIn('analyzer', mbw.calls[0])
707
708 def test_gyp_crosscompile(self):
709 mbw = self.fake_mbw()
710 self.check(['gen', '-c', 'gyp_crosscompile', '//out/Release'],
711 mbw=mbw, ret=0)
712 self.assertTrue(mbw.cross_compile)
713
714 def test_gyp_gen(self):
715 self.check(['gen', '-c', 'gyp_rel_bot', '-g', '/goma', '//out/Release'],
716 ret=0,
717 out=("GYP_DEFINES='goma=1 gomadir=/goma'\n"
718 "python build/gyp_chromium -G output_dir=out\n"))
719
720 mbw = self.fake_mbw(win32=True)
721 self.check(['gen', '-c', 'gyp_rel_bot', '-g', 'c:\\goma', '//out/Release'],
722 mbw=mbw, ret=0,
723 out=("set GYP_DEFINES=goma=1 gomadir='c:\\goma'\n"
724 "python build\\gyp_chromium -G output_dir=out\n"))
725
726 def test_gyp_gen_fails(self):
727 mbw = self.fake_mbw()
728 mbw.Call = lambda cmd, env=None, buffer_output=True: (1, '', '')
729 self.check(['gen', '-c', 'gyp_rel_bot', '//out/Release'], mbw=mbw, ret=1)
730
731 def test_gyp_lookup_goma_dir_expansion(self):
732 self.check(['lookup', '-c', 'gyp_rel_bot', '-g', '/foo'], ret=0,
733 out=("GYP_DEFINES='goma=1 gomadir=/foo'\n"
734 "python build/gyp_chromium -G output_dir=_path_\n"))
735
736 def test_help(self):
737 orig_stdout = sys.stdout
738 try:
739 sys.stdout = StringIO.StringIO()
740 self.assertRaises(SystemExit, self.check, ['-h'])
741 self.assertRaises(SystemExit, self.check, ['help'])
742 self.assertRaises(SystemExit, self.check, ['help', 'gen'])
743 finally:
744 sys.stdout = orig_stdout
745
746 def test_multiple_phases(self):
747 # Check that not passing a --phase to a multi-phase builder fails.
748 mbw = self.check(['lookup', '-m', 'fake_master', '-b', 'fake_multi_phase'],
749 ret=1)
750 self.assertIn('Must specify a build --phase', mbw.out)
751
752 # Check that passing a --phase to a single-phase builder fails.
753 mbw = self.check(['lookup', '-m', 'fake_master', '-b', 'fake_gn_builder',
754 '--phase', 'phase_1'], ret=1)
755 self.assertIn('Must not specify a build --phase', mbw.out)
756
757 # Check that passing a wrong phase key to a multi-phase builder fails.
758 mbw = self.check(['lookup', '-m', 'fake_master', '-b', 'fake_multi_phase',
759 '--phase', 'wrong_phase'], ret=1)
760 self.assertIn('Phase wrong_phase doesn\'t exist', mbw.out)
761
762 # Check that passing a correct phase key to a multi-phase builder passes.
763 mbw = self.check(['lookup', '-m', 'fake_master', '-b', 'fake_multi_phase',
764 '--phase', 'phase_1'], ret=0)
765 self.assertIn('phase = 1', mbw.out)
766
767 mbw = self.check(['lookup', '-m', 'fake_master', '-b', 'fake_multi_phase',
768 '--phase', 'phase_2'], ret=0)
769 self.assertIn('phase = 2', mbw.out)
770
771 def test_validate(self):
772 mbw = self.fake_mbw()
773 self.check(['validate'], mbw=mbw, ret=0)
774
kjellandera013a022016-11-14 05:54:22 -0800775 def test_gyp_env_hacks(self):
776 mbw = self.fake_mbw()
777 mbw.files[mbw.default_config] = GYP_HACKS_CONFIG
778 self.check(['lookup', '-c', 'fake_config'], mbw=mbw,
779 ret=0,
780 out=("GYP_DEFINES='foo=bar baz=1'\n"
781 "GYP_LINK_CONCURRENCY=1\n"
782 "LLVM_FORCE_HEAD_REVISION=1\n"
783 "python build/gyp_chromium -G output_dir=_path_\n"))
784
785
786if __name__ == '__main__':
787 unittest.main()