test.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2012 The Chromium OS Authors.
  3. #
  4. import os
  5. import shutil
  6. import sys
  7. import tempfile
  8. import time
  9. import unittest
  10. from buildman import board
  11. from buildman import boards
  12. from buildman import bsettings
  13. from buildman import builder
  14. from buildman import cfgutil
  15. from buildman import control
  16. from buildman import toolchain
  17. from patman import commit
  18. from u_boot_pylib import command
  19. from u_boot_pylib import terminal
  20. from u_boot_pylib import test_util
  21. from u_boot_pylib import tools
  22. use_network = True
  23. settings_data = '''
  24. # Buildman settings file
  25. [toolchain]
  26. main: /usr/sbin
  27. [toolchain-alias]
  28. x86: i386 x86_64
  29. '''
  30. migration = '''===================== WARNING ======================
  31. This board does not use CONFIG_DM. CONFIG_DM will be
  32. compulsory starting with the v2020.01 release.
  33. Failure to update may result in board removal.
  34. See doc/develop/driver-model/migration.rst for more info.
  35. ====================================================
  36. '''
  37. errors = [
  38. '''main.c: In function 'main_loop':
  39. main.c:260:6: warning: unused variable 'joe' [-Wunused-variable]
  40. ''',
  41. '''main.c: In function 'main_loop2':
  42. main.c:295:2: error: 'fred' undeclared (first use in this function)
  43. main.c:295:2: note: each undeclared identifier is reported only once for each function it appears in
  44. make[1]: *** [main.o] Error 1
  45. make: *** [common/libcommon.o] Error 2
  46. Make failed
  47. ''',
  48. '''arch/arm/dts/socfpga_arria10_socdk_sdmmc.dtb: Warning \
  49. (avoid_unnecessary_addr_size): /clocks: unnecessary #address-cells/#size-cells \
  50. without "ranges" or child "reg" property
  51. ''',
  52. '''powerpc-linux-ld: warning: dot moved backwards before `.bss'
  53. powerpc-linux-ld: warning: dot moved backwards before `.bss'
  54. powerpc-linux-ld: u-boot: section .text lma 0xfffc0000 overlaps previous sections
  55. powerpc-linux-ld: u-boot: section .rodata lma 0xfffef3ec overlaps previous sections
  56. powerpc-linux-ld: u-boot: section .reloc lma 0xffffa400 overlaps previous sections
  57. powerpc-linux-ld: u-boot: section .data lma 0xffffcd38 overlaps previous sections
  58. powerpc-linux-ld: u-boot: section .u_boot_cmd lma 0xffffeb40 overlaps previous sections
  59. powerpc-linux-ld: u-boot: section .bootpg lma 0xfffff198 overlaps previous sections
  60. ''',
  61. '''In file included from %(basedir)sarch/sandbox/cpu/cpu.c:9:0:
  62. %(basedir)sarch/sandbox/include/asm/state.h:44:0: warning: "xxxx" redefined [enabled by default]
  63. %(basedir)sarch/sandbox/include/asm/state.h:43:0: note: this is the location of the previous definition
  64. %(basedir)sarch/sandbox/cpu/cpu.c: In function 'do_reset':
  65. %(basedir)sarch/sandbox/cpu/cpu.c:27:1: error: unknown type name 'blah'
  66. %(basedir)sarch/sandbox/cpu/cpu.c:28:12: error: expected declaration specifiers or '...' before numeric constant
  67. make[2]: *** [arch/sandbox/cpu/cpu.o] Error 1
  68. make[1]: *** [arch/sandbox/cpu] Error 2
  69. make[1]: *** Waiting for unfinished jobs....
  70. In file included from %(basedir)scommon/board_f.c:55:0:
  71. %(basedir)sarch/sandbox/include/asm/state.h:44:0: warning: "xxxx" redefined [enabled by default]
  72. %(basedir)sarch/sandbox/include/asm/state.h:43:0: note: this is the location of the previous definition
  73. make: *** [sub-make] Error 2
  74. '''
  75. ]
  76. # hash, subject, return code, list of errors/warnings
  77. commits = [
  78. ['1234', 'upstream/master, migration warning', 0, []],
  79. ['5678', 'Second commit, a warning', 0, errors[0:1]],
  80. ['9012', 'Third commit, error', 1, errors[0:2]],
  81. ['3456', 'Fourth commit, warning', 0, [errors[0], errors[2]]],
  82. ['7890', 'Fifth commit, link errors', 1, [errors[0], errors[3]]],
  83. ['abcd', 'Sixth commit, fixes all errors', 0, []],
  84. ['ef01', 'Seventh commit, fix migration, check directory suppression', 1,
  85. [errors[4]]],
  86. ]
  87. BOARDS = [
  88. ['Active', 'arm', 'armv7', '', 'Tester', 'ARM Board 1', 'board0', ''],
  89. ['Active', 'arm', 'armv7', '', 'Tester', 'ARM Board 2', 'board1', ''],
  90. ['Active', 'powerpc', 'powerpc', '', 'Tester', 'PowerPC board 1', 'board2', ''],
  91. ['Active', 'powerpc', 'mpc83xx', '', 'Tester', 'PowerPC board 2', 'board3', ''],
  92. ['Active', 'sandbox', 'sandbox', '', 'Tester', 'Sandbox board', 'board4', ''],
  93. ]
  94. BASE_DIR = 'base'
  95. OUTCOME_OK, OUTCOME_WARN, OUTCOME_ERR = range(3)
  96. class Options:
  97. """Class that holds build options"""
  98. pass
  99. class TestBuild(unittest.TestCase):
  100. """Test buildman
  101. TODO: Write tests for the rest of the functionality
  102. """
  103. def setUp(self):
  104. # Set up commits to build
  105. self.commits = []
  106. sequence = 0
  107. for commit_info in commits:
  108. comm = commit.Commit(commit_info[0])
  109. comm.subject = commit_info[1]
  110. comm.return_code = commit_info[2]
  111. comm.error_list = commit_info[3]
  112. if sequence < 6:
  113. comm.error_list += [migration]
  114. comm.sequence = sequence
  115. sequence += 1
  116. self.commits.append(comm)
  117. # Set up boards to build
  118. self.brds = boards.Boards()
  119. for brd in BOARDS:
  120. self.brds.add_board(board.Board(*brd))
  121. self.brds.select_boards([])
  122. # Add some test settings
  123. bsettings.setup(None)
  124. bsettings.add_file(settings_data)
  125. # Set up the toolchains
  126. self.toolchains = toolchain.Toolchains()
  127. self.toolchains.Add('arm-linux-gcc', test=False)
  128. self.toolchains.Add('sparc-linux-gcc', test=False)
  129. self.toolchains.Add('powerpc-linux-gcc', test=False)
  130. self.toolchains.Add('gcc', test=False)
  131. # Avoid sending any output
  132. terminal.set_print_test_mode()
  133. self._col = terminal.Color()
  134. self.base_dir = tempfile.mkdtemp()
  135. if not os.path.isdir(self.base_dir):
  136. os.mkdir(self.base_dir)
  137. def tearDown(self):
  138. shutil.rmtree(self.base_dir)
  139. def Make(self, commit, brd, stage, *args, **kwargs):
  140. result = command.CommandResult()
  141. boardnum = int(brd.target[-1])
  142. result.return_code = 0
  143. result.stderr = ''
  144. result.stdout = ('This is the test output for board %s, commit %s' %
  145. (brd.target, commit.hash))
  146. if ((boardnum >= 1 and boardnum >= commit.sequence) or
  147. boardnum == 4 and commit.sequence == 6):
  148. result.return_code = commit.return_code
  149. result.stderr = (''.join(commit.error_list)
  150. % {'basedir' : self.base_dir + '/.bm-work/00/'})
  151. elif commit.sequence < 6:
  152. result.stderr = migration
  153. result.combined = result.stdout + result.stderr
  154. return result
  155. def assertSummary(self, text, arch, plus, brds, outcome=OUTCOME_ERR):
  156. col = self._col
  157. expected_colour = (col.GREEN if outcome == OUTCOME_OK else
  158. col.YELLOW if outcome == OUTCOME_WARN else col.RED)
  159. expect = '%10s: ' % arch
  160. # TODO(sjg@chromium.org): If plus is '', we shouldn't need this
  161. expect += ' ' + col.build(expected_colour, plus)
  162. expect += ' '
  163. for brd in brds:
  164. expect += col.build(expected_colour, ' %s' % brd)
  165. self.assertEqual(text, expect)
  166. def _SetupTest(self, echo_lines=False, threads=1, **kwdisplay_args):
  167. """Set up the test by running a build and summary
  168. Args:
  169. echo_lines: True to echo lines to the terminal to aid test
  170. development
  171. kwdisplay_args: Dict of arguemnts to pass to
  172. Builder.SetDisplayOptions()
  173. Returns:
  174. Iterator containing the output lines, each a PrintLine() object
  175. """
  176. build = builder.Builder(self.toolchains, self.base_dir, None, threads,
  177. 2, checkout=False, show_unknown=False)
  178. build.do_make = self.Make
  179. board_selected = self.brds.get_selected_dict()
  180. # Build the boards for the pre-defined commits and warnings/errors
  181. # associated with each. This calls our Make() to inject the fake output.
  182. build.build_boards(self.commits, board_selected, keep_outputs=False,
  183. verbose=False)
  184. lines = terminal.get_print_test_lines()
  185. count = 0
  186. for line in lines:
  187. if line.text.strip():
  188. count += 1
  189. # We should get two starting messages, an update for every commit built
  190. # and a summary message
  191. self.assertEqual(count, len(commits) * len(BOARDS) + 3)
  192. build.set_display_options(**kwdisplay_args);
  193. build.show_summary(self.commits, board_selected)
  194. if echo_lines:
  195. terminal.echo_print_test_lines()
  196. return iter(terminal.get_print_test_lines())
  197. def _CheckOutput(self, lines, list_error_boards=False,
  198. filter_dtb_warnings=False,
  199. filter_migration_warnings=False):
  200. """Check for expected output from the build summary
  201. Args:
  202. lines: Iterator containing the lines returned from the summary
  203. list_error_boards: Adjust the check for output produced with the
  204. --list-error-boards flag
  205. filter_dtb_warnings: Adjust the check for output produced with the
  206. --filter-dtb-warnings flag
  207. """
  208. def add_line_prefix(prefix, brds, error_str, colour):
  209. """Add a prefix to each line of a string
  210. The training \n in error_str is removed before processing
  211. Args:
  212. prefix: String prefix to add
  213. error_str: Error string containing the lines
  214. colour: Expected colour for the line. Note that the board list,
  215. if present, always appears in magenta
  216. Returns:
  217. New string where each line has the prefix added
  218. """
  219. lines = error_str.strip().splitlines()
  220. new_lines = []
  221. for line in lines:
  222. if brds:
  223. expect = self._col.build(colour, prefix + '(')
  224. expect += self._col.build(self._col.MAGENTA, brds,
  225. bright=False)
  226. expect += self._col.build(colour, ') %s' % line)
  227. else:
  228. expect = self._col.build(colour, prefix + line)
  229. new_lines.append(expect)
  230. return '\n'.join(new_lines)
  231. col = terminal.Color()
  232. boards01234 = ('board0 board1 board2 board3 board4'
  233. if list_error_boards else '')
  234. boards1234 = 'board1 board2 board3 board4' if list_error_boards else ''
  235. boards234 = 'board2 board3 board4' if list_error_boards else ''
  236. boards34 = 'board3 board4' if list_error_boards else ''
  237. boards4 = 'board4' if list_error_boards else ''
  238. # Upstream commit: migration warnings only
  239. self.assertEqual(next(lines).text, '01: %s' % commits[0][1])
  240. if not filter_migration_warnings:
  241. self.assertSummary(next(lines).text, 'arm', 'w+',
  242. ['board0', 'board1'], outcome=OUTCOME_WARN)
  243. self.assertSummary(next(lines).text, 'powerpc', 'w+',
  244. ['board2', 'board3'], outcome=OUTCOME_WARN)
  245. self.assertSummary(next(lines).text, 'sandbox', 'w+', ['board4'],
  246. outcome=OUTCOME_WARN)
  247. self.assertEqual(next(lines).text,
  248. add_line_prefix('+', boards01234, migration, col.RED))
  249. # Second commit: all archs should fail with warnings
  250. self.assertEqual(next(lines).text, '02: %s' % commits[1][1])
  251. if filter_migration_warnings:
  252. self.assertSummary(next(lines).text, 'arm', 'w+',
  253. ['board1'], outcome=OUTCOME_WARN)
  254. self.assertSummary(next(lines).text, 'powerpc', 'w+',
  255. ['board2', 'board3'], outcome=OUTCOME_WARN)
  256. self.assertSummary(next(lines).text, 'sandbox', 'w+', ['board4'],
  257. outcome=OUTCOME_WARN)
  258. # Second commit: The warnings should be listed
  259. self.assertEqual(next(lines).text,
  260. add_line_prefix('w+', boards1234, errors[0], col.YELLOW))
  261. # Third commit: Still fails
  262. self.assertEqual(next(lines).text, '03: %s' % commits[2][1])
  263. if filter_migration_warnings:
  264. self.assertSummary(next(lines).text, 'arm', '',
  265. ['board1'], outcome=OUTCOME_OK)
  266. self.assertSummary(next(lines).text, 'powerpc', '+',
  267. ['board2', 'board3'])
  268. self.assertSummary(next(lines).text, 'sandbox', '+', ['board4'])
  269. # Expect a compiler error
  270. self.assertEqual(next(lines).text,
  271. add_line_prefix('+', boards234, errors[1], col.RED))
  272. # Fourth commit: Compile errors are fixed, just have warning for board3
  273. self.assertEqual(next(lines).text, '04: %s' % commits[3][1])
  274. if filter_migration_warnings:
  275. expect = '%10s: ' % 'powerpc'
  276. expect += ' ' + col.build(col.GREEN, '')
  277. expect += ' '
  278. expect += col.build(col.GREEN, ' %s' % 'board2')
  279. expect += ' ' + col.build(col.YELLOW, 'w+')
  280. expect += ' '
  281. expect += col.build(col.YELLOW, ' %s' % 'board3')
  282. self.assertEqual(next(lines).text, expect)
  283. else:
  284. self.assertSummary(next(lines).text, 'powerpc', 'w+',
  285. ['board2', 'board3'], outcome=OUTCOME_WARN)
  286. self.assertSummary(next(lines).text, 'sandbox', 'w+', ['board4'],
  287. outcome=OUTCOME_WARN)
  288. # Compile error fixed
  289. self.assertEqual(next(lines).text,
  290. add_line_prefix('-', boards234, errors[1], col.GREEN))
  291. if not filter_dtb_warnings:
  292. self.assertEqual(
  293. next(lines).text,
  294. add_line_prefix('w+', boards34, errors[2], col.YELLOW))
  295. # Fifth commit
  296. self.assertEqual(next(lines).text, '05: %s' % commits[4][1])
  297. if filter_migration_warnings:
  298. self.assertSummary(next(lines).text, 'powerpc', '', ['board3'],
  299. outcome=OUTCOME_OK)
  300. self.assertSummary(next(lines).text, 'sandbox', '+', ['board4'])
  301. # The second line of errors[3] is a duplicate, so buildman will drop it
  302. expect = errors[3].rstrip().split('\n')
  303. expect = [expect[0]] + expect[2:]
  304. expect = '\n'.join(expect)
  305. self.assertEqual(next(lines).text,
  306. add_line_prefix('+', boards4, expect, col.RED))
  307. if not filter_dtb_warnings:
  308. self.assertEqual(
  309. next(lines).text,
  310. add_line_prefix('w-', boards34, errors[2], col.CYAN))
  311. # Sixth commit
  312. self.assertEqual(next(lines).text, '06: %s' % commits[5][1])
  313. if filter_migration_warnings:
  314. self.assertSummary(next(lines).text, 'sandbox', '', ['board4'],
  315. outcome=OUTCOME_OK)
  316. else:
  317. self.assertSummary(next(lines).text, 'sandbox', 'w+', ['board4'],
  318. outcome=OUTCOME_WARN)
  319. # The second line of errors[3] is a duplicate, so buildman will drop it
  320. expect = errors[3].rstrip().split('\n')
  321. expect = [expect[0]] + expect[2:]
  322. expect = '\n'.join(expect)
  323. self.assertEqual(next(lines).text,
  324. add_line_prefix('-', boards4, expect, col.GREEN))
  325. self.assertEqual(next(lines).text,
  326. add_line_prefix('w-', boards4, errors[0], col.CYAN))
  327. # Seventh commit
  328. self.assertEqual(next(lines).text, '07: %s' % commits[6][1])
  329. if filter_migration_warnings:
  330. self.assertSummary(next(lines).text, 'sandbox', '+', ['board4'])
  331. else:
  332. self.assertSummary(next(lines).text, 'arm', '', ['board0', 'board1'],
  333. outcome=OUTCOME_OK)
  334. self.assertSummary(next(lines).text, 'powerpc', '',
  335. ['board2', 'board3'], outcome=OUTCOME_OK)
  336. self.assertSummary(next(lines).text, 'sandbox', '+', ['board4'])
  337. # Pick out the correct error lines
  338. expect_str = errors[4].rstrip().replace('%(basedir)s', '').split('\n')
  339. expect = expect_str[3:8] + [expect_str[-1]]
  340. expect = '\n'.join(expect)
  341. if not filter_migration_warnings:
  342. self.assertEqual(
  343. next(lines).text,
  344. add_line_prefix('-', boards01234, migration, col.GREEN))
  345. self.assertEqual(next(lines).text,
  346. add_line_prefix('+', boards4, expect, col.RED))
  347. # Now the warnings lines
  348. expect = [expect_str[0]] + expect_str[10:12] + [expect_str[9]]
  349. expect = '\n'.join(expect)
  350. self.assertEqual(next(lines).text,
  351. add_line_prefix('w+', boards4, expect, col.YELLOW))
  352. def testOutput(self):
  353. """Test basic builder operation and output
  354. This does a line-by-line verification of the summary output.
  355. """
  356. lines = self._SetupTest(show_errors=True)
  357. self._CheckOutput(lines, list_error_boards=False,
  358. filter_dtb_warnings=False)
  359. def testErrorBoards(self):
  360. """Test output with --list-error-boards
  361. This does a line-by-line verification of the summary output.
  362. """
  363. lines = self._SetupTest(show_errors=True, list_error_boards=True)
  364. self._CheckOutput(lines, list_error_boards=True)
  365. def testFilterDtb(self):
  366. """Test output with --filter-dtb-warnings
  367. This does a line-by-line verification of the summary output.
  368. """
  369. lines = self._SetupTest(show_errors=True, filter_dtb_warnings=True)
  370. self._CheckOutput(lines, filter_dtb_warnings=True)
  371. def testFilterMigration(self):
  372. """Test output with --filter-migration-warnings
  373. This does a line-by-line verification of the summary output.
  374. """
  375. lines = self._SetupTest(show_errors=True,
  376. filter_migration_warnings=True)
  377. self._CheckOutput(lines, filter_migration_warnings=True)
  378. def testSingleThread(self):
  379. """Test operation without threading"""
  380. lines = self._SetupTest(show_errors=True, threads=0)
  381. self._CheckOutput(lines, list_error_boards=False,
  382. filter_dtb_warnings=False)
  383. def _testGit(self):
  384. """Test basic builder operation by building a branch"""
  385. options = Options()
  386. options.git = os.getcwd()
  387. options.summary = False
  388. options.jobs = None
  389. options.dry_run = False
  390. #options.git = os.path.join(self.base_dir, 'repo')
  391. options.branch = 'test-buildman'
  392. options.force_build = False
  393. options.list_tool_chains = False
  394. options.count = -1
  395. options.git_dir = None
  396. options.threads = None
  397. options.show_unknown = False
  398. options.quick = False
  399. options.show_errors = False
  400. options.keep_outputs = False
  401. args = ['tegra20']
  402. control.do_buildman(options, args)
  403. def testBoardSingle(self):
  404. """Test single board selection"""
  405. self.assertEqual(self.brds.select_boards(['sandbox']),
  406. ({'all': ['board4'], 'sandbox': ['board4']}, []))
  407. def testBoardArch(self):
  408. """Test single board selection"""
  409. self.assertEqual(self.brds.select_boards(['arm']),
  410. ({'all': ['board0', 'board1'],
  411. 'arm': ['board0', 'board1']}, []))
  412. def testBoardArchSingle(self):
  413. """Test single board selection"""
  414. self.assertEqual(self.brds.select_boards(['arm sandbox']),
  415. ({'sandbox': ['board4'],
  416. 'all': ['board0', 'board1', 'board4'],
  417. 'arm': ['board0', 'board1']}, []))
  418. def testBoardArchSingleMultiWord(self):
  419. """Test single board selection"""
  420. self.assertEqual(self.brds.select_boards(['arm', 'sandbox']),
  421. ({'sandbox': ['board4'],
  422. 'all': ['board0', 'board1', 'board4'],
  423. 'arm': ['board0', 'board1']}, []))
  424. def testBoardSingleAnd(self):
  425. """Test single board selection"""
  426. self.assertEqual(self.brds.select_boards(['Tester & arm']),
  427. ({'Tester&arm': ['board0', 'board1'],
  428. 'all': ['board0', 'board1']}, []))
  429. def testBoardTwoAnd(self):
  430. """Test single board selection"""
  431. self.assertEqual(self.brds.select_boards(['Tester', '&', 'arm',
  432. 'Tester' '&', 'powerpc',
  433. 'sandbox']),
  434. ({'sandbox': ['board4'],
  435. 'all': ['board0', 'board1', 'board2', 'board3',
  436. 'board4'],
  437. 'Tester&powerpc': ['board2', 'board3'],
  438. 'Tester&arm': ['board0', 'board1']}, []))
  439. def testBoardAll(self):
  440. """Test single board selection"""
  441. self.assertEqual(self.brds.select_boards([]),
  442. ({'all': ['board0', 'board1', 'board2', 'board3',
  443. 'board4']}, []))
  444. def testBoardRegularExpression(self):
  445. """Test single board selection"""
  446. self.assertEqual(self.brds.select_boards(['T.*r&^Po']),
  447. ({'all': ['board2', 'board3'],
  448. 'T.*r&^Po': ['board2', 'board3']}, []))
  449. def testBoardDuplicate(self):
  450. """Test single board selection"""
  451. self.assertEqual(self.brds.select_boards(['sandbox sandbox',
  452. 'sandbox']),
  453. ({'all': ['board4'], 'sandbox': ['board4']}, []))
  454. def CheckDirs(self, build, dirname):
  455. self.assertEqual('base%s' % dirname, build.get_output_dir(1))
  456. self.assertEqual('base%s/fred' % dirname,
  457. build.get_build_dir(1, 'fred'))
  458. self.assertEqual('base%s/fred/done' % dirname,
  459. build.get_done_file(1, 'fred'))
  460. self.assertEqual('base%s/fred/u-boot.sizes' % dirname,
  461. build.get_func_sizes_file(1, 'fred', 'u-boot'))
  462. self.assertEqual('base%s/fred/u-boot.objdump' % dirname,
  463. build.get_objdump_file(1, 'fred', 'u-boot'))
  464. self.assertEqual('base%s/fred/err' % dirname,
  465. build.get_err_file(1, 'fred'))
  466. def testOutputDir(self):
  467. build = builder.Builder(self.toolchains, BASE_DIR, None, 1, 2,
  468. checkout=False, show_unknown=False)
  469. build.commits = self.commits
  470. build.commit_count = len(self.commits)
  471. subject = self.commits[1].subject.translate(builder.trans_valid_chars)
  472. dirname ='/%02d_g%s_%s' % (2, commits[1][0], subject[:20])
  473. self.CheckDirs(build, dirname)
  474. def testOutputDirCurrent(self):
  475. build = builder.Builder(self.toolchains, BASE_DIR, None, 1, 2,
  476. checkout=False, show_unknown=False)
  477. build.commits = None
  478. build.commit_count = 0
  479. self.CheckDirs(build, '/current')
  480. def testOutputDirNoSubdirs(self):
  481. build = builder.Builder(self.toolchains, BASE_DIR, None, 1, 2,
  482. checkout=False, show_unknown=False,
  483. no_subdirs=True)
  484. build.commits = None
  485. build.commit_count = 0
  486. self.CheckDirs(build, '')
  487. def testToolchainAliases(self):
  488. self.assertTrue(self.toolchains.Select('arm') != None)
  489. with self.assertRaises(ValueError):
  490. self.toolchains.Select('no-arch')
  491. with self.assertRaises(ValueError):
  492. self.toolchains.Select('x86')
  493. self.toolchains = toolchain.Toolchains()
  494. self.toolchains.Add('x86_64-linux-gcc', test=False)
  495. self.assertTrue(self.toolchains.Select('x86') != None)
  496. self.toolchains = toolchain.Toolchains()
  497. self.toolchains.Add('i386-linux-gcc', test=False)
  498. self.assertTrue(self.toolchains.Select('x86') != None)
  499. def testToolchainDownload(self):
  500. """Test that we can download toolchains"""
  501. if use_network:
  502. with test_util.capture_sys_output() as (stdout, stderr):
  503. url = self.toolchains.LocateArchUrl('arm')
  504. self.assertRegexpMatches(url, 'https://www.kernel.org/pub/tools/'
  505. 'crosstool/files/bin/x86_64/.*/'
  506. 'x86_64-gcc-.*-nolibc[-_]arm-.*linux-gnueabi.tar.xz')
  507. def testGetEnvArgs(self):
  508. """Test the GetEnvArgs() function"""
  509. tc = self.toolchains.Select('arm')
  510. self.assertEqual('arm-linux-',
  511. tc.GetEnvArgs(toolchain.VAR_CROSS_COMPILE))
  512. self.assertEqual('', tc.GetEnvArgs(toolchain.VAR_PATH))
  513. self.assertEqual('arm',
  514. tc.GetEnvArgs(toolchain.VAR_ARCH))
  515. self.assertEqual('', tc.GetEnvArgs(toolchain.VAR_MAKE_ARGS))
  516. self.toolchains.Add('/path/to/x86_64-linux-gcc', test=False)
  517. tc = self.toolchains.Select('x86')
  518. self.assertEqual('/path/to',
  519. tc.GetEnvArgs(toolchain.VAR_PATH))
  520. tc.override_toolchain = 'clang'
  521. self.assertEqual('HOSTCC=clang CC=clang',
  522. tc.GetEnvArgs(toolchain.VAR_MAKE_ARGS))
  523. def testPrepareOutputSpace(self):
  524. def _Touch(fname):
  525. tools.write_file(os.path.join(base_dir, fname), b'')
  526. base_dir = tempfile.mkdtemp()
  527. # Add various files that we want removed and left alone
  528. to_remove = ['01_g0982734987_title', '102_g92bf_title',
  529. '01_g2938abd8_title']
  530. to_leave = ['something_else', '01-something.patch', '01_another']
  531. for name in to_remove + to_leave:
  532. _Touch(name)
  533. build = builder.Builder(self.toolchains, base_dir, None, 1, 2)
  534. build.commits = self.commits
  535. build.commit_count = len(commits)
  536. result = set(build._get_output_space_removals())
  537. expected = set([os.path.join(base_dir, f) for f in to_remove])
  538. self.assertEqual(expected, result)
  539. def test_adjust_cfg_nop(self):
  540. """check various adjustments of config that are nops"""
  541. # enable an enabled CONFIG
  542. self.assertEqual(
  543. 'CONFIG_FRED=y',
  544. cfgutil.adjust_cfg_line('CONFIG_FRED=y', {'FRED':'FRED'})[0])
  545. # disable a disabled CONFIG
  546. self.assertEqual(
  547. '# CONFIG_FRED is not set',
  548. cfgutil.adjust_cfg_line(
  549. '# CONFIG_FRED is not set', {'FRED':'~FRED'})[0])
  550. # use the adjust_cfg_lines() function
  551. self.assertEqual(
  552. ['CONFIG_FRED=y'],
  553. cfgutil.adjust_cfg_lines(['CONFIG_FRED=y'], {'FRED':'FRED'}))
  554. self.assertEqual(
  555. ['# CONFIG_FRED is not set'],
  556. cfgutil.adjust_cfg_lines(['CONFIG_FRED=y'], {'FRED':'~FRED'}))
  557. # handling an empty line
  558. self.assertEqual('#', cfgutil.adjust_cfg_line('#', {'FRED':'~FRED'})[0])
  559. def test_adjust_cfg(self):
  560. """check various adjustments of config"""
  561. # disable a CONFIG
  562. self.assertEqual(
  563. '# CONFIG_FRED is not set',
  564. cfgutil.adjust_cfg_line('CONFIG_FRED=1' , {'FRED':'~FRED'})[0])
  565. # enable a disabled CONFIG
  566. self.assertEqual(
  567. 'CONFIG_FRED=y',
  568. cfgutil.adjust_cfg_line(
  569. '# CONFIG_FRED is not set', {'FRED':'FRED'})[0])
  570. # enable a CONFIG that doesn't exist
  571. self.assertEqual(
  572. ['CONFIG_FRED=y'],
  573. cfgutil.adjust_cfg_lines([], {'FRED':'FRED'}))
  574. # disable a CONFIG that doesn't exist
  575. self.assertEqual(
  576. ['# CONFIG_FRED is not set'],
  577. cfgutil.adjust_cfg_lines([], {'FRED':'~FRED'}))
  578. # disable a value CONFIG
  579. self.assertEqual(
  580. '# CONFIG_FRED is not set',
  581. cfgutil.adjust_cfg_line('CONFIG_FRED="fred"' , {'FRED':'~FRED'})[0])
  582. # setting a value CONFIG
  583. self.assertEqual(
  584. 'CONFIG_FRED="fred"',
  585. cfgutil.adjust_cfg_line('# CONFIG_FRED is not set' ,
  586. {'FRED':'FRED="fred"'})[0])
  587. # changing a value CONFIG
  588. self.assertEqual(
  589. 'CONFIG_FRED="fred"',
  590. cfgutil.adjust_cfg_line('CONFIG_FRED="ernie"' ,
  591. {'FRED':'FRED="fred"'})[0])
  592. # setting a value for a CONFIG that doesn't exist
  593. self.assertEqual(
  594. ['CONFIG_FRED="fred"'],
  595. cfgutil.adjust_cfg_lines([], {'FRED':'FRED="fred"'}))
  596. def test_convert_adjust_cfg_list(self):
  597. """Check conversion of the list of changes into a dict"""
  598. self.assertEqual({}, cfgutil.convert_list_to_dict(None))
  599. expect = {
  600. 'FRED':'FRED',
  601. 'MARY':'~MARY',
  602. 'JOHN':'JOHN=0x123',
  603. 'ALICE':'ALICE="alice"',
  604. 'AMY':'AMY',
  605. 'ABE':'~ABE',
  606. 'MARK':'MARK=0x456',
  607. 'ANNA':'ANNA="anna"',
  608. }
  609. actual = cfgutil.convert_list_to_dict(
  610. ['FRED', '~MARY', 'JOHN=0x123', 'ALICE="alice"',
  611. 'CONFIG_AMY', '~CONFIG_ABE', 'CONFIG_MARK=0x456',
  612. 'CONFIG_ANNA="anna"'])
  613. self.assertEqual(expect, actual)
  614. def test_check_cfg_file(self):
  615. """Test check_cfg_file detects conflicts as expected"""
  616. # Check failure to disable CONFIG
  617. result = cfgutil.check_cfg_lines(['CONFIG_FRED=1'], {'FRED':'~FRED'})
  618. self.assertEqual([['~FRED', 'CONFIG_FRED=1']], result)
  619. result = cfgutil.check_cfg_lines(
  620. ['CONFIG_FRED=1', 'CONFIG_MARY="mary"'], {'FRED':'~FRED'})
  621. self.assertEqual([['~FRED', 'CONFIG_FRED=1']], result)
  622. result = cfgutil.check_cfg_lines(
  623. ['CONFIG_FRED=1', 'CONFIG_MARY="mary"'], {'MARY':'~MARY'})
  624. self.assertEqual([['~MARY', 'CONFIG_MARY="mary"']], result)
  625. # Check failure to enable CONFIG
  626. result = cfgutil.check_cfg_lines(
  627. ['# CONFIG_FRED is not set'], {'FRED':'FRED'})
  628. self.assertEqual([['FRED', '# CONFIG_FRED is not set']], result)
  629. # Check failure to set CONFIG value
  630. result = cfgutil.check_cfg_lines(
  631. ['# CONFIG_FRED is not set', 'CONFIG_MARY="not"'],
  632. {'MARY':'MARY="mary"', 'FRED':'FRED'})
  633. self.assertEqual([
  634. ['FRED', '# CONFIG_FRED is not set'],
  635. ['MARY="mary"', 'CONFIG_MARY="not"']], result)
  636. # Check failure to add CONFIG value
  637. result = cfgutil.check_cfg_lines([], {'MARY':'MARY="mary"'})
  638. self.assertEqual([
  639. ['MARY="mary"', 'Missing expected line: CONFIG_MARY="mary"']], result)
  640. if __name__ == "__main__":
  641. unittest.main()