blob: 5eed013d51c4ee02e3fd1cfdf9d2fd6f50e07d6f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glassc05694f2013-04-03 11:07:16 +00002# Copyright (c) 2012 The Chromium OS Authors.
3#
Simon Glassc05694f2013-04-03 11:07:16 +00004
Simon Glassc229d322024-06-23 11:55:15 -06005from filelock import FileLock
Simon Glassc05694f2013-04-03 11:07:16 +00006import os
7import shutil
8import sys
9import tempfile
10import time
11import unittest
Simon Glassc229d322024-06-23 11:55:15 -060012from unittest.mock import patch
Simon Glassc05694f2013-04-03 11:07:16 +000013
Simon Glassf0d9c102020-04-17 18:09:02 -060014from buildman import board
Simon Glass20751d62022-07-11 19:04:03 -060015from buildman import boards
Simon Glassf0d9c102020-04-17 18:09:02 -060016from buildman import bsettings
17from buildman import builder
Simon Glass22901f92022-01-22 05:07:31 -070018from buildman import cfgutil
Simon Glassf0d9c102020-04-17 18:09:02 -060019from buildman import control
20from buildman import toolchain
Simon Glassa997ea52020-04-17 18:09:04 -060021from patman import commit
Simon Glass131444f2023-02-23 18:18:04 -070022from u_boot_pylib import command
23from u_boot_pylib import terminal
24from u_boot_pylib import test_util
25from u_boot_pylib import tools
Simon Glassc05694f2013-04-03 11:07:16 +000026
Simon Glass2bfc6972017-11-12 21:52:14 -070027use_network = True
28
Simon Glass4bea81e2014-12-01 17:34:04 -070029settings_data = '''
30# Buildman settings file
31
32[toolchain]
33main: /usr/sbin
34
35[toolchain-alias]
36x86: i386 x86_64
37'''
38
Simon Glassf4ebfba2020-04-09 15:08:53 -060039migration = '''===================== WARNING ======================
40This board does not use CONFIG_DM. CONFIG_DM will be
41compulsory starting with the v2020.01 release.
42Failure to update may result in board removal.
Johannes Krottmayerce53c8b2022-03-01 04:49:51 +010043See doc/develop/driver-model/migration.rst for more info.
Simon Glassf4ebfba2020-04-09 15:08:53 -060044====================================================
45'''
46
Simon Glassc05694f2013-04-03 11:07:16 +000047errors = [
48 '''main.c: In function 'main_loop':
49main.c:260:6: warning: unused variable 'joe' [-Wunused-variable]
50''',
Simon Glass7fb8aa22014-09-05 19:00:08 -060051 '''main.c: In function 'main_loop2':
Simon Glassc05694f2013-04-03 11:07:16 +000052main.c:295:2: error: 'fred' undeclared (first use in this function)
53main.c:295:2: note: each undeclared identifier is reported only once for each function it appears in
54make[1]: *** [main.o] Error 1
55make: *** [common/libcommon.o] Error 2
56Make failed
57''',
Simon Glass0db94432018-11-06 16:02:11 -070058 '''arch/arm/dts/socfpga_arria10_socdk_sdmmc.dtb: Warning \
59(avoid_unnecessary_addr_size): /clocks: unnecessary #address-cells/#size-cells \
60without "ranges" or child "reg" property
Simon Glassc05694f2013-04-03 11:07:16 +000061''',
62 '''powerpc-linux-ld: warning: dot moved backwards before `.bss'
63powerpc-linux-ld: warning: dot moved backwards before `.bss'
64powerpc-linux-ld: u-boot: section .text lma 0xfffc0000 overlaps previous sections
65powerpc-linux-ld: u-boot: section .rodata lma 0xfffef3ec overlaps previous sections
66powerpc-linux-ld: u-boot: section .reloc lma 0xffffa400 overlaps previous sections
67powerpc-linux-ld: u-boot: section .data lma 0xffffcd38 overlaps previous sections
68powerpc-linux-ld: u-boot: section .u_boot_cmd lma 0xffffeb40 overlaps previous sections
69powerpc-linux-ld: u-boot: section .bootpg lma 0xfffff198 overlaps previous sections
Simon Glassa8b7b1b2014-09-05 19:00:21 -060070''',
71 '''In file included from %(basedir)sarch/sandbox/cpu/cpu.c:9: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%(basedir)sarch/sandbox/cpu/cpu.c: In function 'do_reset':
75%(basedir)sarch/sandbox/cpu/cpu.c:27:1: error: unknown type name 'blah'
76%(basedir)sarch/sandbox/cpu/cpu.c:28:12: error: expected declaration specifiers or '...' before numeric constant
77make[2]: *** [arch/sandbox/cpu/cpu.o] Error 1
78make[1]: *** [arch/sandbox/cpu] Error 2
79make[1]: *** Waiting for unfinished jobs....
80In file included from %(basedir)scommon/board_f.c:55:0:
81%(basedir)sarch/sandbox/include/asm/state.h:44:0: warning: "xxxx" redefined [enabled by default]
82%(basedir)sarch/sandbox/include/asm/state.h:43:0: note: this is the location of the previous definition
83make: *** [sub-make] Error 2
Simon Glassc05694f2013-04-03 11:07:16 +000084'''
85]
86
87
88# hash, subject, return code, list of errors/warnings
89commits = [
Simon Glassf4ebfba2020-04-09 15:08:53 -060090 ['1234', 'upstream/master, migration warning', 0, []],
Simon Glassc05694f2013-04-03 11:07:16 +000091 ['5678', 'Second commit, a warning', 0, errors[0:1]],
92 ['9012', 'Third commit, error', 1, errors[0:2]],
93 ['3456', 'Fourth commit, warning', 0, [errors[0], errors[2]]],
94 ['7890', 'Fifth commit, link errors', 1, [errors[0], errors[3]]],
Simon Glassa8b7b1b2014-09-05 19:00:21 -060095 ['abcd', 'Sixth commit, fixes all errors', 0, []],
Simon Glassf4ebfba2020-04-09 15:08:53 -060096 ['ef01', 'Seventh commit, fix migration, check directory suppression', 1,
97 [errors[4]]],
Simon Glassc05694f2013-04-03 11:07:16 +000098]
99
Simon Glassb0786df2022-07-11 19:03:59 -0600100BOARDS = [
Simon Glass584cf862013-09-23 17:35:16 -0600101 ['Active', 'arm', 'armv7', '', 'Tester', 'ARM Board 1', 'board0', ''],
102 ['Active', 'arm', 'armv7', '', 'Tester', 'ARM Board 2', 'board1', ''],
103 ['Active', 'powerpc', 'powerpc', '', 'Tester', 'PowerPC board 1', 'board2', ''],
Simon Glass5e0f06d2017-11-12 21:52:15 -0700104 ['Active', 'powerpc', 'mpc83xx', '', 'Tester', 'PowerPC board 2', 'board3', ''],
Simon Glass584cf862013-09-23 17:35:16 -0600105 ['Active', 'sandbox', 'sandbox', '', 'Tester', 'Sandbox board', 'board4', ''],
Simon Glassc05694f2013-04-03 11:07:16 +0000106]
107
Simon Glass2e1646c2014-12-01 17:33:51 -0700108BASE_DIR = 'base'
109
Simon Glass071a1782018-11-06 16:02:13 -0700110OUTCOME_OK, OUTCOME_WARN, OUTCOME_ERR = range(3)
111
Simon Glassc05694f2013-04-03 11:07:16 +0000112class Options:
113 """Class that holds build options"""
114 pass
115
116class TestBuild(unittest.TestCase):
117 """Test buildman
118
119 TODO: Write tests for the rest of the functionality
120 """
121 def setUp(self):
122 # Set up commits to build
123 self.commits = []
124 sequence = 0
125 for commit_info in commits:
126 comm = commit.Commit(commit_info[0])
127 comm.subject = commit_info[1]
128 comm.return_code = commit_info[2]
129 comm.error_list = commit_info[3]
Simon Glassf4ebfba2020-04-09 15:08:53 -0600130 if sequence < 6:
131 comm.error_list += [migration]
Simon Glassc05694f2013-04-03 11:07:16 +0000132 comm.sequence = sequence
133 sequence += 1
134 self.commits.append(comm)
135
136 # Set up boards to build
Simon Glass20751d62022-07-11 19:04:03 -0600137 self.brds = boards.Boards()
Simon Glassb0786df2022-07-11 19:03:59 -0600138 for brd in BOARDS:
Simon Glass127a2392022-07-11 19:04:02 -0600139 self.brds.add_board(board.Board(*brd))
140 self.brds.select_boards([])
Simon Glassc05694f2013-04-03 11:07:16 +0000141
Simon Glass4bea81e2014-12-01 17:34:04 -0700142 # Add some test settings
Simon Glass06b83a52023-07-19 17:49:05 -0600143 bsettings.setup(None)
144 bsettings.add_file(settings_data)
Simon Glass4bea81e2014-12-01 17:34:04 -0700145
Simon Glassc05694f2013-04-03 11:07:16 +0000146 # Set up the toolchains
Simon Glassc05694f2013-04-03 11:07:16 +0000147 self.toolchains = toolchain.Toolchains()
148 self.toolchains.Add('arm-linux-gcc', test=False)
149 self.toolchains.Add('sparc-linux-gcc', test=False)
150 self.toolchains.Add('powerpc-linux-gcc', test=False)
Simon Glass038a3dd2024-08-15 13:57:44 -0600151 self.toolchains.Add('/path/to/aarch64-linux-gcc', test=False)
Simon Glassc05694f2013-04-03 11:07:16 +0000152 self.toolchains.Add('gcc', test=False)
153
Simon Glass7fb8aa22014-09-05 19:00:08 -0600154 # Avoid sending any output
Simon Glass02811582022-01-29 14:14:18 -0700155 terminal.set_print_test_mode()
Simon Glass7fb8aa22014-09-05 19:00:08 -0600156 self._col = terminal.Color()
157
Simon Glass09bbfcd2020-04-09 15:08:31 -0600158 self.base_dir = tempfile.mkdtemp()
159 if not os.path.isdir(self.base_dir):
160 os.mkdir(self.base_dir)
Simon Glassa8b7b1b2014-09-05 19:00:21 -0600161
Simon Glassc229d322024-06-23 11:55:15 -0600162 self.cur_time = 0
163 self.valid_pids = []
164 self.finish_time = None
165 self.finish_pid = None
166
Simon Glass09bbfcd2020-04-09 15:08:31 -0600167 def tearDown(self):
168 shutil.rmtree(self.base_dir)
169
170 def Make(self, commit, brd, stage, *args, **kwargs):
Simon Glassc05694f2013-04-03 11:07:16 +0000171 result = command.CommandResult()
172 boardnum = int(brd.target[-1])
173 result.return_code = 0
174 result.stderr = ''
175 result.stdout = ('This is the test output for board %s, commit %s' %
176 (brd.target, commit.hash))
Simon Glassa8b7b1b2014-09-05 19:00:21 -0600177 if ((boardnum >= 1 and boardnum >= commit.sequence) or
178 boardnum == 4 and commit.sequence == 6):
Simon Glassc05694f2013-04-03 11:07:16 +0000179 result.return_code = commit.return_code
Simon Glassa8b7b1b2014-09-05 19:00:21 -0600180 result.stderr = (''.join(commit.error_list)
Simon Glass09bbfcd2020-04-09 15:08:31 -0600181 % {'basedir' : self.base_dir + '/.bm-work/00/'})
Simon Glassf4ebfba2020-04-09 15:08:53 -0600182 elif commit.sequence < 6:
183 result.stderr = migration
Simon Glassc05694f2013-04-03 11:07:16 +0000184
185 result.combined = result.stdout + result.stderr
186 return result
187
Simon Glassb0786df2022-07-11 19:03:59 -0600188 def assertSummary(self, text, arch, plus, brds, outcome=OUTCOME_ERR):
Simon Glass7fb8aa22014-09-05 19:00:08 -0600189 col = self._col
Simon Glass071a1782018-11-06 16:02:13 -0700190 expected_colour = (col.GREEN if outcome == OUTCOME_OK else
191 col.YELLOW if outcome == OUTCOME_WARN else col.RED)
Simon Glass7fb8aa22014-09-05 19:00:08 -0600192 expect = '%10s: ' % arch
193 # TODO(sjg@chromium.org): If plus is '', we shouldn't need this
Simon Glassf45d3742022-01-29 14:14:17 -0700194 expect += ' ' + col.build(expected_colour, plus)
Simon Glass7fb8aa22014-09-05 19:00:08 -0600195 expect += ' '
Simon Glassb0786df2022-07-11 19:03:59 -0600196 for brd in brds:
Simon Glass8132f982022-07-11 19:03:57 -0600197 expect += col.build(expected_colour, ' %s' % brd)
Simon Glass7fb8aa22014-09-05 19:00:08 -0600198 self.assertEqual(text, expect)
199
Simon Glassc635d892021-01-30 22:17:46 -0700200 def _SetupTest(self, echo_lines=False, threads=1, **kwdisplay_args):
Simon Glass724c1752020-04-09 15:08:32 -0600201 """Set up the test by running a build and summary
Simon Glass7fb8aa22014-09-05 19:00:08 -0600202
Simon Glass724c1752020-04-09 15:08:32 -0600203 Args:
204 echo_lines: True to echo lines to the terminal to aid test
205 development
Heinrich Schuchardt37153112024-04-19 13:37:46 +0200206 kwdisplay_args: Dict of arguments to pass to
Simon Glass724c1752020-04-09 15:08:32 -0600207 Builder.SetDisplayOptions()
208
209 Returns:
210 Iterator containing the output lines, each a PrintLine() object
Simon Glass7fb8aa22014-09-05 19:00:08 -0600211 """
Simon Glassc635d892021-01-30 22:17:46 -0700212 build = builder.Builder(self.toolchains, self.base_dir, None, threads,
213 2, checkout=False, show_unknown=False)
Simon Glassc05694f2013-04-03 11:07:16 +0000214 build.do_make = self.Make
Simon Glass127a2392022-07-11 19:04:02 -0600215 board_selected = self.brds.get_selected_dict()
Simon Glassc05694f2013-04-03 11:07:16 +0000216
Simon Glass071a1782018-11-06 16:02:13 -0700217 # Build the boards for the pre-defined commits and warnings/errors
218 # associated with each. This calls our Make() to inject the fake output.
Simon Glassbc74d942023-07-19 17:49:06 -0600219 build.build_boards(self.commits, board_selected, keep_outputs=False,
220 verbose=False)
Simon Glass02811582022-01-29 14:14:18 -0700221 lines = terminal.get_print_test_lines()
Simon Glass7fb8aa22014-09-05 19:00:08 -0600222 count = 0
223 for line in lines:
224 if line.text.strip():
225 count += 1
226
Simon Glass726ae812020-04-09 15:08:47 -0600227 # We should get two starting messages, an update for every commit built
228 # and a summary message
Simon Glassb0786df2022-07-11 19:03:59 -0600229 self.assertEqual(count, len(commits) * len(BOARDS) + 3)
Simon Glassbc74d942023-07-19 17:49:06 -0600230 build.set_display_options(**kwdisplay_args);
231 build.show_summary(self.commits, board_selected)
Simon Glass724c1752020-04-09 15:08:32 -0600232 if echo_lines:
Simon Glass02811582022-01-29 14:14:18 -0700233 terminal.echo_print_test_lines()
234 return iter(terminal.get_print_test_lines())
Simon Glass071a1782018-11-06 16:02:13 -0700235
Simon Glassf4ebfba2020-04-09 15:08:53 -0600236 def _CheckOutput(self, lines, list_error_boards=False,
237 filter_dtb_warnings=False,
238 filter_migration_warnings=False):
Simon Glass724c1752020-04-09 15:08:32 -0600239 """Check for expected output from the build summary
240
241 Args:
242 lines: Iterator containing the lines returned from the summary
Simon Glassdacbe1e2020-04-09 15:08:34 -0600243 list_error_boards: Adjust the check for output produced with the
244 --list-error-boards flag
Simon Glass9ea93812020-04-09 15:08:52 -0600245 filter_dtb_warnings: Adjust the check for output produced with the
246 --filter-dtb-warnings flag
Simon Glass724c1752020-04-09 15:08:32 -0600247 """
Simon Glassb0786df2022-07-11 19:03:59 -0600248 def add_line_prefix(prefix, brds, error_str, colour):
Simon Glass6643eb42020-04-09 15:08:33 -0600249 """Add a prefix to each line of a string
250
251 The training \n in error_str is removed before processing
252
253 Args:
254 prefix: String prefix to add
255 error_str: Error string containing the lines
Simon Glassea49f9b2020-04-09 15:08:37 -0600256 colour: Expected colour for the line. Note that the board list,
257 if present, always appears in magenta
Simon Glass6643eb42020-04-09 15:08:33 -0600258
259 Returns:
260 New string where each line has the prefix added
261 """
262 lines = error_str.strip().splitlines()
Simon Glassea49f9b2020-04-09 15:08:37 -0600263 new_lines = []
264 for line in lines:
Simon Glassb0786df2022-07-11 19:03:59 -0600265 if brds:
Simon Glassf45d3742022-01-29 14:14:17 -0700266 expect = self._col.build(colour, prefix + '(')
Simon Glassb0786df2022-07-11 19:03:59 -0600267 expect += self._col.build(self._col.MAGENTA, brds,
Simon Glassea49f9b2020-04-09 15:08:37 -0600268 bright=False)
Simon Glassf45d3742022-01-29 14:14:17 -0700269 expect += self._col.build(colour, ') %s' % line)
Simon Glassea49f9b2020-04-09 15:08:37 -0600270 else:
Simon Glassf45d3742022-01-29 14:14:17 -0700271 expect = self._col.build(colour, prefix + line)
Simon Glassea49f9b2020-04-09 15:08:37 -0600272 new_lines.append(expect)
Simon Glass6643eb42020-04-09 15:08:33 -0600273 return '\n'.join(new_lines)
274
Simon Glassf4ebfba2020-04-09 15:08:53 -0600275 col = terminal.Color()
276 boards01234 = ('board0 board1 board2 board3 board4'
277 if list_error_boards else '')
Simon Glass070589b2020-04-09 15:08:38 -0600278 boards1234 = 'board1 board2 board3 board4' if list_error_boards else ''
279 boards234 = 'board2 board3 board4' if list_error_boards else ''
280 boards34 = 'board3 board4' if list_error_boards else ''
Simon Glassdacbe1e2020-04-09 15:08:34 -0600281 boards4 = 'board4' if list_error_boards else ''
282
Simon Glassf4ebfba2020-04-09 15:08:53 -0600283 # Upstream commit: migration warnings only
Simon Glass575ce512020-04-09 15:08:30 -0600284 self.assertEqual(next(lines).text, '01: %s' % commits[0][1])
Simon Glass071a1782018-11-06 16:02:13 -0700285
Simon Glassf4ebfba2020-04-09 15:08:53 -0600286 if not filter_migration_warnings:
287 self.assertSummary(next(lines).text, 'arm', 'w+',
288 ['board0', 'board1'], outcome=OUTCOME_WARN)
289 self.assertSummary(next(lines).text, 'powerpc', 'w+',
290 ['board2', 'board3'], outcome=OUTCOME_WARN)
291 self.assertSummary(next(lines).text, 'sandbox', 'w+', ['board4'],
292 outcome=OUTCOME_WARN)
293
294 self.assertEqual(next(lines).text,
295 add_line_prefix('+', boards01234, migration, col.RED))
296
Simon Glass071a1782018-11-06 16:02:13 -0700297 # Second commit: all archs should fail with warnings
Simon Glass575ce512020-04-09 15:08:30 -0600298 self.assertEqual(next(lines).text, '02: %s' % commits[1][1])
Simon Glass7fb8aa22014-09-05 19:00:08 -0600299
Simon Glassf4ebfba2020-04-09 15:08:53 -0600300 if filter_migration_warnings:
301 self.assertSummary(next(lines).text, 'arm', 'w+',
302 ['board1'], outcome=OUTCOME_WARN)
303 self.assertSummary(next(lines).text, 'powerpc', 'w+',
304 ['board2', 'board3'], outcome=OUTCOME_WARN)
305 self.assertSummary(next(lines).text, 'sandbox', 'w+', ['board4'],
306 outcome=OUTCOME_WARN)
Simon Glass7fb8aa22014-09-05 19:00:08 -0600307
Simon Glass071a1782018-11-06 16:02:13 -0700308 # Second commit: The warnings should be listed
Simon Glassea49f9b2020-04-09 15:08:37 -0600309 self.assertEqual(next(lines).text,
310 add_line_prefix('w+', boards1234, errors[0], col.YELLOW))
Simon Glass7fb8aa22014-09-05 19:00:08 -0600311
Simon Glass071a1782018-11-06 16:02:13 -0700312 # Third commit: Still fails
Simon Glass575ce512020-04-09 15:08:30 -0600313 self.assertEqual(next(lines).text, '03: %s' % commits[2][1])
Simon Glassf4ebfba2020-04-09 15:08:53 -0600314 if filter_migration_warnings:
315 self.assertSummary(next(lines).text, 'arm', '',
316 ['board1'], outcome=OUTCOME_OK)
Simon Glass575ce512020-04-09 15:08:30 -0600317 self.assertSummary(next(lines).text, 'powerpc', '+',
318 ['board2', 'board3'])
319 self.assertSummary(next(lines).text, 'sandbox', '+', ['board4'])
Simon Glass7fb8aa22014-09-05 19:00:08 -0600320
Simon Glass071a1782018-11-06 16:02:13 -0700321 # Expect a compiler error
Simon Glassea49f9b2020-04-09 15:08:37 -0600322 self.assertEqual(next(lines).text,
323 add_line_prefix('+', boards234, errors[1], col.RED))
Simon Glass7fb8aa22014-09-05 19:00:08 -0600324
Simon Glass071a1782018-11-06 16:02:13 -0700325 # Fourth commit: Compile errors are fixed, just have warning for board3
Simon Glass575ce512020-04-09 15:08:30 -0600326 self.assertEqual(next(lines).text, '04: %s' % commits[3][1])
Simon Glassf4ebfba2020-04-09 15:08:53 -0600327 if filter_migration_warnings:
328 expect = '%10s: ' % 'powerpc'
Simon Glassf45d3742022-01-29 14:14:17 -0700329 expect += ' ' + col.build(col.GREEN, '')
Simon Glassf4ebfba2020-04-09 15:08:53 -0600330 expect += ' '
Simon Glassf45d3742022-01-29 14:14:17 -0700331 expect += col.build(col.GREEN, ' %s' % 'board2')
332 expect += ' ' + col.build(col.YELLOW, 'w+')
Simon Glassf4ebfba2020-04-09 15:08:53 -0600333 expect += ' '
Simon Glassf45d3742022-01-29 14:14:17 -0700334 expect += col.build(col.YELLOW, ' %s' % 'board3')
Simon Glassf4ebfba2020-04-09 15:08:53 -0600335 self.assertEqual(next(lines).text, expect)
336 else:
337 self.assertSummary(next(lines).text, 'powerpc', 'w+',
338 ['board2', 'board3'], outcome=OUTCOME_WARN)
Simon Glass575ce512020-04-09 15:08:30 -0600339 self.assertSummary(next(lines).text, 'sandbox', 'w+', ['board4'],
Simon Glassf4ebfba2020-04-09 15:08:53 -0600340 outcome=OUTCOME_WARN)
Simon Glass7fb8aa22014-09-05 19:00:08 -0600341
342 # Compile error fixed
Simon Glassea49f9b2020-04-09 15:08:37 -0600343 self.assertEqual(next(lines).text,
344 add_line_prefix('-', boards234, errors[1], col.GREEN))
Simon Glass7fb8aa22014-09-05 19:00:08 -0600345
Simon Glass9ea93812020-04-09 15:08:52 -0600346 if not filter_dtb_warnings:
347 self.assertEqual(
348 next(lines).text,
349 add_line_prefix('w+', boards34, errors[2], col.YELLOW))
Simon Glass7fb8aa22014-09-05 19:00:08 -0600350
Simon Glass071a1782018-11-06 16:02:13 -0700351 # Fifth commit
Simon Glass575ce512020-04-09 15:08:30 -0600352 self.assertEqual(next(lines).text, '05: %s' % commits[4][1])
Simon Glassf4ebfba2020-04-09 15:08:53 -0600353 if filter_migration_warnings:
354 self.assertSummary(next(lines).text, 'powerpc', '', ['board3'],
355 outcome=OUTCOME_OK)
Simon Glass575ce512020-04-09 15:08:30 -0600356 self.assertSummary(next(lines).text, 'sandbox', '+', ['board4'])
Simon Glass7fb8aa22014-09-05 19:00:08 -0600357
358 # The second line of errors[3] is a duplicate, so buildman will drop it
359 expect = errors[3].rstrip().split('\n')
360 expect = [expect[0]] + expect[2:]
Simon Glass6643eb42020-04-09 15:08:33 -0600361 expect = '\n'.join(expect)
Simon Glassea49f9b2020-04-09 15:08:37 -0600362 self.assertEqual(next(lines).text,
363 add_line_prefix('+', boards4, expect, col.RED))
Simon Glass7fb8aa22014-09-05 19:00:08 -0600364
Simon Glass9ea93812020-04-09 15:08:52 -0600365 if not filter_dtb_warnings:
366 self.assertEqual(
367 next(lines).text,
368 add_line_prefix('w-', boards34, errors[2], col.CYAN))
Simon Glass7fb8aa22014-09-05 19:00:08 -0600369
Simon Glass071a1782018-11-06 16:02:13 -0700370 # Sixth commit
Simon Glass575ce512020-04-09 15:08:30 -0600371 self.assertEqual(next(lines).text, '06: %s' % commits[5][1])
Simon Glassf4ebfba2020-04-09 15:08:53 -0600372 if filter_migration_warnings:
373 self.assertSummary(next(lines).text, 'sandbox', '', ['board4'],
374 outcome=OUTCOME_OK)
375 else:
376 self.assertSummary(next(lines).text, 'sandbox', 'w+', ['board4'],
377 outcome=OUTCOME_WARN)
Simon Glass7fb8aa22014-09-05 19:00:08 -0600378
379 # The second line of errors[3] is a duplicate, so buildman will drop it
380 expect = errors[3].rstrip().split('\n')
381 expect = [expect[0]] + expect[2:]
Simon Glass6643eb42020-04-09 15:08:33 -0600382 expect = '\n'.join(expect)
Simon Glassea49f9b2020-04-09 15:08:37 -0600383 self.assertEqual(next(lines).text,
384 add_line_prefix('-', boards4, expect, col.GREEN))
385 self.assertEqual(next(lines).text,
386 add_line_prefix('w-', boards4, errors[0], col.CYAN))
Simon Glass7fb8aa22014-09-05 19:00:08 -0600387
Simon Glass071a1782018-11-06 16:02:13 -0700388 # Seventh commit
Simon Glass575ce512020-04-09 15:08:30 -0600389 self.assertEqual(next(lines).text, '07: %s' % commits[6][1])
Simon Glassf4ebfba2020-04-09 15:08:53 -0600390 if filter_migration_warnings:
391 self.assertSummary(next(lines).text, 'sandbox', '+', ['board4'])
392 else:
393 self.assertSummary(next(lines).text, 'arm', '', ['board0', 'board1'],
394 outcome=OUTCOME_OK)
395 self.assertSummary(next(lines).text, 'powerpc', '',
396 ['board2', 'board3'], outcome=OUTCOME_OK)
397 self.assertSummary(next(lines).text, 'sandbox', '+', ['board4'])
Simon Glassa8b7b1b2014-09-05 19:00:21 -0600398
399 # Pick out the correct error lines
400 expect_str = errors[4].rstrip().replace('%(basedir)s', '').split('\n')
401 expect = expect_str[3:8] + [expect_str[-1]]
Simon Glass6643eb42020-04-09 15:08:33 -0600402 expect = '\n'.join(expect)
Simon Glassf4ebfba2020-04-09 15:08:53 -0600403 if not filter_migration_warnings:
404 self.assertEqual(
405 next(lines).text,
406 add_line_prefix('-', boards01234, migration, col.GREEN))
407
Simon Glassea49f9b2020-04-09 15:08:37 -0600408 self.assertEqual(next(lines).text,
409 add_line_prefix('+', boards4, expect, col.RED))
Simon Glassa8b7b1b2014-09-05 19:00:21 -0600410
411 # Now the warnings lines
412 expect = [expect_str[0]] + expect_str[10:12] + [expect_str[9]]
Simon Glass6643eb42020-04-09 15:08:33 -0600413 expect = '\n'.join(expect)
Simon Glassea49f9b2020-04-09 15:08:37 -0600414 self.assertEqual(next(lines).text,
415 add_line_prefix('w+', boards4, expect, col.YELLOW))
Simon Glassa8b7b1b2014-09-05 19:00:21 -0600416
Simon Glass724c1752020-04-09 15:08:32 -0600417 def testOutput(self):
418 """Test basic builder operation and output
419
420 This does a line-by-line verification of the summary output.
421 """
422 lines = self._SetupTest(show_errors=True)
Simon Glass9ea93812020-04-09 15:08:52 -0600423 self._CheckOutput(lines, list_error_boards=False,
424 filter_dtb_warnings=False)
Simon Glassdacbe1e2020-04-09 15:08:34 -0600425
426 def testErrorBoards(self):
427 """Test output with --list-error-boards
428
429 This does a line-by-line verification of the summary output.
430 """
431 lines = self._SetupTest(show_errors=True, list_error_boards=True)
Simon Glassf4ebfba2020-04-09 15:08:53 -0600432 self._CheckOutput(lines, list_error_boards=True)
Simon Glass9ea93812020-04-09 15:08:52 -0600433
434 def testFilterDtb(self):
435 """Test output with --filter-dtb-warnings
436
437 This does a line-by-line verification of the summary output.
438 """
439 lines = self._SetupTest(show_errors=True, filter_dtb_warnings=True)
Simon Glassf4ebfba2020-04-09 15:08:53 -0600440 self._CheckOutput(lines, filter_dtb_warnings=True)
441
442 def testFilterMigration(self):
443 """Test output with --filter-migration-warnings
444
445 This does a line-by-line verification of the summary output.
446 """
447 lines = self._SetupTest(show_errors=True,
448 filter_migration_warnings=True)
449 self._CheckOutput(lines, filter_migration_warnings=True)
Simon Glass724c1752020-04-09 15:08:32 -0600450
Simon Glassc635d892021-01-30 22:17:46 -0700451 def testSingleThread(self):
452 """Test operation without threading"""
453 lines = self._SetupTest(show_errors=True, threads=0)
454 self._CheckOutput(lines, list_error_boards=False,
455 filter_dtb_warnings=False)
456
Simon Glassc05694f2013-04-03 11:07:16 +0000457 def _testGit(self):
458 """Test basic builder operation by building a branch"""
Simon Glassc05694f2013-04-03 11:07:16 +0000459 options = Options()
460 options.git = os.getcwd()
461 options.summary = False
462 options.jobs = None
463 options.dry_run = False
Simon Glass09bbfcd2020-04-09 15:08:31 -0600464 #options.git = os.path.join(self.base_dir, 'repo')
Simon Glassc05694f2013-04-03 11:07:16 +0000465 options.branch = 'test-buildman'
466 options.force_build = False
467 options.list_tool_chains = False
468 options.count = -1
469 options.git_dir = None
470 options.threads = None
471 options.show_unknown = False
472 options.quick = False
473 options.show_errors = False
474 options.keep_outputs = False
475 args = ['tegra20']
Simon Glassc1e1e1d2023-07-19 17:48:30 -0600476 control.do_buildman(options, args)
Simon Glassc05694f2013-04-03 11:07:16 +0000477
Simon Glassaa40f9a2014-08-09 15:33:08 -0600478 def testBoardSingle(self):
479 """Test single board selection"""
Simon Glass127a2392022-07-11 19:04:02 -0600480 self.assertEqual(self.brds.select_boards(['sandbox']),
Simon Glassd9eb9f02018-06-11 23:26:46 -0600481 ({'all': ['board4'], 'sandbox': ['board4']}, []))
Simon Glassaa40f9a2014-08-09 15:33:08 -0600482
483 def testBoardArch(self):
484 """Test single board selection"""
Simon Glass127a2392022-07-11 19:04:02 -0600485 self.assertEqual(self.brds.select_boards(['arm']),
Simon Glassd9eb9f02018-06-11 23:26:46 -0600486 ({'all': ['board0', 'board1'],
487 'arm': ['board0', 'board1']}, []))
Simon Glassaa40f9a2014-08-09 15:33:08 -0600488
489 def testBoardArchSingle(self):
490 """Test single board selection"""
Simon Glass127a2392022-07-11 19:04:02 -0600491 self.assertEqual(self.brds.select_boards(['arm sandbox']),
Simon Glassd9eb9f02018-06-11 23:26:46 -0600492 ({'sandbox': ['board4'],
Simon Glass5e0f06d2017-11-12 21:52:15 -0700493 'all': ['board0', 'board1', 'board4'],
Simon Glassd9eb9f02018-06-11 23:26:46 -0600494 'arm': ['board0', 'board1']}, []))
Simon Glass5e0f06d2017-11-12 21:52:15 -0700495
Simon Glassaa40f9a2014-08-09 15:33:08 -0600496
497 def testBoardArchSingleMultiWord(self):
498 """Test single board selection"""
Simon Glass127a2392022-07-11 19:04:02 -0600499 self.assertEqual(self.brds.select_boards(['arm', 'sandbox']),
Simon Glassd9eb9f02018-06-11 23:26:46 -0600500 ({'sandbox': ['board4'],
501 'all': ['board0', 'board1', 'board4'],
502 'arm': ['board0', 'board1']}, []))
Simon Glassaa40f9a2014-08-09 15:33:08 -0600503
504 def testBoardSingleAnd(self):
505 """Test single board selection"""
Simon Glass127a2392022-07-11 19:04:02 -0600506 self.assertEqual(self.brds.select_boards(['Tester & arm']),
Simon Glassd9eb9f02018-06-11 23:26:46 -0600507 ({'Tester&arm': ['board0', 'board1'],
508 'all': ['board0', 'board1']}, []))
Simon Glassaa40f9a2014-08-09 15:33:08 -0600509
510 def testBoardTwoAnd(self):
511 """Test single board selection"""
Simon Glass127a2392022-07-11 19:04:02 -0600512 self.assertEqual(self.brds.select_boards(['Tester', '&', 'arm',
Simon Glassaa40f9a2014-08-09 15:33:08 -0600513 'Tester' '&', 'powerpc',
514 'sandbox']),
Simon Glassd9eb9f02018-06-11 23:26:46 -0600515 ({'sandbox': ['board4'],
Simon Glass5e0f06d2017-11-12 21:52:15 -0700516 'all': ['board0', 'board1', 'board2', 'board3',
517 'board4'],
518 'Tester&powerpc': ['board2', 'board3'],
Simon Glassd9eb9f02018-06-11 23:26:46 -0600519 'Tester&arm': ['board0', 'board1']}, []))
Simon Glassaa40f9a2014-08-09 15:33:08 -0600520
521 def testBoardAll(self):
522 """Test single board selection"""
Simon Glass127a2392022-07-11 19:04:02 -0600523 self.assertEqual(self.brds.select_boards([]),
Simon Glassd9eb9f02018-06-11 23:26:46 -0600524 ({'all': ['board0', 'board1', 'board2', 'board3',
525 'board4']}, []))
Simon Glassaa40f9a2014-08-09 15:33:08 -0600526
527 def testBoardRegularExpression(self):
528 """Test single board selection"""
Simon Glass127a2392022-07-11 19:04:02 -0600529 self.assertEqual(self.brds.select_boards(['T.*r&^Po']),
Simon Glassd9eb9f02018-06-11 23:26:46 -0600530 ({'all': ['board2', 'board3'],
531 'T.*r&^Po': ['board2', 'board3']}, []))
Simon Glassaa40f9a2014-08-09 15:33:08 -0600532
533 def testBoardDuplicate(self):
534 """Test single board selection"""
Simon Glass127a2392022-07-11 19:04:02 -0600535 self.assertEqual(self.brds.select_boards(['sandbox sandbox',
Simon Glassaa40f9a2014-08-09 15:33:08 -0600536 'sandbox']),
Simon Glassd9eb9f02018-06-11 23:26:46 -0600537 ({'all': ['board4'], 'sandbox': ['board4']}, []))
Simon Glass2e1646c2014-12-01 17:33:51 -0700538 def CheckDirs(self, build, dirname):
Simon Glass4cb54682023-07-19 17:49:10 -0600539 self.assertEqual('base%s' % dirname, build.get_output_dir(1))
Simon Glass2e1646c2014-12-01 17:33:51 -0700540 self.assertEqual('base%s/fred' % dirname,
Simon Glassbc74d942023-07-19 17:49:06 -0600541 build.get_build_dir(1, 'fred'))
Simon Glass2e1646c2014-12-01 17:33:51 -0700542 self.assertEqual('base%s/fred/done' % dirname,
Simon Glassbc74d942023-07-19 17:49:06 -0600543 build.get_done_file(1, 'fred'))
Simon Glass2e1646c2014-12-01 17:33:51 -0700544 self.assertEqual('base%s/fred/u-boot.sizes' % dirname,
Simon Glassbc74d942023-07-19 17:49:06 -0600545 build.get_func_sizes_file(1, 'fred', 'u-boot'))
Simon Glass2e1646c2014-12-01 17:33:51 -0700546 self.assertEqual('base%s/fred/u-boot.objdump' % dirname,
Simon Glassbc74d942023-07-19 17:49:06 -0600547 build.get_objdump_file(1, 'fred', 'u-boot'))
Simon Glass2e1646c2014-12-01 17:33:51 -0700548 self.assertEqual('base%s/fred/err' % dirname,
Simon Glassbc74d942023-07-19 17:49:06 -0600549 build.get_err_file(1, 'fred'))
Simon Glass2e1646c2014-12-01 17:33:51 -0700550
551 def testOutputDir(self):
552 build = builder.Builder(self.toolchains, BASE_DIR, None, 1, 2,
553 checkout=False, show_unknown=False)
554 build.commits = self.commits
555 build.commit_count = len(self.commits)
556 subject = self.commits[1].subject.translate(builder.trans_valid_chars)
Simon Glass36971712020-07-19 12:28:11 -0600557 dirname ='/%02d_g%s_%s' % (2, commits[1][0], subject[:20])
Simon Glass2e1646c2014-12-01 17:33:51 -0700558 self.CheckDirs(build, dirname)
559
560 def testOutputDirCurrent(self):
561 build = builder.Builder(self.toolchains, BASE_DIR, None, 1, 2,
562 checkout=False, show_unknown=False)
563 build.commits = None
564 build.commit_count = 0
565 self.CheckDirs(build, '/current')
Simon Glassaa40f9a2014-08-09 15:33:08 -0600566
Simon Glasse87bde12014-12-01 17:33:55 -0700567 def testOutputDirNoSubdirs(self):
568 build = builder.Builder(self.toolchains, BASE_DIR, None, 1, 2,
569 checkout=False, show_unknown=False,
570 no_subdirs=True)
571 build.commits = None
572 build.commit_count = 0
573 self.CheckDirs(build, '')
574
Simon Glassc1528c12014-12-01 17:34:05 -0700575 def testToolchainAliases(self):
576 self.assertTrue(self.toolchains.Select('arm') != None)
577 with self.assertRaises(ValueError):
578 self.toolchains.Select('no-arch')
579 with self.assertRaises(ValueError):
580 self.toolchains.Select('x86')
581
582 self.toolchains = toolchain.Toolchains()
583 self.toolchains.Add('x86_64-linux-gcc', test=False)
584 self.assertTrue(self.toolchains.Select('x86') != None)
585
586 self.toolchains = toolchain.Toolchains()
587 self.toolchains.Add('i386-linux-gcc', test=False)
588 self.assertTrue(self.toolchains.Select('x86') != None)
589
Simon Glass7e803e12014-12-01 17:34:06 -0700590 def testToolchainDownload(self):
591 """Test that we can download toolchains"""
Simon Glass2bfc6972017-11-12 21:52:14 -0700592 if use_network:
Simon Glass04150022018-10-01 21:12:43 -0600593 with test_util.capture_sys_output() as (stdout, stderr):
594 url = self.toolchains.LocateArchUrl('arm')
Brandon Maiera657bc62024-06-04 16:16:05 +0000595 self.assertRegex(url, 'https://www.kernel.org/pub/tools/'
Simon Glass72e358c2018-10-01 21:12:35 -0600596 'crosstool/files/bin/x86_64/.*/'
Simon Glassdd65c5f2020-04-17 17:51:30 -0600597 'x86_64-gcc-.*-nolibc[-_]arm-.*linux-gnueabi.tar.xz')
Simon Glass7e803e12014-12-01 17:34:06 -0700598
Simon Glass48ac42e2019-12-05 15:59:14 -0700599 def testGetEnvArgs(self):
600 """Test the GetEnvArgs() function"""
601 tc = self.toolchains.Select('arm')
602 self.assertEqual('arm-linux-',
603 tc.GetEnvArgs(toolchain.VAR_CROSS_COMPILE))
604 self.assertEqual('', tc.GetEnvArgs(toolchain.VAR_PATH))
605 self.assertEqual('arm',
606 tc.GetEnvArgs(toolchain.VAR_ARCH))
607 self.assertEqual('', tc.GetEnvArgs(toolchain.VAR_MAKE_ARGS))
608
609 self.toolchains.Add('/path/to/x86_64-linux-gcc', test=False)
610 tc = self.toolchains.Select('x86')
611 self.assertEqual('/path/to',
612 tc.GetEnvArgs(toolchain.VAR_PATH))
613 tc.override_toolchain = 'clang'
614 self.assertEqual('HOSTCC=clang CC=clang',
615 tc.GetEnvArgs(toolchain.VAR_MAKE_ARGS))
616
Simon Glass5dc1ca72020-03-18 09:42:45 -0600617 def testPrepareOutputSpace(self):
618 def _Touch(fname):
Simon Glass80025522022-01-29 14:14:04 -0700619 tools.write_file(os.path.join(base_dir, fname), b'')
Simon Glass5dc1ca72020-03-18 09:42:45 -0600620
621 base_dir = tempfile.mkdtemp()
622
623 # Add various files that we want removed and left alone
Ovidiu Panaitee8e9cb2020-05-15 09:30:12 +0300624 to_remove = ['01_g0982734987_title', '102_g92bf_title',
625 '01_g2938abd8_title']
626 to_leave = ['something_else', '01-something.patch', '01_another']
Simon Glass5dc1ca72020-03-18 09:42:45 -0600627 for name in to_remove + to_leave:
628 _Touch(name)
629
630 build = builder.Builder(self.toolchains, base_dir, None, 1, 2)
631 build.commits = self.commits
632 build.commit_count = len(commits)
Simon Glassbc74d942023-07-19 17:49:06 -0600633 result = set(build._get_output_space_removals())
Simon Glass5dc1ca72020-03-18 09:42:45 -0600634 expected = set([os.path.join(base_dir, f) for f in to_remove])
635 self.assertEqual(expected, result)
Simon Glass7e803e12014-12-01 17:34:06 -0700636
Simon Glass22901f92022-01-22 05:07:31 -0700637 def test_adjust_cfg_nop(self):
638 """check various adjustments of config that are nops"""
639 # enable an enabled CONFIG
640 self.assertEqual(
641 'CONFIG_FRED=y',
642 cfgutil.adjust_cfg_line('CONFIG_FRED=y', {'FRED':'FRED'})[0])
643
644 # disable a disabled CONFIG
645 self.assertEqual(
646 '# CONFIG_FRED is not set',
647 cfgutil.adjust_cfg_line(
648 '# CONFIG_FRED is not set', {'FRED':'~FRED'})[0])
649
650 # use the adjust_cfg_lines() function
651 self.assertEqual(
652 ['CONFIG_FRED=y'],
653 cfgutil.adjust_cfg_lines(['CONFIG_FRED=y'], {'FRED':'FRED'}))
654 self.assertEqual(
655 ['# CONFIG_FRED is not set'],
656 cfgutil.adjust_cfg_lines(['CONFIG_FRED=y'], {'FRED':'~FRED'}))
657
658 # handling an empty line
659 self.assertEqual('#', cfgutil.adjust_cfg_line('#', {'FRED':'~FRED'})[0])
660
661 def test_adjust_cfg(self):
662 """check various adjustments of config"""
663 # disable a CONFIG
664 self.assertEqual(
665 '# CONFIG_FRED is not set',
666 cfgutil.adjust_cfg_line('CONFIG_FRED=1' , {'FRED':'~FRED'})[0])
667
668 # enable a disabled CONFIG
669 self.assertEqual(
670 'CONFIG_FRED=y',
671 cfgutil.adjust_cfg_line(
672 '# CONFIG_FRED is not set', {'FRED':'FRED'})[0])
673
674 # enable a CONFIG that doesn't exist
675 self.assertEqual(
676 ['CONFIG_FRED=y'],
677 cfgutil.adjust_cfg_lines([], {'FRED':'FRED'}))
678
679 # disable a CONFIG that doesn't exist
680 self.assertEqual(
681 ['# CONFIG_FRED is not set'],
682 cfgutil.adjust_cfg_lines([], {'FRED':'~FRED'}))
683
684 # disable a value CONFIG
685 self.assertEqual(
686 '# CONFIG_FRED is not set',
687 cfgutil.adjust_cfg_line('CONFIG_FRED="fred"' , {'FRED':'~FRED'})[0])
688
689 # setting a value CONFIG
690 self.assertEqual(
691 'CONFIG_FRED="fred"',
692 cfgutil.adjust_cfg_line('# CONFIG_FRED is not set' ,
693 {'FRED':'FRED="fred"'})[0])
694
695 # changing a value CONFIG
696 self.assertEqual(
697 'CONFIG_FRED="fred"',
698 cfgutil.adjust_cfg_line('CONFIG_FRED="ernie"' ,
699 {'FRED':'FRED="fred"'})[0])
700
701 # setting a value for a CONFIG that doesn't exist
702 self.assertEqual(
703 ['CONFIG_FRED="fred"'],
704 cfgutil.adjust_cfg_lines([], {'FRED':'FRED="fred"'}))
705
706 def test_convert_adjust_cfg_list(self):
707 """Check conversion of the list of changes into a dict"""
708 self.assertEqual({}, cfgutil.convert_list_to_dict(None))
709
710 expect = {
711 'FRED':'FRED',
712 'MARY':'~MARY',
713 'JOHN':'JOHN=0x123',
714 'ALICE':'ALICE="alice"',
715 'AMY':'AMY',
716 'ABE':'~ABE',
717 'MARK':'MARK=0x456',
718 'ANNA':'ANNA="anna"',
719 }
720 actual = cfgutil.convert_list_to_dict(
721 ['FRED', '~MARY', 'JOHN=0x123', 'ALICE="alice"',
722 'CONFIG_AMY', '~CONFIG_ABE', 'CONFIG_MARK=0x456',
723 'CONFIG_ANNA="anna"'])
724 self.assertEqual(expect, actual)
725
726 def test_check_cfg_file(self):
727 """Test check_cfg_file detects conflicts as expected"""
728 # Check failure to disable CONFIG
729 result = cfgutil.check_cfg_lines(['CONFIG_FRED=1'], {'FRED':'~FRED'})
730 self.assertEqual([['~FRED', 'CONFIG_FRED=1']], result)
731
732 result = cfgutil.check_cfg_lines(
733 ['CONFIG_FRED=1', 'CONFIG_MARY="mary"'], {'FRED':'~FRED'})
734 self.assertEqual([['~FRED', 'CONFIG_FRED=1']], result)
735
736 result = cfgutil.check_cfg_lines(
737 ['CONFIG_FRED=1', 'CONFIG_MARY="mary"'], {'MARY':'~MARY'})
738 self.assertEqual([['~MARY', 'CONFIG_MARY="mary"']], result)
739
740 # Check failure to enable CONFIG
741 result = cfgutil.check_cfg_lines(
742 ['# CONFIG_FRED is not set'], {'FRED':'FRED'})
743 self.assertEqual([['FRED', '# CONFIG_FRED is not set']], result)
744
745 # Check failure to set CONFIG value
746 result = cfgutil.check_cfg_lines(
747 ['# CONFIG_FRED is not set', 'CONFIG_MARY="not"'],
748 {'MARY':'MARY="mary"', 'FRED':'FRED'})
749 self.assertEqual([
750 ['FRED', '# CONFIG_FRED is not set'],
751 ['MARY="mary"', 'CONFIG_MARY="not"']], result)
752
753 # Check failure to add CONFIG value
754 result = cfgutil.check_cfg_lines([], {'MARY':'MARY="mary"'})
755 self.assertEqual([
756 ['MARY="mary"', 'Missing expected line: CONFIG_MARY="mary"']], result)
757
Simon Glassc229d322024-06-23 11:55:15 -0600758 def get_procs(self):
759 running_fname = os.path.join(self.base_dir, control.RUNNING_FNAME)
760 items = tools.read_file(running_fname, binary=False).split()
761 return [int(x) for x in items]
762
763 def get_time(self):
764 return self.cur_time
765
766 def inc_time(self, amount):
767 self.cur_time += amount
768
769 # Handle a process exiting
770 if self.finish_time == self.cur_time:
771 self.valid_pids = [pid for pid in self.valid_pids
772 if pid != self.finish_pid]
773
774 def kill(self, pid, signal):
775 if pid not in self.valid_pids:
776 raise OSError('Invalid PID')
777
778 def test_process_limit(self):
779 """Test wait_for_process_limit() function"""
780 tmpdir = self.base_dir
781
782 with (patch('time.time', side_effect=self.get_time),
Simon Glassf4f0acc2024-08-22 07:57:46 -0600783 patch('time.monotonic', side_effect=self.get_time),
Simon Glassc229d322024-06-23 11:55:15 -0600784 patch('time.sleep', side_effect=self.inc_time),
785 patch('os.kill', side_effect=self.kill)):
786 # Grab the process. Since there is no other profcess, this should
787 # immediately succeed
788 control.wait_for_process_limit(1, tmpdir=tmpdir, pid=1)
789 lines = terminal.get_print_test_lines()
790 self.assertEqual(0, self.cur_time)
791 self.assertEqual('Waiting for other buildman processes...',
792 lines[0].text)
793 self.assertEqual(self._col.RED, lines[0].colour)
794 self.assertEqual(False, lines[0].newline)
795 self.assertEqual(True, lines[0].bright)
796
797 self.assertEqual('done...', lines[1].text)
798 self.assertEqual(None, lines[1].colour)
799 self.assertEqual(False, lines[1].newline)
800 self.assertEqual(True, lines[1].bright)
801
802 self.assertEqual('starting build', lines[2].text)
803 self.assertEqual([1], control.read_procs(tmpdir))
804 self.assertEqual(None, lines[2].colour)
805 self.assertEqual(False, lines[2].newline)
806 self.assertEqual(True, lines[2].bright)
807
808 # Try again, with a different PID...this should eventually timeout
809 # and start the build anyway
810 self.cur_time = 0
811 self.valid_pids = [1]
812 control.wait_for_process_limit(1, tmpdir=tmpdir, pid=2)
813 lines = terminal.get_print_test_lines()
814 self.assertEqual('Waiting for other buildman processes...',
815 lines[0].text)
816 self.assertEqual('timeout...', lines[1].text)
817 self.assertEqual(None, lines[1].colour)
818 self.assertEqual(False, lines[1].newline)
819 self.assertEqual(True, lines[1].bright)
820 self.assertEqual('starting build', lines[2].text)
821 self.assertEqual([1, 2], control.read_procs(tmpdir))
822 self.assertEqual(control.RUN_WAIT_S, self.cur_time)
823
824 # Check lock-busting
825 self.cur_time = 0
826 self.valid_pids = [1, 2]
827 lock_fname = os.path.join(tmpdir, control.LOCK_FNAME)
828 lock = FileLock(lock_fname)
829 lock.acquire(timeout=1)
830 control.wait_for_process_limit(1, tmpdir=tmpdir, pid=3)
831 lines = terminal.get_print_test_lines()
832 self.assertEqual('Waiting for other buildman processes...',
833 lines[0].text)
834 self.assertEqual('failed to get lock: busting...', lines[1].text)
835 self.assertEqual(None, lines[1].colour)
836 self.assertEqual(False, lines[1].newline)
837 self.assertEqual(True, lines[1].bright)
838 self.assertEqual('timeout...', lines[2].text)
839 self.assertEqual('starting build', lines[3].text)
840 self.assertEqual([1, 2, 3], control.read_procs(tmpdir))
841 self.assertEqual(control.RUN_WAIT_S, self.cur_time)
842 lock.release()
843
844 # Check handling of dead processes. Here we have PID 2 as a running
845 # process, even though the PID file contains 1, 2 and 3. So we can
846 # add one more PID, to make 2 and 4
847 self.cur_time = 0
848 self.valid_pids = [2]
849 control.wait_for_process_limit(2, tmpdir=tmpdir, pid=4)
850 lines = terminal.get_print_test_lines()
851 self.assertEqual('Waiting for other buildman processes...',
852 lines[0].text)
853 self.assertEqual('done...', lines[1].text)
854 self.assertEqual('starting build', lines[2].text)
855 self.assertEqual([2, 4], control.read_procs(tmpdir))
856 self.assertEqual(0, self.cur_time)
857
858 # Try again, with PID 2 quitting at time 50. This allows the new
859 # build to start
860 self.cur_time = 0
861 self.valid_pids = [2, 4]
862 self.finish_pid = 2
863 self.finish_time = 50
864 control.wait_for_process_limit(2, tmpdir=tmpdir, pid=5)
865 lines = terminal.get_print_test_lines()
866 self.assertEqual('Waiting for other buildman processes...',
867 lines[0].text)
868 self.assertEqual('done...', lines[1].text)
869 self.assertEqual('starting build', lines[2].text)
870 self.assertEqual([4, 5], control.read_procs(tmpdir))
871 self.assertEqual(self.finish_time, self.cur_time)
872
Simon Glass038a3dd2024-08-15 13:57:44 -0600873 def call_make_environment(self, tchn, full_path, in_env=None):
874 """Call Toolchain.MakeEnvironment() and process the result
875
876 Args:
877 tchn (Toolchain): Toolchain to use
878 full_path (bool): True to return the full path in CROSS_COMPILE
879 rather than adding it to the PATH variable
880 in_env (dict): Input environment to use, None to use current env
881
882 Returns:
883 tuple:
884 dict: Changes that MakeEnvironment has made to the environment
885 key: Environment variable that was changed
886 value: New value (for PATH this only includes components
887 which were added)
888 str: Full value of the new PATH variable
889 """
890 env = tchn.MakeEnvironment(full_path, env=in_env)
891
892 # Get the original environment
893 orig_env = dict(os.environb if in_env is None else in_env)
894 orig_path = orig_env[b'PATH'].split(b':')
895
896 # Find new variables
897 diff = dict((k, env[k]) for k in env if orig_env.get(k) != env[k])
898
899 # Find new / different path components
900 diff_path = None
901 new_path = None
902 if b'PATH' in diff:
903 new_path = diff[b'PATH'].split(b':')
904 diff_paths = [p for p in new_path if p not in orig_path]
905 diff_path = b':'.join(p for p in new_path if p not in orig_path)
906 if diff_path:
907 diff[b'PATH'] = diff_path
908 else:
909 del diff[b'PATH']
910 return diff, new_path
911
912 def test_toolchain_env(self):
913 """Test PATH and other environment settings for toolchains"""
914 # Use a toolchain which has a path, so that full_path makes a difference
915 tchn = self.toolchains.Select('aarch64')
916
917 # Normal cases
918 diff = self.call_make_environment(tchn, full_path=False)[0]
919 self.assertEqual(
920 {b'CROSS_COMPILE': b'aarch64-linux-', b'LC_ALL': b'C',
921 b'PATH': b'/path/to'}, diff)
922
923 diff = self.call_make_environment(tchn, full_path=True)[0]
924 self.assertEqual(
925 {b'CROSS_COMPILE': b'/path/to/aarch64-linux-', b'LC_ALL': b'C'},
926 diff)
927
928 # When overriding the toolchain, only LC_ALL should be set
929 tchn.override_toolchain = True
930 diff = self.call_make_environment(tchn, full_path=True)[0]
931 self.assertEqual({b'LC_ALL': b'C'}, diff)
932
933 # Test that virtualenv is handled correctly
934 tchn.override_toolchain = False
935 sys.prefix = '/some/venv'
936 env = dict(os.environb)
937 env[b'PATH'] = b'/some/venv/bin:other/things'
938 tchn.path = '/my/path'
939 diff, diff_path = self.call_make_environment(tchn, False, env)
940
941 self.assertIn(b'PATH', diff)
942 self.assertEqual([b'/some/venv/bin', b'/my/path', b'other/things'],
943 diff_path)
944 self.assertEqual(
945 {b'CROSS_COMPILE': b'aarch64-linux-', b'LC_ALL': b'C',
946 b'PATH': b'/my/path'}, diff)
947
948 # Handle a toolchain wrapper
949 tchn.path = ''
950 bsettings.add_section('toolchain-wrapper')
951 bsettings.set_item('toolchain-wrapper', 'my-wrapper', 'fred')
952 diff = self.call_make_environment(tchn, full_path=True)[0]
953 self.assertEqual(
954 {b'CROSS_COMPILE': b'fred aarch64-linux-', b'LC_ALL': b'C'}, diff)
955
Simon Glass22901f92022-01-22 05:07:31 -0700956
Simon Glassc05694f2013-04-03 11:07:16 +0000957if __name__ == "__main__":
958 unittest.main()