test.py 26 KB

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